File indexing completed on 2026-04-27 07:41:42
0001 """
0002 Test Django HTTPS authentication for the dual server configuration.
0003 This ensures that Django HTTPS endpoints properly require and validate authentication.
0004 """
0005
0006 from django.test import TestCase
0007 from django.contrib.auth.models import User
0008 from rest_framework.test import APIClient
0009 from rest_framework.authtoken.models import Token
0010 from rest_framework import status
0011 from monitor_app.models import SystemAgent
0012 import urllib3
0013
0014
0015 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
0016
0017
0018 class DjangoHTTPSAuthenticationTest(TestCase):
0019 """Test Django HTTPS endpoints with proper authentication."""
0020
0021 def setUp(self):
0022 """Set up test environment with user and token."""
0023
0024 self.user = User.objects.create_user(
0025 username='testuser',
0026 email='test@example.com',
0027 password='testpass123'
0028 )
0029
0030
0031 self.token = Token.objects.create(user=self.user)
0032
0033
0034 self.client = APIClient()
0035
0036
0037 self.agent = SystemAgent.objects.create(
0038 instance_name='test-agent-1',
0039 agent_type='test',
0040 description='Test agent for Django HTTPS authentication',
0041 status='OK'
0042 )
0043
0044 def test_django_https_unauthenticated_request_returns_403(self):
0045 """Test that unauthenticated Django HTTPS requests return 403 Forbidden."""
0046
0047 response = self.client.get('/api/systemagents/')
0048
0049
0050 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
0051
0052 def test_django_https_authenticated_request_with_token(self):
0053 """Test that authenticated Django HTTPS requests with token work correctly."""
0054
0055 self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')
0056
0057
0058 response = self.client.get('/api/systemagents/')
0059
0060
0061 self.assertEqual(response.status_code, status.HTTP_200_OK)
0062
0063
0064 data = response.json()
0065 self.assertIsInstance(data, list)
0066 self.assertEqual(len(data), 1)
0067 self.assertEqual(data[0]['instance_name'], 'test-agent-1')
0068
0069 def test_django_https_authenticated_request_with_session(self):
0070 """Test that authenticated Django HTTPS requests with session work correctly."""
0071
0072 self.client.login(username='testuser', password='testpass123')
0073
0074
0075 response = self.client.get('/api/systemagents/')
0076
0077
0078 self.assertEqual(response.status_code, status.HTTP_200_OK)
0079
0080
0081 data = response.json()
0082 self.assertEqual(len(data), 1)
0083
0084 def test_django_https_invalid_token_returns_403(self):
0085 """Test that invalid token on Django HTTPS returns 403 Forbidden (due to DjangoModelPermissionsOrAnonReadOnly)."""
0086
0087 self.client.credentials(HTTP_AUTHORIZATION='Token invalid-token-12345')
0088
0089
0090 response = self.client.get('/api/systemagents/')
0091
0092
0093 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
0094
0095 def test_django_https_create_agent_with_authentication(self):
0096 """Test creating a system agent via Django HTTPS with authentication."""
0097
0098 self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')
0099
0100
0101 new_agent_data = {
0102 'instance_name': 'test-agent-2',
0103 'agent_type': 'test',
0104 'description': 'Created via Django HTTPS test',
0105 'status': 'OK'
0106 }
0107
0108
0109 response = self.client.post('/api/systemagents/', new_agent_data, format='json')
0110
0111
0112 self.assertEqual(response.status_code, status.HTTP_201_CREATED)
0113
0114
0115 self.assertEqual(SystemAgent.objects.count(), 2)
0116 created_agent = SystemAgent.objects.get(instance_name='test-agent-2')
0117 self.assertEqual(created_agent.description, 'Created via Django HTTPS test')
0118
0119 def test_django_https_heartbeat_endpoint_with_authentication(self):
0120 """Test the Django HTTPS heartbeat endpoint requires authentication."""
0121
0122 response = self.client.post('/api/systemagents/heartbeat/', {
0123 'instance_name': 'test-agent-1',
0124 'status': 'OK'
0125 }, format='json')
0126 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
0127
0128
0129 self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')
0130 response = self.client.post('/api/systemagents/heartbeat/', {
0131 'instance_name': 'test-agent-1',
0132 'status': 'OK'
0133 }, format='json')
0134 self.assertEqual(response.status_code, status.HTTP_200_OK)
0135
0136 def test_django_http_logs_endpoint_allows_anonymous(self):
0137 """Test that Django HTTP logs endpoint allows anonymous access (for HTTP logging on port 8002)."""
0138
0139 log_data = {
0140 'app_name': 'test_app',
0141 'instance_name': 'test_instance',
0142 'timestamp': '2025-08-04T10:00:00',
0143 'level': 20,
0144 'levelname': 'INFO',
0145 'message': 'Test log message via Django HTTP',
0146 'module': 'test_module',
0147 'funcname': 'test_func',
0148 'lineno': 100,
0149 'process': 1234,
0150 'thread': 5678
0151 }
0152
0153 response = self.client.post('/api/logs/', log_data, format='json')
0154
0155
0156 self.assertEqual(response.status_code, status.HTTP_201_CREATED)
0157
0158 def tearDown(self):
0159 """Clean up after tests."""
0160
0161 pass