-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
107 lines (88 loc) · 3.84 KB
/
test_api.py
File metadata and controls
107 lines (88 loc) · 3.84 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
"""
Test script for the Python Auth Backend FastAPI
"""
import requests
import json
BASE_URL = "http://localhost:5000"
def test_health_endpoint():
"""Test the health check endpoint (no auth required)"""
print("🔍 Testing /api/health endpoint...")
try:
response = requests.get(f"{BASE_URL}/api/health")
print(f"Status Code: {response.status_code}")
print(f"Response: {response.json()}")
print("✅ Health endpoint works!\n")
except Exception as e:
print(f"❌ Error testing health endpoint: {e}\n")
def test_protected_endpoint_without_token():
"""Test the protected endpoint without token (should return 403)"""
print("🔍 Testing /api/helloworld without token...")
try:
response = requests.get(f"{BASE_URL}/api/helloworld")
print(f"Status Code: {response.status_code}")
print(f"Response: {response.json()}")
if response.status_code == 403:
print("✅ Protected endpoint correctly returns 403 without token!\n")
else:
print(f"❌ Expected 403 but got {response.status_code}\n")
except Exception as e:
print(f"❌ Error testing protected endpoint: {e}\n")
def test_protected_endpoint_with_invalid_token():
"""Test the protected endpoint with invalid token (should return 401)"""
print("🔍 Testing /api/helloworld with invalid token...")
try:
headers = {"Authorization": "Bearer invalid_token_here"}
response = requests.get(f"{BASE_URL}/api/helloworld", headers=headers)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.json()}")
if response.status_code == 401:
print("✅ Protected endpoint correctly returns 401 with invalid token!\n")
else:
print(f"❌ Expected 401 but got {response.status_code}\n")
except Exception as e:
print(f"❌ Error testing protected endpoint with invalid token: {e}\n")
def test_auth_info_endpoint():
"""Test the auth info endpoint"""
print("🔍 Testing /api/auth/info endpoint...")
try:
response = requests.get(f"{BASE_URL}/api/auth/info")
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
print("✅ Auth info endpoint works!\n")
except Exception as e:
print(f"❌ Error testing auth info endpoint: {e}\n")
def test_api_docs():
"""Test the automatic API documentation endpoints"""
print("🔍 Testing FastAPI documentation endpoints...")
try:
# Test Swagger UI
response = requests.get(f"{BASE_URL}/docs")
print(f"Swagger UI (/docs): {response.status_code}")
# Test ReDoc
response = requests.get(f"{BASE_URL}/redoc")
print(f"ReDoc (/redoc): {response.status_code}")
# Test OpenAPI schema
response = requests.get(f"{BASE_URL}/openapi.json")
print(f"OpenAPI Schema (/openapi.json): {response.status_code}")
print("✅ All documentation endpoints work!\n")
except Exception as e:
print(f"❌ Error testing documentation endpoints: {e}\n")
if __name__ == "__main__":
print("🚀 Starting FastAPI Tests...\n")
test_health_endpoint()
test_protected_endpoint_without_token()
test_protected_endpoint_with_invalid_token()
test_auth_info_endpoint()
test_api_docs()
print("📖 FastAPI Documentation:")
print(" • Swagger UI: http://localhost:5000/docs")
print(" • ReDoc: http://localhost:5000/redoc")
print(" • OpenAPI Schema: http://localhost:5000/openapi.json")
print("")
print("📝 To test with a valid Microsoft token:")
print(" 1. Get a token from Azure AD")
print(
" 2. Run: curl -H 'Authorization: Bearer YOUR_TOKEN' http://localhost:5000/api/helloworld"
)
print("\n🎉 Tests completed!")