Repository navigation
Expand file tree
/
Copy pathtest_api.py
More file actions
538 lines (459 loc) · 21 KB
/
Copy pathtest_api.py
File metadata and controls
538 lines (459 loc) · 21 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
"""
Test Suite for Support Copilot API
Comprehensive testing for all endpoints and functionality
"""
import pytest
import json
import jwt
from datetime import datetime, timedelta
from unittest.mock import Mock, patch
from app import app
from database.db_manager import DatabaseManager
from services.issue_analysis_service import IssueAnalysisService
from services.guidance_service import GuidanceService
from services.summarization_service import SummarizationService
class TestSupportCopilotAPI:
@pytest.fixture
def client(self):
"""Create test client"""
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'test-secret-key'
with app.test_client() as client:
yield client
@pytest.fixture
def auth_headers(self):
"""Create authentication headers"""
token = jwt.encode(
{'user_id': 1, 'exp': datetime.utcnow() + timedelta(hours=1)},
'test-secret-key',
algorithm='HS256'
)
return {'Authorization': f'Bearer {token}'}
def test_health_check(self, client):
"""Test health check endpoint"""
response = client.get('/health')
assert response.status_code == 200
data = json.loads(response.data)
assert data['status'] == 'healthy'
assert 'timestamp' in data
assert 'version' in data
def test_login_success(self, client):
"""Test successful login"""
with patch('utils.auth.AuthManager.authenticate_user') as mock_auth:
mock_auth.return_value = {
'user_id': 1,
'email': 'test@example.com',
'name': 'Test User'
}
response = client.post('/auth/login', json={
'email': 'test@example.com',
'password': 'password123'
})
assert response.status_code == 200
data = json.loads(response.data)
assert 'token' in data
assert 'user' in data
assert data['user']['email'] == 'test@example.com'
def test_login_invalid_credentials(self, client):
"""Test login with invalid credentials"""
with patch('utils.auth.AuthManager.authenticate_user') as mock_auth:
mock_auth.return_value = None
response = client.post('/auth/login', json={
'email': 'test@example.com',
'password': 'wrongpassword'
})
assert response.status_code == 401
data = json.loads(response.data)
assert data['error'] == 'Invalid credentials'
def test_login_missing_fields(self, client):
"""Test login with missing fields"""
response = client.post('/auth/login', json={
'email': 'test@example.com'
# Missing password
})
assert response.status_code == 400
data = json.loads(response.data)
assert 'Email and password required' in data['error']
def test_analyze_issue_success(self, client, auth_headers):
"""Test successful issue analysis"""
with patch('services.issue_analysis_service.IssueAnalysisService.analyze_new_issue') as mock_analyze:
mock_analyze.return_value = {
'issue_id': 123,
'severity': 'High',
'priority_score': 8,
'similar_issues': [],
'critical_alerts': [],
'ai_insights': {
'root_cause': 'Authentication service issue',
'estimated_time_hours': 4
}
}
response = client.post('/api/issues/analyze',
headers=auth_headers,
json={
'customer_id': 1,
'title': 'Login not working',
'description': 'Users cannot log in after update',
'category': 'Technical'
}
)
assert response.status_code == 200
data = json.loads(response.data)
assert data['issue_id'] == 123
assert data['severity'] == 'High'
assert data['priority_score'] == 8
def test_analyze_issue_missing_fields(self, client, auth_headers):
"""Test issue analysis with missing required fields"""
response = client.post('/api/issues/analyze',
headers=auth_headers,
json={
'customer_id': 1,
'title': 'Login not working'
# Missing description and category
}
)
assert response.status_code == 400
data = json.loads(response.data)
assert 'Missing required field' in data['error']
def test_analyze_issue_unauthorized(self, client):
"""Test issue analysis without authentication"""
response = client.post('/api/issues/analyze', json={
'customer_id': 1,
'title': 'Login not working',
'description': 'Users cannot log in',
'category': 'Technical'
})
assert response.status_code == 401
data = json.loads(response.data)
assert data['error'] == 'Token is missing'
def test_get_similar_issues(self, client, auth_headers):
"""Test getting similar issues"""
with patch('services.issue_analysis_service.IssueAnalysisService.find_similar_issues') as mock_similar:
mock_similar.return_value = [
{
'issue_id': 456,
'title': 'Authentication failure',
'similarity_score': 0.85,
'resolution_time_hours': 6
}
]
response = client.get('/api/issues/123/similar?limit=5', headers=auth_headers)
assert response.status_code == 200
data = json.loads(response.data)
assert data['issue_id'] == 123
assert len(data['similar_issues']) == 1
assert data['similar_issues'][0]['similarity_score'] == 0.85
def test_get_customer_history(self, client, auth_headers):
"""Test getting customer history"""
with patch('services.issue_analysis_service.IssueAnalysisService.get_customer_history') as mock_history:
mock_history.return_value = {
'customer_info': {
'customer_id': 1,
'customer_name': 'Test Customer',
'tier': 'Premium'
},
'statistics': {
'total_issues': 5,
'resolved_issues': 4,
'avg_resolution_time': 24.5
}
}
response = client.get('/api/customers/1/history', headers=auth_headers)
assert response.status_code == 200
data = json.loads(response.data)
assert data['customer_info']['customer_name'] == 'Test Customer'
assert data['statistics']['total_issues'] == 5
def test_generate_response_template(self, client, auth_headers):
"""Test generating response template"""
with patch('services.guidance_service.GuidanceService.generate_response_template') as mock_template:
mock_template.return_value = {
'issue_id': 123,
'template_category': 'initial_response',
'recommended_template': {
'content': 'Dear {{customer_name}}, thank you for contacting us...',
'variables': ['customer_name', 'issue_id'],
'suggested_values': {
'customer_name': 'Test Customer',
'issue_id': '123'
}
},
'confidence_score': 0.85
}
response = client.post('/api/guidance/template',
headers=auth_headers,
json={
'issue_id': 123,
'message_content': 'I need help with login issues'
}
)
assert response.status_code == 200
data = json.loads(response.data)
assert data['issue_id'] == 123
assert data['template_category'] == 'initial_response'
assert data['confidence_score'] == 0.85
def test_generate_template_missing_fields(self, client, auth_headers):
"""Test template generation with missing fields"""
response = client.post('/api/guidance/template',
headers=auth_headers,
json={
'issue_id': 123
# Missing message_content
}
)
assert response.status_code == 400
data = json.loads(response.data)
assert 'Missing required fields' in data['error']
def test_get_templates(self, client, auth_headers):
"""Test getting available templates"""
with patch('services.guidance_service.GuidanceService.get_available_templates') as mock_templates:
mock_templates.return_value = [
{
'template_id': 1,
'template_name': 'Initial Response - Technical',
'category': 'initial_response',
'effectiveness_score': 0.9
}
]
response = client.get('/api/guidance/templates?category=initial_response',
headers=auth_headers)
assert response.status_code == 200
data = json.loads(response.data)
assert len(data['templates']) == 1
assert data['templates'][0]['template_name'] == 'Initial Response - Technical'
def test_summarize_conversation(self, client, auth_headers):
"""Test conversation summarization"""
with patch('services.summarization_service.SummarizationService.generate_conversation_summary') as mock_summary:
mock_summary.return_value = {
'issue_id': 123,
'summary_text': 'Customer reported login issues...',
'key_points': ['Authentication failure', 'Recent system update'],
'action_items': [
{
'description': 'Check authentication service',
'assigned_to': 'Support',
'priority': 'High'
}
]
}
response = client.post('/api/issues/123/summarize', headers=auth_headers)
assert response.status_code == 200
data = json.loads(response.data)
assert data['issue_id'] == 123
assert 'Customer reported login issues' in data['summary_text']
assert len(data['action_items']) == 1
def test_get_critical_alerts(self, client, auth_headers):
"""Test getting critical alerts"""
with patch('services.issue_analysis_service.IssueAnalysisService.get_critical_alerts') as mock_alerts:
mock_alerts.return_value = [
{
'alert_id': 1,
'issue_id': 123,
'alert_type': 'Unattended',
'severity': 'Critical',
'created_at': '2023-01-01T10:00:00'
}
]
response = client.get('/api/alerts/critical', headers=auth_headers)
assert response.status_code == 200
data = json.loads(response.data)
assert len(data['alerts']) == 1
assert data['alerts'][0]['alert_type'] == 'Unattended'
def test_acknowledge_alert(self, client, auth_headers):
"""Test acknowledging an alert"""
with patch('services.issue_analysis_service.IssueAnalysisService.acknowledge_alert') as mock_ack:
mock_ack.return_value = {
'success': True,
'message': 'Alert acknowledged successfully'
}
response = client.post('/api/alerts/1/acknowledge', headers=auth_headers)
assert response.status_code == 200
data = json.loads(response.data)
assert data['success'] is True
assert 'acknowledged successfully' in data['message']
def test_get_issues_with_filters(self, client, auth_headers):
"""Test getting issues with filters"""
with patch('database.db_manager.DatabaseManager.get_issues_with_filters') as mock_issues:
mock_issues.return_value = {
'issues': [
{
'issue_id': 123,
'title': 'Login problem',
'severity': 'High',
'status': 'Open',
'customer_name': 'Test Customer'
}
],
'pagination': {
'page': 1,
'per_page': 20,
'total': 1,
'pages': 1
}
}
response = client.get('/api/issues?status=Open&severity=High',
headers=auth_headers)
assert response.status_code == 200
data = json.loads(response.data)
assert len(data['issues']) == 1
assert data['issues'][0]['severity'] == 'High'
assert data['pagination']['total'] == 1
def test_get_issue_details(self, client, auth_headers):
"""Test getting issue details"""
with patch('database.db_manager.DatabaseManager.get_issue_details') as mock_details:
mock_details.return_value = {
'issue_id': 123,
'title': 'Login problem',
'description': 'Users cannot log in',
'severity': 'High',
'status': 'Open',
'customer_name': 'Test Customer',
'created_at': '2023-01-01T10:00:00'
}
response = client.get('/api/issues/123', headers=auth_headers)
assert response.status_code == 200
data = json.loads(response.data)
assert data['issue_id'] == 123
assert data['title'] == 'Login problem'
assert data['severity'] == 'High'
def test_get_issue_details_not_found(self, client, auth_headers):
"""Test getting details for non-existent issue"""
with patch('database.db_manager.DatabaseManager.get_issue_details') as mock_details:
mock_details.return_value = None
response = client.get('/api/issues/999', headers=auth_headers)
assert response.status_code == 404
data = json.loads(response.data)
assert data['error'] == 'Issue not found'
def test_update_issue_status(self, client, auth_headers):
"""Test updating issue status"""
with patch('database.db_manager.DatabaseManager.update_issue_status') as mock_update:
mock_update.return_value = {
'success': True,
'message': 'Issue status updated to Resolved'
}
response = client.put('/api/issues/123/status',
headers=auth_headers,
json={'status': 'Resolved'}
)
assert response.status_code == 200
data = json.loads(response.data)
assert data['success'] is True
assert 'Resolved' in data['message']
def test_update_issue_status_missing_status(self, client, auth_headers):
"""Test updating issue status without providing status"""
response = client.put('/api/issues/123/status',
headers=auth_headers,
json={}
)
assert response.status_code == 400
data = json.loads(response.data)
assert data['error'] == 'Status is required'
def test_get_dashboard_analytics(self, client, auth_headers):
"""Test getting dashboard analytics"""
with patch('database.db_manager.DatabaseManager.get_dashboard_analytics') as mock_analytics:
mock_analytics.return_value = {
'issue_statistics': {
'total_issues': 100,
'open_issues': 25,
'resolved_issues': 70,
'critical_issues': 5
},
'performance_metrics': {
'avg_response_time_ms': 2500,
'error_count': 2
}
}
response = client.get('/api/analytics/dashboard', headers=auth_headers)
assert response.status_code == 200
data = json.loads(response.data)
assert data['issue_statistics']['total_issues'] == 100
assert data['performance_metrics']['avg_response_time_ms'] == 2500
def test_rate_limiting(self, client, auth_headers):
"""Test rate limiting functionality"""
# This would require actual rate limiting setup
# For now, we'll test that the endpoint responds normally
response = client.get('/api/issues', headers=auth_headers)
assert response.status_code in [200, 429] # Either success or rate limited
def test_invalid_token(self, client):
"""Test request with invalid token"""
headers = {'Authorization': 'Bearer invalid-token'}
response = client.get('/api/issues', headers=headers)
assert response.status_code == 401
data = json.loads(response.data)
assert 'Token is invalid' in data['error']
def test_expired_token(self, client):
"""Test request with expired token"""
expired_token = jwt.encode(
{'user_id': 1, 'exp': datetime.utcnow() - timedelta(hours=1)},
'test-secret-key',
algorithm='HS256'
)
headers = {'Authorization': f'Bearer {expired_token}'}
response = client.get('/api/issues', headers=headers)
assert response.status_code == 401
data = json.loads(response.data)
assert 'Token has expired' in data['error']
def test_404_endpoint(self, client):
"""Test non-existent endpoint"""
response = client.get('/api/nonexistent')
assert response.status_code == 404
data = json.loads(response.data)
assert data['error'] == 'Endpoint not found'
# Performance Tests
class TestPerformance:
def test_response_time_under_threshold(self, client, auth_headers):
"""Test that API responses are under 15 seconds"""
import time
start_time = time.time()
response = client.get('/health')
end_time = time.time()
response_time = end_time - start_time
assert response_time < 15.0 # Should be much faster than 15 seconds
assert response.status_code == 200
# Integration Tests
class TestIntegration:
@pytest.fixture
def mock_db(self):
"""Mock database for integration tests"""
with patch('database.db_manager.DatabaseManager') as mock:
yield mock
def test_full_issue_workflow(self, client, auth_headers, mock_db):
"""Test complete issue workflow from creation to resolution"""
# This would test the full workflow in a real integration test
# For now, we'll test that the endpoints can be called in sequence
# 1. Analyze new issue
with patch('services.issue_analysis_service.IssueAnalysisService.analyze_new_issue'):
response = client.post('/api/issues/analyze',
headers=auth_headers,
json={
'customer_id': 1,
'title': 'Test issue',
'description': 'Test description',
'category': 'Technical'
}
)
assert response.status_code == 200
# 2. Generate response template
with patch('services.guidance_service.GuidanceService.generate_response_template'):
response = client.post('/api/guidance/template',
headers=auth_headers,
json={
'issue_id': 123,
'message_content': 'Customer needs help'
}
)
assert response.status_code == 200
# 3. Update issue status
with patch('database.db_manager.DatabaseManager.update_issue_status'):
response = client.put('/api/issues/123/status',
headers=auth_headers,
json={'status': 'Resolved'}
)
assert response.status_code == 200
# 4. Generate summary
with patch('services.summarization_service.SummarizationService.generate_conversation_summary'):
response = client.post('/api/issues/123/summarize',
headers=auth_headers
)
assert response.status_code == 200
if __name__ == '__main__':
pytest.main([__file__, '-v'])