-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_bulk_delete_user_id_test.py
More file actions
354 lines (288 loc) · 11.6 KB
/
agent_bulk_delete_user_id_test.py
File metadata and controls
354 lines (288 loc) · 11.6 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
#!/usr/bin/env python3
import requests
import json
import time
import os
import sys
from dotenv import load_dotenv
import uuid
# 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}")
# Global variables
auth_token = None
test_user_id = None
created_agent_ids = []
def login():
"""Login to get auth token"""
global auth_token, test_user_id
# Try using the email/password login with admin credentials
login_data = {
"email": "dino@cytonic.com",
"password": "Observerinho8"
}
url = f"{API_URL}/auth/login"
print(f"Logging in with admin credentials: {url}")
try:
response = requests.post(url, json=login_data)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
response_data = response.json()
auth_token = response_data.get("access_token")
user_data = response_data.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
else:
print(f"Login failed: {response.text}")
return False
except Exception as e:
print(f"Error during login: {e}")
return False
def create_agent_with_user_id():
"""Create a test agent with user_id explicitly set"""
global auth_token, test_user_id, created_agent_ids
if not auth_token or not test_user_id:
print("Cannot create agent without authentication")
return False
agent_data = {
"name": f"Test Agent with User ID {uuid.uuid4().hex[:8]}",
"archetype": "scientist",
"personality": {
"extroversion": 5,
"optimism": 5,
"curiosity": 8,
"cooperativeness": 7,
"energy": 6
},
"goal": "Test goal for agent with user ID",
"expertise": "Test expertise with user ID",
"background": "Test background with user ID",
"user_id": test_user_id # Explicitly set user_id
}
url = f"{API_URL}/agents"
print(f"Creating agent with user_id: {url}")
headers = {"Authorization": f"Bearer {auth_token}"}
try:
response = requests.post(url, json=agent_data, headers=headers)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
response_data = response.json()
agent_id = response_data.get("id")
print(f"Created agent with ID: {agent_id}")
print(f"Response: {json.dumps(response_data, indent=2)}")
created_agent_ids.append(agent_id)
return agent_id
else:
print(f"Failed to create agent: {response.text}")
return None
except Exception as e:
print(f"Error creating agent: {e}")
return None
def get_agents():
"""Get all agents"""
global auth_token
if not auth_token:
print("Cannot get agents without authentication")
return None
url = f"{API_URL}/agents"
print(f"Getting agents: {url}")
headers = {"Authorization": f"Bearer {auth_token}"}
try:
response = requests.get(url, headers=headers)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
response_data = response.json()
print(f"Found {len(response_data)} agents")
return response_data
else:
print(f"Failed to get agents: {response.text}")
return None
except Exception as e:
print(f"Error getting agents: {e}")
return None
def test_bulk_delete_with_user_id():
"""Test bulk delete with agents that have user_id set"""
global auth_token, created_agent_ids
if not auth_token:
print("Cannot test bulk delete without authentication")
return False
# Create multiple agents with user_id
print("\nCreating multiple agents with user_id for bulk delete testing...")
agent_ids = []
for i in range(3):
agent_id = create_agent_with_user_id()
if agent_id:
agent_ids.append(agent_id)
if not agent_ids:
print("Failed to create any agents with user_id")
return False
print(f"Created {len(agent_ids)} agents with user_id: {agent_ids}")
# Test DELETE /api/agents/bulk endpoint
url = f"{API_URL}/agents/bulk"
print(f"\nTesting DELETE /api/agents/bulk with user_id: {url}")
headers = {"Authorization": f"Bearer {auth_token}"}
try:
response = requests.delete(url, json=agent_ids, headers=headers)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
response_data = response.json()
print(f"Response: {json.dumps(response_data, indent=2)}")
deleted_count = response_data.get("deleted_count", 0)
print(f"Successfully deleted {deleted_count} agents")
return True
else:
print(f"Failed to delete agents: {response.text}")
return False
except Exception as e:
print(f"Error deleting agents: {e}")
return False
def test_bulk_delete_post_with_user_id():
"""Test POST /api/agents/bulk-delete with agents that have user_id set"""
global auth_token, created_agent_ids
if not auth_token:
print("Cannot test bulk delete without authentication")
return False
# Create multiple agents with user_id
print("\nCreating multiple agents with user_id for bulk delete testing...")
agent_ids = []
for i in range(3):
agent_id = create_agent_with_user_id()
if agent_id:
agent_ids.append(agent_id)
if not agent_ids:
print("Failed to create any agents with user_id")
return False
print(f"Created {len(agent_ids)} agents with user_id: {agent_ids}")
# Test POST /api/agents/bulk-delete endpoint
url = f"{API_URL}/agents/bulk-delete"
print(f"\nTesting POST /api/agents/bulk-delete with user_id: {url}")
headers = {"Authorization": f"Bearer {auth_token}"}
data = {"agent_ids": agent_ids}
try:
response = requests.post(url, json=data, headers=headers)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
response_data = response.json()
print(f"Response: {json.dumps(response_data, indent=2)}")
deleted_count = response_data.get("deleted_count", 0)
print(f"Successfully deleted {deleted_count} agents")
return True
else:
print(f"Failed to delete agents: {response.text}")
return False
except Exception as e:
print(f"Error deleting agents: {e}")
return False
def test_direct_array_format():
"""Test POST /api/agents/bulk-delete with direct array format"""
global auth_token, created_agent_ids
if not auth_token:
print("Cannot test bulk delete without authentication")
return False
# Create multiple agents with user_id
print("\nCreating multiple agents with user_id for direct array format testing...")
agent_ids = []
for i in range(3):
agent_id = create_agent_with_user_id()
if agent_id:
agent_ids.append(agent_id)
if not agent_ids:
print("Failed to create any agents with user_id")
return False
print(f"Created {len(agent_ids)} agents with user_id: {agent_ids}")
# Test POST /api/agents/bulk-delete endpoint with direct array format
url = f"{API_URL}/agents/bulk-delete"
print(f"\nTesting POST /api/agents/bulk-delete with direct array format: {url}")
headers = {"Authorization": f"Bearer {auth_token}"}
try:
response = requests.post(url, json=agent_ids, headers=headers)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
response_data = response.json()
print(f"Response: {json.dumps(response_data, indent=2)}")
deleted_count = response_data.get("deleted_count", 0)
print(f"Successfully deleted {deleted_count} agents")
return True
else:
print(f"Failed to delete agents: {response.text}")
return False
except Exception as e:
print(f"Error deleting agents: {e}")
return False
def test_clear_all():
"""Test clear all functionality"""
global auth_token, test_user_id
if not auth_token or not test_user_id:
print("Cannot test clear all without authentication")
return False
# Create multiple agents with user_id
print("\nCreating multiple agents with user_id for clear all testing...")
for i in range(5):
create_agent_with_user_id()
# Get all agents
agents = get_agents()
if not agents:
print("Failed to get agents")
return False
# Filter agents that belong to the current user
user_agents = [agent for agent in agents if agent.get("user_id") == test_user_id]
print(f"Found {len(user_agents)} agents belonging to the current user")
if not user_agents:
print("No agents found belonging to the current user")
return False
# Get agent IDs
agent_ids = [agent.get("id") for agent in user_agents]
# Test DELETE /api/agents/bulk endpoint
url = f"{API_URL}/agents/bulk"
print(f"\nTesting clear all with DELETE /api/agents/bulk: {url}")
headers = {"Authorization": f"Bearer {auth_token}"}
try:
response = requests.delete(url, json=agent_ids, headers=headers)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
response_data = response.json()
print(f"Response: {json.dumps(response_data, indent=2)}")
deleted_count = response_data.get("deleted_count", 0)
print(f"Successfully deleted {deleted_count} agents")
# Verify all agents are deleted
agents_after = get_agents()
if agents_after:
user_agents_after = [agent for agent in agents_after if agent.get("user_id") == test_user_id]
if not user_agents_after:
print("All user agents were successfully deleted")
return True
else:
print(f"{len(user_agents_after)} user agents still exist after clear all")
return False
else:
print("Failed to get agents after clear all")
return False
else:
print(f"Failed to delete agents: {response.text}")
return False
except Exception as e:
print(f"Error deleting agents: {e}")
return False
if __name__ == "__main__":
print("Starting agent bulk delete with user_id tests...")
# Login first to get auth token
if login():
# Test bulk delete with user_id
test_bulk_delete_with_user_id()
# Test bulk delete post with user_id
test_bulk_delete_post_with_user_id()
# Test direct array format
test_direct_array_format()
# Test clear all
test_clear_all()
else:
print("Login failed. Cannot proceed with tests.")