gabriel / musehub public
test_repo_create_owner_auth.py python
122 lines 6.2 KB
Raw
sha256:12a47d936d91d16ca58705b5e19fc1782cb8888b124c2b6087ecf0ae1a773a96 fix(csp): every template runs under the strict CSP; remove … Opus 5.5 minor ⚠ breaking 1 day ago
1 """Repository creation is limited to the caller's own namespace.
2
3 ``POST /api/repos`` takes ``owner`` from the request body. It must refuse
4 any owner other than the authenticated caller's handle, and must leave every existing repository untouched.
5 System flows that create repositories server-side (identity registration, organisation creation) are unaffected.
6 """
7 from __future__ import annotations
8
9 from collections.abc import AsyncGenerator
10
11 import pytest
12 from httpx import AsyncClient
13 from sqlalchemy import select
14 from sqlalchemy.ext.asyncio import AsyncSession
15
16 from musehub.auth.request_signing import MSignContext, optional_signed_request, require_signed_request
17 from musehub.core.genesis import compute_identity_id
18 from musehub.db.musehub_identity_models import MusehubIdentity
19 from musehub.db.musehub_repo_models import MusehubRepo
20 from musehub.main import app
21
22 ROUTES = ["/api/repos"]
23
24
25 def _as(handle: str, *, is_admin: bool = False) -> None:
26 ctx = MSignContext(handle=handle, identity_id=compute_identity_id(handle.encode()), is_agent=False, is_admin=is_admin)
27 app.dependency_overrides[require_signed_request] = lambda: ctx
28 app.dependency_overrides[optional_signed_request] = lambda: ctx
29
30
31 @pytest.fixture
32 async def people(db_session: AsyncSession) -> AsyncGenerator[None, None]:
33 for h in ("alice", "bob"):
34 db_session.add(MusehubIdentity(identity_id=compute_identity_id(h.encode()), handle=h, display_name=h, identity_type="human"))
35 await db_session.commit()
36 yield
37 app.dependency_overrides.pop(require_signed_request, None)
38 app.dependency_overrides.pop(optional_signed_request, None)
39
40
41 async def _slugs(db: AsyncSession, owner: str) -> list[str]:
42 return [r.slug for r in (await db.execute(select(MusehubRepo).where(MusehubRepo.owner == owner))).scalars()]
43
44
45 async def _identity_record_present(db: AsyncSession, handle: str) -> bool:
46 from musehub.api.routes.api.identities import _read_identity_record_from_repo
47 return (await _read_identity_record_from_repo(db, handle)) is not None
48
49
50 @pytest.mark.parametrize("path", ROUTES)
51 async def test_own_namespace_is_created(client: AsyncClient, db_session: AsyncSession, people: None, path: str) -> None:
52 _as("alice")
53 r = await client.post(path, json={"name": "mine", "owner": "alice"})
54 assert r.status_code == 201, (r.status_code, r.text[:200])
55 assert await _slugs(db_session, "alice") == ["mine"]
56
57
58 @pytest.mark.parametrize("path", ROUTES)
59 async def test_another_users_namespace_is_refused(client: AsyncClient, db_session: AsyncSession, people: None, path: str) -> None:
60 _as("alice")
61 r = await client.post(path, json={"name": "not-mine", "owner": "bob"})
62 assert r.status_code == 403, (r.status_code, r.text[:200])
63 assert await _slugs(db_session, "bob") == []
64
65
66 @pytest.mark.parametrize("path", ROUTES)
67 async def test_admin_flag_does_not_bypass_the_owner_check(client: AsyncClient, db_session: AsyncSession, people: None, path: str) -> None:
68 # No route creates repositories on another identity's behalf, so the admin flag grants nothing here.
69 _as("alice", is_admin=True)
70 r = await client.post(path, json={"name": "not-mine", "owner": "bob"})
71 assert r.status_code == 403, (r.status_code, r.text[:200])
72 assert await _slugs(db_session, "bob") == []
73
74
75 @pytest.mark.parametrize("path", ROUTES)
76 async def test_unregistered_namespace_is_refused(client: AsyncClient, db_session: AsyncSession, people: None, path: str) -> None:
77 _as("alice")
78 for name in ("identity", "anything"):
79 r = await client.post(path, json={"name": name, "owner": "carol"})
80 assert r.status_code == 403, (name, r.status_code, r.text[:200])
81 assert await _slugs(db_session, "carol") == []
82
83
84 async def test_existing_repository_is_untouched(client: AsyncClient, db_session: AsyncSession, people: None) -> None:
85 _as("bob")
86 assert (await client.post("/api/repos", json={"name": "keep", "owner": "bob"})).status_code == 201
87 row = (await db_session.execute(select(MusehubRepo).where(MusehubRepo.owner == "bob", MusehubRepo.slug == "keep"))).scalar_one()
88 before = (row.repo_id, row.owner_user_id, row.visibility, row.description)
89 _as("alice")
90 for path in ROUTES:
91 r = await client.post(path, json={"name": "keep", "owner": "bob", "visibility": "private", "description": "changed"})
92 assert r.status_code == 403, (path, r.status_code) # refused before any lookup: no existence signal
93 db_session.expire_all()
94 row = (await db_session.execute(select(MusehubRepo).where(MusehubRepo.owner == "bob", MusehubRepo.slug == "keep"))).scalar_one()
95 assert (row.repo_id, row.owner_user_id, row.visibility, row.description) == before
96 _as("bob")
97 assert (await client.post("/api/repos", json={"name": "keep", "owner": "bob"})).status_code == 409
98
99
100 async def test_registration_creates_the_identity_record(client: AsyncClient, db_session: AsyncSession, people: None) -> None:
101 from musehub.services.musehub_auth import _create_identity_repo
102 _as("alice")
103 assert (await client.post("/api/repos", json={"name": "identity", "owner": "carol"})).status_code == 403
104 cid = compute_identity_id(b"carol")
105 db_session.add(MusehubIdentity(identity_id=cid, handle="carol", display_name="carol", identity_type="human"))
106 await db_session.commit()
107 await _create_identity_repo(db_session, identity_id=cid, handle="carol", public_key_b64="A" * 43)
108 await db_session.commit()
109 assert await _identity_record_present(db_session, "carol")
110
111
112 async def test_organisation_creation_and_namespace(client: AsyncClient, db_session: AsyncSession, people: None) -> None:
113 from musehub.services.musehub_orgs import create_org
114 _as("alice")
115 assert (await client.post("/api/repos", json={"name": "identity", "owner": "acme"})).status_code == 403
116 await create_org(db_session, handle="acme", display_name="Acme", quorum=1, creator_identity_id=compute_identity_id(b"bob"))
117 assert await _identity_record_present(db_session, "acme")
118 # An existing organisation's namespace is refused to a non-member, through both routes.
119 for path in ROUTES:
120 r = await client.post(path, json={"name": "org-repo", "owner": "acme"})
121 assert r.status_code == 403, (path, r.status_code)
122 assert await _slugs(db_session, "acme") == ["identity"]
File History 2 commits
sha256:12a47d936d91d16ca58705b5e19fc1782cb8888b124c2b6087ecf0ae1a773a96 fix(csp): every template runs under the strict CSP; remove … Opus 5.5 minor ⚠ 1 day ago
sha256:39de580659e4fbb872694275ab464b77c905aea8053798629ce42da3224c90ef fix(repos): only create repositories under the caller's own… Opus 5.5 patch 8 days ago