|
| 1 | +"""API contract guard: responses must carry snake_case keys only. |
| 2 | +
|
| 3 | +Regression net for the D7 lesson: a camel+snake dual-field contract crept |
| 4 | +back through build_public_config/build_public_meta (file-based scoping hid |
| 5 | +that they serve a live endpoint). Any key matching [a-z]+[A-Z] camel shape |
| 6 | +anywhere in these documented response payloads now fails the suite. |
| 7 | +""" |
| 8 | +import re |
| 9 | + |
| 10 | +import httpx |
| 11 | +import pytest |
| 12 | + |
| 13 | +CAMEL_KEY = re.compile(r"^[a-z0-9]+(?:[A-Z][a-zA-Z0-9]*)+$") |
| 14 | + |
| 15 | + |
| 16 | +def _assert_no_camel_keys(node, path, violations): |
| 17 | + if isinstance(node, dict): |
| 18 | + for key, value in node.items(): |
| 19 | + key_path = f"{path}.{key}" |
| 20 | + if isinstance(key, str) and CAMEL_KEY.match(key): |
| 21 | + violations.append(key_path) |
| 22 | + _assert_no_camel_keys(value, key_path, violations) |
| 23 | + elif isinstance(node, list): |
| 24 | + for index, item in enumerate(node): |
| 25 | + _assert_no_camel_keys(item, f"{path}[{index}]", violations) |
| 26 | + |
| 27 | + |
| 28 | +async def _login(client: httpx.AsyncClient) -> str: |
| 29 | + from tests.conftest import TEST_ADMIN_PASSWORD |
| 30 | + |
| 31 | + response = await client.post( |
| 32 | + "/admin/login", json={"password": TEST_ADMIN_PASSWORD} |
| 33 | + ) |
| 34 | + assert response.status_code == 200, response.text |
| 35 | + return response.json()["detail"]["token"] |
| 36 | + |
| 37 | + |
| 38 | +def _check_contract(payload: dict, url: str) -> None: |
| 39 | + violations = [] |
| 40 | + _assert_no_camel_keys(payload, url, violations) |
| 41 | + assert not violations, f"camelCase keys leaked into {url}: {violations}" |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.asyncio |
| 45 | +class TestApiContractSnakeCase: |
| 46 | + async def test_public_config(self, initialized_client): |
| 47 | + response = await initialized_client.get("/api/v1/config") |
| 48 | + assert response.status_code == 200 |
| 49 | + _check_contract(response.json(), "/api/v1/config") |
| 50 | + |
| 51 | + async def test_dashboard(self, initialized_client): |
| 52 | + token = await _login(initialized_client) |
| 53 | + response = await initialized_client.get( |
| 54 | + "/admin/dashboard", headers={"Authorization": f"Bearer {token}"} |
| 55 | + ) |
| 56 | + assert response.status_code == 200 |
| 57 | + _check_contract(response.json(), "/admin/dashboard") |
| 58 | + |
| 59 | + async def test_admin_file_list(self, initialized_client): |
| 60 | + token = await _login(initialized_client) |
| 61 | + response = await initialized_client.get( |
| 62 | + "/admin/file/list", headers={"Authorization": f"Bearer {token}"} |
| 63 | + ) |
| 64 | + assert response.status_code == 200 |
| 65 | + _check_contract(response.json(), "/admin/file/list") |
| 66 | + |
| 67 | + async def test_share_metadata(self, initialized_client): |
| 68 | + share = await initialized_client.post( |
| 69 | + "/share/text/", data={"text": "contract guard", "expire_value": 1, "expire_style": "day"} |
| 70 | + ) |
| 71 | + assert share.status_code == 200, share.text |
| 72 | + code = share.json()["detail"]["code"] |
| 73 | + response = await initialized_client.get( |
| 74 | + "/share/metadata/", params={"code": code} |
| 75 | + ) |
| 76 | + assert response.status_code == 200 |
| 77 | + _check_contract(response.json(), "/share/metadata/") |
0 commit comments