-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_management_test.py
More file actions
710 lines (605 loc) · 23 KB
/
agent_management_test.py
File metadata and controls
710 lines (605 loc) · 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#!/usr/bin/env python3
import requests
import json
import time
import os
import sys
from dotenv import load_dotenv
import uuid
import jwt
from datetime import datetime, timedelta
# Load environment variables from frontend/.env
load_dotenv('/app/frontend/.env')
# Get the backend URL from environment variables
BACKEND_URL = os.environ.get('REACT_APP_BACKEND_URL')
if not BACKEND_URL:
print("Error: REACT_APP_BACKEND_URL not found in environment variables")
sys.exit(1)
# Ensure the URL ends with /api
API_URL = f"{BACKEND_URL}/api"
print(f"Using API URL: {API_URL}")
# Load JWT secret from backend/.env for testing
load_dotenv('/app/backend/.env')
JWT_SECRET = os.environ.get('JWT_SECRET')
if not JWT_SECRET:
print("Warning: JWT_SECRET not found in environment variables. Some tests may fail.")
JWT_SECRET = "test_secret"
# Test results tracking
test_results = {
"passed": 0,
"failed": 0,
"tests": []
}
# Global variables for auth testing
auth_token = None
test_user_id = None
test_user_email = f"test.user.{uuid.uuid4()}@example.com"
test_user_password = "securePassword123"
test_user_name = "Test User"
created_agent_id = None
saved_agent_id = None
def run_test(test_name, endpoint, method="GET", data=None, expected_status=200, expected_keys=None, auth=False, headers=None, params=None, measure_time=False):
"""Run a test against the specified endpoint"""
url = f"{API_URL}{endpoint}"
print(f"\n{'='*80}\nTesting: {test_name} ({method} {url})")
# Set up headers with auth token if needed
if headers is None:
headers = {}
if auth and auth_token:
headers["Authorization"] = f"Bearer {auth_token}"
try:
start_time = time.time()
if method == "GET":
response = requests.get(url, headers=headers, params=params)
elif method == "POST":
response = requests.post(url, json=data, headers=headers, params=params)
elif method == "PUT":
response = requests.put(url, json=data, headers=headers, params=params)
elif method == "DELETE":
if data is not None:
response = requests.delete(url, json=data, headers=headers, params=params)
else:
response = requests.delete(url, headers=headers, params=params)
else:
print(f"Unsupported method: {method}")
return False, None
end_time = time.time()
response_time = end_time - start_time
# Print response details
print(f"Status Code: {response.status_code}")
print(f"Response Headers: {json.dumps(dict(response.headers), indent=2)}")
if measure_time:
print(f"Response Time: {response_time:.4f} seconds")
# Check if response is JSON
try:
response_data = response.json()
print(f"Response: {json.dumps(response_data, indent=2)}")
except json.JSONDecodeError:
print(f"Response is not JSON: {response.text}")
response_data = {}
# Verify status code
status_ok = response.status_code == expected_status
# Verify expected keys if provided
keys_ok = True
if expected_keys and status_ok:
for key in expected_keys:
if key not in response_data:
print(f"Missing expected key in response: {key}")
keys_ok = False
# Determine test result
test_passed = status_ok and keys_ok
# Update test results
result = "PASSED" if test_passed else "FAILED"
print(f"Test Result: {result}")
test_result = {
"name": test_name,
"endpoint": endpoint,
"method": method,
"status_code": response.status_code,
"expected_status": expected_status,
"result": result
}
if measure_time:
test_result["response_time"] = response_time
test_results["tests"].append(test_result)
if test_passed:
test_results["passed"] += 1
else:
test_results["failed"] += 1
return test_passed, response_data
except Exception as e:
print(f"Error during test: {e}")
test_results["tests"].append({
"name": test_name,
"endpoint": endpoint,
"method": method,
"result": "ERROR",
"error": str(e)
})
test_results["failed"] += 1
return False, None
def print_summary():
"""Print a summary of all test results"""
print("\n" + "="*80)
print(f"TEST SUMMARY: {test_results['passed']} passed, {test_results['failed']} failed")
print("="*80)
for i, test in enumerate(test_results["tests"], 1):
result_symbol = "✅" if test["result"] == "PASSED" else "❌"
print(f"{i}. {result_symbol} {test['name']} ({test['method']} {test['endpoint']})")
print("="*80)
overall_result = "PASSED" if test_results["failed"] == 0 else "FAILED"
print(f"OVERALL RESULT: {overall_result}")
print("="*80)
def test_register_user():
"""Register a new test user"""
global auth_token, test_user_id
register_data = {
"email": test_user_email,
"password": test_user_password,
"name": test_user_name
}
register_test, register_response = run_test(
"Register with valid credentials",
"/auth/register",
method="POST",
data=register_data,
expected_keys=["access_token", "token_type", "user"]
)
if register_test and register_response:
print("✅ Registration successful")
auth_token = register_response.get("access_token")
user_data = register_response.get("user", {})
test_user_id = user_data.get("id")
print(f"User registered with ID: {test_user_id}")
print(f"JWT Token: {auth_token}")
return True
else:
print("❌ Registration failed")
return False
def test_login():
"""Login with test endpoint to get auth token"""
global auth_token, test_user_id
# Try using the email/password login first
login_data = {
"email": test_user_email,
"password": test_user_password
}
login_test, login_response = run_test(
"Login with credentials",
"/auth/login",
method="POST",
data=login_data,
expected_keys=["access_token", "token_type", "user"]
)
# If email/password login fails, try the test login endpoint
if not login_test or not login_response:
test_login_test, test_login_response = run_test(
"Test Login Endpoint",
"/auth/test-login",
method="POST",
expected_keys=["access_token", "token_type", "user"]
)
# Store the token for further testing if successful
if test_login_test and test_login_response:
auth_token = test_login_response.get("access_token")
user_data = test_login_response.get("user", {})
test_user_id = user_data.get("id")
print(f"Test login successful. User ID: {test_user_id}")
print(f"JWT Token: {auth_token}")
return True
else:
print("Test login failed. Some tests may not work correctly.")
return False
else:
# Store the token from email/password login
auth_token = login_response.get("access_token")
user_data = login_response.get("user", {})
test_user_id = user_data.get("id")
print(f"Login successful. User ID: {test_user_id}")
print(f"JWT Token: {auth_token}")
return True
def test_create_agent():
"""Create a test agent"""
global created_agent_id
# Login first to get auth token
if not auth_token:
if not test_login():
print("❌ Cannot create test agent without authentication")
return False
# Create a test agent
agent_data = {
"name": f"Test Agent {uuid.uuid4().hex[:8]}",
"archetype": "scientist",
"personality": {
"extroversion": 7,
"optimism": 8,
"curiosity": 9,
"cooperativeness": 6,
"energy": 7
},
"goal": "To test the agent management functionality",
"expertise": "Software Testing",
"background": "Experienced in testing APIs and backend systems",
"memory_summary": "Created for testing agent update functionality",
"avatar_prompt": "A professional software tester with glasses"
}
create_agent_test, create_agent_response = run_test(
"Create Test Agent",
"/agents",
method="POST",
data=agent_data,
auth=True,
expected_keys=["id", "name", "archetype"]
)
if create_agent_test and create_agent_response:
created_agent_id = create_agent_response.get("id")
print(f"✅ Created test agent with ID: {created_agent_id}")
return True
else:
print("❌ Failed to create test agent")
return False
def test_update_agent():
"""Test updating an agent's details"""
global created_agent_id
# Create an agent first if needed
if not created_agent_id:
if not test_create_agent():
print("❌ Cannot test agent update without a test agent")
return False
# Update the agent's details
update_data = {
"name": f"Updated Agent {uuid.uuid4().hex[:8]}",
"archetype": "leader",
"personality": {
"extroversion": 9,
"optimism": 9,
"curiosity": 7,
"cooperativeness": 8,
"energy": 9
},
"goal": "To verify the agent update functionality works correctly",
"expertise": "API Testing and Verification",
"background": "Updated background information for testing",
"memory_summary": "Memory updated during testing"
}
update_agent_test, update_agent_response = run_test(
"Update Agent",
f"/agents/{created_agent_id}",
method="PUT",
data=update_data,
auth=True,
expected_keys=["id", "name", "archetype"]
)
if update_agent_test and update_agent_response:
# Verify the update was successful
if update_agent_response.get("name") == update_data["name"] and \
update_agent_response.get("archetype") == update_data["archetype"] and \
update_agent_response.get("goal") == update_data["goal"]:
print("✅ Agent update was successful")
return True
else:
print("❌ Agent update was not applied correctly")
return False
else:
print("❌ Failed to update agent")
return False
def test_save_agent_to_library():
"""Test saving an agent to the user's library"""
global created_agent_id, saved_agent_id
# Create an agent first if needed
if not created_agent_id:
if not test_create_agent():
print("❌ Cannot test saving agent without a test agent")
return False
# Since there's no GET endpoint for individual agents, we'll create a new agent data
# directly for saving to the library
save_data = {
"name": f"Saved Agent {uuid.uuid4().hex[:8]}",
"archetype": "scientist",
"personality": {
"extroversion": 7,
"optimism": 8,
"curiosity": 9,
"cooperativeness": 6,
"energy": 7
},
"goal": "To test the saved agent functionality",
"expertise": "Software Testing",
"background": "Experienced in testing APIs and backend systems",
"avatar_url": "",
"avatar_prompt": "A professional software tester with glasses",
"is_template": False
}
save_agent_test, save_agent_response = run_test(
"Save Agent to Library",
"/saved-agents",
method="POST",
data=save_data,
auth=True,
expected_keys=["id", "name", "user_id"]
)
if save_agent_test and save_agent_response:
saved_agent_id = save_agent_response.get("id")
print(f"✅ Saved agent to library with ID: {saved_agent_id}")
# Verify the saved agent belongs to the current user
if save_agent_response.get("user_id") == test_user_id:
print("✅ Saved agent is correctly associated with the current user")
return True
else:
print("❌ Saved agent is not correctly associated with the current user")
return False
else:
print("❌ Failed to save agent to library")
return False
def test_get_saved_agents():
"""Test retrieving the user's saved agents"""
global saved_agent_id
# Save an agent first if needed
if not saved_agent_id:
if not test_save_agent_to_library():
print("❌ Cannot test getting saved agents without a saved agent")
return False
# Get the user's saved agents
get_saved_agents_test, get_saved_agents_response = run_test(
"Get Saved Agents",
"/saved-agents",
method="GET",
auth=True
)
if get_saved_agents_test and get_saved_agents_response:
# Verify the saved agent is in the list
saved_agent_found = False
for agent in get_saved_agents_response:
if agent.get("id") == saved_agent_id:
saved_agent_found = True
break
if saved_agent_found:
print("✅ Saved agent found in the user's library")
return True
else:
print("❌ Saved agent not found in the user's library")
return False
else:
print("❌ Failed to get saved agents")
return False
def test_update_saved_agent():
"""Test updating a saved agent's details"""
global saved_agent_id
# Save an agent first if needed
if not saved_agent_id:
if not test_save_agent_to_library():
print("❌ Cannot test updating saved agent without a saved agent")
return False
# Update the saved agent's details
update_data = {
"name": f"Updated Saved Agent {uuid.uuid4().hex[:8]}",
"archetype": "optimist",
"personality": {
"extroversion": 8,
"optimism": 10,
"curiosity": 7,
"cooperativeness": 9,
"energy": 8
},
"goal": "To verify the saved agent update functionality works correctly",
"expertise": "Updated expertise for testing",
"background": "Updated background for saved agent testing",
"avatar_url": "",
"avatar_prompt": "A cheerful optimist with a bright smile"
}
update_saved_agent_test, update_saved_agent_response = run_test(
"Update Saved Agent",
f"/saved-agents/{saved_agent_id}",
method="PUT",
data=update_data,
auth=True,
expected_keys=["id", "name", "user_id"]
)
if update_saved_agent_test and update_saved_agent_response:
# Verify the update was successful
if update_saved_agent_response.get("name") == update_data["name"] and \
update_saved_agent_response.get("archetype") == update_data["archetype"] and \
update_saved_agent_response.get("goal") == update_data["goal"]:
print("✅ Saved agent update was successful")
return True
else:
print("❌ Saved agent update was not applied correctly")
return False
else:
print("❌ Failed to update saved agent")
return False
def test_delete_saved_agent():
"""Test deleting a saved agent"""
global saved_agent_id
# Save an agent first if needed
if not saved_agent_id:
if not test_save_agent_to_library():
print("❌ Cannot test deleting saved agent without a saved agent")
return False
# Delete the saved agent
delete_saved_agent_test, delete_saved_agent_response = run_test(
"Delete Saved Agent",
f"/saved-agents/{saved_agent_id}",
method="DELETE",
auth=True,
expected_keys=["message"]
)
if delete_saved_agent_test and delete_saved_agent_response:
print("✅ Saved agent deleted successfully")
# Verify the agent is actually deleted
get_saved_agents_test, get_saved_agents_response = run_test(
"Verify Saved Agent Deletion",
"/saved-agents",
method="GET",
auth=True
)
if get_saved_agents_test and get_saved_agents_response is not None:
# Check if the deleted agent is still in the list
agent_found = False
for agent in get_saved_agents_response:
if agent.get("id") == saved_agent_id:
agent_found = True
break
if not agent_found:
print("✅ Saved agent confirmed deleted")
return True
else:
print("❌ Saved agent still exists after deletion")
return False
else:
print("❌ Failed to verify saved agent deletion")
return False
else:
print("❌ Failed to delete saved agent")
return False
def test_authentication_flow():
"""Test the authentication flow for agent management endpoints"""
# Create a test agent without authentication
agent_data = {
"name": f"Unauthorized Agent {uuid.uuid4().hex[:8]}",
"archetype": "scientist",
"personality": {
"extroversion": 5,
"optimism": 5,
"curiosity": 5,
"cooperativeness": 5,
"energy": 5
},
"goal": "To test authentication",
"expertise": "Authentication Testing",
"background": "Testing authentication flow"
}
# Test creating agent without auth
no_auth_create_test, _ = run_test(
"Create Agent Without Auth",
"/agents",
method="POST",
data=agent_data,
expected_status=403 # Should fail with 403 Forbidden
)
if no_auth_create_test:
print("✅ Creating agent without authentication correctly fails")
else:
print("❌ Creating agent without authentication does not fail as expected")
# Test getting saved agents without auth
no_auth_get_test, _ = run_test(
"Get Saved Agents Without Auth",
"/saved-agents",
method="GET",
expected_status=403 # Should fail with 403 Forbidden
)
if no_auth_get_test:
print("✅ Getting saved agents without authentication correctly fails")
else:
print("❌ Getting saved agents without authentication does not fail as expected")
# Test with invalid token
invalid_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJpbnZhbGlkQGV4YW1wbGUuY29tIiwidXNlcl9pZCI6ImludmFsaWQtdXNlci1pZCIsImV4cCI6MTcxNjIzOTAyMn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
invalid_token_test, _ = run_test(
"Get Saved Agents With Invalid Token",
"/saved-agents",
method="GET",
headers={"Authorization": f"Bearer {invalid_token}"},
expected_status=401 # Should fail with 401 Unauthorized
)
if invalid_token_test:
print("✅ Using invalid token correctly fails")
else:
print("❌ Using invalid token does not fail as expected")
return no_auth_create_test and no_auth_get_test and invalid_token_test
def test_agent_management_workflow():
"""Test the complete agent management workflow"""
# Step 1: Register a new user
print("\n" + "="*80)
print("STEP 1: Register a new user")
print("="*80)
register_success = test_register_user()
if not register_success:
print("❌ Failed to register user, cannot continue workflow test")
return False
# Step 2: Create an agent
print("\n" + "="*80)
print("STEP 2: Create an agent")
print("="*80)
create_success = test_create_agent()
if not create_success:
print("❌ Failed to create agent, cannot continue workflow test")
return False
# Step 3: Update the agent's details
print("\n" + "="*80)
print("STEP 3: Update the agent's details")
print("="*80)
update_success = test_update_agent()
if not update_success:
print("❌ Failed to update agent, continuing workflow test")
# Step 4: Save the agent to library
print("\n" + "="*80)
print("STEP 4: Save the agent to library")
print("="*80)
save_success = test_save_agent_to_library()
if not save_success:
print("❌ Failed to save agent to library, cannot continue workflow test")
return False
# Step 5: Retrieve saved agents
print("\n" + "="*80)
print("STEP 5: Retrieve saved agents")
print("="*80)
get_success = test_get_saved_agents()
if not get_success:
print("❌ Failed to retrieve saved agents, continuing workflow test")
# Step 6: Update saved agent details
print("\n" + "="*80)
print("STEP 6: Update saved agent details")
print("="*80)
update_saved_success = test_update_saved_agent()
if not update_saved_success:
print("❌ Failed to update saved agent, continuing workflow test")
# Step 7: Delete saved agent
print("\n" + "="*80)
print("STEP 7: Delete saved agent")
print("="*80)
delete_success = test_delete_saved_agent()
if not delete_success:
print("❌ Failed to delete saved agent")
# Print workflow summary
print("\n" + "="*80)
print("AGENT MANAGEMENT WORKFLOW SUMMARY")
print("="*80)
workflow_steps = [
("Register User", register_success),
("Create Agent", create_success),
("Update Agent", update_success),
("Save Agent to Library", save_success),
("Retrieve Saved Agents", get_success),
("Update Saved Agent", update_saved_success),
("Delete Saved Agent", delete_success)
]
for step, success in workflow_steps:
result = "✅ PASSED" if success else "❌ FAILED"
print(f"{step}: {result}")
overall_success = all([
register_success,
create_success,
save_success
])
print("\nOverall Workflow: " + ("✅ PASSED" if overall_success else "❌ FAILED"))
return overall_success
def main():
"""Run all tests"""
print("\n" + "="*80)
print("TESTING AGENT MANAGEMENT FUNCTIONALITY")
print("="*80)
# Test authentication flow
print("\n" + "="*80)
print("TESTING AUTHENTICATION FLOW")
print("="*80)
auth_flow_success = test_authentication_flow()
# Test complete workflow
print("\n" + "="*80)
print("TESTING COMPLETE AGENT MANAGEMENT WORKFLOW")
print("="*80)
workflow_success = test_agent_management_workflow()
# Print overall summary
print_summary()
return auth_flow_success and workflow_success
if __name__ == "__main__":
main()