"""The app as it is with `webauthn` not installed. These live in their own module rather than in `test_passkeys.py` on purpose: that one is skipped wholesale when the extra is missing, which is exactly the situation being tested here. The behaviour without the package has to be covered by tests that always run, or it is covered by nothing. The extra is removed by putting `None` in `sys.modules`, which is what makes `import webauthn` raise `ImportError` -- so `available()` is exercised rather than stubbed, and every lazy import inside the ceremonies is too. uv run pytest auth/tests/test_without_passkeys.py -q """ from __future__ import annotations import logging import sys from collections.abc import Callable, Iterator import pytest from fastapi.testclient import TestClient from auth import AuthStore, passkeys from auth.tests import PASSWORD, sign_in from auth.tests.harness import build @pytest.fixture def no_extra(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: """`import webauthn` raises ImportError, as it does without the extra.""" monkeypatch.setitem(sys.modules, "webauthn", None) assert passkeys.available() is False yield def test_the_account_screen_says_what_to_install(no_extra, signed_in: TestClient): account = signed_in.get("/api/account").json() assert account["passkeys_available"] is False said = account["passkeys_blocked"] assert "uv sync --extra passkeys" in said # And the half that is easy to drop as verbose: run that command on its own # and uv uninstalls every other extra, which fails silently and later. assert "replaces the set" in said def test_the_account_screen_still_loads(no_extra, signed_in: TestClient): """The whole point: a missing optional dependency is a sentence on a screen, not a 500 that takes the account page with it.""" assert signed_in.get("/api/account").status_code == 200 def test_the_passkey_routes_answer_501_rather_than_crashing( no_extra, signed_in: TestClient): """501 is what the element watches for -- "this route exists and this build cannot perform it" -- and it takes the passkey section off the screen.""" for path in ("/api/account/passkeys/options", "/login/passkey/options"): answer = signed_in.post(path) assert answer.status_code == 501, path assert "passkeys" in answer.json()["detail"] assert "uv sync" in answer.json()["detail"] def test_the_login_page_offers_no_button_it_cannot_honour( no_extra, locked: TestClient): """`{passkeys}` is substituted false, so the button is never drawn -- rather than drawn and then failing inside the browser.""" page = locked.get("/login").text assert "const passkeysOffered = false;" in page def test_the_password_still_works_without_the_extra(no_extra, locked: TestClient): """The reason none of this is an emergency.""" assert sign_in(locked, PASSWORD).status_code == 200 assert locked.get("/api/thing").status_code == 200 def test_startup_says_so_when_registered_keys_cannot_be_used( no_extra, tmp_path, caplog): """`uv sync` without the extra silently removes the package. The next start has keys it cannot verify, and the log is where that should surface -- before anyone is standing at a login page wondering where the button went. """ db = tmp_path / "keys.db" store = AuthStore(db) store.set_password(PASSWORD) store.add_passkey(b"cred-id", b"public-key", 0, "usb", "YubiKey") with caplog.at_level(logging.WARNING), TestClient(build(db, enabled="always")): pass assert "uv sync --extra passkeys" in caplog.text assert "nothing is lost" in caplog.text def test_startup_is_quiet_when_there_are_no_passkeys( no_extra, make_client: Callable[..., TestClient], caplog): """An install that simply does not use passkeys has nothing to be told.""" with caplog.at_level(logging.WARNING): make_client(enabled="always") assert "--extra passkeys" not in caplog.text