test(partners): assert the admin gate by request, not by route internals (#983)

The admin-gate test from #952 read `route.include_context`. That attribute
only exists from FastAPI 0.141, where `include_router` stopped flattening the
sub-router into the parent app; before that the routes are copied onto
`app.routes` and there is no `include_context` at all. This project pins
`fastapi>=0.100.0` with no upper bound, so the test passed on CI (which
installs the latest) and raised `StopIteration` on any older satisfying
install — `5115 passed, 1 failed` locally on 0.135.1.

No structural assertion works across that range, so assert the actual
property instead: send a valid `role=user` token and require 403. A response
code does not care how the router is represented.

An unauthenticated request would not have been enough — `require_auth` alone
answers that with 401, so downgrading the router from `_admin` to `_auth`
would still have looked gated. With a real non-admin token, both weakenings
(`_admin` → `_auth`, and dropping the dependency entirely) let the request
reach the handler and fail the test. Also patches `AUTH_SECRET`, without
which `decode_token` rejects every token in a bare test environment and the
403 could never be distinguished from a 401.

Co-authored-by: Frank <bingxizhao@novix.science>
This commit is contained in:
Bingxi Zhao
2026-08-24 17:54:04 +08:00
committed by GitHub
co-authored by Frank
parent 50f8ccbaa4
commit 87ccedd549
+43 -13
View File
@@ -204,23 +204,53 @@ class TestChannelOnboarding:
)
assert cancelled.json()["status"] == "cancelled"
def test_onboarding_routes_inherit_the_full_partners_admin_gate(self):
from deeptutor.api.main import app
from deeptutor.api.routers.auth import require_admin
def test_onboarding_routes_inherit_the_full_partners_admin_gate(self, monkeypatch):
"""The real app must gate the onboarding routes on *admin*, not just auth.
The ``client`` fixture above mounts the partners router *without* the
admin dependency so the endpoint tests can drive it directly, so
nothing else in this file would notice if ``main.py`` stopped applying
the gate.
Asserted by sending a valid **non-admin** token and requiring 403,
rather than by inspecting ``app.routes``: FastAPI 0.141 stopped
flattening ``include_router`` into the parent app (it keeps the
sub-router nested behind ``include_context``), so every structural
assertion available here holds on only one side of this project's own
``fastapi>=0.100.0`` range. A response code does not care how the
router is represented. An unauthenticated request would not do:
``require_auth`` alone answers that with 401, so downgrading the
router from ``_admin`` to ``_auth`` would still look gated.
"""
from deeptutor.api import main as api_main
from deeptutor.api.routers import auth as auth_module
from deeptutor.api.routers import partners as partners_module
from deeptutor.services import auth as auth_service
included = next(
route
for route in app.routes
if getattr(getattr(route, "include_context", None), "prefix", None)
== "/api/v1/partners"
)
assert any(
route.path == "/{partner_id}/channel-onboarding/start"
for route in included.original_router.routes
for route in partners_module.router.routes
)
assert any(
getattr(dependency, "dependency", None) is require_admin
for dependency in included.include_context.dependencies
# ``require_admin`` waves everything through when auth is disabled,
# which is the default in tests — turn it on so the gate is observable.
monkeypatch.setattr(auth_module, "AUTH_ENABLED", True)
# ``decode_token`` refuses every token when no secret is configured,
# which is the state of a bare test environment — without this the
# request would 401 and the assertion below could not tell an
# admin-only route from a merely authenticated one.
monkeypatch.setattr(auth_service, "AUTH_SECRET", "test-secret")
monkeypatch.setattr(auth_service, "POCKETBASE_ENABLED", False)
token = auth_service.create_token("not-an-admin", role="user", user_id="u-1")
# No context manager: this must not run the app's lifespan.
client = TestClient(api_main.app)
response = client.post(
"/api/v1/partners/ada/channel-onboarding/start",
json={},
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 403, (
f"onboarding start is not admin-gated (a role=user token got {response.status_code})"
)
def test_duplicate_id_conflicts(self, client):