|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import pytest |
1 | 4 | from drf_spectacular.generators import SchemaGenerator |
2 | 5 | from drf_spectacular.openapi import AutoSchema |
3 | 6 | from typing_extensions import TypedDict |
4 | 7 |
|
5 | | -from api.openapi import TypedDictSchemaExtension |
| 8 | +from api.openapi import ( |
| 9 | + TAGS, |
| 10 | + TypedDictSchemaExtension, |
| 11 | + postprocessing_assign_tags, |
| 12 | + preprocessing_filter_spec, |
| 13 | +) |
6 | 14 |
|
7 | 15 |
|
8 | 16 | def test_typeddict_schema_extension__nested_typed_dict__renders_expected_schema() -> ( |
@@ -88,6 +96,162 @@ class ResponseModel(TypedDict): |
88 | 96 | } |
89 | 97 |
|
90 | 98 |
|
| 99 | +@pytest.mark.parametrize( |
| 100 | + "path, expected_tag", |
| 101 | + [ |
| 102 | + ("/api/v1/organisations/", "Organisations"), |
| 103 | + ("/api/v1/organisations/{id}/groups/", "Organisations"), |
| 104 | + ("/api/v1/projects/{id}/", "Projects"), |
| 105 | + ("/api/v1/environments/{api_key}/", "Environments"), |
| 106 | + ("/api/v1/projects/{id}/features/", "Features"), |
| 107 | + ("/api/v1/flags/{feature_id}/multivariate-options/", "Features"), |
| 108 | + ("/api/v1/environments/{api_key}/featurestates/{id}/", "Feature states"), |
| 109 | + ("/api/v1/environment-feature-versions/{id}/", "Feature states"), |
| 110 | + ("/api/v1/environments/{api_key}/identities/{id}/", "Identities"), |
| 111 | + ("/api/v1/environments/{api_key}/edge-identities/{id}/", "Identities"), |
| 112 | + ("/api/v1/traits/", "Identities"), |
| 113 | + ("/api/v1/segments/{id}/", "Segments"), |
| 114 | + ("/api/v1/environments/{api_key}/integrations/amplitude/{id}/", "Integrations"), |
| 115 | + ("/api/v1/projects/{id}/integrations/datadog/{id}/", "Integrations"), |
| 116 | + ("/api/v1/organisations/{id}/integrations/github/", "Integrations"), |
| 117 | + ("/api/v1/environments/{api_key}/user-permissions/{id}/", "Permissions"), |
| 118 | + ("/api/v1/projects/{id}/user-group-permissions/{id}/", "Permissions"), |
| 119 | + ("/api/v1/environments/{api_key}/webhooks/{id}/", "Webhooks"), |
| 120 | + ("/api/v1/cb-webhook/", "Webhooks"), |
| 121 | + ("/api/v1/github-webhook/", "Webhooks"), |
| 122 | + ("/api/v1/audit/", "Audit"), |
| 123 | + ("/api/v1/auth/login/", "Authentication"), |
| 124 | + ("/api/v1/users/join/{hash}/", "Authentication"), |
| 125 | + ("/api/v1/analytics/flags/", "Analytics"), |
| 126 | + ("/api/v1/metadata/fields/", "Metadata"), |
| 127 | + ("/api/v1/onboarding/request/send/", "Onboarding"), |
| 128 | + ("/api/v1/admin/dashboard/summary/", "Admin dashboard"), |
| 129 | + ], |
| 130 | +) |
| 131 | +def test_postprocessing_assign_tags__parametrized_path__assigns_correct_tag( |
| 132 | + path: str, expected_tag: str |
| 133 | +) -> None: |
| 134 | + # Given |
| 135 | + result: dict[str, Any] = { |
| 136 | + "paths": { |
| 137 | + path: { |
| 138 | + "get": { |
| 139 | + "operationId": "test_op", |
| 140 | + "tags": ["api"], |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + # When |
| 147 | + postprocessing_assign_tags(result, generator=None) |
| 148 | + |
| 149 | + # Then |
| 150 | + assert result["paths"][path]["get"]["tags"] == [expected_tag] |
| 151 | + |
| 152 | + |
| 153 | +def test_postprocessing_assign_tags__explicit_tags__preserved() -> None: |
| 154 | + # Given |
| 155 | + result: dict[str, Any] = { |
| 156 | + "paths": { |
| 157 | + "/api/v1/flags/": { |
| 158 | + "get": { |
| 159 | + "operationId": "sdk_flags", |
| 160 | + "tags": ["sdk"], |
| 161 | + }, |
| 162 | + }, |
| 163 | + "/api/v1/organisations/": { |
| 164 | + "get": { |
| 165 | + "operationId": "organisations_list", |
| 166 | + "tags": ["mcp", "organisations"], |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + } |
| 171 | + |
| 172 | + # When |
| 173 | + postprocessing_assign_tags(result, generator=None) |
| 174 | + |
| 175 | + # Then |
| 176 | + assert result["paths"]["/api/v1/flags/"]["get"]["tags"] == ["sdk"] |
| 177 | + assert result["paths"]["/api/v1/organisations/"]["get"]["tags"] == [ |
| 178 | + "mcp", |
| 179 | + "organisations", |
| 180 | + ] |
| 181 | + |
| 182 | + |
| 183 | +def test_postprocessing_assign_tags__empty_paths__sets_tags_list_on_result() -> None: |
| 184 | + # Given |
| 185 | + result: dict[str, Any] = {"paths": {}} |
| 186 | + |
| 187 | + # When |
| 188 | + postprocessing_assign_tags(result, generator=None) |
| 189 | + |
| 190 | + # Then |
| 191 | + assert result["tags"] == TAGS |
| 192 | + |
| 193 | + |
| 194 | +def test_postprocessing_assign_tags__unmatched_path__assigned_other_tag() -> None: |
| 195 | + # Given |
| 196 | + result: dict[str, Any] = { |
| 197 | + "paths": { |
| 198 | + "/api/v1/unknown-endpoint/": { |
| 199 | + "get": { |
| 200 | + "operationId": "unknown", |
| 201 | + "tags": ["api"], |
| 202 | + }, |
| 203 | + }, |
| 204 | + }, |
| 205 | + } |
| 206 | + |
| 207 | + # When |
| 208 | + postprocessing_assign_tags(result, generator=None) |
| 209 | + |
| 210 | + # Then |
| 211 | + assert result["paths"]["/api/v1/unknown-endpoint/"]["get"]["tags"] == ["Other"] |
| 212 | + |
| 213 | + |
| 214 | +def test_postprocessing_assign_tags__non_dict_operation__skipped() -> None: |
| 215 | + # Given - a path item with both a real operation and a non-dict entry |
| 216 | + # (e.g. path-level `parameters`, which is a list rather than an operation dict) |
| 217 | + result: dict[str, Any] = { |
| 218 | + "paths": { |
| 219 | + "/api/v1/projects/{id}/": { |
| 220 | + "parameters": [{"name": "id", "in": "path"}], |
| 221 | + "get": { |
| 222 | + "operationId": "projects_retrieve", |
| 223 | + "tags": ["api"], |
| 224 | + }, |
| 225 | + }, |
| 226 | + }, |
| 227 | + } |
| 228 | + |
| 229 | + # When |
| 230 | + postprocessing_assign_tags(result, generator=None) |
| 231 | + |
| 232 | + # Then - the real operation got its tag, and the non-dict entry was left alone |
| 233 | + assert result["paths"]["/api/v1/projects/{id}/"]["get"]["tags"] == ["Projects"] |
| 234 | + assert result["paths"]["/api/v1/projects/{id}/"]["parameters"] == [ |
| 235 | + {"name": "id", "in": "path"} |
| 236 | + ] |
| 237 | + |
| 238 | + |
| 239 | +def test_preprocessing_filter_spec__swagger_endpoints__removed() -> None: |
| 240 | + # Given |
| 241 | + endpoints = [ |
| 242 | + ("/api/v1/organisations/", "^api/v1/organisations/", "GET", None), |
| 243 | + ("/api/v1/swagger.json", "^api/v1/swagger.json", "GET", None), |
| 244 | + ("/api/v1/swagger.yaml", "^api/v1/swagger.yaml", "GET", None), |
| 245 | + ] |
| 246 | + |
| 247 | + # When |
| 248 | + filtered = preprocessing_filter_spec(endpoints) |
| 249 | + |
| 250 | + # Then |
| 251 | + assert len(filtered) == 1 |
| 252 | + assert filtered[0][0] == "/api/v1/organisations/" |
| 253 | + |
| 254 | + |
91 | 255 | def test_typeddict_schema_extension__simple_model__returns_correct_name() -> None: |
92 | 256 | # Given |
93 | 257 | class MyModel(TypedDict): |
|
0 commit comments