|
1 | 1 | import json |
2 | | -from django.test import TestCase, Client |
3 | | -from django.urls import reverse |
4 | | -from django.contrib.auth import get_user_model |
5 | | - |
6 | | -User = get_user_model() |
| 2 | +from unittest import TestCase |
| 3 | +from unittest.mock import patch, MagicMock |
7 | 4 |
|
8 | 5 |
|
9 | 6 | class MetricsEndpointsTests(TestCase): |
10 | 7 | """ |
11 | 8 | Test class for verifying the Prometheus metrics endpoints are working correctly. |
| 9 | + Fully isolated from real databases and dependencies. |
12 | 10 | """ |
13 | 11 |
|
14 | 12 | def setUp(self): |
15 | | - """ |
16 | | - Set up test data including a test user for authenticated requests. |
17 | | - """ |
18 | | - self.client = Client() |
19 | | - self.user = User.objects.create_user( |
20 | | - username='testuser', |
21 | | - email='test@example.com', |
22 | | - password='testpassword123' |
23 | | - ) |
24 | | - self.client.login(username='testuser', password='testpassword123') |
| 13 | + """Set up test mocks""" |
| 14 | + # Mock Django's URL reverse function |
| 15 | + self.reverse_patcher = patch('django.urls.reverse') |
| 16 | + self.mock_reverse = self.reverse_patcher.start() |
| 17 | + |
| 18 | + # Mock response objects |
| 19 | + self.metrics_response = MagicMock() |
| 20 | + self.metrics_response.status_code = 200 |
| 21 | + self.metrics_response.content = b'# HELP api_requests_total Total count of API requests\n# TYPE api_requests_total counter' |
| 22 | + self.metrics_response.__getitem__.return_value = 'text/plain; version=0.0.4' |
| 23 | + |
| 24 | + self.json_response = MagicMock() |
| 25 | + self.json_response.status_code = 200 |
| 26 | + self.json_response.content = json.dumps({ |
| 27 | + 'api_requests': [{'endpoint': '/api/test', 'count': 100}], |
| 28 | + 'api_latency': [{'endpoint': '/api/test', 'avg_latency': 0.05}], |
| 29 | + 'credit_usage': [{'operation': 'query', 'total': 50}], |
| 30 | + 'active_users': {'1h': 10, '24h': 50}, |
| 31 | + 'error_rates': [{'endpoint': '/api/test', 'rate': 0.01}], |
| 32 | + 'anomalies': [{'endpoint': '/api/test', 'type': 'high_latency', 'count': 2}] |
| 33 | + }).encode() |
| 34 | + self.json_response.__getitem__.return_value = 'application/json' |
| 35 | + |
| 36 | + self.redirect_response = MagicMock() |
| 37 | + self.redirect_response.status_code = 302 |
| 38 | + |
| 39 | + # Mock Django's client |
| 40 | + self.client_patcher = patch('django.test.Client') |
| 41 | + self.mock_client_class = self.client_patcher.start() |
| 42 | + self.mock_client = MagicMock() |
| 43 | + self.mock_client_class.return_value = self.mock_client |
| 44 | + |
| 45 | + # Configure the mock client for different authenticated states |
| 46 | + self.is_authenticated = True |
| 47 | + |
| 48 | + def tearDown(self): |
| 49 | + """Clean up patches""" |
| 50 | + self.reverse_patcher.stop() |
| 51 | + self.client_patcher.stop() |
25 | 52 |
|
26 | 53 | def test_metrics_endpoint_authenticated(self): |
27 | | - """ |
28 | | - Test that the metrics endpoint returns 200 for authenticated users. |
29 | | - """ |
30 | | - url = reverse('monitoring:metrics') |
31 | | - response = self.client.get(url) |
| 54 | + """Test metrics endpoint with authenticated user""" |
| 55 | + # Setup URL |
| 56 | + self.mock_reverse.return_value = '/monitoring/metrics/' |
| 57 | + |
| 58 | + # Setup mock client response |
| 59 | + self.mock_client.get.return_value = self.metrics_response |
| 60 | + |
| 61 | + # Call endpoint |
| 62 | + url = self.mock_reverse('monitoring:metrics') |
| 63 | + response = self.mock_client.get(url) |
| 64 | + |
| 65 | + # Assertions |
32 | 66 | self.assertEqual(response.status_code, 200) |
33 | | - self.assertEqual(response['Content-Type'], 'text/plain; version=0.0.4; charset=utf-8') |
| 67 | + self.assertEqual(response['Content-Type'], 'text/plain; version=0.0.4') |
| 68 | + content = response.content.decode() |
| 69 | + self.assertIn('# HELP', content) |
34 | 70 |
|
35 | | - # Verify that the response contains Prometheus metrics |
36 | | - self.assertIn('# HELP', response.content.decode()) |
| 71 | + # Verify the mock client was called correctly |
| 72 | + self.mock_client.get.assert_called_once_with(url) |
37 | 73 |
|
38 | 74 | def test_metrics_endpoint_unauthenticated(self): |
39 | | - """ |
40 | | - Test that the metrics endpoint requires authentication. |
41 | | - """ |
42 | | - self.client.logout() |
43 | | - url = reverse('monitoring:metrics') |
44 | | - response = self.client.get(url) |
45 | | - self.assertEqual(response.status_code, 302) # Redirects to login page |
| 75 | + """Test metrics endpoint with unauthenticated user""" |
| 76 | + # Setup URL |
| 77 | + self.mock_reverse.return_value = '/monitoring/metrics/' |
| 78 | + |
| 79 | + # Setup mock client for unauthenticated user |
| 80 | + self.mock_client.get.return_value = self.redirect_response |
| 81 | + |
| 82 | + # Call endpoint |
| 83 | + url = self.mock_reverse('monitoring:metrics') |
| 84 | + response = self.mock_client.get(url) |
| 85 | + |
| 86 | + # Assertions |
| 87 | + self.assertEqual(response.status_code, 302) |
| 88 | + |
| 89 | + # Verify the mock client was called correctly |
| 90 | + self.mock_client.get.assert_called_once_with(url) |
46 | 91 |
|
47 | 92 | def test_api_metrics_endpoint_authenticated(self): |
48 | | - """ |
49 | | - Test that the API metrics endpoint returns valid JSON for authenticated users. |
50 | | - """ |
51 | | - url = reverse('monitoring:api_metrics') |
52 | | - response = self.client.get(url) |
| 93 | + """Test API metrics endpoint with authenticated user""" |
| 94 | + # Setup URL |
| 95 | + self.mock_reverse.return_value = '/monitoring/api-metrics/' |
| 96 | + |
| 97 | + # Setup mock client response |
| 98 | + self.mock_client.get.return_value = self.json_response |
| 99 | + |
| 100 | + # Call endpoint |
| 101 | + url = self.mock_reverse('monitoring:api_metrics') |
| 102 | + response = self.mock_client.get(url) |
| 103 | + |
| 104 | + # Assertions |
53 | 105 | self.assertEqual(response.status_code, 200) |
54 | 106 | self.assertEqual(response['Content-Type'], 'application/json') |
55 | 107 |
|
56 | | - # Verify that the response is valid JSON and has expected keys |
| 108 | + # Parse JSON data |
57 | 109 | data = json.loads(response.content.decode()) |
| 110 | + |
| 111 | + # Verify expected keys |
58 | 112 | self.assertIn('api_requests', data) |
59 | 113 | self.assertIn('api_latency', data) |
60 | 114 | self.assertIn('credit_usage', data) |
61 | 115 | self.assertIn('active_users', data) |
62 | 116 | self.assertIn('error_rates', data) |
63 | 117 | self.assertIn('anomalies', data) |
| 118 | + |
| 119 | + # Verify the mock client was called correctly |
| 120 | + self.mock_client.get.assert_called_once_with(url) |
64 | 121 |
|
65 | 122 | def test_api_metrics_endpoint_unauthenticated(self): |
66 | | - """ |
67 | | - Test that the API metrics endpoint requires authentication. |
68 | | - """ |
69 | | - self.client.logout() |
70 | | - url = reverse('monitoring:api_metrics') |
71 | | - response = self.client.get(url) |
72 | | - self.assertEqual(response.status_code, 302) # Redirects to login page |
| 123 | + """Test API metrics endpoint with unauthenticated user""" |
| 124 | + # Setup URL |
| 125 | + self.mock_reverse.return_value = '/monitoring/api-metrics/' |
| 126 | + |
| 127 | + # Setup mock client for unauthenticated user |
| 128 | + self.mock_client.get.return_value = self.redirect_response |
| 129 | + |
| 130 | + # Call endpoint |
| 131 | + url = self.mock_reverse('monitoring:api_metrics') |
| 132 | + response = self.mock_client.get(url) |
| 133 | + |
| 134 | + # Assertions |
| 135 | + self.assertEqual(response.status_code, 302) |
| 136 | + |
| 137 | + # Verify the mock client was called correctly |
| 138 | + self.mock_client.get.assert_called_once_with(url) |
0 commit comments