test_authz_dead_dismiss.py
python
sha256:2a6a94eeecc7c8db2e0c7c470c8605ff045f32d380cd6f6fa659367b212af8fb
fix(intel): dead-code dismiss uses the standard repository …
Opus 5.5
patch
4 days ago
| 1 | """Authorization regression for the dead-code dismiss write (musehub #131 Phase 3). |
| 2 | |
| 3 | Dismissing a dead-code candidate mutates repo state, so it requires the repo |
| 4 | owner or an accepted write/admin collaborator. Anonymous and read-only callers |
| 5 | must be rejected and the row left untouched. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | from datetime import datetime, timezone |
| 10 | |
| 11 | import pytest |
| 12 | from httpx import ASGITransport, AsyncClient |
| 13 | from sqlalchemy import select |
| 14 | from sqlalchemy.dialects.postgresql import insert as pg_insert |
| 15 | from sqlalchemy.ext.asyncio import AsyncSession |
| 16 | |
| 17 | from muse.core.types import long_id |
| 18 | from musehub.auth.dependencies import optional_token |
| 19 | from musehub.auth.request_signing import MSignContext |
| 20 | from musehub.core.genesis import compute_collaborator_id, compute_identity_id |
| 21 | from musehub.db.database import get_db |
| 22 | from musehub.db.musehub_collaborator_models import MusehubCollaborator |
| 23 | from musehub.db.musehub_intel_models import MusehubIntelDead |
| 24 | from musehub.main import app |
| 25 | from tests.factories import create_repo |
| 26 | |
| 27 | _OWNER = "dd-owner" |
| 28 | _SLUG = "dd-authz" |
| 29 | _ADDR = "pkg/a.py::fn" |
| 30 | |
| 31 | |
| 32 | def _ctx(handle: str) -> MSignContext: |
| 33 | return MSignContext(handle=handle, identity_id=compute_identity_id(handle.encode()), |
| 34 | is_agent=False, is_admin=False) |
| 35 | |
| 36 | |
| 37 | async def _setup(db: AsyncSession, collab: tuple[str, str] | None = None) -> str: |
| 38 | repo = await create_repo(db, owner=_OWNER, slug=_SLUG) |
| 39 | await db.commit() |
| 40 | repo_id = repo.repo_id |
| 41 | await db.execute(pg_insert(MusehubIntelDead).values( |
| 42 | repo_id=repo_id, address=_ADDR, kind="function", confidence="high", |
| 43 | reason="r", ref=long_id("a" * 64), dismissed=False)) |
| 44 | if collab: |
| 45 | handle, perm = collab |
| 46 | row = MusehubCollaborator( |
| 47 | id=compute_collaborator_id(repo_id, compute_identity_id(handle.encode()), |
| 48 | datetime.now(timezone.utc).isoformat()), |
| 49 | repo_id=repo_id, identity_handle=handle, permission=perm) |
| 50 | db.add(row) |
| 51 | await db.flush() |
| 52 | row.accepted_at = row.invited_at |
| 53 | await db.commit() |
| 54 | return repo_id |
| 55 | |
| 56 | |
| 57 | async def _post(db: AsyncSession, claims: MSignContext | None) -> int: |
| 58 | async def _db(): |
| 59 | yield db |
| 60 | app.dependency_overrides[get_db] = _db |
| 61 | app.dependency_overrides[optional_token] = lambda: claims |
| 62 | try: |
| 63 | async with AsyncClient(transport=ASGITransport(app=app), base_url="https://localhost:1337") as c: |
| 64 | r = await c.post(f"/{_OWNER}/{_SLUG}/intel/dead/dismiss", |
| 65 | data={"address": _ADDR}, follow_redirects=False) |
| 66 | return r.status_code |
| 67 | finally: |
| 68 | app.dependency_overrides.pop(optional_token, None) |
| 69 | app.dependency_overrides.pop(get_db, None) |
| 70 | |
| 71 | |
| 72 | async def _dismissed(db: AsyncSession, repo_id: str) -> bool: |
| 73 | db.expire_all() |
| 74 | return (await db.execute(select(MusehubIntelDead.dismissed).where( |
| 75 | MusehubIntelDead.repo_id == repo_id, MusehubIntelDead.address == _ADDR))).scalar_one() |
| 76 | |
| 77 | |
| 78 | @pytest.mark.asyncio |
| 79 | async def test_anonymous_rejected(db_session: AsyncSession) -> None: |
| 80 | rid = await _setup(db_session) |
| 81 | assert await _post(db_session, None) == 401 |
| 82 | assert await _dismissed(db_session, rid) is False |
| 83 | |
| 84 | |
| 85 | @pytest.mark.asyncio |
| 86 | async def test_non_collaborator_rejected(db_session: AsyncSession) -> None: |
| 87 | rid = await _setup(db_session) |
| 88 | assert await _post(db_session, _ctx("dd-stranger")) == 403 |
| 89 | assert await _dismissed(db_session, rid) is False |
| 90 | |
| 91 | |
| 92 | @pytest.mark.asyncio |
| 93 | async def test_read_only_collaborator_rejected(db_session: AsyncSession) -> None: |
| 94 | rid = await _setup(db_session, ("dd-reader", "read")) |
| 95 | assert await _post(db_session, _ctx("dd-reader")) == 403 |
| 96 | assert await _dismissed(db_session, rid) is False |
| 97 | |
| 98 | |
| 99 | @pytest.mark.asyncio |
| 100 | async def test_write_collaborator_allowed(db_session: AsyncSession) -> None: |
| 101 | rid = await _setup(db_session, ("dd-writer", "write")) |
| 102 | assert await _post(db_session, _ctx("dd-writer")) == 302 |
| 103 | assert await _dismissed(db_session, rid) is True |
| 104 | |
| 105 | |
| 106 | @pytest.mark.asyncio |
| 107 | async def test_owner_allowed(db_session: AsyncSession) -> None: |
| 108 | rid = await _setup(db_session) |
| 109 | assert await _post(db_session, _ctx(_OWNER)) == 302 |
| 110 | assert await _dismissed(db_session, rid) is True |
File History
1 commit
sha256:2a6a94eeecc7c8db2e0c7c470c8605ff045f32d380cd6f6fa659367b212af8fb
fix(intel): dead-code dismiss uses the standard repository …
Opus 5.5
patch
4 days ago