from django.test import TestCase from django.urls import reverse from rest_framework.test import APIClient from rest_framework import status from django.contrib.auth import get_user_model User = get_user_model() class Phase0InfrastructureTests(TestCase): """ Automated verification tests for Phase 0 infrastructure: - OpenAPI 3.0 schema endpoints via drf-spectacular - Swagger UI & ReDoc interfaces - JWT token issue, refresh, and verification endpoints - Core API health checks """ def setUp(self): self.client = APIClient() self.user = User.objects.create_user( email='tester@shiadeepconnect.org', username='tester@shiadeepconnect.org', password='SecurePassword123!', fullname='Tester User' ) def test_health_check_endpoint(self): response = self.client.get('/api/v1/health/') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data.get('status'), 'healthy') self.assertEqual(response.data.get('version'), '1.0.0') def test_openapi_schema_endpoint(self): response = self.client.get('/api/schema/') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertIn('openapi', response.content.decode('utf-8')) def test_swagger_ui_endpoint(self): response = self.client.get('/api/schema/swagger-ui/') self.assertEqual(response.status_code, status.HTTP_200_OK) def test_redoc_endpoint(self): response = self.client.get('/api/schema/redoc/') self.assertEqual(response.status_code, status.HTTP_200_OK) def test_swagger_legacy_redirect(self): response = self.client.get('/swagger/') self.assertIn(response.status_code, [status.HTTP_302_FOUND, status.HTTP_301_MOVED_PERMANENTLY]) self.assertEqual(response.url, '/api/schema/swagger-ui/') def test_jwt_token_obtain_pair(self): # Obtain JWT tokens payload = { 'email': 'tester@shiadeepconnect.org', 'password': 'SecurePassword123!' } response = self.client.post('/api/v1/auth/token/', payload, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertIn('access', response.data) self.assertIn('refresh', response.data) access_token = response.data['access'] refresh_token = response.data['refresh'] # Verify token verify_response = self.client.post( '/api/v1/auth/token/verify/', {'token': access_token}, format='json' ) self.assertEqual(verify_response.status_code, status.HTTP_200_OK) # Refresh token refresh_response = self.client.post( '/api/v1/auth/token/refresh/', {'refresh': refresh_token}, format='json' ) self.assertEqual(refresh_response.status_code, status.HTTP_200_OK) self.assertIn('access', refresh_response.data)