"""The fixtures: an app that wants a password, and the same one that does not. The interesting question is asked twice. `locked` is an app that was told it binds a real address and therefore wants a password; `open_app` is the same code on loopback, which is what an app on a laptop is. The app itself is `harness.py`, built here rather than imported, because this folder is not allowed to reach for `app/`. """ from __future__ import annotations from collections.abc import Callable, Iterator from pathlib import Path import pytest from fastapi.testclient import TestClient from auth import AuthStore from auth.tests import PASSWORD, sign_in from auth.tests.harness import build @pytest.fixture(autouse=True) def no_pinned_password(monkeypatch: pytest.MonkeyPatch) -> None: """The variable pins the password while it is set, which would quietly change what half of these tests are testing. A test that wants it sets it.""" monkeypatch.delenv("APP_PASSWORD", raising=False) @pytest.fixture def make_client(tmp_path: Path) -> Iterator[Callable[..., TestClient]]: """`make_client(enabled="always")`: an app, its store seeded, a client on it. A factory rather than a fixture per shape, because half the tests here are about one setting being different, and a fixture per setting is a file of fixtures nobody can hold in their head. """ clients: list[TestClient] = [] def make(*, host: str = "0.0.0.0", password: str | None = PASSWORD, base_url: str = "http://testserver", **settings: object) -> TestClient: db = tmp_path / f"auth{len(clients)}.db" if password is not None: AuthStore(db).set_password(password) client = TestClient(build(db, host=host, **settings), follow_redirects=False, base_url=base_url) clients.append(client) # Entered, so the lifespan runs and the bootstrap with it. Closed at # the end of the test, which is what releases the SQLite file on # Windows before pytest tries to delete the temp directory. return client.__enter__() yield make for client in reversed(clients): client.__exit__(None, None, None) @pytest.fixture def store(tmp_path: Path) -> AuthStore: return AuthStore(tmp_path / "store.db") @pytest.fixture def locked(make_client: Callable[..., TestClient]) -> TestClient: """An app that wants a password, with one already set.""" return make_client(enabled="always") @pytest.fixture def open_app(make_client: Callable[..., TestClient]) -> TestClient: """The same app on loopback under `auto` — no password asked for.""" return make_client(enabled="auto", host="127.0.0.1", password=None) @pytest.fixture def signed_in(locked: TestClient) -> TestClient: """The locked app, already through the door.""" assert sign_in(locked).status_code == 200 return locked