-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend_test.py
More file actions
292 lines (254 loc) · 8.69 KB
/
backend_test.py
File metadata and controls
292 lines (254 loc) · 8.69 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
#!/usr/bin/env python3
import requests
import sys
import json
from datetime import datetime
class TelegramBotAPITester:
def __init__(self, base_url="https://message-assistant-2.preview.emergentagent.com"):
self.base_url = base_url
self.api_url = f"{base_url}/api"
self.tests_run = 0
self.tests_passed = 0
self.failed_tests = []
def run_test(self, name, method, endpoint, expected_status, data=None, params=None):
"""Run a single API test"""
url = f"{self.api_url}/{endpoint}"
headers = {'Content-Type': 'application/json'}
self.tests_run += 1
print(f"\n🔍 Testing {name}...")
print(f" URL: {url}")
try:
if method == 'GET':
response = requests.get(url, headers=headers, params=params, timeout=10)
elif method == 'POST':
response = requests.post(url, json=data, headers=headers, timeout=10)
elif method == 'PUT':
response = requests.put(url, json=data, headers=headers, timeout=10)
elif method == 'DELETE':
response = requests.delete(url, headers=headers, timeout=10)
success = response.status_code == expected_status
if success:
self.tests_passed += 1
print(f"✅ Passed - Status: {response.status_code}")
try:
response_data = response.json()
print(f" Response: {json.dumps(response_data, indent=2)[:200]}...")
except:
print(f" Response: {response.text[:100]}...")
else:
print(f"❌ Failed - Expected {expected_status}, got {response.status_code}")
print(f" Response: {response.text[:200]}...")
self.failed_tests.append({
"test": name,
"endpoint": endpoint,
"expected": expected_status,
"actual": response.status_code,
"response": response.text[:200]
})
return success, response.json() if success and response.text else {}
except requests.exceptions.Timeout:
print(f"❌ Failed - Request timeout")
self.failed_tests.append({
"test": name,
"endpoint": endpoint,
"error": "Request timeout"
})
return False, {}
except requests.exceptions.ConnectionError:
print(f"❌ Failed - Connection error")
self.failed_tests.append({
"test": name,
"endpoint": endpoint,
"error": "Connection error"
})
return False, {}
except Exception as e:
print(f"❌ Failed - Error: {str(e)}")
self.failed_tests.append({
"test": name,
"endpoint": endpoint,
"error": str(e)
})
return False, {}
def test_root_endpoint(self):
"""Test root API endpoint"""
return self.run_test(
"Root API Endpoint",
"GET",
"",
200
)
def test_dashboard_stats(self):
"""Test dashboard statistics endpoint"""
return self.run_test(
"Dashboard Statistics",
"GET",
"dashboard/stats",
200
)
def test_users_endpoint(self):
"""Test users list endpoint"""
return self.run_test(
"Users List",
"GET",
"users",
200
)
def test_users_count(self):
"""Test users count endpoint"""
return self.run_test(
"Users Count",
"GET",
"users/count",
200
)
def test_daily_analytics(self):
"""Test daily analytics endpoint"""
return self.run_test(
"Daily Analytics",
"GET",
"analytics/daily",
200
)
def test_bot_status(self):
"""Test bot status endpoint"""
return self.run_test(
"Bot Status",
"GET",
"bot/status",
200
)
def test_users_with_filters(self):
"""Test users endpoint with filters"""
# Test country filter
success1, _ = self.run_test(
"Users with Country Filter",
"GET",
"users",
200,
params={"country": "ukraine"}
)
# Test language filter
success2, _ = self.run_test(
"Users with Language Filter",
"GET",
"users",
200,
params={"language": "en"}
)
# Test pagination
success3, _ = self.run_test(
"Users with Pagination",
"GET",
"users",
200,
params={"skip": 0, "limit": 10}
)
return success1 and success2 and success3
def test_users_count_with_filters(self):
"""Test users count with filters"""
success1, _ = self.run_test(
"Users Count with Country Filter",
"GET",
"users/count",
200,
params={"country": "ukraine"}
)
success2, _ = self.run_test(
"Users Count with Language Filter",
"GET",
"users/count",
200,
params={"language": "en"}
)
return success1 and success2
def test_daily_analytics_with_params(self):
"""Test daily analytics with different day parameters"""
return self.run_test(
"Daily Analytics (14 days)",
"GET",
"analytics/daily",
200,
params={"days": 14}
)
def test_applications_endpoint(self):
"""Test applications list endpoint"""
return self.run_test(
"Applications List",
"GET",
"applications",
200
)
def test_applications_count(self):
"""Test applications count endpoint"""
return self.run_test(
"Applications Count",
"GET",
"applications/count",
200
)
def test_applications_with_filters(self):
"""Test applications endpoint with filters"""
# Test status filter
success1, _ = self.run_test(
"Applications with Status Filter",
"GET",
"applications",
200,
params={"status": "new"}
)
# Test pagination
success2, _ = self.run_test(
"Applications with Pagination",
"GET",
"applications",
200,
params={"skip": 0, "limit": 10}
)
return success1 and success2
def test_applications_count_with_filters(self):
"""Test applications count with filters"""
return self.run_test(
"Applications Count with Status Filter",
"GET",
"applications/count",
200,
params={"status": "new"}
)
def main():
print("🚀 Starting Telegram HR Bot API Tests")
print("=" * 50)
# Setup
tester = TelegramBotAPITester()
# Run core API tests
print("\n📋 Testing Core API Endpoints...")
tester.test_root_endpoint()
tester.test_dashboard_stats()
tester.test_users_endpoint()
tester.test_users_count()
tester.test_daily_analytics()
tester.test_bot_status()
# Run filtered endpoint tests
print("\n🔍 Testing Filtered Endpoints...")
tester.test_users_with_filters()
tester.test_users_count_with_filters()
tester.test_daily_analytics_with_params()
# Run applications endpoint tests
print("\n📝 Testing Applications Endpoints...")
tester.test_applications_endpoint()
tester.test_applications_count()
tester.test_applications_with_filters()
tester.test_applications_count_with_filters()
# Print results
print("\n" + "=" * 50)
print(f"📊 Test Results: {tester.tests_passed}/{tester.tests_run} passed")
if tester.failed_tests:
print(f"\n❌ Failed Tests ({len(tester.failed_tests)}):")
for test in tester.failed_tests:
error_msg = test.get('error', f"Expected {test.get('expected')}, got {test.get('actual')}")
print(f" - {test['test']}: {error_msg}")
success_rate = (tester.tests_passed / tester.tests_run) * 100 if tester.tests_run > 0 else 0
print(f"\n✨ Success Rate: {success_rate:.1f}%")
return 0 if tester.tests_passed == tester.tests_run else 1
if __name__ == "__main__":
sys.exit(main())