Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

33.0 KB
Raw Blame
"""Regression coverage for credential-lifetime guards in the FastAPI stack.

Three guards decide, on live request paths, *whose* database password a
request is allowed to reach. Each test group fails if its named guard is
deleted:

``web/auth/password_utils.get_user_password`` — cross-session identity guard
    Live on all three research entry points (``routers/research.py``,
    ``routers/followup.py``, ``routers/chat.py``). It refuses to resolve a
    password when the request context belongs to a *different* user than the
    one being asked about, rather than widening the lookup to whatever
    session happens to be live. The only test file that mentioned the module
    (``tests/chat/test_chat_route_helpers.py``) patches it out entirely and
    its docstring credits a deleted file for the coverage, so the module had
    zero real coverage.

``web/dependencies/auth.ensure_user_database`` — temp-auth cross-user binding
    ``stored_username == username`` is the only thing binding the one-time
    post-login bootstrap credential to the identity claimed in the cookie.
    It runs on every authenticated request through ``DatabaseMiddleware``.
    ``tests/web/test_ensure_user_database_token_ordering.py`` covers the
    *ordering* of the token block against the ``is_user_connected`` fast
    path with a single test; it says nothing about the binding.

``web/fastapi_app._enforce_session_revocation`` — revocation for the two
routes that bypass ``require_auth``
    ``GET /`` and ``GET /auth/check`` read ``request.session["username"]``
    directly, so ``require_auth``'s server-side session check (covered by
    ``tests/web/dependencies/test_session_revocation.py``, not duplicated
    here) never runs for them. This middleware hook is the only thing that
    stops a replayed post-logout cookie from rendering the authenticated
    index and answering ``authenticated: true``.

Every guard is tested as a matched pair: a negative assertion (the wrong
credential is refused) plus a positive control on the *same* state proving
the right credential is still returned. Both halves matter — each of these
helpers returns ``None``/no-op on any failure, so a lone "must not equal the
other user's password" assertion passes just as happily when the whole path
is broken.
"""

from __future__ import annotations

import types
import uuid

import pytest

from local_deep_research.database import encrypted_db
from local_deep_research.database.session_passwords import (
    session_password_store,
)
from local_deep_research.database.temp_auth import temp_auth_store
from local_deep_research.utilities.request_context import request_user
from local_deep_research.web.auth import password_utils
from local_deep_research.web.dependencies import auth as auth_dep

# Opt out of tests/conftest.py's autouse ``_legacy_bare_username_auth`` shim,
# which patches ``_server_session_valid`` to accept unconditionally so legacy
# bare-username route tests keep working. The HTTP tests below exist to prove
# a revoked session IS rejected, so the shim must never be able to relax what
# is under test. (It patches ``require_auth``'s helper, which ``/`` and
# ``/auth/check`` also reach by calling ``require_auth`` in their bodies, so
# the gate must be real here.)
pytestmark = pytest.mark.real_session_check


ALICE_PW = "alice-Correct-Horse-1!"  # noqa: S105
BOB_PW = "bob-Battery-Staple-2!"  # noqa: S105


@pytest.fixture
def store_cleanup():
    """Track (username, session_id) keys written into the module-level
    ``session_password_store`` singleton and drop them afterwards.

    The store is a process-wide singleton that ``reset_all_singletons`` does
    not touch, so a leaked entry would be visible to every later test in the
    same worker.
    """
    keys: list[tuple[str, str]] = []

    def _seed(username: str, session_id: str, password: str) -> None:
        session_password_store.store_session_password(
            username, session_id, password
        )
        keys.append((username, session_id))

    _seed.track = lambda u, s: keys.append((u, s))  # type: ignore[attr-defined]
    yield _seed
    for username, session_id in keys:
        session_password_store.clear_session(username, session_id)


# ---------------------------------------------------------------------------
# INVARIANT 1 — web/auth/password_utils
# ---------------------------------------------------------------------------


class TestGetUserPasswordCrossSessionGuard:
    """``get_user_password``'s "the context belongs to someone else" guard.

    Both tests in the first pair run against the *identical* store state —
    one entry keyed ``(bob, SHARED_SID)``. The only difference is whose
    request context is on the stack. That is what makes the negative half
    non-vacuous: the entry demonstrably resolves (positive control), so a
    ``None`` in the cross-user case can only come from the guard, never from
    "the store had nothing to give".
    """

    def test_matching_user_gets_their_password(self, store_cleanup):
        """Positive control for the whole helper: in the user's own request
        context the password is resolved from the contextvar's session_id."""
        sid = f"sid_{uuid.uuid4().hex[:12]}"
        store_cleanup("alice", sid, ALICE_PW)

        with request_user("alice", sid):
            assert password_utils.get_user_password("alice") == ALICE_PW

    def test_cross_user_request_context_refuses_to_resolve(self, store_cleanup):
        """A service call asking for BOB's password from inside ALICE's
        request must get ``None``, not a lookup under Alice's session_id.

        The store is seeded with ``(bob, SHARED_SID)`` so the un-guarded code
        path — ``get_session_password(bob, <alice's session_id>)`` — has a
        real value to return. Delete the ``ctx_username != username`` guard
        and this test hands back Bob's password.
        """
        shared_sid = f"sid_{uuid.uuid4().hex[:12]}"
        store_cleanup("bob", shared_sid, BOB_PW)

        # Positive control on the SAME store state: in Bob's own context the
        # entry resolves. Anything else would mean the assertion below is
        # passing because the lookup is broken, not because of the guard.
        with request_user("bob", shared_sid):
            assert password_utils.get_user_password("bob") == BOB_PW

        with request_user("alice", shared_sid):
            leaked = password_utils.get_user_password("bob")
        assert leaked is None, (
            "get_user_password() resolved another user's password using the "
            "CURRENT request's session_id — the cross-session guard "
            "(ctx_username != username -> None) is gone"
        )

    def test_no_session_id_in_context_returns_none(self, store_cleanup):
        """No session_id on the contextvar (outside any request) must return
        None rather than widening to "any session for this user"."""
        sid = f"sid_{uuid.uuid4().hex[:12]}"
        store_cleanup("alice", sid, ALICE_PW)

        # Positive control: same user, same store entry, session_id present.
        with request_user("alice", sid):
            assert password_utils.get_user_password("alice") == ALICE_PW

        # Username set but no session_id — e.g. a background context pushed
        # with only a username.
        with request_user("alice", None):
            assert password_utils.get_user_password("alice") is None

        # No context at all.
        assert password_utils.get_user_password("alice") is None

    def test_context_without_username_still_resolves_by_session_id(
        self, store_cleanup
    ):
        """The guard is deliberately ``ctx_username is not None and ...``.

        A context carrying a session_id but no username (a worker that only
        knows which session it is servicing) is NOT a cross-user call, so it
        must still resolve. Pins the ``is not None`` half of the condition,
        which a naive tightening to ``ctx_username != username`` would break.
        """
        sid = f"sid_{uuid.uuid4().hex[:12]}"
        store_cleanup("alice", sid, ALICE_PW)

        with request_user(None, sid):
            assert password_utils.get_user_password("alice") == ALICE_PW

    def test_password_belongs_to_the_session_not_the_username(
        self, store_cleanup
    ):
        """Same user, different session: the lookup is keyed by the CURRENT
        session_id, so a stale session's password is not handed out."""
        live_sid = f"sid_{uuid.uuid4().hex[:12]}"
        stale_sid = f"sid_{uuid.uuid4().hex[:12]}"
        store_cleanup("alice", stale_sid, "alice-OLD-password-3!")

        # The stale entry resolves in its own session (positive control)...
        with request_user("alice", stale_sid):
            assert (
                password_utils.get_user_password("alice")
                == "alice-OLD-password-3!"
            )

        # ...but must not be reachable from a different, live session.
        with request_user("alice", live_sid):
            assert password_utils.get_user_password("alice") is None


class TestResolveUserPasswordContract:
    """``resolve_user_password`` returns ``(password, session_expired)``.

    ``session_expired`` is the flag the three research entry points use to
    decide between "run without metrics" and "401, log back in". Getting it
    wrong in the encrypted case means a research run that appears to start
    while every background DB/metric write is silently dropped (#4457);
    getting it wrong in the unencrypted case means refusing to run at all.
    """

    @pytest.fixture
    def encrypted(self, monkeypatch):
        monkeypatch.setattr(encrypted_db.db_manager, "has_encryption", True)

    @pytest.fixture
    def unencrypted(self, monkeypatch):
        monkeypatch.setattr(encrypted_db.db_manager, "has_encryption", False)

    def test_encrypted_db_with_password_is_not_expired(
        self, encrypted, store_cleanup
    ):
        sid = f"sid_{uuid.uuid4().hex[:12]}"
        store_cleanup("alice", sid, ALICE_PW)

        with request_user("alice", sid):
            password, session_expired = password_utils.resolve_user_password(
                "alice"
            )

        assert password == ALICE_PW
        assert session_expired is False

    def test_encrypted_db_without_password_is_session_expired(self, encrypted):
        """The one case that must reject the request: encryption on, no
        password recoverable (store TTL expiry or a server restart)."""
        sid = f"sid_{uuid.uuid4().hex[:12]}"

        with request_user("alice", sid):
            password, session_expired = password_utils.resolve_user_password(
                "alice"
            )

        assert password is None
        assert session_expired is True, (
            "an encrypted database with no available password must report "
            "session_expired=True so the caller 401s — returning False here "
            "starts a research run whose every metric write is dropped "
            "(issue #4457)"
        )

    def test_unencrypted_db_without_password_is_never_session_expired(
        self, unencrypted
    ):
        """For unencrypted databases a missing password is legitimate:
        ``session_expired`` must stay False so the run proceeds."""
        sid = f"sid_{uuid.uuid4().hex[:12]}"

        with request_user("alice", sid):
            password, session_expired = password_utils.resolve_user_password(
                "alice"
            )

        assert password is None
        assert session_expired is False, (
            "session_expired must be False for unencrypted databases — a "
            "True here 401s every research start on an unencrypted install"
        )

    def test_unencrypted_db_with_password_is_not_expired(
        self, unencrypted, store_cleanup
    ):
        sid = f"sid_{uuid.uuid4().hex[:12]}"
        store_cleanup("alice", sid, ALICE_PW)

        with request_user("alice", sid):
            password, session_expired = password_utils.resolve_user_password(
                "alice"
            )

        assert password == ALICE_PW
        assert session_expired is False

    def test_cross_user_call_inherits_the_leak_guard(
        self, encrypted, store_cleanup
    ):
        """``resolve_user_password`` is built on ``get_user_password``, so the
        cross-session guard must survive the composition: asking for Bob's
        password from Alice's request yields ``(None, True)`` — refused, not
        leaked — even though Bob's entry is resolvable under that session_id.
        """
        shared_sid = f"sid_{uuid.uuid4().hex[:12]}"
        store_cleanup("bob", shared_sid, BOB_PW)

        # Positive control: Bob's own context resolves the same entry.
        with request_user("bob", shared_sid):
            assert password_utils.resolve_user_password("bob") == (
                BOB_PW,
                False,
            )

        with request_user("alice", shared_sid):
            password, session_expired = password_utils.resolve_user_password(
                "bob"
            )

        assert password is None, (
            "resolve_user_password() leaked another user's password across "
            "the request-context boundary"
        )
        assert session_expired is True


# ---------------------------------------------------------------------------
# INVARIANT 2 — ensure_user_database temp-auth cross-user binding
# ---------------------------------------------------------------------------


def _fake_request(session: dict):
    """Minimal request double exposing only ``.session``.

    ``ensure_user_database`` only ever ``.get``/``.pop``s on it, mirroring
    ``DatabaseMiddleware``'s own inline ``_MinimalRequest`` shim, which is
    how production calls this function.
    """
    return types.SimpleNamespace(session=session)


class _SpyDBManager:
    """Records ``open_user_database`` calls; opens nothing.

    Returning None matches what the real manager effectively does when the
    supplied key cannot decrypt the target database, so the spy cannot
    accidentally make a cross-user open look successful.
    """

    def __init__(self, connected=(), has_encryption=True):
        self._connected = set(connected)
        self.has_encryption = has_encryption
        self.open_calls: list[tuple[str, str]] = []

    def is_user_connected(self, username):
        return username in self._connected

    def open_user_database(self, username, password):
        self.open_calls.append((username, password))
        # Implicitly None: matches what the real manager effectively yields
        # when the supplied key cannot decrypt the target database.


class TestEnsureUserDatabaseTempAuthBinding:
    """``stored_username == username`` binds the bootstrap credential to the
    identity claimed in the cookie.

    The token is a one-time, 10s-TTL credential minted at login/registration
    and carried in the signed session cookie. If the comparison is dropped,
    any holder of a token can present it alongside a cookie claiming a
    *different* username and have that user's password written into the
    session password store under the victim's key — promoting a 10s
    bootstrap credential into a 24h cross-user one.
    """

    def test_token_for_another_user_is_not_bound_to_the_claimed_identity(
        self, monkeypatch, store_cleanup
    ):
        """A temp-auth token issued for ALICE, presented in a session
        claiming BOB, must not be consumed on Bob's behalf."""
        alice = f"alice_{uuid.uuid4().hex[:10]}"
        bob = f"bob_{uuid.uuid4().hex[:10]}"
        bob_sid = f"sid_{uuid.uuid4().hex[:12]}"
        store_cleanup.track(bob, bob_sid)  # type: ignore[attr-defined]

        spy = _SpyDBManager(connected=(), has_encryption=True)
        monkeypatch.setattr(auth_dep, "db_manager", spy)

        token = temp_auth_store.store_auth(alice, ALICE_PW)
        try:
            session = {
                "username": bob,
                "session_id": bob_sid,
                "temp_auth_token": token,
            }
            auth_dep.ensure_user_database(_fake_request(session))

            assert (
                session_password_store.get_session_password(bob, bob_sid)
                is None
            ), (
                "Alice's password was promoted into Bob's session password "
                "store entry — the temp-auth token is no longer bound to the "
                "username claimed in the cookie, so a 10s bootstrap "
                "credential just became a 24h cross-user one"
            )
            assert session.get("temp_auth_token") == token, (
                "the mismatched token must not be popped from the session: "
                "popping it means the consumption block ran for a user the "
                "token was never issued to"
            )

            # Current fallthrough behavior, pinned precisely: ``password`` is
            # unpacked before the identity comparison. On a mismatch it remains
            # the argument for the Source-3 ``open_user_database`` attempt. The
            # real manager rejects a key that cannot decrypt Bob's database,
            # and the assertions above prove it is not retained. If unpacking
            # moves inside the matching branch, update this expectation while
            # retaining the identity and storage assertions above.
            assert spy.open_calls == [(bob, ALICE_PW)], (
                "expected the mismatched token password to reach the current "
                f"Source-3 open attempt; got "
                f"{spy.open_calls!r}"
            )
        finally:
            temp_auth_store.retrieve_auth(token)

    def test_token_for_the_claimed_user_is_consumed_and_promoted(
        self, monkeypatch, store_cleanup
    ):
        """Positive control for the identical flow.

        Without this, the assertions above would pass even if the whole
        token block were deleted.
        """
        alice = f"alice_{uuid.uuid4().hex[:10]}"
        alice_sid = f"sid_{uuid.uuid4().hex[:12]}"
        store_cleanup.track(alice, alice_sid)  # type: ignore[attr-defined]

        spy = _SpyDBManager(connected=(), has_encryption=True)
        monkeypatch.setattr(auth_dep, "db_manager", spy)

        token = temp_auth_store.store_auth(alice, ALICE_PW)
        try:
            session = {
                "username": alice,
                "session_id": alice_sid,
                "temp_auth_token": token,
            }
            auth_dep.ensure_user_database(_fake_request(session))

            assert (
                session_password_store.get_session_password(alice, alice_sid)
                == ALICE_PW
            ), "a matching token must promote the password for its own user"
            assert "temp_auth_token" not in session, (
                "a matching token must be popped from the session (single use)"
            )
            assert temp_auth_store.peek_auth(token) is None, (
                "a matching token must be consumed from the store"
            )
            assert spy.open_calls == [(alice, ALICE_PW)]
        finally:
            temp_auth_store.retrieve_auth(token)

    def test_mismatched_token_does_not_open_the_victims_real_database(
        self, monkeypatch, tmp_path, store_cleanup
    ):
        """End-to-end against the REAL db_manager and real encrypted files.

        The spy-based test above pins the binding at the store/session level;
        this one pins the property that actually matters — Bob's decrypted
        database is never opened by a credential minted for Alice — using
        real SQLCipher key derivation, with a positive control proving Bob's
        OWN token does open it.
        """
        if not encrypted_db.db_manager.has_encryption:
            pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

        monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
        # sqlcipher_utils declares MIN_KDF_ITERATIONS_TESTING=1 precisely so
        # tests that create real encrypted databases don't spend seconds in
        # PBKDF2; the production default would dominate this test's runtime.
        monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")

        db_manager = encrypted_db.db_manager
        alice = f"alice_{uuid.uuid4().hex[:10]}"
        bob = f"bob_{uuid.uuid4().hex[:10]}"
        bob_sid = f"sid_{uuid.uuid4().hex[:12]}"
        store_cleanup.track(bob, bob_sid)  # type: ignore[attr-defined]

        original_data_dir = db_manager.data_dir
        token = None
        try:
            db_manager.data_dir = tmp_path / "encrypted_databases"
            db_manager.create_user_database(alice, ALICE_PW)
            db_manager.create_user_database(bob, BOB_PW)
            # Cold start: neither connection is cached, so the
            # is_user_connected() fast path cannot mask the outcome.
            db_manager.close_all_databases()
            assert db_manager.is_user_connected(bob) is False

            token = temp_auth_store.store_auth(alice, ALICE_PW)
            session = {
                "username": bob,
                "session_id": bob_sid,
                "temp_auth_token": token,
            }
            auth_dep.ensure_user_database(_fake_request(session))

            assert db_manager.is_user_connected(bob) is False, (
                "a temp-auth token issued for a DIFFERENT user opened Bob's "
                "encrypted database"
            )
            assert (
                session_password_store.get_session_password(bob, bob_sid)
                is None
            )

            # Positive control: Bob's own token opens Bob's database through
            # the very same code path, so the assertion above is a real
            # refusal and not a broken path failing for everyone.
            temp_auth_store.retrieve_auth(token)
            token = temp_auth_store.store_auth(bob, BOB_PW)
            session = {
                "username": bob,
                "session_id": bob_sid,
                "temp_auth_token": token,
            }
            auth_dep.ensure_user_database(_fake_request(session))

            assert db_manager.is_user_connected(bob) is True, (
                "Bob's own temp-auth token must open Bob's database"
            )
            assert (
                session_password_store.get_session_password(bob, bob_sid)
                == BOB_PW
            )
        finally:
            if token is not None:
                temp_auth_store.retrieve_auth(token)
            db_manager.close_all_databases()
            db_manager.data_dir = original_data_dir


# ---------------------------------------------------------------------------
# INVARIANT 3 — _enforce_session_revocation on require_auth-free routes
# ---------------------------------------------------------------------------


class _FakeSessionManager:
    """Server-side session store: session_id -> username, or nothing."""

    def __init__(self, sessions=None):
        self._sessions = dict(sessions or {})

    def validate_session(self, session_id):
        return self._sessions.get(session_id)


class TestEnforceSessionRevocationUnit:
    """Direct tests for the middleware hook.

    ``_enforce_session_revocation`` imports ``session_manager`` inside the
    function body, so the patch target is the source module.
    """

    @staticmethod
    def _patch_sessions(monkeypatch, sessions):
        from local_deep_research.web.auth import session_manager as sm_module

        monkeypatch.setattr(
            sm_module, "session_manager", _FakeSessionManager(sessions)
        )

    def test_live_session_is_left_alone(self, monkeypatch):
        """Positive control — without it, every assertion below passes for a
        hook that unconditionally clears."""
        from local_deep_research.web import fastapi_app

        self._patch_sessions(monkeypatch, {"s_live": "alice"})
        session = {"username": "alice", "session_id": "s_live", "k": "v"}

        fastapi_app._enforce_session_revocation(session)

        assert session == {
            "username": "alice",
            "session_id": "s_live",
            "k": "v",
        }

    def test_destroyed_session_is_cleared(self, monkeypatch):
        from local_deep_research.web import fastapi_app

        # The store knows only about a LATER session — the state after
        # logout followed by a fresh login from any device.
        self._patch_sessions(monkeypatch, {"s_new": "alice"})
        session = {"username": "alice", "session_id": "s_destroyed"}

        fastapi_app._enforce_session_revocation(session)

        assert session == {}

    def test_session_id_for_another_user_is_cleared(self, monkeypatch):
        from local_deep_research.web import fastapi_app

        self._patch_sessions(monkeypatch, {"s1": "bob"})
        session = {"username": "alice", "session_id": "s1"}

        fastapi_app._enforce_session_revocation(session)

        assert session == {}

    def test_missing_session_id_is_cleared(self, monkeypatch):
        from local_deep_research.web import fastapi_app

        self._patch_sessions(monkeypatch, {"s1": "alice"})
        session = {"username": "alice"}

        fastapi_app._enforce_session_revocation(session)

        assert session == {}

    def test_anonymous_session_is_untouched(self, monkeypatch):
        """Anonymous visitors must be left alone — ``/`` and ``/auth/check``
        both have to keep answering for them."""
        from local_deep_research.web import fastapi_app

        self._patch_sessions(monkeypatch, {})
        session = {"flash_messages": ["hi"]}

        fastapi_app._enforce_session_revocation(session)

        assert session == {"flash_messages": ["hi"]}


@pytest.fixture
def live_app(tmp_path, monkeypatch):
    """The real assembled app on a temp data dir.

    Mirrors ``tests/security/test_login_cached_connection_password_lockout.py``:
    the routes read module-level singletons (``db_manager``,
    ``session_manager``), so the app must run against those exact instances
    and the data dir has to be repointed on the singleton itself.
    """
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_TESTING_WITH_MOCKS", "true")
    # Not the adaptive search-engine knob (LDR_RATE_LIMITING_ENABLED) — this
    # is the HTTP limiter guarding /auth/login, which the register + login +
    # re-login sequence below would otherwise trip.
    monkeypatch.setenv("LDR_DISABLE_RATE_LIMITING", "true")
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")

    from local_deep_research.database.auth_db import init_auth_database
    from local_deep_research.database.encrypted_db import db_manager
    from local_deep_research.web.fastapi_app import app as fastapi_app
    import local_deep_research.web.routers.auth as auth_routes

    original_data_dir = db_manager.data_dir
    try:
        db_manager.data_dir = tmp_path / "encrypted_databases"
        init_auth_database()
        # Keep the synchronous test off the real post-login worker threads.
        monkeypatch.setattr(
            auth_routes,
            "_perform_post_login_tasks",
            lambda _u, _p, _sid=None: None,
        )
        yield fastapi_app, db_manager
    finally:
        db_manager.close_all_databases()
        db_manager.data_dir = original_data_dir


def _client(app):
    """A TestClient with its own peer address.

    Rate limiting is keyed per client IP, so every client in a test gets a
    distinct X-Forwarded-For and cannot be bucketed with its siblings.
    """
    from fastapi.testclient import TestClient

    client = TestClient(app, raise_server_exceptions=False)
    octets = uuid.uuid4().int
    client.headers.update(
        {
            "X-Forwarded-For": f"10.{octets % 254 + 1}.{octets // 254 % 254 + 1}.9"
        }
    )
    return client


def _csrf(client):
    client.get("/auth/login")
    resp = client.get("/auth/csrf-token")
    return resp.json().get("csrf_token", "") if resp.status_code == 200 else ""


def _register(client, username, password):
    token = _csrf(client)
    return client.post(
        "/auth/register",
        data={
            "username": username,
            "password": password,
            "confirm_password": password,
            "acknowledge": "true",
            "csrf_token": token,
        },
        headers={"X-CSRFToken": token},
        follow_redirects=False,
    )


def _login(client, username, password):
    token = _csrf(client)
    return client.post(
        "/auth/login",
        data={
            "username": username,
            "password": password,
            "csrf_token": token,
        },
        headers={"X-CSRFToken": token},
        follow_redirects=False,
    )


def _logout(client):
    token = _csrf(client)
    return client.post(
        "/auth/logout",
        data={"csrf_token": token},
        headers={"X-CSRFToken": token},
        follow_redirects=False,
    )


class TestRevokedCookieOnRequireAuthFreeRoutes:
    """``GET /`` and ``GET /auth/check`` are the only two GET routes that
    grant a revoked session strictly more than an anonymous one.

    The regression state matters after logout followed by a later login, which
    reopens the database and repopulates the password store. At that point
    username-scoped checks such as ``is_user_connected`` and
    ``get_any_session_password`` are true again, so
    ``_enforce_session_revocation`` must still reject the older cookie.
    """

    @pytest.fixture
    def revoked_cookie(self, live_app):
        """Log in, capture the raw signed cookie, log out, log in again.

        Returns ``(app, username, revoked_cookie_value)``. The final login
        is what restores the username-scoped state, so the replay below
        cannot pass for the wrong reason.
        """
        app, db_manager = live_app
        username = f"revoke_{uuid.uuid4().hex[:8]}"
        password = "R3voke-Me-Please!"  # noqa: S105

        victim = _client(app)
        reg = _register(victim, username, password)
        assert reg.status_code in (200, 302), (
            f"registration failed: {reg.status_code} / {reg.text[:400]}"
        )

        login = _login(victim, username, password)
        assert login.status_code == 302, (
            f"login failed: {login.status_code} / {login.text[:400]}"
        )

        captured = victim.cookies.get("session")
        assert captured, "no session cookie was issued"

        # Sanity + positive control: the cookie works while its session is
        # live. If this ever stopped being true the rejection assertions
        # below would be meaningless.
        assert victim.get("/auth/check").status_code == 200
        assert victim.get("/", follow_redirects=False).status_code == 200

        assert _logout(victim).status_code in (200, 302)

        # Log in again from a different client: reopens the database and
        # repopulates the password store, so every username-scoped check is
        # satisfied again for the replayed cookie.
        relogin_client = _client(app)
        relogin = _login(relogin_client, username, password)
        assert relogin.status_code == 302
        assert db_manager.is_user_connected(username), (
            "the re-login must leave the database open — otherwise the "
            "replay below would be refused by the staleness check rather "
            "than by session revocation, and this test would prove nothing"
        )
        assert relogin_client.get("/auth/check").status_code == 200, (
            "the NEW session must be fully authenticated"
        )

        return app, username, captured

    @staticmethod
    def _replay(app, cookie_value):
        client = _client(app)
        client.cookies.set("session", cookie_value)
        return client

    def test_auth_check_rejects_a_revoked_cookie(self, revoked_cookie):
        app, username, cookie = revoked_cookie
        replay = self._replay(app, cookie)

        resp = replay.get("/auth/check")

        assert resp.status_code == 401, (
            "GET /auth/check answered for a cookie whose server-side session "
            "was destroyed at logout — it bypasses require_auth, so "
            "_enforce_session_revocation is the only gate it has"
        )
        assert resp.json().get("authenticated") is False
        assert username not in resp.text

    def test_index_redirects_a_revoked_cookie_to_login(self, revoked_cookie):
        app, _username, cookie = revoked_cookie
        replay = self._replay(app, cookie)

        resp = replay.get("/", follow_redirects=False)

        assert resp.status_code == 302, (
            "GET / rendered for a cookie whose server-side session was "
            "destroyed at logout — and the page would come back with the "
            "user's real saved settings"
        )
        assert resp.headers.get("location", "").startswith("/auth/login")

    def test_revoked_cookie_is_cleared_so_the_client_stops_presenting_it(
        self, revoked_cookie
    ):
        """The hook clears the session dict in place, so the response also
        re-issues an empty cookie: a revoked session becomes indistinguishable
        from "never logged in" for everything downstream."""
        app, _username, cookie = revoked_cookie
        replay = self._replay(app, cookie)

        replay.get("/auth/check")
        # Second request on the SAME client, now carrying whatever the app
        # sent back — it must still be anonymous, never re-authenticated.
        assert replay.get("/auth/check").status_code == 401
        assert replay.get("/", follow_redirects=False).status_code == 302