|
| 1 | +import json |
| 2 | + |
| 3 | +import yaml |
| 4 | +from django.test import TestCase |
| 5 | +from django.urls import reverse |
| 6 | + |
| 7 | + |
| 8 | +class OpenAPISchemaTests(TestCase): |
| 9 | + |
| 10 | + SCHEMA_URL = reverse('openapi-schema') |
| 11 | + SCHEMA_URL_JSON = SCHEMA_URL + '?format=json' |
| 12 | + SCHEMA_URL_YAML = SCHEMA_URL + '?format=yaml' |
| 13 | + |
| 14 | + def _get_schema(self): |
| 15 | + response = self.client.get(self.SCHEMA_URL_JSON) |
| 16 | + self.assertEqual(response.status_code, 200) |
| 17 | + return json.loads(response.content) |
| 18 | + |
| 19 | + def test_schema_default_format_returns_valid_yaml_on_default_url(self): |
| 20 | + response = self.client.get(self.SCHEMA_URL) |
| 21 | + self.assertEqual(response.status_code, 200) |
| 22 | + schema = yaml.safe_load(response.content) |
| 23 | + self.assertIn('openapi', schema) |
| 24 | + self.assertIn('paths', schema) |
| 25 | + |
| 26 | + def test_schema_default_format_returns_valid_yaml(self): |
| 27 | + response = self.client.get(self.SCHEMA_URL_YAML) |
| 28 | + self.assertEqual(response.status_code, 200) |
| 29 | + schema = yaml.safe_load(response.content) |
| 30 | + self.assertIn('openapi', schema) |
| 31 | + self.assertIn('paths', schema) |
| 32 | + |
| 33 | + def test_schema_returns_200(self): |
| 34 | + response = self.client.get(self.SCHEMA_URL_JSON) |
| 35 | + self.assertEqual(response.status_code, 200) |
| 36 | + |
| 37 | + def test_schema_is_valid_json(self): |
| 38 | + schema = self._get_schema() |
| 39 | + self.assertIn('openapi', schema) |
| 40 | + self.assertIn('paths', schema) |
| 41 | + self.assertIn('info', schema) |
| 42 | + |
| 43 | + def test_schema_version_is_3_1(self): |
| 44 | + schema = self._get_schema() |
| 45 | + self.assertTrue(schema['openapi'].startswith('3.1')) |
| 46 | + |
| 47 | + def test_schema_info(self): |
| 48 | + schema = self._get_schema() |
| 49 | + self.assertEqual(schema['info']['title'], 'Summit Reports API') |
| 50 | + |
| 51 | + def test_schema_contains_public_and_private_tags(self): |
| 52 | + schema = self._get_schema() |
| 53 | + tag_names = [t['name'] for t in schema.get('tags', [])] |
| 54 | + self.assertIn('Public', tag_names) |
| 55 | + self.assertIn('Private', tag_names) |
| 56 | + |
| 57 | + def test_schema_contains_oauth2_security_scheme(self): |
| 58 | + schema = self._get_schema() |
| 59 | + security_schemes = schema.get('components', {}).get('securitySchemes', {}) |
| 60 | + self.assertIn('OAuth2', security_schemes) |
| 61 | + self.assertEqual(security_schemes['OAuth2']['type'], 'oauth2') |
| 62 | + |
| 63 | + def test_schema_has_paths_key(self): |
| 64 | + schema = self._get_schema() |
| 65 | + self.assertIn('paths', schema) |
| 66 | + |
| 67 | + |
| 68 | +class SwaggerUITests(TestCase): |
| 69 | + |
| 70 | + DOCS_URL = reverse('swagger-ui') |
| 71 | + |
| 72 | + def test_swagger_ui_returns_200(self): |
| 73 | + response = self.client.get(self.DOCS_URL) |
| 74 | + self.assertEqual(response.status_code, 200) |
| 75 | + |
| 76 | + def test_swagger_ui_contains_html(self): |
| 77 | + response = self.client.get(self.DOCS_URL) |
| 78 | + self.assertIn('text/html', response['Content-Type']) |
| 79 | + |
| 80 | + |
| 81 | +class RedocTests(TestCase): |
| 82 | + |
| 83 | + DOCS_URL = reverse('redoc') |
| 84 | + |
| 85 | + def test_redoc_returns_200(self): |
| 86 | + response = self.client.get(self.DOCS_URL) |
| 87 | + self.assertEqual(response.status_code, 200) |
| 88 | + |
| 89 | + def test_redoc_contains_html(self): |
| 90 | + response = self.client.get(self.DOCS_URL) |
| 91 | + self.assertIn('text/html', response['Content-Type']) |
0 commit comments