File size: 2,389 Bytes
931223d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import requests
import sys

BASE_URL = "http://127.0.0.1:8022"

def test_health():
    print(f"Testing Health Check at {BASE_URL}/health...")
    try:
        r = requests.get(f"{BASE_URL}/health")
        if r.status_code == 200:
            print("βœ… Health Check Passed")
            return True
    except Exception as e:
        print(f"❌ Health Check Failed: {e}")
    return False

def test_auth():
    print("Testing Authentication...")
    
    # 1. Try to access protected route without token
    try:
        r = requests.post(f"{BASE_URL}/analyze")
        if r.status_code == 401:
            print("βœ… Protected Endpoint correctly rejected unauthorized request (401)")
        else:
            print(f"❌ Protected Endpoint Failed: Expected 401, got {r.status_code}")
            return False
            
        # 2. Login to get token
        payload = {"username": "admin", "password": "secret"}
        r = requests.post(f"{BASE_URL}/token", data=payload)
        if r.status_code == 200:
            token = r.json().get("access_token")
            if token:
                print("βœ… Login Successful. Token received.")
            else:
                print("❌ Login Failed: No token in response")
                return False
        else:
            print(f"❌ Login Failed: {r.status_code} - {r.text}")
            return False
            
        # 3. Access protected route WITH token (Should fail on 422 Validation 'Field required' for file, NOT 401)
        headers = {"Authorization": f"Bearer {token}"}
        # We don't send file, expecting 422 Unprocessable Entity (Missing File), which means Auth passed!
        r = requests.post(f"{BASE_URL}/analyze", headers=headers)
        if r.status_code == 422:
             print("βœ… Protected Endpoint correctly accepted token (Got 422 for missing file, not 401)")
             return True
        elif r.status_code == 401:
             print("❌ Protected Endpoint rejected valid token (401)")
             return False
        else:
             print(f"⚠️ Unexpected status with token: {r.status_code}")
             return True # Acceptable for now
             
    except Exception as e:
        print(f"❌ Test Exception: {e}")
        return False

if __name__ == "__main__":
    if test_health():
        test_auth()