gabriel / musehub public

test_auth_authorization.py file-level

at sha256:3 · View file ↗ · Intel ↗

History
1 files
1 commits
0 hotspots
0 🧊 dead
0 πŸ’₯ blast risk
sha256:e fix(9A-4 F7): env alias + staging secrets for overseer provenance gate.… · aaronrene · Jul 13, 2026
1 """Tests for checklist section 1.2 β€” Authorization guards.
2
3 Covers the three guard helpers that enforce repo-level access control:
4
5 _guard_visibility β€” 404 on missing repo; 401 on private + unauthenticated
6 _guard_write_access β€” 403 when non-owner tries to write to a private repo
7 _guard_repo_owner β€” 403 when non-owner attempts an owner-only action
8
9 All tests are synchronous unit tests β€” no DB or HTTP fixtures needed.
10 """
11 from __future__ import annotations
12
13 import pytest
14 from fastapi import HTTPException
15 from unittest.mock import AsyncMock, MagicMock
16
17
18 # ---------------------------------------------------------------------------
19 # Helpers β€” build lightweight repo/claims stand-ins without importing ORM
20 # ---------------------------------------------------------------------------
21
22 def _repo(*, visibility: str = "public", owner: str = "alice") -> MagicMock:
23 r = MagicMock()
24 r.visibility = visibility
25 r.owner = owner
26 return r
27
28
29 def _claims(*, handle: str = "alice") -> MagicMock:
30 c = MagicMock()
31 c.handle = handle
32 return c
33
34
35 def _db_no_collab() -> AsyncMock:
36 """Async DB session stub that returns no collaborator row."""
37 result = MagicMock()
38 result.scalar_one_or_none.return_value = None
39 db = AsyncMock()
40 db.execute = AsyncMock(return_value=result)
41 return db
42
43
44 def _db_with_collab() -> AsyncMock:
45 """Async DB session stub that returns an accepted write/admin collaborator row."""
46 result = MagicMock()
47 result.scalar_one_or_none.return_value = MagicMock() # any truthy row
48 db = AsyncMock()
49 db.execute = AsyncMock(return_value=result)
50 return db
51
52
53 # ---------------------------------------------------------------------------
54 # _guard_visibility
55 # ---------------------------------------------------------------------------
56
57 class TestGuardVisibility:
58 def setup_method(self) -> None:
59 from musehub.api.routes.musehub.repos import _guard_visibility
60 self.guard = _guard_visibility
61
62 def test_none_repo_raises_404(self) -> None:
63 with pytest.raises(HTTPException) as exc_info:
64 self.guard(None, _claims())
65 assert exc_info.value.status_code == 404
66
67 def test_none_repo_none_claims_raises_404(self) -> None:
68 """404 takes priority over 401 β€” missing repo is never exposed as 401."""
69 with pytest.raises(HTTPException) as exc_info:
70 self.guard(None, None)
71 assert exc_info.value.status_code == 404
72
73 def test_public_repo_no_claims_allowed(self) -> None:
74 """Public repo with no auth must not raise."""
75 self.guard(_repo(visibility="public"), None)
76
77 def test_public_repo_with_claims_allowed(self) -> None:
78 self.guard(_repo(visibility="public"), _claims())
79
80 def test_private_repo_with_claims_allowed(self) -> None:
81 """Authenticated user may access a private repo."""
82 self.guard(_repo(visibility="private"), _claims())
83
84 def test_private_repo_no_claims_raises_401(self) -> None:
85 with pytest.raises(HTTPException) as exc_info:
86 self.guard(_repo(visibility="private"), None)
87 assert exc_info.value.status_code == 401
88
89 def test_private_repo_401_includes_www_authenticate(self) -> None:
90 with pytest.raises(HTTPException) as exc_info:
91 self.guard(_repo(visibility="private"), None)
92 assert "WWW-Authenticate" in exc_info.value.headers
93 assert "MSign" in exc_info.value.headers["WWW-Authenticate"]
94
95
96 # ---------------------------------------------------------------------------
97 # _guard_write_access
98 # ---------------------------------------------------------------------------
99
100 class TestGuardWriteAccess:
101 def setup_method(self) -> None:
102 from musehub.api.routes.musehub.issues import _guard_write_access
103 self.guard = _guard_write_access
104
105 def test_public_repo_any_user_may_write(self) -> None:
106 """Any authenticated user can write to a public repo."""
107 self.guard(_repo(visibility="public", owner="alice"), "bob")
108
109 def test_public_repo_owner_may_write(self) -> None:
110 self.guard(_repo(visibility="public", owner="alice"), "alice")
111
112 def test_private_repo_owner_may_write(self) -> None:
113 self.guard(_repo(visibility="private", owner="alice"), "alice")
114
115 def test_private_repo_non_owner_raises_403(self) -> None:
116 with pytest.raises(HTTPException) as exc_info:
117 self.guard(_repo(visibility="private", owner="alice"), "bob")
118 assert exc_info.value.status_code == 403
119
120 def test_private_repo_403_detail_message(self) -> None:
121 with pytest.raises(HTTPException) as exc_info:
122 self.guard(_repo(visibility="private", owner="alice"), "bob")
123 assert "private" in exc_info.value.detail.lower()
124
125
126 # ---------------------------------------------------------------------------
127 # _guard_repo_owner
128 # ---------------------------------------------------------------------------
129
130 class TestGuardRepoOwner:
131 async def test_owner_allowed(self) -> None:
132 from musehub.api.routes.musehub.issues import _guard_repo_owner
133 # Owner check short-circuits before any DB call.
134 await _guard_repo_owner(_repo(owner="alice"), "alice", _db_no_collab())
135
136 async def test_non_owner_raises_403(self) -> None:
137 from musehub.api.routes.musehub.issues import _guard_repo_owner
138 with pytest.raises(HTTPException) as exc_info:
139 await _guard_repo_owner(_repo(owner="alice"), "bob", _db_no_collab())
140 assert exc_info.value.status_code == 403
141
142 async def test_non_owner_public_repo_still_raises_403(self) -> None:
143 """Even public repos: only owner may perform owner-only actions."""
144 from musehub.api.routes.musehub.issues import _guard_repo_owner
145 with pytest.raises(HTTPException) as exc_info:
146 await _guard_repo_owner(_repo(visibility="public", owner="alice"), "bob", _db_no_collab())
147 assert exc_info.value.status_code == 403
148
149 async def test_403_detail_message(self) -> None:
150 from musehub.api.routes.musehub.issues import _guard_repo_owner
151 with pytest.raises(HTTPException) as exc_info:
152 await _guard_repo_owner(_repo(owner="alice"), "bob", _db_no_collab())
153 assert "owner" in exc_info.value.detail.lower()
154
155 async def test_write_collaborator_allowed(self) -> None:
156 """A non-owner with an accepted write/admin collaborator row is allowed."""
157 from musehub.api.routes.musehub.issues import _guard_repo_owner
158 await _guard_repo_owner(_repo(owner="alice"), "bob", _db_with_collab())
159
160
161 # ---------------------------------------------------------------------------
162 # proposals._guard_repo_owner β€” musehub#132-adjacent: aaronrene (a write
163 # collaborator, and the proposal's own author) got a 403 requesting reviewers
164 # on his own proposal, because proposals.py has its own, weaker, sync-only
165 # _guard_repo_owner that only checks literal ownership -- unlike issues.py's
166 # collaborator-aware version above, which this ports the same logic into.
167 # ---------------------------------------------------------------------------
168
169 class TestProposalsGuardRepoOwner:
170 async def test_owner_allowed(self) -> None:
171 from musehub.api.routes.musehub.proposals import _guard_repo_owner
172 await _guard_repo_owner(_repo(owner="alice"), "alice", _db_no_collab())
173
174 async def test_non_owner_no_collab_raises_403(self) -> None:
175 from musehub.api.routes.musehub.proposals import _guard_repo_owner
176 with pytest.raises(HTTPException) as exc_info:
177 await _guard_repo_owner(_repo(owner="alice"), "bob", _db_no_collab())
178 assert exc_info.value.status_code == 403
179
180 async def test_write_collaborator_allowed(self) -> None:
181 """The bug: a write collaborator (e.g. requesting reviewers on their
182 own proposal) must be allowed, not just the literal repo owner."""
183 from musehub.api.routes.musehub.proposals import _guard_repo_owner
184 await _guard_repo_owner(_repo(owner="alice"), "bob", _db_with_collab())
185
186 async def test_403_detail_message(self) -> None:
187 from musehub.api.routes.musehub.proposals import _guard_repo_owner
188 with pytest.raises(HTTPException) as exc_info:
189 await _guard_repo_owner(_repo(owner="alice"), "bob", _db_no_collab())
190 assert "owner" in exc_info.value.detail.lower() or "collaborator" in exc_info.value.detail.lower()