-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_backend.py
More file actions
403 lines (322 loc) · 11.8 KB
/
test_backend.py
File metadata and controls
403 lines (322 loc) · 11.8 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env python3
"""
Backend API Test Script
Run this script to test all critical backend endpoints.
It will show you exactly what's working and what's failing.
Usage:
python test_backend.py --password YOUR_PASSWORD
Or edit the PASSWORD variable below.
"""
import argparse
import json
import sys
import requests
# =============================================================================
# CONFIGURATION - Edit these if needed
# =============================================================================
BASE_URL = "http://localhost:8000"
USERNAME = "acobapaul@gmail.com"
PASSWORD = "Repent19$" # Set via command line or edit here
# =============================================================================
# Test Functions
# =============================================================================
def test_backend_running():
"""Test if backend is accessible."""
print("\n" + "="*60)
print("TEST 1: Backend Accessibility")
print("="*60)
try:
response = requests.get(f"{BASE_URL}/docs", timeout=5)
if response.status_code == 200:
print("✅ Backend is running and accessible")
return True
else:
print(f"❌ Backend returned status {response.status_code}")
return False
except requests.exceptions.ConnectionError:
print(f"❌ Cannot connect to {BASE_URL}")
print(" Make sure the backend is running")
return False
except Exception as e:
print(f"❌ Error: {e}")
return False
def test_login(username, password):
"""Test login and get JWT token."""
print("\n" + "="*60)
print("TEST 2: Login / JWT Authentication")
print("="*60)
url = f"{BASE_URL}/api/v1/auth/login"
print(f"POST {url}")
try:
response = requests.post(
url,
json={"username": username, "password": password},
headers={"Content-Type": "application/json"}
)
print(f"Status: {response.status_code}")
data = response.json()
print(f"Response: {json.dumps(data, indent=2)[:500]}")
if response.status_code == 200 and data.get("code") in [0, 200]:
token = data.get("data", {}).get("access_token")
if token:
print(f"✅ Login successful! Token: {token[:50]}...")
return token
else:
print("❌ Login response missing access_token")
return None
else:
print(f"❌ Login failed: {data.get('msg', 'Unknown error')}")
return None
except Exception as e:
print(f"❌ Error: {e}")
return None
def test_list_sessions(token):
"""Test listing chat sessions."""
print("\n" + "="*60)
print("TEST 3: List Chat Sessions")
print("="*60)
url = f"{BASE_URL}/agent/chat-sessions"
print(f"GET {url}")
try:
response = requests.get(
url,
headers={"Authorization": f"Bearer {token}"}
)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Response: {json.dumps(data, indent=2)[:1000]}")
sessions = data.get("data", {}).get("sessions", [])
print(f"✅ Found {len(sessions)} sessions")
return True
elif response.status_code == 404:
print(f"❌ 404 Not Found")
print(" The endpoint might not be registered correctly")
print(f" Response: {response.text[:500]}")
return False
else:
print(f"❌ Error: {response.text[:500]}")
return False
except Exception as e:
print(f"❌ Error: {e}")
return False
def test_create_session(token):
"""Test creating a new chat session."""
print("\n" + "="*60)
print("TEST 4: Create Chat Session")
print("="*60)
url = f"{BASE_URL}/agent/chat-sessions"
print(f"POST {url}")
try:
response = requests.post(
url,
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
},
json={
"name": "Test Session from Python Script",
"agent_type": "chat"
}
)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Response: {json.dumps(data, indent=2)[:1000]}")
session_id = data.get("data", {}).get("id")
if session_id:
print(f"✅ Created session: {session_id}")
return session_id
else:
print("❌ Session created but no ID returned")
return None
else:
print(f"❌ Error: {response.text[:500]}")
return None
except Exception as e:
print(f"❌ Error: {e}")
return None
def test_get_session(token, session_id):
"""Test getting a specific session."""
print("\n" + "="*60)
print("TEST 5: Get Specific Session")
print("="*60)
url = f"{BASE_URL}/agent/chat-sessions/{session_id}"
print(f"GET {url}")
try:
response = requests.get(
url,
headers={"Authorization": f"Bearer {token}"}
)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Response: {json.dumps(data, indent=2)[:1000]}")
print("✅ Session retrieved successfully")
return True
elif response.status_code == 404:
print(f"❌ 404 Not Found - Session doesn't exist or belongs to different user")
print(f" Response: {response.text[:500]}")
return False
else:
print(f"❌ Error: {response.text[:500]}")
return False
except Exception as e:
print(f"❌ Error: {e}")
return False
def test_file_upload(token):
"""Test file upload URL generation."""
print("\n" + "="*60)
print("TEST 6: Generate Upload URL")
print("="*60)
url = f"{BASE_URL}/agent/chat/generate-upload-url"
print(f"POST {url}")
try:
response = requests.post(
url,
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
},
json={
"file_name": "test.txt",
"content_type": "text/plain",
"file_size": 100
}
)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Response: {json.dumps(data, indent=2)[:500]}")
print("✅ Upload URL generated successfully")
return True
elif response.status_code == 500:
print(f"❌ 500 Internal Server Error")
print(f" Response: {response.text[:500]}")
print(" This might be a storage configuration issue")
return False
else:
print(f"❌ Error: {response.text[:500]}")
return False
except Exception as e:
print(f"❌ Error: {e}")
return False
def test_google_drive_status(token):
"""Test Google Drive connector status."""
print("\n" + "="*60)
print("TEST 7: Google Drive Status")
print("="*60)
url = f"{BASE_URL}/agent/connectors/google-drive/status"
print(f"GET {url}")
try:
response = requests.get(
url,
headers={"Authorization": f"Bearer {token}"}
)
print(f"Status: {response.status_code}")
data = response.json()
print(f"Response: {json.dumps(data, indent=2)}")
if response.status_code == 200:
print("✅ Google Drive endpoint working")
return True
else:
print(f"❌ Error")
return False
except Exception as e:
print(f"❌ Error: {e}")
return False
def print_available_routes():
"""Try to list available routes (if openapi.json is accessible)."""
print("\n" + "="*60)
print("AVAILABLE ROUTES (from OpenAPI)")
print("="*60)
try:
response = requests.get(f"{BASE_URL}/openapi.json", timeout=5)
if response.status_code == 200:
data = response.json()
paths = data.get("paths", {})
# Filter for agent routes
agent_routes = [path for path in paths.keys() if "agent" in path.lower() or "chat" in path.lower()]
print(f"Found {len(agent_routes)} agent/chat related routes:")
for path in sorted(agent_routes)[:30]:
methods = list(paths[path].keys())
print(f" {', '.join(m.upper() for m in methods)} {path}")
if len(agent_routes) > 30:
print(f" ... and {len(agent_routes) - 30} more")
else:
print(f"Could not fetch OpenAPI spec: {response.status_code}")
except Exception as e:
print(f"Error fetching routes: {e}")
# =============================================================================
# Main
# =============================================================================
def main():
global PASSWORD, BASE_URL, USERNAME
parser = argparse.ArgumentParser(description="Test backend API endpoints")
parser.add_argument("--password", "-p", required=False, help="Password for login")
parser.add_argument("--base-url", "-u", default=BASE_URL, help="Base URL of backend")
parser.add_argument("--username", default=USERNAME, help="Username for login")
args = parser.parse_args()
if args.password:
PASSWORD = args.password
if not PASSWORD:
print("ERROR: Password required. Use --password YOUR_PASSWORD")
print("Or edit the PASSWORD variable in this script")
sys.exit(1)
BASE_URL = args.base_url
USERNAME = args.username
print("="*60)
print("BACKEND API TEST SCRIPT")
print("="*60)
print(f"Base URL: {BASE_URL}")
print(f"Username: {USERNAME}")
# Run tests
results = {}
# Test 1: Backend running
if not test_backend_running():
print("\n❌ Backend is not running. Cannot continue tests.")
sys.exit(1)
results["backend_running"] = True
# Print available routes
print_available_routes()
# Test 2: Login
token = test_login(USERNAME, PASSWORD)
results["login"] = token is not None
if not token:
print("\n❌ Login failed. Cannot continue tests.")
sys.exit(1)
# Test 3: List sessions
results["list_sessions"] = test_list_sessions(token)
# Test 4: Create session
session_id = test_create_session(token)
results["create_session"] = session_id is not None
# Test 5: Get session
if session_id:
results["get_session"] = test_get_session(token, session_id)
else:
results["get_session"] = False
# Test 6: File upload
results["file_upload"] = test_file_upload(token)
# Test 7: Google Drive
results["google_drive"] = test_google_drive_status(token)
# Summary
print("\n" + "="*60)
print("TEST SUMMARY")
print("="*60)
for test_name, passed in results.items():
status = "✅ PASS" if passed else "❌ FAIL"
print(f" {test_name}: {status}")
total_passed = sum(1 for v in results.values() if v)
total_tests = len(results)
print(f"\nTotal: {total_passed}/{total_tests} tests passed")
if total_passed < total_tests:
print("\n⚠️ Some tests failed. Check the output above for details.")
print("\nNow run this SQL in Supabase to verify sessions:")
print("""
SELECT uuid, user_id, name, status, created_time
FROM agent_chat_sessions
WHERE user_id = 20
ORDER BY created_time DESC;
""")
if __name__ == "__main__":
main()