-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_auth_api.py
More file actions
166 lines (139 loc) · 5.23 KB
/
test_auth_api.py
File metadata and controls
166 lines (139 loc) · 5.23 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
"""
Complete API test for HelixFlow Platform
Tests authentication and inference endpoints
"""
import requests
import json
import sys
from urllib3.exceptions import InsecureRequestWarning
# Suppress TLS warnings for self-signed certificates
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
def get_auth_token():
"""Get authentication token from auth service"""
print("Getting authentication token...")
try:
# First try to register a test user
register_data = {
"username": "testuser",
"email": "test@example.com",
"password": "testpass123"
}
register_response = requests.post(
"http://localhost:8082/register",
json=register_data,
timeout=5
)
if register_response.status_code == 201:
print(" ✅ Test user registered successfully")
elif register_response.status_code == 409:
print(" ℹ️ Test user already exists")
else:
print(f" ⚠️ Registration: {register_response.status_code}")
# Login to get token
login_data = {"username": "testuser", "password": "testpass123"}
login_response = requests.post(
"http://localhost:8082/login",
json=login_data,
timeout=5
)
if login_response.status_code == 200:
token_data = login_response.json()
token = token_data.get("token")
print(" ✅ Authentication token obtained")
return token
else:
print(f" ❌ Login failed: {login_response.status_code} - {login_response.text}")
return None
except Exception as e:
print(f" ❌ Authentication error: {e}")
return None
def test_authenticated_api(token):
"""Test API endpoints with authentication"""
print("\nTesting authenticated API endpoints...")
headers = {"Authorization": f"Bearer {token}"}
base_url = "https://localhost:8443"
try:
# Test chat completion with authentication
print(" Testing authenticated chat completion...")
chat_data = {
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello! How are you?"}],
"max_tokens": 100
}
response = requests.post(
f"{base_url}/v1/chat/completions",
json=chat_data,
headers=headers,
verify=False,
timeout=10
)
if response.status_code == 200:
result = response.json()
message = result.get('choices', [{}])[0].get('message', {}).get('content', 'No content')
print(f" ✅ Authenticated Chat: {response.status_code}")
print(f" 🤖 Response: {message}")
return True
else:
print(f" ❌ Authenticated Chat: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f" ❌ Authenticated API error: {e}")
return False
def test_token_revocation(token):
"""Test token revocation functionality"""
print("\nTesting token revocation...")
try:
# Revoke the token
headers = {"Authorization": f"Bearer {token}"}
response = requests.post(
"http://localhost:8082/revoke",
headers=headers,
timeout=5
)
if response.status_code == 200:
print(" ✅ Token revoked successfully")
# Try to use the revoked token
chat_data = {
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "This should fail"}],
"max_tokens": 10
}
response = requests.post(
"https://localhost:8443/v1/chat/completions",
json=chat_data,
headers=headers,
verify=False,
timeout=5
)
if response.status_code == 401:
print(" ✅ Revoked token correctly rejected")
return True
else:
print(f" ❌ Revoked token was accepted: {response.status_code}")
return False
else:
print(f" ❌ Token revocation failed: {response.status_code}")
return False
except Exception as e:
print(f" ❌ Token revocation error: {e}")
return False
if __name__ == "__main__":
print("🔐 Testing HelixFlow Platform Authentication")
print("=" * 50)
# Get authentication token
token = get_auth_token()
if not token:
print("\n❌ Cannot proceed without authentication token")
sys.exit(1)
# Test authenticated API
auth_success = test_authenticated_api(token)
# Test token revocation
revoke_success = test_token_revocation(token)
print("\n" + "=" * 50)
if auth_success and revoke_success:
print("🎉 All authentication tests passed!")
sys.exit(0)
else:
print("❌ Some authentication tests failed")
sys.exit(1)