-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_database_test.py
More file actions
377 lines (303 loc) · 13.1 KB
/
agent_database_test.py
File metadata and controls
377 lines (303 loc) · 13.1 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
#!/usr/bin/env python3
import requests
import json
import os
import sys
from dotenv import load_dotenv
import uuid
import time
# 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 for auth testing
auth_token = None
test_user_id = None
def login_as_admin():
"""Login with admin credentials"""
global auth_token, test_user_id
login_data = {
"email": "dino@cytonic.com",
"password": "Observerinho8"
}
print(f"\n{'='*80}\nLogging in as admin user (dino@cytonic.com)")
url = f"{API_URL}/auth/login"
response = requests.post(url, json=login_data)
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}")
return True
else:
print(f"❌ Login failed. Status code: {response.status_code}")
try:
print(f"Response: {response.json()}")
except:
print(f"Response: {response.text}")
return False
def create_test_user():
"""Create a test user for comparison"""
test_email = f"test.user.{uuid.uuid4()}@example.com"
test_password = "securePassword123"
test_name = "Test User"
print(f"\n{'='*80}\nCreating test user ({test_email})")
register_data = {
"email": test_email,
"password": test_password,
"name": test_name
}
url = f"{API_URL}/auth/register"
response = requests.post(url, json=register_data)
if response.status_code == 200:
response_data = response.json()
test_token = response_data.get("access_token")
test_user_data = response_data.get("user", {})
test_user_id = test_user_data.get("id")
print(f"✅ Test user created. User ID: {test_user_id}")
return test_token, test_user_id
else:
print(f"❌ Test user creation failed. Status code: {response.status_code}")
try:
print(f"Response: {response.json()}")
except:
print(f"Response: {response.text}")
return None, None
def get_agents(token=None):
"""Get all agents for the current user"""
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
url = f"{API_URL}/agents"
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
print(f"❌ Failed to get agents. Status code: {response.status_code}")
try:
print(f"Response: {response.json()}")
except:
print(f"Response: {response.text}")
return []
def send_observer_message(message, token=None):
"""Send an observer message"""
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
url = f"{API_URL}/observer/send-message"
data = {
"observer_message": message
}
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
return response.json()
else:
print(f"❌ Failed to send observer message. Status code: {response.status_code}")
try:
print(f"Response: {response.json()}")
except:
print(f"Response: {response.text}")
return None
def get_observer_messages(token=None):
"""Get all observer messages"""
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
url = f"{API_URL}/observer/messages"
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
print(f"❌ Failed to get observer messages. Status code: {response.status_code}")
try:
print(f"Response: {response.json()}")
except:
print(f"Response: {response.text}")
return []
def analyze_agents(agents):
"""Analyze the agents data"""
print(f"\n{'='*80}\nANALYZING {len(agents)} AGENTS")
# Count agents by archetype
archetype_counts = {}
for agent in agents:
archetype = agent.get("archetype", "unknown")
if archetype in archetype_counts:
archetype_counts[archetype] += 1
else:
archetype_counts[archetype] = 1
print("\nAgent count by archetype:")
for archetype, count in archetype_counts.items():
print(f" - {archetype}: {count}")
# Check user_id field
user_id_present = 0
user_id_missing = 0
user_ids = set()
for agent in agents:
user_id = agent.get("user_id", "")
if user_id:
user_id_present += 1
user_ids.add(user_id)
else:
user_id_missing += 1
print(f"\nUser ID analysis:")
print(f" - Agents with user_id: {user_id_present}")
print(f" - Agents without user_id: {user_id_missing}")
print(f" - Unique user_ids: {len(user_ids)}")
if user_ids:
print("\nUnique user_ids found:")
for uid in user_ids:
print(f" - {uid}")
# Check for test agents
test_agents = []
for agent in agents:
name = agent.get("name", "").lower()
if "test" in name or "dummy" in name or "sample" in name:
test_agents.append(agent)
if test_agents:
print(f"\nPotential test agents found: {len(test_agents)}")
for agent in test_agents:
print(f" - {agent.get('name')} (ID: {agent.get('id')}, User ID: {agent.get('user_id', 'None')})")
else:
print("\nNo obvious test agents found")
return {
"total": len(agents),
"archetype_counts": archetype_counts,
"user_id_present": user_id_present,
"user_id_missing": user_id_missing,
"unique_user_ids": list(user_ids),
"test_agents": len(test_agents)
}
def test_observer_message_with_auth():
"""Test the observer message endpoint with authentication"""
print(f"\n{'='*80}\nTESTING OBSERVER MESSAGE ENDPOINT WITH AUTHENTICATION")
if not auth_token:
print("❌ No auth token available. Please login first.")
return False
# Get agents before sending message
agents_before = get_agents(auth_token)
print(f"Found {len(agents_before)} agents for the current user")
# Send observer message
message = "This is a test message from the observer. Please acknowledge."
print(f"\nSending observer message: '{message}'")
response = send_observer_message(message, auth_token)
if response:
print("✅ Observer message sent successfully")
# Check agent responses
agent_responses = response.get("agent_responses", {}).get("messages", [])
# First message should be the observer message
if agent_responses and len(agent_responses) > 0:
first_message = agent_responses[0]
if first_message.get("agent_name") == "Observer (You)" and first_message.get("message") == message:
print("✅ Observer message appears as first message in response")
else:
print("❌ Observer message does not appear as first message in response")
# Count agent responses
agent_response_count = len(agent_responses) - 1 # Subtract 1 for the observer message
print(f"Received responses from {agent_response_count} agents")
# Compare with number of agents for the user
if agent_response_count == len(agents_before):
print("✅ Number of responses matches number of user's agents")
else:
print(f"❌ Number of responses ({agent_response_count}) does not match number of user's agents ({len(agents_before)})")
print("This suggests the observer message endpoint is not properly filtering agents by user_id")
return True
else:
print("❌ Failed to send observer message")
return False
def test_observer_message_without_auth():
"""Test the observer message endpoint without authentication"""
print(f"\n{'='*80}\nTESTING OBSERVER MESSAGE ENDPOINT WITHOUT AUTHENTICATION")
# Send observer message without auth token
message = "This is a test message without authentication."
print(f"\nSending observer message without auth: '{message}'")
response = send_observer_message(message)
if response:
print("❌ Observer message sent successfully without authentication")
print("This is a security issue - the endpoint should require authentication")
return False
else:
print("✅ Observer message endpoint correctly rejected unauthenticated request")
return True
def main():
"""Main test function"""
print(f"\n{'='*80}")
print("AGENT DATABASE AND OBSERVER MESSAGE TESTING")
print(f"{'='*80}")
# Login as admin
if not login_as_admin():
print("❌ Cannot proceed with testing without admin login")
sys.exit(1)
# Get all agents for admin user
admin_agents = get_agents(auth_token)
print(f"\nFound {len(admin_agents)} agents for admin user")
# Analyze admin agents
admin_analysis = analyze_agents(admin_agents)
# Create a test user for comparison
test_token, test_user_id = create_test_user()
if test_token:
# Get agents for test user
test_agents = get_agents(test_token)
print(f"\nFound {len(test_agents)} agents for test user")
# Analyze test user agents
if test_agents:
test_analysis = analyze_agents(test_agents)
# Compare admin and test user
print(f"\n{'='*80}\nCOMPARING ADMIN AND TEST USER")
print(f"Admin user has {len(admin_agents)} agents")
print(f"Test user has {len(test_agents)} agents")
# Test observer message with admin auth
test_observer_message_with_auth()
# Test observer message without auth
test_observer_message_without_auth()
# Test observer message with test user auth
print(f"\n{'='*80}\nTESTING OBSERVER MESSAGE WITH TEST USER AUTH")
# Get agents before sending message
test_user_agents = get_agents(test_token)
print(f"Found {len(test_user_agents)} agents for test user")
# Send observer message
message = "This is a test message from the test user observer. Please acknowledge."
print(f"\nSending observer message as test user: '{message}'")
response = send_observer_message(message, test_token)
if response:
print("✅ Observer message sent successfully as test user")
# Check agent responses
agent_responses = response.get("agent_responses", {}).get("messages", [])
# Count agent responses
agent_response_count = len(agent_responses) - 1 # Subtract 1 for the observer message
print(f"Received responses from {agent_response_count} agents")
# Compare with number of agents for the test user
if agent_response_count == len(test_user_agents):
print("✅ Number of responses matches number of test user's agents")
else:
print(f"❌ Number of responses ({agent_response_count}) does not match number of test user's agents ({len(test_user_agents)})")
print("This suggests the observer message endpoint is not properly filtering agents by user_id")
# Print summary
print(f"\n{'='*80}\nSUMMARY")
print(f"{'='*80}")
print(f"Admin user has {len(admin_agents)} agents")
if admin_analysis["user_id_missing"] > 0:
print(f"⚠️ {admin_analysis['user_id_missing']} agents have no user_id")
if admin_analysis["unique_user_ids"] and len(admin_analysis["unique_user_ids"]) > 1:
print(f"⚠️ Found {len(admin_analysis['unique_user_ids'])} different user_ids in admin's agents")
if admin_analysis["test_agents"] > 0:
print(f"⚠️ Found {admin_analysis['test_agents']} potential test agents")
# Check observer message endpoint
print("\nObserver Message Endpoint:")
print("- The observer message endpoint is not properly filtering agents by user_id")
print("- It's getting all agents from the database instead of just the current user's agents")
print("- This is why so many agents are responding to observer messages")
# Provide recommendations
print("\nRecommendations:")
print("1. Fix the observer message endpoint to filter agents by user_id")
print("2. Add authentication requirement to the observer message endpoint")
print("3. Clean up any test agents or agents without user_id")
print("4. Ensure all agents have the correct user_id")
if __name__ == "__main__":
main()