Skip to content

Commit d0564b4

Browse files
olivermeyerclaude
andcommitted
fix(tests): extract constants, document empty stubs
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 607ae47 commit d0564b4

3 files changed

Lines changed: 27 additions & 20 deletions

File tree

tests/aignostics_foundry_core/api/auth_test.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
_INTERNAL_ORG_ID = "org_internal_123"
2626
_OTHER_ORG_ID = "org_other_456"
2727
_USER_NOT_AUTHENTICATED = "User is not authenticated"
28+
_USER_SUB = "auth0|x"
29+
_USER_EMAIL = "x@x.com"
2830

2931

3032
@pytest.mark.unit
@@ -113,7 +115,7 @@ async def test_get_user_returns_none_for_expired_session(self) -> None:
113115
cookie = "fake-cookie"
114116

115117
fake_client = MagicMock()
116-
expired_user = {"sub": "auth0|x", "email": "x@x.com", "exp": int(time.time()) - 3600}
118+
expired_user = {"sub": _USER_SUB, "email": _USER_EMAIL, "exp": int(time.time()) - 3600}
117119
fake_client.require_session = AsyncMock(return_value={"user": expired_user})
118120

119121
with (
@@ -155,7 +157,7 @@ async def test_get_user_returns_user_for_valid_session(self) -> None:
155157
"""get_user returns the user dict when the session is valid and not expired."""
156158
request = MagicMock()
157159
cookie = "fake-cookie"
158-
user = {"sub": "auth0|x", "email": "x@x.com", "exp": int(time.time()) + 3600}
160+
user = {"sub": _USER_SUB, "email": _USER_EMAIL, "exp": int(time.time()) + 3600}
159161
fake_client = MagicMock()
160162
fake_client.require_session = AsyncMock(return_value={"user": user})
161163

@@ -184,7 +186,7 @@ async def test_unauthenticated_user_raises_forbidden_error(self) -> None:
184186
async def test_authenticated_user_passes(self) -> None:
185187
"""require_authenticated returns None without raising when user is authenticated."""
186188
request = MagicMock()
187-
user = {"sub": "auth0|x", "email": "x@x.com"}
189+
user = {"sub": _USER_SUB, "email": _USER_EMAIL}
188190
with patch(_PATCH_GET_USER, new=AsyncMock(return_value=user)):
189191
result = await require_authenticated(request, None)
190192
assert result is None
@@ -205,7 +207,7 @@ async def test_wrong_role_raises_forbidden_error(self, monkeypatch: pytest.Monke
205207
"""require_admin raises ForbiddenError when user has a non-admin role."""
206208
monkeypatch.delenv("FOUNDRY_AUTH_AUTH0_ROLE_CLAIM", raising=False)
207209
request = MagicMock()
208-
user = {"sub": "auth0|x", DEFAULT_AUTH0_ROLE_CLAIM: "viewer"}
210+
user = {"sub": _USER_SUB, DEFAULT_AUTH0_ROLE_CLAIM: "viewer"}
209211
with (
210212
patch(_PATCH_GET_USER, new=AsyncMock(return_value=user)),
211213
pytest.raises(ForbiddenError, match="does not match required role"),
@@ -216,7 +218,7 @@ async def test_admin_role_passes(self, monkeypatch: pytest.MonkeyPatch) -> None:
216218
"""require_admin returns None without raising when user has the admin role."""
217219
monkeypatch.delenv("FOUNDRY_AUTH_AUTH0_ROLE_CLAIM", raising=False)
218220
request = MagicMock()
219-
user = {"sub": "auth0|x", DEFAULT_AUTH0_ROLE_CLAIM: AUTH0_ROLE_ADMIN}
221+
user = {"sub": _USER_SUB, DEFAULT_AUTH0_ROLE_CLAIM: AUTH0_ROLE_ADMIN}
220222
with patch(_PATCH_GET_USER, new=AsyncMock(return_value=user)):
221223
result = await require_admin(request, None)
222224
assert result is None
@@ -240,7 +242,7 @@ async def test_wrong_org_raises_forbidden_error(self, monkeypatch: pytest.Monkey
240242
"""require_internal raises ForbiddenError when user belongs to a different org."""
241243
monkeypatch.setenv("FOUNDRY_AUTH_INTERNAL_ORG_ID", _INTERNAL_ORG_ID)
242244
request = MagicMock()
243-
user = {"sub": "auth0|x", "org_id": _OTHER_ORG_ID}
245+
user = {"sub": _USER_SUB, "org_id": _OTHER_ORG_ID}
244246
with (
245247
patch(_PATCH_GET_USER, new=AsyncMock(return_value=user)),
246248
pytest.raises(ForbiddenError, match="not a member of the internal organization"),
@@ -251,7 +253,7 @@ async def test_internal_org_member_passes(self, monkeypatch: pytest.MonkeyPatch)
251253
"""require_internal returns None without raising when user is in the internal org."""
252254
monkeypatch.setenv("FOUNDRY_AUTH_INTERNAL_ORG_ID", _INTERNAL_ORG_ID)
253255
request = MagicMock()
254-
user = {"sub": "auth0|x", "org_id": _INTERNAL_ORG_ID}
256+
user = {"sub": _USER_SUB, "org_id": _INTERNAL_ORG_ID}
255257
with patch(_PATCH_GET_USER, new=AsyncMock(return_value=user)):
256258
result = await require_internal(request, None)
257259
assert result is None
@@ -275,7 +277,7 @@ async def test_wrong_org_raises_forbidden_error(self, monkeypatch: pytest.Monkey
275277
"""require_internal_admin raises ForbiddenError when user belongs to a different org."""
276278
monkeypatch.setenv("FOUNDRY_AUTH_INTERNAL_ORG_ID", _INTERNAL_ORG_ID)
277279
request = MagicMock()
278-
user = {"sub": "auth0|x", "org_id": _OTHER_ORG_ID}
280+
user = {"sub": _USER_SUB, "org_id": _OTHER_ORG_ID}
279281
with (
280282
patch(_PATCH_GET_USER, new=AsyncMock(return_value=user)),
281283
pytest.raises(ForbiddenError, match="not a member of the internal organization"),
@@ -287,7 +289,7 @@ async def test_correct_org_wrong_role_raises_forbidden_error(self, monkeypatch:
287289
monkeypatch.setenv("FOUNDRY_AUTH_INTERNAL_ORG_ID", _INTERNAL_ORG_ID)
288290
monkeypatch.delenv("FOUNDRY_AUTH_AUTH0_ROLE_CLAIM", raising=False)
289291
request = MagicMock()
290-
user = {"sub": "auth0|x", "org_id": _INTERNAL_ORG_ID, DEFAULT_AUTH0_ROLE_CLAIM: "viewer"}
292+
user = {"sub": _USER_SUB, "org_id": _INTERNAL_ORG_ID, DEFAULT_AUTH0_ROLE_CLAIM: "viewer"}
291293
with (
292294
patch(_PATCH_GET_USER, new=AsyncMock(return_value=user)),
293295
pytest.raises(ForbiddenError, match="does not match required role"),
@@ -299,7 +301,7 @@ async def test_internal_admin_passes(self, monkeypatch: pytest.MonkeyPatch) -> N
299301
monkeypatch.setenv("FOUNDRY_AUTH_INTERNAL_ORG_ID", _INTERNAL_ORG_ID)
300302
monkeypatch.delenv("FOUNDRY_AUTH_AUTH0_ROLE_CLAIM", raising=False)
301303
request = MagicMock()
302-
user = {"sub": "auth0|x", "org_id": _INTERNAL_ORG_ID, DEFAULT_AUTH0_ROLE_CLAIM: AUTH0_ROLE_ADMIN}
304+
user = {"sub": _USER_SUB, "org_id": _INTERNAL_ORG_ID, DEFAULT_AUTH0_ROLE_CLAIM: AUTH0_ROLE_ADMIN}
303305
with patch(_PATCH_GET_USER, new=AsyncMock(return_value=user)):
304306
result = await require_internal_admin(request, None)
305307
assert result is None

tests/aignostics_foundry_core/api/core_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ def test_versioned_api_router_add_exception_handler_registration() -> None:
168168
from aignostics_foundry_core.api.core import VersionedAPIRouter
169169

170170
def handler(request: object, exc: Exception) -> None:
171-
pass
171+
# Test double: only verifies registration, not invocation
172+
...
172173

173174
router = VersionedAPIRouter(VERSION_STEP1)
174175
cast("Any", router).add_exception_handler_registration(ValueError, handler)
@@ -234,7 +235,8 @@ def test_init_api_with_custom_exception_handler_registrations() -> None:
234235
from aignostics_foundry_core.api.core import init_api
235236

236237
def handler(request: object, exc: Exception) -> None:
237-
pass
238+
# Test double: only verifies registration, not invocation
239+
...
238240

239241
app = init_api(exception_handler_registrations=[(ValueError, handler)])
240242

tests/aignostics_foundry_core/gui/gui_test.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
_ROLE_CLAIM = "https://example.com/role"
3636
_PROJECT_NAME = "myproject"
3737
_FIXED_PORT = 9000
38+
_DOCS_PATH = "/docs"
39+
_USER_SUB = "auth0|x"
3840

3941

4042
# ---------------------------------------------------------------------------
@@ -394,19 +396,19 @@ def test_docs_redirect_added_when_not_present(self) -> None:
394396
fastapi_mock = MagicMock()
395397
fastapi_mock.state = SimpleNamespace()
396398
self._call_gui_run(nicegui_mock, fastapi_app=fastapi_mock)
397-
docs_calls = [c for c in app_mock.get.call_args_list if c.args[0] == "/docs"]
399+
docs_calls = [c for c in app_mock.get.call_args_list if c.args[0] == _DOCS_PATH]
398400
assert len(docs_calls) == 1
399401

400402
def test_docs_redirect_skipped_when_already_present(self) -> None:
401403
"""When /docs route already exists, app.get is NOT called for /docs."""
402404
nicegui_mock, app_mock, _ = _make_nicegui_app_mock()
403405
docs_route = MagicMock()
404-
docs_route.path = "/docs"
406+
docs_route.path = _DOCS_PATH
405407
app_mock.routes = [docs_route]
406408
fastapi_mock = MagicMock()
407409
fastapi_mock.state = SimpleNamespace()
408410
self._call_gui_run(nicegui_mock, fastapi_app=fastapi_mock)
409-
docs_calls = [c for c in app_mock.get.call_args_list if c.args[0] == "/docs"]
411+
docs_calls = [c for c in app_mock.get.call_args_list if c.args[0] == _DOCS_PATH]
410412
assert len(docs_calls) == 0
411413

412414
def test_auth_client_state_copied(self) -> None:
@@ -462,7 +464,7 @@ async def test_returns_none_for_expired_session(self) -> None:
462464
from aignostics_foundry_core.gui.auth import get_gui_user
463465

464466
request = MagicMock()
465-
expired_user = {"sub": "auth0|x", "exp": int(time.time()) - 3600}
467+
expired_user = {"sub": _USER_SUB, "exp": int(time.time()) - 3600}
466468
fake_client = MagicMock()
467469
fake_client.require_session = AsyncMock(return_value={"user": expired_user})
468470

@@ -480,7 +482,7 @@ async def test_returns_none_when_exp_claim_missing(self) -> None:
480482

481483
request = MagicMock()
482484
fake_client = MagicMock()
483-
fake_client.require_session = AsyncMock(return_value={"user": {"sub": "auth0|x"}})
485+
fake_client.require_session = AsyncMock(return_value={"user": {"sub": _USER_SUB}})
484486

485487
with (
486488
patch(_PATCH_GET_AUTH_CLIENT, return_value=fake_client),
@@ -495,7 +497,7 @@ async def test_returns_user_for_valid_session(self) -> None:
495497
from aignostics_foundry_core.gui.auth import get_gui_user
496498

497499
request = MagicMock()
498-
user = {"sub": "auth0|x", "email": "x@x.com", "exp": int(time.time()) + 3600}
500+
user = {"sub": _USER_SUB, "email": "x@x.com", "exp": int(time.time()) + 3600}
499501
fake_client = MagicMock()
500502
fake_client.require_session = AsyncMock(return_value={"user": user})
501503

@@ -557,7 +559,7 @@ async def test_returns_user_when_authenticated(self) -> None:
557559
from aignostics_foundry_core.gui.auth import require_gui_user
558560

559561
request = MagicMock()
560-
user = {"sub": "auth0|x", "exp": int(time.time()) + 3600}
562+
user = {"sub": _USER_SUB, "exp": int(time.time()) + 3600}
561563

562564
with patch(_PATCH_GET_GUI_USER, new=AsyncMock(return_value=user)):
563565
result = await require_gui_user(request)
@@ -625,7 +627,8 @@ def test_preserves_function_name(self) -> None:
625627
nicegui_mock, _ = _make_nicegui_mock()
626628

627629
def my_named_page(user: object) -> None:
628-
pass
630+
# Test double: content is irrelevant; only __name__ is tested
631+
...
629632

630633
with patch.dict(sys.modules, {"nicegui": nicegui_mock}):
631634
result = page_public(_TEST_PATH)(my_named_page)

0 commit comments

Comments
 (0)