gabriel / musehub public
test_branch_delete_signed.py python
79 lines 4.0 KB
Raw
sha256:533a428669ed74e9f74471413a844d4c2f95bb336de16b058c62d21ca1b0fc65 Merge branch 'task/wave-hub-ui-bugs' into dev Opus 5.5 minor ⚠ breaking 1 day ago
1 """musehub#94: a real MSign-signed branch DELETE, end to end through the app (no auth override).
2
3 The CLI (``muse push <remote> --delete <branch>``) signs ``DELETE {repo}/branches/{quote(branch, safe='')}``, so a
4 ``feat/x`` branch is signed with ``feat%2Fx`` in the path. The 401 came from the server verifying the decoded path;
5 the signed path is now taken from ``scope["raw_path"]``. These tests sign exactly as Muse's transport does.
6
7 BD_01 the owner's signed DELETE of a slash-named branch is 200 and the branch is gone
8 BD_02 the same for a plain branch name
9 BD_03 a signature over the decoded path is rejected (the path really is bound)
10 BD_04 an unsigned DELETE is 401
11 """
12 from __future__ import annotations
13
14 import secrets
15 import urllib.parse
16
17 from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
18 from httpx import AsyncClient
19 from sqlalchemy import select, text
20 from sqlalchemy.ext.asyncio import AsyncSession
21
22 from muse.core.msign import build_msign_header
23 from muse.core.transport import SigningIdentity
24 from musehub.core.genesis import compute_branch_id
25 from musehub.db.musehub_repo_models import MusehubBranch
26 from tests.factories import create_repo
27 from tests.test_identity_integration import _seed_identity
28
29
30 async def _setup(db: AsyncSession, branch: str) -> tuple[SigningIdentity, str, str]:
31 priv = Ed25519PrivateKey.generate()
32 handle = "bd" + secrets.token_hex(4)
33 await _seed_identity(db, handle, priv, priv.public_key().public_bytes_raw())
34 repo = await create_repo(db, visibility="public")
35 slug = "r" + secrets.token_hex(4)
36 await db.execute(text("UPDATE musehub_repos SET owner = :o, slug = :s, name = :s WHERE repo_id = :r"),
37 {"o": handle, "s": slug, "r": repo.repo_id})
38 for name in ("main", branch):
39 db.add(MusehubBranch(branch_id=compute_branch_id(str(repo.repo_id), name), repo_id=repo.repo_id, name=name))
40 await db.commit()
41 return SigningIdentity(handle=handle, private_key=priv), f"/{handle}/{slug}", str(repo.repo_id)
42
43
44 async def _branches(db: AsyncSession, repo_id: str) -> set[str]:
45 db.expire_all()
46 rows = await db.execute(select(MusehubBranch.name).where(MusehubBranch.repo_id == repo_id))
47 return set(rows.scalars())
48
49
50 async def _signed_delete(client: AsyncClient, signing: SigningIdentity, base: str, branch: str) -> int:
51 path = f"{base}/branches/{urllib.parse.quote(branch, safe='')}" # muse.core.transport.delete_branch
52 auth = build_msign_header(signing, "DELETE", f"http://test{path}")
53 return (await client.delete(path, headers={"Authorization": auth})).status_code
54
55
56 async def test_bd_01_signed_delete_of_slash_branch(client: AsyncClient, db_session: AsyncSession) -> None:
57 signing, base, repo_id = await _setup(db_session, "feat/fetch-mpack-cache")
58 assert await _signed_delete(client, signing, base, "feat/fetch-mpack-cache") == 200
59 assert await _branches(db_session, repo_id) == {"main"}
60
61
62 async def test_bd_02_signed_delete_of_plain_branch(client: AsyncClient, db_session: AsyncSession) -> None:
63 signing, base, repo_id = await _setup(db_session, "scratch")
64 assert await _signed_delete(client, signing, base, "scratch") == 200
65 assert await _branches(db_session, repo_id) == {"main"}
66
67
68 async def test_bd_03_signature_over_decoded_path_is_rejected(client: AsyncClient, db_session: AsyncSession) -> None:
69 signing, base, repo_id = await _setup(db_session, "feat/x")
70 auth = build_msign_header(signing, "DELETE", f"http://test{base}/branches/feat/x")
71 resp = await client.delete(f"{base}/branches/feat%2Fx", headers={"Authorization": auth})
72 assert resp.status_code == 401
73 assert await _branches(db_session, repo_id) == {"main", "feat/x"}
74
75
76 async def test_bd_04_unsigned_delete_is_401(client: AsyncClient, db_session: AsyncSession) -> None:
77 _, base, repo_id = await _setup(db_session, "feat/y")
78 assert (await client.delete(f"{base}/branches/feat%2Fy")).status_code == 401
79 assert await _branches(db_session, repo_id) == {"main", "feat/y"}
File History 2 commits
sha256:533a428669ed74e9f74471413a844d4c2f95bb336de16b058c62d21ca1b0fc65 Merge branch 'task/wave-hub-ui-bugs' into dev Opus 5.5 minor ⚠ 1 day ago
sha256:1fc436ba9542c2caa9eae7e700609047e22c5805135a38690dc2ad29aa1cc2fd test(wire): a real MSign-signed branch DELETE is accepted f… Opus 5.5 1 day ago