test_reserved_names.py
python
sha256:41ed2ded57d17544c111c81960ee6a7a7febc9a0d81a778616319265ac93a654
merge: rc25 — Mists wave (#316 #317 #318 #322; #319 #320 #3…
Opus 5.5
minor
⚠ breaking
2 days ago
| 1 | """Reserved handles and repo slugs (review follow-up M1 to musehub#317). |
| 2 | |
| 3 | A handle or repo slug equal to a fixed route's literal segment would be unreachable, so creation refuses it with a |
| 4 | 422. The lists live in ``musehub.core.reserved_names``; these tests derive the literal segments from the app's own |
| 5 | routes, so a new fixed route that is not reserved fails here. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import inspect |
| 11 | import re |
| 12 | |
| 13 | import pytest |
| 14 | from fastapi.routing import iter_route_contexts |
| 15 | from httpx import AsyncClient |
| 16 | |
| 17 | from musehub.core import reserved_names as rn |
| 18 | from musehub.main import app |
| 19 | from musehub.types.json_types import StrDict |
| 20 | |
| 21 | _HANDLE_RE = re.compile(r"[a-z0-9_-]+") |
| 22 | |
| 23 | |
| 24 | def _literal_segments() -> tuple[set[str], set[str]]: |
| 25 | """(first segments of fixed paths, second segments of ``/{owner}/<literal>`` paths) across every web route.""" |
| 26 | first, second = set(), set() |
| 27 | for ctx in iter_route_contexts(app.routes): |
| 28 | segs = (ctx.path or "").strip("/").split("/") |
| 29 | if not segs or not segs[0]: |
| 30 | continue |
| 31 | if not segs[0].startswith("{"): |
| 32 | first.add(segs[0]) |
| 33 | if len(segs) == 2 and segs[0].startswith("{") and not segs[1].startswith("{"): |
| 34 | second.add(segs[1]) |
| 35 | return first, second |
| 36 | |
| 37 | |
| 38 | @pytest.mark.no_db |
| 39 | class TestTheListsCoverTheRoutes: |
| 40 | def test_every_fixed_first_segment_that_is_a_valid_handle_is_reserved(self) -> None: |
| 41 | first, _ = _literal_segments() |
| 42 | assert {s for s in first if _HANDLE_RE.fullmatch(s)} <= rn.RESERVED_HANDLES |
| 43 | |
| 44 | def test_every_fixed_owner_sub_page_is_a_reserved_repo_slug(self) -> None: |
| 45 | _, second = _literal_segments() |
| 46 | assert second <= rn.RESERVED_REPO_SLUGS |
| 47 | assert "mists" in second # the list page musehub#317 put before the repo wildcard |
| 48 | |
| 49 | def test_mists_is_reserved_both_ways(self) -> None: |
| 50 | assert "mists" in rn.RESERVED_HANDLES and "mists" in rn.RESERVED_REPO_SLUGS |
| 51 | |
| 52 | @pytest.mark.parametrize("name", ["mists", "MISTS", " Mists "]) |
| 53 | def test_check_handle(self, name: str) -> None: |
| 54 | with pytest.raises(rn.ReservedNameError, match="reserved"): |
| 55 | rn.check_handle(name) |
| 56 | |
| 57 | def test_ordinary_names_pass(self) -> None: |
| 58 | rn.check_handle("gabriel") |
| 59 | rn.check_repo_slug("mists-notes") |
| 60 | rn.check_repo_slug("explore") # /{owner}/explore is a repo page, not a fixed route |
| 61 | |
| 62 | |
| 63 | @pytest.mark.no_db |
| 64 | class TestCreationPathsRefuse: |
| 65 | async def test_create_repo_service_refuses_a_reserved_slug_before_any_write(self) -> None: |
| 66 | from musehub.services import musehub_repository |
| 67 | |
| 68 | for name in ("mists", "Mists", "search"): |
| 69 | with pytest.raises(rn.ReservedNameError): |
| 70 | await musehub_repository.create_repo( |
| 71 | None, # type: ignore[arg-type] - the check runs before the session is touched |
| 72 | name=name, owner="alice", visibility="public", owner_user_id="alice", owner_identity_id="x", |
| 73 | ) |
| 74 | |
| 75 | async def test_mcp_create_repo_answers_invalid_args(self) -> None: |
| 76 | from unittest.mock import patch |
| 77 | |
| 78 | from musehub.mcp.write_tools import repos as tools |
| 79 | |
| 80 | class _Ctx: |
| 81 | async def __aenter__(self) -> object: |
| 82 | class _S: |
| 83 | async def execute(self, *a: object, **k: object) -> object: |
| 84 | from types import SimpleNamespace |
| 85 | |
| 86 | return SimpleNamespace(scalar_one_or_none=lambda: None) |
| 87 | return _S() |
| 88 | |
| 89 | async def __aexit__(self, *a: object) -> None: ... |
| 90 | |
| 91 | with ( |
| 92 | patch.object(tools, "_check_db_available", return_value=None), |
| 93 | patch.object(tools, "AsyncSessionLocal", new=lambda: _Ctx()), |
| 94 | patch.object(tools.musehub_repository, "get_identity_id_for_handle", new=_async_return("x")), |
| 95 | ): |
| 96 | result = await tools.execute_create_repo(name="mists", owner="alice", owner_user_id="alice") |
| 97 | assert (result.ok, result.error_code) == (False, "invalid_args") and "reserved" in result.error_message |
| 98 | |
| 99 | async def test_org_and_profile_creation_refuse_a_reserved_handle(self) -> None: |
| 100 | from musehub.services import musehub_profile |
| 101 | from musehub.services.musehub_auth import AuthError |
| 102 | from musehub.services.musehub_orgs import create_org |
| 103 | |
| 104 | with pytest.raises(AuthError) as err: |
| 105 | await create_org(None, handle="mists", display_name="M", quorum=1, creator_identity_id="x") # type: ignore[arg-type] |
| 106 | assert err.value.status_code == 422 |
| 107 | with pytest.raises(rn.ReservedNameError): |
| 108 | await musehub_profile.create_profile(None, user_id="u", username="mists") # type: ignore[arg-type] |
| 109 | |
| 110 | def test_both_key_registration_paths_check_before_creating_the_identity(self) -> None: |
| 111 | from musehub.services import musehub_auth |
| 112 | |
| 113 | src = inspect.getsource(musehub_auth) |
| 114 | # human verify-registration and operator agent-registration: the check precedes the MusehubIdentity insert |
| 115 | for marker in ("# Truly new identity — normal registration path.", "# New registration."): |
| 116 | block = src.split(marker, 1)[1][:400] |
| 117 | assert block.index("_reject_reserved_handle(handle)") < block.index("MusehubIdentity("), marker |
| 118 | |
| 119 | def test_reject_reserved_handle_is_a_422(self) -> None: |
| 120 | from musehub.services.musehub_auth import AuthError, _reject_reserved_handle |
| 121 | |
| 122 | with pytest.raises(AuthError) as err: |
| 123 | _reject_reserved_handle("mists") |
| 124 | assert err.value.status_code == 422 |
| 125 | _reject_reserved_handle("gabriel") |
| 126 | |
| 127 | |
| 128 | def _async_return(value: object) -> object: |
| 129 | async def _f(*a: object, **k: object) -> object: |
| 130 | return value |
| 131 | |
| 132 | return _f |
| 133 | |
| 134 | |
| 135 | @pytest.mark.asyncio |
| 136 | async def test_rest_repo_create_refuses_mists(client: AsyncClient, auth_headers: StrDict) -> None: |
| 137 | """``POST /api/repos`` with name ``mists`` is a 422 naming the reason, not a repo whose home page is unreachable.""" |
| 138 | resp = await client.post("/api/repos", json={"name": "mists", "owner": "testuser"}, headers=auth_headers) |
| 139 | assert resp.status_code == 422, resp.text |
| 140 | assert "reserved" in resp.text |
| 141 | ok = await client.post("/api/repos", json={"name": "mists-notes", "owner": "testuser"}, headers=auth_headers) |
| 142 | assert ok.status_code == 201, ok.text |
File History
2 commits
sha256:41ed2ded57d17544c111c81960ee6a7a7febc9a0d81a778616319265ac93a654
merge: rc25 — Mists wave (#316 #317 #318 #322; #319 #320 #3…
Opus 5.5
minor
⚠
2 days ago
sha256:39410e826ef743cd50269de7aa0a1c5a1b2c4d988949231dfb014f519e0c219d
fix(names): reserve handles and repo slugs that fixed route…
Opus 5.5
patch
2 days ago