Build a TestClient whose get_server dependency is stubbed out. Callers pass the desired CGC_API_KEY (or None to leave it unset) so each test controls whether auth is active.
(monkeypatch)
| 31 | |
| 32 | @pytest.fixture |
| 33 | def client_factory(monkeypatch): |
| 34 | """Build a TestClient whose get_server dependency is stubbed out. |
| 35 | |
| 36 | Callers pass the desired CGC_API_KEY (or None to leave it unset) so each |
| 37 | test controls whether auth is active. |
| 38 | """ |
| 39 | |
| 40 | def _make(api_key): |
| 41 | if api_key is None: |
| 42 | monkeypatch.delenv("CGC_API_KEY", raising=False) |
| 43 | else: |
| 44 | monkeypatch.setenv("CGC_API_KEY", api_key) |
| 45 | app = create_app() |
| 46 | app.dependency_overrides[get_server] = lambda: FakeServer() |
| 47 | return TestClient(app) |
| 48 | |
| 49 | return _make |
| 50 | |
| 51 | |
| 52 | # --- Key configured: auth is enforced --------------------------------------- |
no outgoing calls
no test coverage detected