test_mist_phase7_rate_limits.py
python
sha256:fc04e4cae9e1774d6a21b65c45daeed0e6787eb581d13aa1b03bfe9384a34226
Merge branch 'fix/two-column-scroll-layout' into dev
Human
9 days ago
| 1 | """Phase 7 TDD: Rate limiting audit for /api/mists/* endpoints. |
| 2 | |
| 3 | Tests are written RED first. Run before touching mists.py and rate_limits.py |
| 4 | to confirm failure, then implement. |
| 5 | |
| 6 | Gap: |
| 7 | - POST /api/mists and POST /api/mists/{id}/fork already have @limiter.limit. |
| 8 | - PATCH /api/mists/{id} and DELETE /api/mists/{id} have NO rate limit decorator. |
| 9 | - GET read endpoints (explore, get, forks, owner list, embed) have no per-route |
| 10 | limit — they fall through to the global 300/min IP bucket, which is too loose. |
| 11 | |
| 12 | Work: |
| 13 | 1. Add MIST_UPDATE_LIMIT and MIST_DELETE_LIMIT to rate_limits.py (keyed on |
| 14 | MSign handle like create/fork). |
| 15 | 2. Add MIST_READ_LIMIT to rate_limits.py (keyed on IP, read-only endpoints). |
| 16 | 3. Decorate update_mist and delete_mist with @limiter.limit(..., key_func=get_msign_handle). |
| 17 | 4. Decorate all GET handlers with @limiter.limit(MIST_READ_LIMIT). |
| 18 | """ |
| 19 | from __future__ import annotations |
| 20 | |
| 21 | import pytest |
| 22 | from httpx import AsyncClient |
| 23 | |
| 24 | |
| 25 | # --------------------------------------------------------------------------- |
| 26 | # 1. Rate limit constants defined in rate_limits.py |
| 27 | # --------------------------------------------------------------------------- |
| 28 | |
| 29 | class TestRateLimitConstants: |
| 30 | def test_mist_update_limit_defined(self) -> None: |
| 31 | """MIST_UPDATE_LIMIT must be defined in rate_limits.""" |
| 32 | from musehub.rate_limits import MIST_UPDATE_LIMIT |
| 33 | assert isinstance(MIST_UPDATE_LIMIT, str) |
| 34 | assert "/" in MIST_UPDATE_LIMIT, ( |
| 35 | f"MIST_UPDATE_LIMIT must be a rate string like '20/minute'; got {MIST_UPDATE_LIMIT!r}" |
| 36 | ) |
| 37 | |
| 38 | def test_mist_delete_limit_defined(self) -> None: |
| 39 | """MIST_DELETE_LIMIT must be defined in rate_limits.""" |
| 40 | from musehub.rate_limits import MIST_DELETE_LIMIT |
| 41 | assert isinstance(MIST_DELETE_LIMIT, str) |
| 42 | assert "/" in MIST_DELETE_LIMIT |
| 43 | |
| 44 | def test_mist_read_limit_defined(self) -> None: |
| 45 | """MIST_READ_LIMIT must be defined in rate_limits.""" |
| 46 | from musehub.rate_limits import MIST_READ_LIMIT |
| 47 | assert isinstance(MIST_READ_LIMIT, str) |
| 48 | assert "/" in MIST_READ_LIMIT |
| 49 | |
| 50 | |
| 51 | # --------------------------------------------------------------------------- |
| 52 | # 2. Mutating endpoints have @limiter.limit decorators (source inspection) |
| 53 | # --------------------------------------------------------------------------- |
| 54 | |
| 55 | class TestMutatingEndpointsHaveLimiters: |
| 56 | def test_update_mist_has_limiter_decorator(self) -> None: |
| 57 | """update_mist must have a @limiter.limit(...) decorator in mists.py.""" |
| 58 | import inspect |
| 59 | from musehub.api.routes.musehub import mists as _mod |
| 60 | src = inspect.getsource(_mod.update_mist) |
| 61 | # The decorator is applied before the function — check the module source |
| 62 | # around the function definition to catch the decorator above it. |
| 63 | full_src = inspect.getsource(_mod) |
| 64 | # Find the update_mist function and check for limiter.limit above it. |
| 65 | update_idx = full_src.find("async def update_mist(") |
| 66 | assert update_idx != -1 |
| 67 | # Look at the 500 chars before the function definition for the decorator. |
| 68 | preamble = full_src[max(0, update_idx - 500): update_idx] |
| 69 | assert "limiter.limit" in preamble, ( |
| 70 | "update_mist must be decorated with @limiter.limit(...); " |
| 71 | "no limiter.limit found in the 500 chars before 'async def update_mist'" |
| 72 | ) |
| 73 | |
| 74 | def test_delete_mist_has_limiter_decorator(self) -> None: |
| 75 | """delete_mist must have a @limiter.limit(...) decorator in mists.py.""" |
| 76 | import inspect |
| 77 | from musehub.api.routes.musehub import mists as _mod |
| 78 | full_src = inspect.getsource(_mod) |
| 79 | delete_idx = full_src.find("async def delete_mist(") |
| 80 | assert delete_idx != -1 |
| 81 | preamble = full_src[max(0, delete_idx - 500): delete_idx] |
| 82 | assert "limiter.limit" in preamble, ( |
| 83 | "delete_mist must be decorated with @limiter.limit(...); " |
| 84 | "no limiter.limit found in the 500 chars before 'async def delete_mist'" |
| 85 | ) |
| 86 | |
| 87 | def test_update_mist_uses_msign_key_func(self) -> None: |
| 88 | """update_mist rate limiter must key on MSign handle, not IP.""" |
| 89 | import inspect |
| 90 | from musehub.api.routes.musehub import mists as _mod |
| 91 | full_src = inspect.getsource(_mod) |
| 92 | update_idx = full_src.find("async def update_mist(") |
| 93 | preamble = full_src[max(0, update_idx - 500): update_idx] |
| 94 | assert "get_msign_handle" in preamble, ( |
| 95 | "update_mist limiter must use key_func=get_msign_handle" |
| 96 | ) |
| 97 | |
| 98 | def test_delete_mist_uses_msign_key_func(self) -> None: |
| 99 | """delete_mist rate limiter must key on MSign handle, not IP.""" |
| 100 | import inspect |
| 101 | from musehub.api.routes.musehub import mists as _mod |
| 102 | full_src = inspect.getsource(_mod) |
| 103 | delete_idx = full_src.find("async def delete_mist(") |
| 104 | preamble = full_src[max(0, delete_idx - 500): delete_idx] |
| 105 | assert "get_msign_handle" in preamble, ( |
| 106 | "delete_mist limiter must use key_func=get_msign_handle" |
| 107 | ) |
| 108 | |
| 109 | |
| 110 | # --------------------------------------------------------------------------- |
| 111 | # 3. Read endpoints have @limiter.limit decorators |
| 112 | # --------------------------------------------------------------------------- |
| 113 | |
| 114 | class TestReadEndpointsHaveLimiters: |
| 115 | def _check_fn_has_limiter(self, fn_name: str) -> None: |
| 116 | import inspect |
| 117 | from musehub.api.routes.musehub import mists as _mod |
| 118 | full_src = inspect.getsource(_mod) |
| 119 | fn_idx = full_src.find(f"async def {fn_name}(") |
| 120 | assert fn_idx != -1, f"Could not find 'async def {fn_name}(' in mists.py" |
| 121 | preamble = full_src[max(0, fn_idx - 500): fn_idx] |
| 122 | assert "limiter.limit" in preamble, ( |
| 123 | f"{fn_name} must be decorated with @limiter.limit(...); " |
| 124 | f"none found in the 500 chars before 'async def {fn_name}'" |
| 125 | ) |
| 126 | |
| 127 | def test_explore_mists_has_limiter(self) -> None: |
| 128 | """explore_mists (GET /api/mists/explore) must have @limiter.limit.""" |
| 129 | self._check_fn_has_limiter("explore_mists") |
| 130 | |
| 131 | def test_get_mist_has_limiter(self) -> None: |
| 132 | """get_mist (GET /api/mists/{mist_id}) must have @limiter.limit.""" |
| 133 | self._check_fn_has_limiter("get_mist") |
| 134 | |
| 135 | def test_list_mist_forks_has_limiter(self) -> None: |
| 136 | """list_mist_forks (GET /api/mists/{mist_id}/forks) must have @limiter.limit.""" |
| 137 | self._check_fn_has_limiter("list_mist_forks") |
| 138 | |
| 139 | def test_list_owner_mists_has_limiter(self) -> None: |
| 140 | """list_owner_mists (GET /api/{owner}/mists) must have @limiter.limit.""" |
| 141 | self._check_fn_has_limiter("list_owner_mists") |
| 142 | |
| 143 | def test_get_mist_embed_has_limiter(self) -> None: |
| 144 | """get_mist_embed (GET /api/{owner}/mists/{id}/embed) must have @limiter.limit.""" |
| 145 | self._check_fn_has_limiter("get_mist_embed") |
| 146 | |
| 147 | |
| 148 | # --------------------------------------------------------------------------- |
| 149 | # 4. Read endpoints still return 200 under threshold (regression) |
| 150 | # --------------------------------------------------------------------------- |
| 151 | |
| 152 | class TestReadEndpointsWorkUnderThreshold: |
| 153 | @pytest.mark.asyncio |
| 154 | async def test_explore_returns_200_under_limit(self, client: AsyncClient) -> None: |
| 155 | r = await client.get("/api/mists/explore") |
| 156 | assert r.status_code == 200, ( |
| 157 | f"GET /api/mists/explore returned {r.status_code} under the rate limit" |
| 158 | ) |
| 159 | |
| 160 | @pytest.mark.asyncio |
| 161 | async def test_get_nonexistent_mist_returns_404_not_429( |
| 162 | self, client: AsyncClient |
| 163 | ) -> None: |
| 164 | """A single GET for an unknown mist_id must return 404, not 429.""" |
| 165 | r = await client.get("/api/mists/nonexistentId12") |
| 166 | assert r.status_code in (404, 200), ( |
| 167 | f"GET /api/mists/nonexistentId12 returned {r.status_code}; " |
| 168 | "429 under single request means the limit is misconfigured" |
| 169 | ) |
| 170 | |
| 171 | @pytest.mark.asyncio |
| 172 | async def test_list_owner_mists_returns_200_under_limit( |
| 173 | self, client: AsyncClient |
| 174 | ) -> None: |
| 175 | r = await client.get("/api/gabriel/mists") |
| 176 | assert r.status_code == 200, ( |
| 177 | f"GET /api/gabriel/mists returned {r.status_code} under the rate limit" |
| 178 | ) |
File History
13 commits
sha256:fc04e4cae9e1774d6a21b65c45daeed0e6787eb581d13aa1b03bfe9384a34226
Merge branch 'fix/two-column-scroll-layout' into dev
Human
9 days ago
sha256:408916fc5973ba59c6e4eebaa80ebdcc801c0a63205651e25009d11548f79454
chore: bump version to 0.2.0.dev2 — nightly.2, matching muse
Sonnet 4.6
patch
11 days ago
sha256:d035733f21ccff27735fddebfbbe0ed24565a32a22db8de5885402262671ecd2
chore: bump version to 0.2.0rc15 for musehub#113 fix release
Sonnet 4.6
patch
15 days ago
sha256:0032d6cfa33bc3c8367436ad768e7dd0e339b4332153160247da8266cb5fa352
Merge branch 'task/version-tags-phase3-server' into dev
Human
17 days ago
sha256:4669620efda9ff41c55bdefd1f7bfe1c239d468428744c84ead9957e5a003a53
merge: rescue snapshot-recovery hardening (c00aa21d) into d…
Opus 4.8
minor
⚠
29 days ago
sha256:a59da49c4611b970fc4b6ae48678ce4943261c213a07ddbd73ce9201df869b4a
fix: remove false-positive proposal_comments index drop fro…
Sonnet 4.6
patch
34 days ago
sha256:0a240d6dbff234f07d98a28a4a9a68db702f3f9ff9260196f24219bdb1c0b6f3
feat: render markdown mists as HTML with heading anchor links
Sonnet 4.6
patch
34 days ago
sha256:24a7d47486ebc4ebd1832830580e177ec6f877b48dced8c000e198cdec4ce9d6
Merge 'task/bump-version-rc12' into 'dev' — proposal: Bump …
Human
35 days ago
sha256:b9ff931d147e0114a1f17060f415b89ed551c170a91ff226c70437aa5c85f9ee
Merge 'task/bump-version-rc12' into 'dev' — proposal: Bump …
Human
35 days ago
sha256:d1122d21e73471879b460037b22c0b50fded7c423444a176f248428f75dac39c
Merge 'task/fix-issue-pagination-cursor' into 'dev' — propo…
Human
35 days ago
sha256:01e18975e73d2b3cd5b6db7929c895bef9aa6e0d4391dc5b2adfc548b41318dd
Merge 'feat/adding-debug-logs-to-staging' into 'dev' — prop…
Human
36 days ago
sha256:6b1949fc2797ca4c1936a637a4cbfec828ef56cf52398a2e74ca3c4f494e728f
fix: use wire_bytes not mpack_bytes_raw in compute_object_b…
Sonnet 4.6
patch
48 days ago
sha256:b99f2455dc346966d040133f5203297e6e3ef5803a93728a2c30568d0a0f7583
rename: delta_add → delta_upsert across wire format, models…
Sonnet 4.6
patch
50 days ago