from django.contrib.auth import get_user_model from django.test import TestCase from rest_framework.test import APIClient from rest_framework_simplejwt.tokens import RefreshToken from basic_info.models import Employee, Merchant, MerchantTypeEnum class APIDocsSchemaAccessTest(TestCase): def setUp(self): self.merchant = Merchant.objects.create(name='文档审查商户', type=MerchantTypeEnum.STORE) self.user = get_user_model().objects.create_user(username='schema-docs-user', password='pass12345') self.employee = Employee.objects.create( merchant=self.merchant, sys_user=self.user, name='文档审查员工', ) def _auth_client(self): token = str(RefreshToken.for_user(self.user).access_token) client = APIClient() client.credentials(HTTP_AUTHORIZATION=f'Bearer {token}') return client def test_schema_and_docs_require_jwt(self): client = APIClient() schema_response = client.get('/api/schema/', HTTP_ACCEPT='application/json') docs_response = client.get('/api/docs/') self.assertEqual(schema_response.status_code, 401) self.assertEqual(docs_response.status_code, 401) def test_authenticated_user_can_access_schema_and_docs(self): client = self._auth_client() schema_response = client.get('/api/schema/', HTTP_ACCEPT='application/json') docs_response = client.get('/api/docs/') self.assertEqual(schema_response.status_code, 200) self.assertEqual(docs_response.status_code, 200) def test_schema_contains_latest_mission_api(self): client = self._auth_client() response = client.get('/api/schema/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, 200) schema_text = response.content.decode('utf-8') expected_fragments = [ 'employee_type_ids', '/api/v2/missions/', '/api/v2/missions/by-printing-order/{printing_order_id}/', '/api/v2/mission-replies/{reply_id}/reject/', ] missing = [fragment for fragment in expected_fragments if fragment not in schema_text] self.assertEqual(missing, [])