-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
347 lines (306 loc) · 11.7 KB
/
Copy pathtest_api.py
File metadata and controls
347 lines (306 loc) · 11.7 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python3
"""
CAN 2025 Guardian API - Test Suite
Tests all API endpoints and verifies functionality
"""
import requests
import json
import time
from typing import Optional
# Configuration
API_BASE = "http://localhost:8888/api/v1"
USERNAME = "admin"
PASSWORD = "admin123"
# Colors for terminal output
GREEN = "\033[92m"
RED = "\033[91m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
RESET = "\033[0m"
class APITester:
def __init__(self):
self.token: Optional[str] = None
self.passed = 0
self.failed = 0
def print_test(self, name: str, status: bool, details: str = ""):
"""Print test result"""
symbol = f"{GREEN}✓{RESET}" if status else f"{RED}✗{RESET}"
print(f"{symbol} {name}")
if details:
print(f" {YELLOW}└─{RESET} {details}")
if status:
self.passed += 1
else:
self.failed += 1
def test_health(self):
"""Test health check endpoint"""
print(f"\n{BLUE}Testing Health Check{RESET}")
print("-" * 50)
try:
response = requests.get("http://localhost:8888/health")
success = response.status_code == 200
self.print_test("Health check", success, f"Status: {response.status_code}")
except Exception as e:
self.print_test("Health check", False, str(e))
def test_authentication(self):
"""Test authentication endpoints"""
print(f"\n{BLUE}Testing Authentication{RESET}")
print("-" * 50)
# Test login
try:
response = requests.post(
f"{API_BASE}/auth/login",
data={"username": USERNAME, "password": PASSWORD},
)
if response.status_code == 200:
data = response.json()
self.token = data.get("access_token")
self.print_test("Login", True, f"Token received: {self.token[:20]}...")
else:
self.print_test("Login", False, f"Status: {response.status_code}")
return
except Exception as e:
self.print_test("Login", False, str(e))
return
# Test get current user
try:
response = requests.get(
f"{API_BASE}/auth/me", headers={"Authorization": f"Bearer {self.token}"}
)
success = response.status_code == 200
user = response.json() if success else {}
self.print_test(
"Get current user", success, f"Username: {user.get('username')}"
)
except Exception as e:
self.print_test("Get current user", False, str(e))
def test_threats(self):
"""Test threat detection endpoints"""
print(f"\n{BLUE}Testing Threat Detection{RESET}")
print("-" * 50)
# Test get history
try:
response = requests.get(
f"{API_BASE}/threats/history?limit=10",
headers={"Authorization": f"Bearer {self.token}"},
)
success = response.status_code == 200
data = response.json() if success else {}
self.print_test(
"Get threat history", success, f"Total: {data.get('total', 0)}"
)
except Exception as e:
self.print_test("Get threat history", False, str(e))
# Test get stats
try:
response = requests.get(
f"{API_BASE}/threats/stats",
headers={"Authorization": f"Bearer {self.token}"},
)
success = response.status_code == 200
data = response.json() if success else {}
self.print_test(
"Get threat stats",
success,
f"Total detections: {data.get('total_detections', 0)}",
)
except Exception as e:
self.print_test("Get threat stats", False, str(e))
def test_ai_chatbot(self):
"""Test AI chatbot endpoints"""
print(f"\n{BLUE}Testing AI Chatbot{RESET}")
print("-" * 50)
# Test get models
try:
response = requests.get(
f"{API_BASE}/ai/models",
headers={"Authorization": f"Bearer {self.token}"},
)
success = response.status_code == 200
models = response.json() if success else []
model_names = (
[m["name"] for m in models] if isinstance(models, list) else []
)
self.print_test(
"Get AI models", success, f"Available: {', '.join(model_names)}"
)
except Exception as e:
self.print_test("Get AI models", False, str(e))
# Test chat
try:
response = requests.post(
f"{API_BASE}/ai/chat",
headers={
"Authorization": f"Bearer {self.token}",
"Content-Type": "application/json",
},
json={
"query": "What are the main security concerns for CAN 2025?",
"model": "openai",
},
)
success = response.status_code == 200
data = response.json() if success else {}
response_text = data.get("response", "")[:50]
self.print_test(
"Send chat message", success, f"Response: {response_text}..."
)
except Exception as e:
self.print_test("Send chat message", False, str(e))
def test_analytics(self):
"""Test analytics endpoints"""
print(f"\n{BLUE}Testing Analytics{RESET}")
print("-" * 50)
# Test dashboard stats
try:
response = requests.get(
f"{API_BASE}/analytics/dashboard",
headers={"Authorization": f"Bearer {self.token}"},
)
success = response.status_code == 200
data = response.json() if success else {}
self.print_test(
"Get dashboard stats",
success,
f"Incidents: {data.get('total_incidents', 0)}, "
f"Alerts: {data.get('active_alerts', 0)}",
)
except Exception as e:
self.print_test("Get dashboard stats", False, str(e))
# Test anomaly detection
try:
response = requests.get(
f"{API_BASE}/analytics/anomalies?metric=threat_count&days=7",
headers={"Authorization": f"Bearer {self.token}"},
)
success = response.status_code == 200
data = response.json() if success else {}
self.print_test(
"Anomaly detection", success, f"Detected: {data.get('detected', 0)}"
)
except Exception as e:
self.print_test("Anomaly detection", False, str(e))
def test_streams(self):
"""Test video stream endpoints"""
print(f"\n{BLUE}Testing Video Streams{RESET}")
print("-" * 50)
# Test list streams
try:
response = requests.get(
f"{API_BASE}/streams", headers={"Authorization": f"Bearer {self.token}"}
)
success = response.status_code == 200
streams = response.json() if success else []
self.print_test(
"List streams",
success,
f"Total streams: {len(streams) if isinstance(streams, list) else 0}",
)
except Exception as e:
self.print_test("List streams", False, str(e))
def test_alerts_and_costs(self):
"""Test alerts and cost tracking endpoints"""
print(f"\n{BLUE}Testing Alerts & Cost Tracking{RESET}")
print("-" * 50)
# Test get cost stats
try:
response = requests.get(
f"{API_BASE}/costs", headers={"Authorization": f"Bearer {self.token}"}
)
success = response.status_code == 200
data = response.json() if success else {}
self.print_test(
"Get cost stats",
success,
f"Daily: ${data.get('daily_cost', 0):.4f}, "
f"Monthly: ${data.get('monthly_cost', 0):.2f}",
)
except Exception as e:
self.print_test("Get cost stats", False, str(e))
# Test budget status
try:
response = requests.get(
f"{API_BASE}/costs/budget",
headers={"Authorization": f"Bearer {self.token}"},
)
success = response.status_code == 200
data = response.json() if success else {}
self.print_test(
"Get budget status",
success,
f"Monthly spent: ${data.get('monthly_spent', 0):.2f} / "
f"${data.get('monthly_budget', 0):.2f}",
)
except Exception as e:
self.print_test("Get budget status", False, str(e))
def test_rate_limiting(self):
"""Test rate limiting"""
print(f"\n{BLUE}Testing Rate Limiting{RESET}")
print("-" * 50)
try:
# Make rapid requests to trigger rate limit
responses = []
for i in range(5):
response = requests.get(
f"{API_BASE}/threats/history?limit=1",
headers={"Authorization": f"Bearer {self.token}"},
)
responses.append(response.status_code)
# Check if we got rate limit headers
last_response = requests.get(
f"{API_BASE}/threats/history?limit=1",
headers={"Authorization": f"Bearer {self.token}"},
)
has_rate_limit_headers = (
"X-RateLimit-Remaining-Minute" in last_response.headers
)
self.print_test(
"Rate limit headers present",
has_rate_limit_headers,
f"Remaining: {last_response.headers.get('X-RateLimit-Remaining-Minute', 'N/A')}",
)
except Exception as e:
self.print_test("Rate limiting", False, str(e))
def run_all_tests(self):
"""Run all tests"""
print(f"\n{BLUE}{'=' * 60}{RESET}")
print(f"{BLUE}CAN 2025 Guardian API - Test Suite{RESET}")
print(f"{BLUE}{'=' * 60}{RESET}")
self.test_health()
self.test_authentication()
if self.token:
self.test_threats()
self.test_ai_chatbot()
self.test_analytics()
self.test_streams()
self.test_alerts_and_costs()
self.test_rate_limiting()
else:
print(f"\n{RED}Skipping authenticated tests (login failed){RESET}")
# Print summary
print(f"\n{BLUE}{'=' * 60}{RESET}")
print(f"{BLUE}Test Summary{RESET}")
print(f"{BLUE}{'=' * 60}{RESET}")
print(f"{GREEN}Passed:{RESET} {self.passed}")
print(f"{RED}Failed:{RESET} {self.failed}")
print(f"Total: {self.passed + self.failed}")
success_rate = (
(self.passed / (self.passed + self.failed) * 100)
if (self.passed + self.failed) > 0
else 0
)
print(f"\nSuccess Rate: {success_rate:.1f}%")
if self.failed == 0:
print(f"\n{GREEN}✓ All tests passed!{RESET} 🎉")
else:
print(f"\n{RED}✗ Some tests failed.{RESET} Please check the API logs.")
if __name__ == "__main__":
print(
f"\n{YELLOW}Make sure the API server is running on http://localhost:8888{RESET}"
)
print(
f"{YELLOW}Start it with: ./start_api.sh or uvicorn api.main:app --reload --port 8888{RESET}\n"
)
input("Press Enter to start tests...")
tester = APITester()
tester.run_all_tests()