"""Admin read routes must not serve data to anonymous callers. The bug this guards, found live on 2026-09-09: `current_org_read` fell back to the default organization when no credentials were presented, documented as acceptable because these were "non-sensitive analytics reads". They were not — an unauthenticated GET to the production Space returned: /api/admin/students -> student names and email addresses /api/admin/curators -> staff names and calendar links /api/admin/overview -> organization id, name, student counts The fallback existed because the dashboard's Server Components fetched without a token. That constraint is gone: every screen is now a client component specifically so the admin's Firebase token is attached. These tests assert the property directly — no credentials, no data — rather than checking the implementation, so they survive a refactor of how auth is resolved. """ import pytest from fastapi.testclient import TestClient import admin_routes # Every admin GET that reads tenant data. /organizations is included even # though it already required a platform admin: it belongs to the same surface, # and a future edit could weaken it the same way. READ_ROUTES = [ "/api/admin/overview", "/api/admin/opportunities", "/api/admin/students", "/api/admin/curators", "/api/admin/inquiries", "/api/admin/bookings", "/api/admin/office-hours", "/api/admin/organizations", ] @pytest.fixture() def client(): import app as app_module return TestClient(app_module.app) @pytest.mark.parametrize("route", READ_ROUTES) def test_anonymous_read_is_rejected(client, route): """No token must mean no data — not a fallback to the default org. 503 counts as refused: routes gated on a platform admin check reach the "auth unavailable" path when Firebase is unconfigured, as it is here. Production returns 401 for these — verified against the live Space. The forbidden outcome is 200, which is what the old fallback produced. """ response = client.get(route) assert response.status_code in (401, 403, 503), ( f"{route} returned {response.status_code} to an anonymous caller. " f"Admin reads expose student names and email addresses; an " f"unauthenticated caller must be refused, never served the default " f"organization's rows." ) assert response.status_code != 200, ( f"{route} SERVED DATA to an anonymous caller — this is the leak." ) @pytest.mark.parametrize("route", READ_ROUTES) def test_garbage_token_is_rejected(client, route): """A malformed bearer token must fail closed, not fall through to the anonymous path and get served anyway. Accepts 503 alongside 401/403: with no Firebase credentials configured (the case in this test environment) token verification cannot run and the route refuses with "auth unavailable". That is still failing closed, which is the property under test. Production, where Firebase IS configured, returns 401 here — verified against the live Space. What must never appear is 200: that would mean an unverifiable token was treated as good enough to serve tenant data. """ response = client.get(route, headers={"Authorization": "Bearer not-a-real-token"}) assert response.status_code in (401, 403, 503), ( f"{route} accepted an invalid bearer token ({response.status_code})" ) assert response.status_code != 200 def test_read_resolver_has_no_anonymous_fallback(): """The specific regression: current_org_read must refuse anonymous callers, exactly as current_org does. Asserted at the resolver rather than only through routes so the reason a route is protected stays legible — and so adding a new admin GET that uses this dependency inherits the guarantee. """ from fastapi import HTTPException with pytest.raises(HTTPException) as exc: admin_routes.current_org_read(cred=None, x_admin_token=None) assert exc.value.status_code in (401, 403), ( "current_org_read returned an organization to a caller with no " "credentials — this is the exact fallback that leaked student data." )