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.

65.6 KB
Raw Blame
"""Auth-flow parity fences missing from the FastAPI migration test suite.

Covers (audit parity list):
- Session fixation: the session cookie value AND the server-side session id
  change across login; pre-auth session content (the attacker-visible CSRF
  token) does not survive into the authenticated session.
- Logout clears the session server-side: the session id is destroyed in
  session_manager, the session-password store entry is removed, and the
  cookie session no longer carries username/session_id.
  (GET /auth/logout being rejected with 405 is already covered by
  tests/web/routers/test_fastapi_migration.py::test_logout_requires_post
  and is intentionally NOT duplicated here.)
- Change-password destroys the user's OTHER sessions via
  session_manager.destroy_all_user_sessions and clears the session
  password store, locking concurrent sessions out.
- Temp-auth token single use at the HTTP layer: the token stored in the
  session at login is consumed by the first request that needs it and is
  removed from the cookie session, so it cannot be replayed.
- Change-password with a whitespace-only current password is a form
  validation error, not a 500: the field check reads the stripped value, so
  the request never reaches the rekey path's own key validation.
  (Store-level single-use semantics are already covered by
  tests/database/test_temp_auth.py::test_retrieve_auth_removes_entry.)

The Starlette session cookie is signed-but-not-encrypted
(``b64(json).timestamp.sig``), so tests decode its first segment to
observe the real server-issued session_id without any mocking.
"""

import base64
import contextlib
import errno
import json
import os
import threading
import uuid
from unittest.mock import patch

import pytest
from fastapi.testclient import TestClient


# ----------------------------------------------------------------------------
# Fixtures / helpers (idioms match test_state_changing_flows.py)
# ----------------------------------------------------------------------------


@pytest.fixture(scope="module")
def app():
    from local_deep_research.web.fastapi_app import app

    return app


@pytest.fixture(scope="module", autouse=True)
def _stub_post_login_worker_target(app):
    """Replace the database-heavy post-login worker target with a no-op.

    Login still dispatches its daemon thread; dedicated tests cover that
    boundary and the real worker wrapper. Root conftest enforces the same
    session-long default; this local patch keeps the auth-focused module's
    isolation requirement explicit and nests safely with that guard.
    """
    mp = pytest.MonkeyPatch()
    mp.setattr(
        "local_deep_research.web.routers.auth._perform_post_login_tasks",
        lambda *_a, **_k: None,
    )
    yield
    mp.undo()


def _unique_ip() -> str:
    """Unique X-Forwarded-For so this module never shares slowapi
    rate-limit buckets (login is 5/15min/IP; registration is also limited per IP)."""
    return f"10.{uuid.uuid4().int % 254 + 1}.{uuid.uuid4().int % 254 + 1}.2"


def _new_client(app) -> TestClient:
    c = TestClient(app, raise_server_exceptions=False)
    c.headers.update({"X-Forwarded-For": _unique_ip()})
    return c


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


def _login(client: TestClient, user: str, pw: str):
    return client.post(
        "/auth/login",
        data={"username": user, "password": pw, "csrf_token": _csrf(client)},
        follow_redirects=False,
    )


def _session_dict(client: TestClient) -> dict:
    """Decode the (signed, unencrypted) Starlette session cookie payload."""
    raw = client.cookies.get("session")
    if not raw:
        return {}
    b64_part = raw.split(".")[0]
    padded = b64_part + "=" * (-len(b64_part) % 4)
    return json.loads(base64.b64decode(padded))


@contextlib.contextmanager
def _sidecar_flock_held_elsewhere(db_path):
    """Hold the user's rekey sidecar flock from a second descriptor.

    flock makes a second open file description contend with the server's
    own descriptor exactly as another process's would, and it bypasses the
    server's in-process gate, like a foreign holder (another worker's
    password change of a large database, a stopped process).
    """
    import fcntl

    fd = os.open(
        db_path.with_name(f"{db_path.name}.rekey.lock"),
        os.O_RDWR | os.O_CREAT,
        0o600,
    )
    try:
        fcntl.flock(fd, fcntl.LOCK_EX)
        yield
    finally:
        os.close(fd)


def _spy_sidecar_waits(monkeypatch, encrypted_db, db_path) -> list:
    """Record the wait each request-path take of ``db_path``'s sidecar gets.

    The cache claim, the cold open and the registration epoch take the
    sidecar through ``encrypted_db.acquire_rekey_lock(..., timeout=...)``;
    each entry is the ``timeout`` one of them was granted. A held sidecar
    makes every such wait last its full timeout, so the sum bounds how long
    the request waited for the holder without timing anything. Takes
    without a timeout (disposals, password changes) and the log drain's own
    probes are not request waits and are left out.
    """
    from pathlib import Path

    real_acquire = encrypted_db.acquire_rekey_lock
    granted: list = []

    def acquire(path, **kwargs):
        if (
            Path(path) == db_path
            and kwargs.get("timeout") is not None
            and threading.current_thread().name != "log-queue-processor"
        ):
            granted.append(kwargs["timeout"])
        return real_acquire(path, **kwargs)

    monkeypatch.setattr(encrypted_db, "acquire_rekey_lock", acquire)
    return granted


def _run_bounded(call, timeout: float):
    """Run ``call`` on a daemon thread and wait at most ``timeout`` seconds.

    A request that hangs then fails the test instead of the CI job.
    """
    outcome: dict = {}

    def target():
        try:
            outcome["value"] = call()
        except BaseException as error:  # noqa: BLE001 - handed to the test
            outcome["error"] = error

    worker = threading.Thread(target=target, daemon=True)
    worker.start()
    worker.join(timeout)
    return worker, outcome


@pytest.fixture(scope="module")
def registered_user(app):
    """Register one real user for the whole module; yield (username, pw)."""
    c = _new_client(app)
    user = f"authgap_{uuid.uuid4().hex[:8]}"
    pw = "TestPassword123!"  # noqa: S105

    resp = c.post(
        "/auth/register",
        data={
            "username": user,
            "password": pw,
            "confirm_password": pw,
            "acknowledge": "true",
            "csrf_token": _csrf(c),
        },
        follow_redirects=False,
    )
    if resp.status_code != 302:
        pytest.fail(
            f"Auth bootstrap broken: registration returned "
            f"{resp.status_code} (expected 302): "
            f"{resp.text[:300]}"
        )

    # Log the fixture client out so tests start unauthenticated.
    token = c.get("/auth/csrf-token").json().get("csrf_token", "")
    c.post(
        "/auth/logout",
        headers={"X-CSRFToken": token},
        follow_redirects=False,
    )
    return user, pw


# ----------------------------------------------------------------------------
# Session fixation
# ----------------------------------------------------------------------------


class TestSessionFixation:
    def test_login_rotates_cookie_and_server_session_id(
        self, app, registered_user
    ):
        """Login must not carry the pre-auth session forward.

        Regression caught: removing ``request.session.clear()`` in the
        login handler (pre-auth _csrf_token would survive), or reusing
        an existing session_id instead of minting a fresh one.
        """
        user, pw = registered_user
        client = _new_client(app)

        # Pre-auth session: visiting login + csrf-token plants a session
        # cookie whose content an attacker could know (fixation setup).
        pre_token = _csrf(client)
        assert pre_token, "CSRF endpoint should mint a pre-auth token"
        pre_cookie = client.cookies.get("session")
        pre_session = _session_dict(client)
        assert "session_id" not in pre_session
        assert pre_session.get("_csrf_token") == pre_token

        resp = _login(client, user, pw)
        assert resp.status_code == 302, f"Login failed: {resp.status_code}"

        post_cookie = client.cookies.get("session")
        post_session = _session_dict(client)

        # Cookie value must change across authentication.
        assert post_cookie != pre_cookie, (
            "Session cookie value did not change across login "
            "(session fixation)"
        )
        # Fresh server-side session id was issued.
        sid1 = post_session.get("session_id")
        assert sid1, "Authenticated session must carry a session_id"
        assert post_session.get("username") == user
        # Pre-auth session content must NOT survive into the
        # authenticated session (request.session.clear() fence).
        assert post_session.get("_csrf_token") != pre_token, (
            "Pre-authentication session data survived login — "
            "session fixation fence (session.clear()) regressed"
        )

        # A second authentication must mint a different session id —
        # login never reuses the existing one.
        resp = _login(client, user, pw)
        assert resp.status_code == 302
        sid2 = _session_dict(client).get("session_id")
        assert sid2 and sid2 != sid1, "Re-login reused the previous session id"


# ----------------------------------------------------------------------------
# Logout server-side invalidation
# ----------------------------------------------------------------------------


class TestLogoutServerSideInvalidation:
    def test_logout_destroys_server_session_and_clears_cookie(
        self, app, registered_user
    ):
        """POST /auth/logout must invalidate the session server-side,
        not merely drop the cookie.

        Regression caught: logout that only clears request.session but
        skips session_manager.destroy_session / password-store cleanup
        (a stolen cookie would remain valid server-side).
        """
        from local_deep_research.database.session_passwords import (
            session_password_store,
        )
        from local_deep_research.web.auth.session_manager import (
            session_manager,
        )

        user, pw = registered_user
        client = _new_client(app)
        resp = _login(client, user, pw)
        assert resp.status_code == 302, f"Login failed: {resp.status_code}"

        sid = _session_dict(client).get("session_id")
        assert sid
        # Server-side session and session-password entry exist pre-logout.
        assert session_manager.validate_session(sid) == user
        assert (
            session_password_store.get_session_password(user, sid) is not None
        )

        token = client.get("/auth/csrf-token").json().get("csrf_token", "")
        resp = client.post(
            "/auth/logout",
            headers={"X-CSRFToken": token},
            follow_redirects=False,
        )
        assert resp.status_code == 302
        assert resp.headers.get("location") == "/auth/login"

        # Server-side session destroyed.
        assert session_manager.validate_session(sid) is None, (
            "Logout left the server-side session alive"
        )
        # Session-password store entry removed.
        assert session_password_store.get_session_password(user, sid) is None
        # Cookie session no longer authenticates.
        post_session = _session_dict(client)
        assert "username" not in post_session
        assert "session_id" not in post_session
        assert "temp_auth_token" not in post_session
        assert client.get("/auth/check").status_code == 401


# ----------------------------------------------------------------------------
# Change-password destroys the user's other sessions
# ----------------------------------------------------------------------------


class TestChangePasswordDestroysOtherSessions:
    def test_other_sessions_invalidated(
        self, app, registered_user, monkeypatch
    ):
        """A successful password change must kill ALL of the user's
        sessions (destroy_all_user_sessions + clear_all_for_user), not
        just the one that submitted the form.

        The SQLCipher rekey itself (db_manager.change_password) and the
        backup refresh are mocked at their boundary — they have their own
        tests; this fence is the router's session-cleanup contract.
        """
        from types import SimpleNamespace

        from local_deep_research.database import temp_auth as temp_auth_module
        from local_deep_research.database.encrypted_db import db_manager
        from local_deep_research.database.session_passwords import (
            session_password_store,
        )
        from local_deep_research.web.auth.session_manager import (
            session_manager,
        )

        user, pw = registered_user

        client_a = _new_client(app)
        assert _login(client_a, user, pw).status_code == 302
        sid_a = _session_dict(client_a).get("session_id")

        client_b = _new_client(app)
        assert _login(client_b, user, pw).status_code == 302
        session_b = _session_dict(client_b)
        sid_b = session_b.get("session_id")
        assert sid_a and sid_b and sid_a != sid_b

        # B is fully usable before the password change (proves the later
        # lockout is caused by the change, not by some unrelated failure).
        # follow_redirects=False so a 401->login redirect can't masquerade
        # as a 200.
        pre = client_b.get("/auth/integrity-check", follow_redirects=False)
        assert pre.status_code == 200, f"Pre-change probe failed: {pre.text}"
        assert pre.json().get("integrity") is not None

        # Simulate B's 10s temp-auth token having expired (it would in any
        # real session); otherwise the mocked rekey lets B silently reopen
        # the DB with the old password, masking the lockout.
        token_b = session_b.get("temp_auth_token")
        if token_b:
            temp_auth_module.temp_auth_store.retrieve_auth(token_b)

        assert session_manager.validate_session(sid_a) == user
        assert session_manager.validate_session(sid_b) == user

        # Boundary mocks: skip the real SQLCipher rekey + backup refresh.
        monkeypatch.setattr(
            db_manager, "change_password", lambda *_a, **_k: True
        )

        class _StubBackupService:
            def __init__(self, *_a, **_k):
                pass

            def purge_and_refresh(self):
                return SimpleNamespace(success=True, error=None)

        monkeypatch.setattr(
            "local_deep_research.database.backup.backup_service.BackupService",
            _StubBackupService,
        )

        new_pw = "NewTestPassword456!"  # noqa: S105
        token = client_a.get("/auth/csrf-token").json().get("csrf_token", "")
        resp = client_a.post(
            "/auth/change-password",
            data={
                "current_password": pw,
                "new_password": new_pw,
                "confirm_password": new_pw,
                "csrf_token": token,
            },
            follow_redirects=False,
        )
        assert resp.status_code == 302, (
            f"Password change did not succeed: {resp.status_code} "
            f"{resp.text[:300]}"
        )
        assert resp.headers.get("location") == "/auth/login"

        # THE fence: every session for this user is gone server-side.
        assert session_manager.validate_session(sid_a) is None, (
            "Change-password left the submitting session alive"
        )
        assert session_manager.validate_session(sid_b) is None, (
            "Change-password left the user's OTHER session alive — "
            "destroy_all_user_sessions regressed"
        )
        # Session-password store wiped for the user.
        assert session_password_store.get_session_password(user, sid_a) is None
        assert session_password_store.get_session_password(user, sid_b) is None

        # The submitting client's cookie session is cleared.
        session_a = _session_dict(client_a)
        assert "username" not in session_a
        assert "session_id" not in session_a
        assert client_a.get("/auth/check").status_code == 401

        # And the concurrent session is genuinely locked out end-to-end:
        # the same probe that returned 200 before now yields an auth
        # failure (JSON 401, or the browser-style 302 to the login page).
        post = client_b.get("/auth/integrity-check", follow_redirects=False)
        assert post.status_code in (401, 302), (
            "Concurrent session still authenticated after password change: "
            f"{post.status_code}"
        )
        if post.status_code == 302:
            assert post.headers.get("location", "").startswith("/auth/login")


class TestChangePasswordIndeterminateOutcome:
    """An indeterminate rekey may have activated the new key, so the route
    must revoke every old-password session exactly as on success, and tell
    the user to try the NEW password first. An unavailable rekey (old key
    provably kept) keeps every session and the retry message.
    """

    def _two_sessions(self, app, user, pw):
        from local_deep_research.database import temp_auth as temp_auth_module

        client_a = _new_client(app)
        assert _login(client_a, user, pw).status_code == 302
        client_b = _new_client(app)
        assert _login(client_b, user, pw).status_code == 302
        session_b = _session_dict(client_b)
        # Expire B's temp-auth token as a real session would after 10 s, so
        # B cannot silently reopen the database with the old password.
        token_b = session_b.get("temp_auth_token")
        if token_b:
            temp_auth_module.temp_auth_store.retrieve_auth(token_b)
        assert client_b.get("/auth/check").status_code == 200
        return client_a, client_b, session_b.get("session_id")

    def _post_change(self, client, pw):
        new_pw = "NewTestPassword456!"  # noqa: S105
        token = client.get("/auth/csrf-token").json().get("csrf_token", "")
        return client.post(
            "/auth/change-password",
            data={
                "current_password": pw,
                "new_password": new_pw,
                "confirm_password": new_pw,
                "csrf_token": token,
            },
            follow_redirects=False,
        )

    def _forbid_backup_purge(self, monkeypatch):
        class _NoBackupService:
            def __init__(self, *_a, **_k):
                raise AssertionError(
                    "no backup may be purged while neither key is proven"
                )

        monkeypatch.setattr(
            "local_deep_research.database.backup.backup_service.BackupService",
            _NoBackupService,
        )

    def test_indeterminate_change_revokes_every_session(
        self, app, registered_user, monkeypatch
    ):
        from pathlib import Path

        from local_deep_research.database.encrypted_db import db_manager
        from local_deep_research.database.rekey_lock import (
            RekeyIndeterminateError,
            RekeyStage,
        )
        from local_deep_research.database.session_passwords import (
            session_password_store,
        )
        from local_deep_research.web.auth.session_manager import (
            session_manager,
        )

        user, pw = registered_user
        client_a, client_b, sid_b = self._two_sessions(app, user, pw)
        cleared: list = []
        import local_deep_research.database.thread_local_session as tls

        real_clear = tls.clear_user_credentials

        def spy_clear(username):
            cleared.append(username)
            return real_clear(username)

        monkeypatch.setattr(tls, "clear_user_credentials", spy_clear)

        def indeterminate(*_a, **_k):
            raise RekeyIndeterminateError(
                Path("/nonexistent/user.db"),
                RekeyStage.CANDIDATE_VERIFICATION,
                None,
            )

        monkeypatch.setattr(db_manager, "change_password", indeterminate)
        self._forbid_backup_purge(monkeypatch)

        resp = self._post_change(client_a, pw)

        assert resp.status_code == 302, resp.text[:300]
        assert resp.headers.get("location") == "/auth/login"
        # The other device's old-password session is revoked server-side
        # and end to end.
        assert session_manager.validate_session(sid_b) is None
        assert session_password_store.get_session_password(user, sid_b) is None
        assert client_b.get("/auth/check").status_code == 401
        assert client_a.get("/auth/check").status_code == 401
        assert cleared == [user]
        # And the user is told the change may have landed.
        page = client_a.get("/auth/login")
        assert "may have completed" in page.text
        assert "Try your NEW" in page.text
        assert "could not be completed safely" not in page.text

    def test_unavailable_change_keeps_sessions_and_retry_message(
        self, app, registered_user, monkeypatch
    ):
        from pathlib import Path

        from local_deep_research.database.encrypted_db import db_manager
        from local_deep_research.database.rekey_lock import (
            RekeyStage,
            RekeyUnavailableError,
        )
        from local_deep_research.web.auth.session_manager import (
            session_manager,
        )

        user, pw = registered_user
        client_a, client_b, sid_b = self._two_sessions(app, user, pw)

        def unavailable(*_a, **_k):
            raise RekeyUnavailableError(
                Path("/nonexistent/user.db"), RekeyStage.PHYSICAL_REKEY
            )

        monkeypatch.setattr(db_manager, "change_password", unavailable)
        self._forbid_backup_purge(monkeypatch)

        resp = self._post_change(client_a, pw)

        assert resp.status_code == 503
        assert "could not be completed safely" in resp.text
        assert "may have completed" not in resp.text
        assert session_manager.validate_session(sid_b) == user
        assert client_b.get("/auth/check").status_code == 200


# ----------------------------------------------------------------------------
# Temp-auth token single use (HTTP layer)
# ----------------------------------------------------------------------------


class TestTempAuthTokenSingleUse:
    def test_token_consumed_and_removed_from_session(
        self, app, registered_user
    ):
        """The temp-auth token minted at login is single-use: the first
        request that needs it consumes it from the store AND removes it
        from the cookie session, handing off to the session-password
        store. A replay of the token must find nothing.

        Regression caught: ensure_user_database peeking instead of
        retrieving (token would stay live for its TTL), or forgetting to
        pop it from the session.
        """
        from local_deep_research.database.encrypted_db import db_manager
        from local_deep_research.database.session_passwords import (
            session_password_store,
        )
        from local_deep_research.database.temp_auth import temp_auth_store

        user, pw = registered_user
        client = _new_client(app)
        assert _login(client, user, pw).status_code == 302

        session = _session_dict(client)
        sid = session.get("session_id")
        token = session.get("temp_auth_token")
        assert token, "Login must stash a temp-auth token in the session"
        assert temp_auth_store.peek_auth(token) is not None

        # Force the reopen path (as after a server restart): the DB
        # connection is gone, so the next request must redeem the token.
        db_manager.close_user_database(user)

        resp = client.get("/auth/check")
        assert resp.status_code == 200
        assert resp.json().get("authenticated") is True

        # Token consumed from the store (single use)...
        assert temp_auth_store.peek_auth(token) is None, (
            "Temp-auth token still redeemable after first use"
        )
        # ...removed from the cookie session...
        assert "temp_auth_token" not in _session_dict(client), (
            "Consumed temp-auth token still present in the session cookie"
        )
        # ...and handed off to the session-password store for follow-ups.
        assert (
            session_password_store.get_session_password(user, sid) is not None
        )


# ----------------------------------------------------------------------------
# Change-password input validation
# ----------------------------------------------------------------------------


class TestChangePasswordWhitespaceCurrent:
    """A whitespace-only current password must be refused by the form.

    ``"   "`` is not empty, so the handler's ``if not current_password``
    check passed it through to ``DatabaseManager.change_password``, whose
    key validation raises ``ValueError`` — a 500 with a traceback where the
    user should see "Current password is required" (#6375).
    """

    def test_whitespace_only_current_password_is_a_validation_error(
        self, app, registered_user
    ):
        user, pw = registered_user
        client = _new_client(app)
        assert _login(client, user, pw).status_code == 302

        new_password = "ReplacementPass123"  # noqa: S105
        resp = client.post(
            "/auth/change-password",
            data={
                "current_password": "   ",
                "new_password": new_password,
                "confirm_password": new_password,
                "csrf_token": _csrf(client),
            },
            follow_redirects=False,
        )

        assert resp.status_code == 400, (
            "a whitespace-only current password must come back as a form "
            f"validation error, got {resp.status_code}: {resp.text[:300]}"
        )
        assert "Current password is required" in resp.text

        # Nothing was rekeyed: the real password still opens the database.
        assert _login(_new_client(app), user, pw).status_code == 302
        assert _login(_new_client(app), user, new_password).status_code == 401


# ----------------------------------------------------------------------------
# Change-password rekey backstop
# ----------------------------------------------------------------------------

_AUTH_MOD = "local_deep_research.web.routers.auth"
_NEW_PASSWORD = "ReplacementPass123"
_REJECTED = "Password change rejected"
_REFUSED_KEY_DETAIL = "file is not a database"
_REFUSED_KEY = f"Invalid encryption key: {_REFUSED_KEY_DETAIL}"


class TestChangePasswordBackstop:
    """A manager `ValueError` must render 400, not 500 (#6497).

    `change_password` validates both keys before it rekeys and raises on an
    invalid one. The form checks above the `try` catch anything a browser can
    send, which is what `TestChangePasswordWhitespaceCurrent` pins, and that is
    also why nothing had ever driven the `except ValueError` beneath them: the
    whitespace request is intercepted at the form check and never reaches the
    `try`, and the db-layer tests call the manager directly. So the remedy
    #6375 added for the 500 was the half of it CI could not see, and removing
    it, narrowing it to another exception type, or breaking its 400 render
    would all have stayed green.
    """

    @contextlib.contextmanager
    def _manager_raising(self, error: Exception):
        """Patch the route's `db_manager` down to just `change_password`.

        Same stubbing shape `_rekey_harness` uses in
        `test_auth_route_port_gaps.py`, kept local because this case needs
        neither the cleanup patches nor the render stub: the point is the real
        template rendering at the real status code.
        """
        with patch(f"{_AUTH_MOD}.db_manager") as mock_db:
            mock_db.change_password.side_effect = error
            yield mock_db

    def _submit(self, client, password: str, error: Exception):
        """Post a valid change-password form while the manager raises `error`.

        The CSRF token is fetched before `db_manager` is patched, so the page
        that carries it is rendered by the real manager. Every cell then checks
        that the request reached the rekey; a form check that turned it away
        first would make the assertions after it pass for the wrong reason.
        """
        token = _csrf(client)
        with self._manager_raising(error) as mock_db:
            resp = client.post(
                "/auth/change-password",
                data={
                    "current_password": password,
                    "new_password": _NEW_PASSWORD,
                    "confirm_password": _NEW_PASSWORD,
                    "csrf_token": token,
                },
                follow_redirects=False,
            )
        assert mock_db.change_password.called, (
            "the request must reach the rekey path; if a form check turned it "
            "away first this cell is measuring the wrong branch"
        )
        return resp

    def _logged_in_client(self, app, user: str, password: str):
        client = _new_client(app)
        assert _login(client, user, password).status_code == 302
        return client

    def test_manager_value_error_renders_400_not_500(
        self, app, registered_user
    ):
        user, pw = registered_user
        client = self._logged_in_client(app, user, pw)

        resp = self._submit(client, pw, ValueError(_REFUSED_KEY))

        assert resp.status_code == 400, (
            f"a manager ValueError must come back as a form error, got "
            f"{resp.status_code}: {resp.text[:300]}"
        )
        assert _REJECTED in resp.text

    def test_the_rejection_does_not_echo_the_manager_message(
        self, app, registered_user
    ):
        """The message is built from the password fields this handler holds in
        plaintext, which is why the route logs the user and not the exception.
        Rendering it would put that back on the page."""
        user, pw = registered_user
        client = self._logged_in_client(app, user, pw)

        resp = self._submit(client, pw, ValueError(_REFUSED_KEY))

        assert resp.status_code == 400
        assert _REFUSED_KEY_DETAIL not in resp.text

    def test_the_old_password_still_logs_in_after_a_refused_rekey(
        self, app, registered_user
    ):
        """Nothing was rekeyed on this path, and the 400 has to mean that. A
        backstop that rendered the error after a partial rekey would pass every
        assertion above and lock the user out of their own database."""
        user, pw = registered_user
        client = self._logged_in_client(app, user, pw)

        assert (
            self._submit(client, pw, ValueError(_REFUSED_KEY)).status_code
            == 400
        )

        fresh = _new_client(app)
        assert _login(fresh, user, pw).status_code == 302

    def test_a_non_value_error_is_not_swallowed_into_a_400(
        self, app, registered_user
    ):
        """The scope control. The backstop exists for the one exception type
        `change_password` raises for a refused key; widening it to `Exception`
        would turn a genuine fault into a form error the user retries forever.
        """
        user, pw = registered_user
        client = self._logged_in_client(app, user, pw)

        resp = self._submit(client, pw, RuntimeError("disk gone"))

        assert resp.status_code == 500, (
            "a RuntimeError is not a refused password field and must surface "
            f"as a server error, got {resp.status_code}"
        )
        assert _REJECTED not in resp.text


# ----------------------------------------------------------------------------
# A rekey-sidecar fault is a retryable 503 that keeps the session
# ----------------------------------------------------------------------------


class TestAuthCheckSidecarFault:
    """A fault on the connectivity probe is a 503, not a 500 or a 401.

    Since the rekey-serialization work, ``is_user_connected`` takes the
    cross-process rekey sidecar, and a failure to release it raises
    ``RekeySidecarIOError`` (a ``RekeyOperationalError``, not an OSError)
    straight at the caller. ``/auth/check`` is polled by XHR clients, so
    an unhandled raise is a 500 on every poll for as long as the fault
    lasts. A 401 is no better: the fault says nothing about the session,
    and ``/`` turns a 401 into a logout. It must be a retryable 503 that
    leaves the session usable once the fault clears.
    """

    def _logged_in_client(self, app, user: str, password: str):
        # Same helper TestChangePasswordBackstop uses: this class was
        # extracted without it, and the test 500'd on AttributeError
        # before ever reaching the probe.
        client = _new_client(app)
        assert _login(client, user, password).status_code == 302
        return client

    def test_probe_sidecar_fault_answers_503_and_keeps_the_session(
        self, app, registered_user
    ):
        from pathlib import Path

        from local_deep_research.database.rekey_lock import (
            RekeySidecarIOError,
            RekeySidecarOperation,
        )

        user, pw = registered_user
        client = self._logged_in_client(app, user, pw)

        with patch(
            "local_deep_research.web.dependencies.auth.db_manager"
        ) as mock_db:
            mock_db.is_user_connected.side_effect = RekeySidecarIOError(
                Path(f"{user}.db"), RekeySidecarOperation.UNLOCK, errno.EIO
            )
            resp = client.get("/auth/check")

        assert mock_db.is_user_connected.called, (
            "the request never reached the probe — measuring the wrong branch"
        )
        assert resp.status_code == 503, (
            "a sidecar fault on the probe must be a retryable 503, "
            f"got {resp.status_code}: {resp.text[:300]}"
        )
        assert resp.headers.get("retry-after") == "5"
        assert "application/json" in resp.headers["content-type"]
        assert _session_dict(client).get("username") == user

        resp = client.get("/auth/check")
        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == {"authenticated": True, "username": user}


@pytest.mark.real_session_check
class TestTransientSidecarFaultKeepsTheSession:
    """An fd-exhaustion spike must not log a signed-in user out.

    ``is_user_connected`` used to be a dictionary lookup, so a warm pool
    kept serving through an fd spike. It now opens and flocks the rekey
    sidecar. When that open fails the cached engine is kept, but the
    request read as "not connected": ``require_auth`` answered 401, ``/``
    turned that 401 into ``end_rejected_session`` (server-side session and
    stored password destroyed, cookie cleared), and DatabaseMiddleware's
    reopen with the stored password paid a full PBKDF2 derivation on
    every request before failing on the same sidecar.

    The fault is injected at the real ``os.open`` of the sidecar file, for
    a user that really logged in: real SQLCipher engine, real server-side
    session and stored password, and the real session check.
    """

    def test_sidecar_emfile_answers_503_and_the_session_survives(
        self, app, registered_user, monkeypatch
    ):
        import os
        from pathlib import Path

        from local_deep_research.database import encrypted_db, rekey_lock
        from local_deep_research.database.session_passwords import (
            session_password_store,
        )
        from local_deep_research.web.auth.session_manager import (
            session_manager,
        )

        user, pw = registered_user
        client = _new_client(app)
        assert _login(client, user, pw).status_code == 302
        # One ordinary request consumes the post-login bootstrap token and
        # stores this session's password, as the first page load does.
        assert client.get("/auth/check").status_code == 200
        session_id = _session_dict(client)["session_id"]
        assert session_password_store.get_session_password(user, session_id)
        engine = encrypted_db.db_manager.connections[user]

        # Spy on key derivations for THIS user's database only, and fail only
        # its sidecar: both seams are process-wide, and no other thread's
        # database may count against (or be broken by) this test.
        user_db = encrypted_db.db_manager._get_user_db_path(user)
        sidecar = os.fspath(user_db.with_name(f"{user_db.name}.rekey.lock"))
        real_derive = encrypted_db.get_key_from_password
        derived = []

        def derive(password, db_path=None):
            if db_path is not None and Path(db_path) == user_db:
                derived.append(db_path)
            return real_derive(password, db_path=db_path)

        monkeypatch.setattr(encrypted_db, "get_key_from_password", derive)
        real_open = rekey_lock.os.open

        def emfile_on_sidecar(path, flags, *args, **kwargs):
            if os.fspath(path) == sidecar:
                raise OSError(errno.EMFILE, "Too many open files")
            return real_open(path, flags, *args, **kwargs)

        monkeypatch.setattr(rekey_lock.os, "open", emfile_on_sidecar)
        try:
            responses = {
                path: client.get(
                    path,
                    headers={"Accept": "text/html"},
                    follow_redirects=False,
                )
                # The index (whose 401 handler ends the session), a page
                # behind Depends(require_auth), and the XHR auth probe.
                for path in ("/", "/auth/change-password", "/auth/check")
            }
            # A caller of "/" that asks for JSON.
            json_index = client.get(
                "/",
                headers={"Accept": "application/json"},
                follow_redirects=False,
            )
        finally:
            monkeypatch.setattr(rekey_lock.os, "open", real_open)

        for path, resp in responses.items():
            assert resp.status_code == 503, (
                f"{path} answered {resp.status_code} "
                f"(location={resp.headers.get('location')!r}) during a "
                "transient sidecar fault instead of a retryable 503"
            )
            assert resp.headers.get("retry-after") == "5", path
        # A browser landing on "/" gets the branded error page, whose button
        # retries "/", rather than a raw JSON body: /auth/login sends every
        # signed-in cookie back to "/", so a lasting fault would otherwise
        # leave the browser on that JSON. A JSON caller keeps the JSON 503.
        index = responses["/"]
        assert index.headers["content-type"].startswith("text/html"), (
            index.text[:300]
        )
        assert 'data-status-code="503"' in index.text
        assert 'href="/"' in index.text
        assert json_index.status_code == 503, json_index.text[:300]
        assert json_index.headers.get("retry-after") == "5"
        assert json_index.json() == {
            "detail": "Your database is temporarily unavailable. "
            "Please try again in a moment."
        }
        # Nothing was torn down ...
        assert session_manager.validate_session(session_id) == user
        assert (
            session_password_store.get_session_password(user, session_id) == pw
        )
        assert _session_dict(client).get("username") == user
        assert _session_dict(client).get("session_id") == session_id
        assert encrypted_db.db_manager.connections[user] is engine
        # ... and no request paid for a key derivation it could not use.
        assert derived == []

        # Once the fault clears, the same session and engine carry on.
        resp = client.get("/auth/check")
        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == {"authenticated": True, "username": user}
        assert encrypted_db.db_manager.connections[user] is engine
        assert derived == []

    def test_spike_after_the_auth_probe_answers_503_and_the_session_survives(
        self, app, registered_user, monkeypatch
    ):
        """The spike can start between two probes of the same request.

        An fd-exhaustion spike fluctuates. ``require_auth``'s probe can
        succeed and, a moment later, the index's own database session
        (``get_user_db_session`` -> the thread-local session ->
        ``open_user_database``'s claim) fail on the same sidecar. That
        failure used to come back as the plain ``DatabaseSessionError`` a
        wrong password produces, and the index's catch-all answered it by
        clearing the cookie and redirecting to the login page, leaving the
        server-side session and stored password orphaned. It must be the
        same retryable 503 ``require_auth`` gives, with nothing torn down.
        """
        import os
        from pathlib import Path

        from local_deep_research.database import encrypted_db, rekey_lock
        from local_deep_research.database.session_passwords import (
            session_password_store,
        )
        from local_deep_research.web.auth.session_manager import (
            session_manager,
        )
        from local_deep_research.web.dependencies import auth as auth_deps

        user, pw = registered_user
        client = _new_client(app)
        assert _login(client, user, pw).status_code == 302
        # One ordinary request consumes the post-login bootstrap token and
        # stores this session's password, as the first page load does.
        assert client.get("/auth/check").status_code == 200
        session_id = _session_dict(client)["session_id"]
        assert (
            session_password_store.get_session_password(user, session_id) == pw
        )
        engine = encrypted_db.db_manager.connections[user]

        # Both seams are process-wide: spy on key derivations for THIS
        # user's database only, and fail only its sidecar.
        user_db = encrypted_db.db_manager._get_user_db_path(user)
        sidecar = os.fspath(user_db.with_name(f"{user_db.name}.rekey.lock"))
        real_derive = encrypted_db.get_key_from_password
        derived = []

        def derive(password, db_path=None):
            if db_path is not None and Path(db_path) == user_db:
                derived.append(db_path)
            return real_derive(password, db_path=db_path)

        monkeypatch.setattr(encrypted_db, "get_key_from_password", derive)
        real_open = rekey_lock.os.open
        spike = {"on": False, "hits": 0}

        def emfile_during_spike(path, flags, *args, **kwargs):
            if spike["on"] and os.fspath(path) == sidecar:
                spike["hits"] += 1
                raise OSError(errno.EMFILE, "Too many open files")
            return real_open(path, flags, *args, **kwargs)

        # The index imports require_auth when it runs, so wrapping the module
        # attribute starts the spike right after its probe has succeeded
        # (DatabaseMiddleware's probe ran earlier still).
        real_require_auth = auth_deps.require_auth
        authenticated = []

        def require_auth_then_spike(request):
            username = real_require_auth(request)
            authenticated.append(username)
            spike["on"] = True
            return username

        monkeypatch.setattr(rekey_lock.os, "open", emfile_during_spike)
        monkeypatch.setattr(auth_deps, "require_auth", require_auth_then_spike)
        try:
            resp = client.get(
                "/", headers={"Accept": "text/html"}, follow_redirects=False
            )
        finally:
            spike["on"] = False
            monkeypatch.setattr(auth_deps, "require_auth", real_require_auth)
            monkeypatch.setattr(rekey_lock.os, "open", real_open)

        assert authenticated == [user], "require_auth's probe did not pass"
        assert spike["hits"] >= 1, (
            "the index's own session open never reached the sidecar: "
            "measuring the wrong branch"
        )
        assert resp.status_code == 503, (
            f"/ answered {resp.status_code} "
            f"(location={resp.headers.get('location')!r}) when the spike hit "
            "its session open after require_auth passed, instead of a "
            "retryable 503"
        )
        assert resp.headers.get("retry-after") == "5"
        assert "location" not in resp.headers
        # Answered to this browser request as the branded error page that
        # retries "/", not as a raw JSON body.
        assert resp.headers["content-type"].startswith("text/html")
        assert 'data-status-code="503"' in resp.text
        # Nothing was torn down: cookie, server-side session, stored password
        # and the cached engine all survive ...
        assert _session_dict(client).get("username") == user
        assert _session_dict(client).get("session_id") == session_id
        assert session_manager.validate_session(session_id) == user
        assert (
            session_password_store.get_session_password(user, session_id) == pw
        )
        assert encrypted_db.db_manager.connections[user] is engine
        # ... and the request failed before deriving a key it could not use.
        assert derived == []

        # Once the spike passes, the same session renders the page.
        resp = client.get(
            "/", headers={"Accept": "text/html"}, follow_redirects=False
        )
        assert resp.status_code == 200, resp.text[:300]
        assert _session_dict(client).get("session_id") == session_id
        assert encrypted_db.db_manager.connections[user] is engine
        assert derived == []

    @pytest.mark.skipif(os.name == "nt", reason="POSIX flock required")
    def test_a_held_sidecar_answers_503_within_the_bound_and_keeps_the_session(
        self, app, registered_user, monkeypatch
    ):
        """A holder that keeps the user's sidecar must not hang the request.

        Every authenticated request claims the user's cached engine under
        the rekey sidecar. A holder that keeps it (another worker's password
        change of a large database, a stopped process, a hung mount) used to
        hang each such request for as long as it held it: no timeout and no
        503. The claim now gives up after a bounded wait, and the request is
        answered with the same retryable 503 as any other sidecar fault.

        The bound is the request's, not each claim's. One request claims
        the sidecar up to four times (DatabaseMiddleware's probe and its
        reopen, then require_auth's probe and its classifying probe), and
        each used to wait out the bound on its own: four bounds (about 20 s)
        before a 503 that says ``Retry-After: 5``, on the shared threads
        that serve every user. After the first wait runs out, the request's
        later waits give up at once.
        """
        from local_deep_research.database import encrypted_db
        from local_deep_research.database.session_passwords import (
            session_password_store,
        )
        from local_deep_research.web.auth.session_manager import (
            session_manager,
        )

        user, pw = registered_user
        client = _new_client(app)
        assert _login(client, user, pw).status_code == 302
        assert client.get("/auth/check").status_code == 200
        session_id = _session_dict(client)["session_id"]
        engine = encrypted_db.db_manager.connections[user]
        bound = 0.3
        monkeypatch.setattr(
            encrypted_db, "_SIDECAR_WAIT_TIMEOUT_SECONDS", bound, raising=False
        )

        user_db = encrypted_db.db_manager._get_user_db_path(user)
        granted = _spy_sidecar_waits(monkeypatch, encrypted_db, user_db)
        # The XHR probe, and the index, whose 401 handler ends the session.
        for path in ("/auth/check", "/"):
            granted.clear()
            with _sidecar_flock_held_elsewhere(user_db):
                worker, outcome = _run_bounded(
                    lambda path=path: client.get(
                        path,
                        headers={"Accept": "text/html"},
                        follow_redirects=False,
                    ),
                    timeout=20,
                )
                finished = not worker.is_alive()
            worker.join(timeout=30)

            assert finished, f"{path} waited for the sidecar holder"
            assert "error" not in outcome, outcome
            resp = outcome["value"]
            assert resp.status_code == 503, (path, resp.text[:300])
            assert resp.headers.get("retry-after") == "5", path
            assert "location" not in resp.headers, path
            # One bound for the whole request: the later claims did reach
            # the sidecar, and were granted no wait of their own.
            assert len(granted) >= 2, (path, granted)
            assert 0 < granted[0] <= bound, (path, granted)
            assert sum(granted) <= bound, (
                f"{path} was granted {sum(granted):.2f} s of sidecar waits "
                f"for one request against a {bound} s bound: {granted}"
            )
            # A retryable fault, not a verdict on the session: nothing torn
            # down.
            assert session_manager.validate_session(session_id) == user, path
            assert (
                session_password_store.get_session_password(user, session_id)
                == pw
            ), path
            assert _session_dict(client).get("session_id") == session_id
            assert encrypted_db.db_manager.connections[user] is engine

        # Once the holder is gone, the same session and engine carry on.
        resp = client.get("/auth/check")
        assert resp.status_code == 200, resp.text[:300]
        assert encrypted_db.db_manager.connections[user] is engine

    @pytest.mark.skipif(os.name == "nt", reason="POSIX flock required")
    def test_a_held_sidecar_with_no_cached_engine_answers_503_and_keeps_the_session(
        self, app, registered_user, monkeypatch
    ):
        """A reopen stopped by a held sidecar is no verdict on the session.

        With no engine cached (the user's other session logged out, the idle
        sweep closed it), DatabaseMiddleware reopens the database with this
        session's stored password. That cold open gives up on a held sidecar
        with ``RekeyLockContentionError``, and ``ensure_user_database`` used
        to swallow it: ``require_auth`` then saw "not connected" and
        answered 401, and ``/`` turned that 401 into
        ``end_rejected_session``, destroying the server-side session and
        the stored password. It must be the retryable 503 with everything
        kept, and the same session must reopen the database once the holder
        is gone.
        """
        from local_deep_research.database import encrypted_db
        from local_deep_research.database.session_passwords import (
            session_password_store,
        )
        from local_deep_research.web.auth.session_manager import (
            session_manager,
        )

        user, pw = registered_user
        client = _new_client(app)
        assert _login(client, user, pw).status_code == 302
        # One ordinary request consumes the post-login bootstrap token and
        # stores this session's password, as the first page load does.
        assert client.get("/auth/check").status_code == 200
        session_id = _session_dict(client)["session_id"]
        assert (
            session_password_store.get_session_password(user, session_id) == pw
        )
        encrypted_db.db_manager.close_user_database(user)
        assert user not in encrypted_db.db_manager.connections
        bound = 0.3
        monkeypatch.setattr(
            encrypted_db, "_SIDECAR_WAIT_TIMEOUT_SECONDS", bound, raising=False
        )

        user_db = encrypted_db.db_manager._get_user_db_path(user)
        granted = _spy_sidecar_waits(monkeypatch, encrypted_db, user_db)
        responses = {}
        # The index (whose 401 handler ends the session), a page behind
        # Depends(require_auth), the XHR probe, and "/" asked for JSON.
        for path, accept in (
            ("/", "text/html"),
            ("/auth/change-password", "text/html"),
            ("/auth/check", "text/html"),
            ("/", "application/json"),
        ):
            granted.clear()
            with _sidecar_flock_held_elsewhere(user_db):
                worker, outcome = _run_bounded(
                    lambda path=path, accept=accept: client.get(
                        path,
                        headers={"Accept": accept},
                        follow_redirects=False,
                    ),
                    timeout=20,
                )
                finished = not worker.is_alive()
            worker.join(timeout=30)

            assert finished, f"{path} waited for the sidecar holder"
            assert "error" not in outcome, outcome
            resp = outcome["value"]
            assert resp.status_code == 503, (
                f"{path} ({accept}) answered {resp.status_code} "
                f"(location={resp.headers.get('location')!r}) when the "
                "reopen met a held sidecar, instead of a retryable 503"
            )
            assert resp.headers.get("retry-after") == "5", path
            assert "location" not in resp.headers, path
            # The reopen did reach the sidecar, and the request waited for
            # it once.
            assert granted, f"{path}: the reopen never took the sidecar"
            assert sum(granted) <= bound, (path, granted)
            responses[path, accept] = resp
            # Nothing was torn down, and nothing was cached.
            assert session_manager.validate_session(session_id) == user, path
            assert (
                session_password_store.get_session_password(user, session_id)
                == pw
            ), path
            assert _session_dict(client).get("username") == user, path
            assert _session_dict(client).get("session_id") == session_id
            assert user not in encrypted_db.db_manager.connections

        # A browser on "/" gets the branded page that retries "/"; a JSON
        # caller gets the JSON 503.
        index = responses["/", "text/html"]
        assert index.headers["content-type"].startswith("text/html")
        assert 'data-status-code="503"' in index.text
        assert 'href="/"' in index.text
        assert responses["/", "application/json"].json() == {
            "detail": "Your database is temporarily unavailable. "
            "Please try again in a moment."
        }

        # Once the holder is gone, the same session reopens the database
        # with its stored password.
        resp = client.get("/auth/check")
        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == {"authenticated": True, "username": user}
        assert user in encrypted_db.db_manager.connections
        assert _session_dict(client).get("session_id") == session_id

    @pytest.mark.skipif(os.name == "nt", reason="POSIX flock required")
    def test_login_while_the_sidecar_is_held_answers_503_without_a_lockout_strike(
        self, app, registered_user, monkeypatch
    ):
        """The cold open's wait is bounded too, and is no password verdict.

        A login opens the database cold under the same sidecar. It used to
        hang for as long as the holder kept it; the open now gives up after
        the bound with ``RekeyLockContentionError``, which the login route
        answers with a 503 that does not count toward the account lockout:
        the password was never checked.
        """
        from local_deep_research.database import encrypted_db
        from local_deep_research.security.account_lockout import (
            get_account_lockout_manager,
        )

        user, pw = registered_user
        encrypted_db.db_manager.close_user_database(user)
        assert user not in encrypted_db.db_manager.connections
        monkeypatch.setattr(
            encrypted_db, "_SIDECAR_WAIT_TIMEOUT_SECONDS", 0.3, raising=False
        )
        client = _new_client(app)
        csrf_token = _csrf(client)
        user_db = encrypted_db.db_manager._get_user_db_path(user)

        with patch.object(
            get_account_lockout_manager(), "record_failure"
        ) as record_failure:
            with _sidecar_flock_held_elsewhere(user_db):
                worker, outcome = _run_bounded(
                    lambda: client.post(
                        "/auth/login",
                        data={
                            "username": user,
                            "password": pw,
                            "csrf_token": csrf_token,
                        },
                        follow_redirects=False,
                    ),
                    timeout=20,
                )
                finished = not worker.is_alive()
            worker.join(timeout=30)

        assert finished, "the login waited for the sidecar holder"
        assert "error" not in outcome, outcome
        resp = outcome["value"]
        assert resp.status_code == 503, resp.text[:300]
        record_failure.assert_not_called()
        assert user not in encrypted_db.db_manager.connections

        # Once the holder is gone, the same password logs in.
        assert _login(client, user, pw).status_code == 302


class TestLockedDatabaseIsNotAWrongPassword:
    """A password check that met a locked database is no credential verdict."""

    def test_login_against_a_locked_database_is_503_without_a_strike(
        self, app, registered_user, monkeypatch
    ):
        from pathlib import Path

        from local_deep_research.database.rekey_lock import (
            DatabaseKeyCheckUnavailableError,
        )
        from local_deep_research.security.account_lockout import (
            get_account_lockout_manager,
        )
        from local_deep_research.web.routers import auth as auth_router

        user, pw = registered_user

        def locked(*_a, **_k):
            raise DatabaseKeyCheckUnavailableError(
                Path("/nonexistent/user.db"), "OperationalError"
            )

        monkeypatch.setattr(
            auth_router.db_manager, "open_user_database", locked
        )
        client = _new_client(app)
        with patch.object(
            get_account_lockout_manager(), "record_failure"
        ) as record_failure:
            resp = _login(client, user, pw)

        assert resp.status_code == 503, resp.text[:300]
        assert "Invalid username or password" not in resp.text
        record_failure.assert_not_called()


# ----------------------------------------------------------------------------
# Change-password keeps a concurrent new-password engine (HTTP path)
# ----------------------------------------------------------------------------


class TestChangePasswordKeepsConcurrentNewPasswordEngine:
    """The route must not evict the engine a racing new-password open cached.

    ``DatabaseManager.change_password`` detaches the cached engine BEFORE
    the rekey, under the rekey sidecar lock every cold open also takes, and
    evicts nothing afterwards. The route used to follow a successful change
    with an unconditional ``db_manager.close_user_database(username)``, so
    an ``open_user_database(new_password)`` that published its engine after
    the rekey committed (a fresh login with the new password, say) lost it
    to the route and the next request had to cold-open again.

    The real rekey runs here. The racing open is driven deterministically
    from a wrapper around ``change_password``: it runs after the manager has
    released the sidecar and returned True, and before the route's
    post-change teardown, which is exactly where the old close landed.
    """

    def test_route_does_not_evict_the_new_password_engine(
        self, app, monkeypatch
    ):
        from types import SimpleNamespace

        from sqlalchemy import text

        from local_deep_research.database.encrypted_db import db_manager

        if not db_manager.has_encryption:
            pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

        # A dedicated user: the real rekey below changes this account's
        # password, so the module-scoped ``registered_user`` must not be it.
        user = f"authgap_keep_{uuid.uuid4().hex[:8]}"
        old_pw = "KeepEngineOld123!"  # noqa: S105
        new_pw = "KeepEngineNew456!"  # noqa: S105
        boot = _new_client(app)
        reg = boot.post(
            "/auth/register",
            data={
                "username": user,
                "password": old_pw,
                "confirm_password": old_pw,
                "acknowledge": "true",
                "csrf_token": _csrf(boot),
            },
            follow_redirects=False,
        )
        assert reg.status_code == 302, (
            f"registration failed: {reg.status_code} {reg.text[:300]}"
        )

        client = _new_client(app)
        assert _login(client, user, old_pw).status_code == 302

        real_change_password = db_manager.change_password
        published = {}

        def _change_then_race(username, old, new):
            result = real_change_password(username, old, new)
            if result is True:
                # The concurrent new-password open, landing after the
                # manager released the sidecar.
                published["engine"] = db_manager.open_user_database(
                    username, new
                )
            return result

        monkeypatch.setattr(db_manager, "change_password", _change_then_race)

        class _StubBackupService:
            def __init__(self, *_a, **_k):
                pass

            def purge_and_refresh(self):
                return SimpleNamespace(success=True, error=None)

        monkeypatch.setattr(
            "local_deep_research.database.backup.backup_service.BackupService",
            _StubBackupService,
        )

        try:
            token = client.get("/auth/csrf-token").json().get("csrf_token", "")
            resp = client.post(
                "/auth/change-password",
                data={
                    "current_password": old_pw,
                    "new_password": new_pw,
                    "confirm_password": new_pw,
                    "csrf_token": token,
                },
                follow_redirects=False,
            )
            assert resp.status_code == 302, (
                f"password change failed: {resp.status_code} {resp.text[:300]}"
            )
            assert resp.headers.get("location") == "/auth/login"

            engine = published.get("engine")
            assert engine is not None, (
                "premise: the racing new-password open must have published "
                "an engine, or nothing below measures the route"
            )
            assert db_manager.connections.get(user) is engine, (
                "the password-change route evicted the engine a concurrent "
                "new-password open had just cached"
            )
            assert db_manager.open_user_database(user, new_pw) is engine
            with engine.connect() as conn:
                conn.execute(
                    text("SELECT name FROM sqlite_master LIMIT 1")
                ).all()
            # And the rekey really happened: the old password is dead.
            assert db_manager.open_user_database(user, old_pw) is None
        finally:
            db_manager.close_user_database(user)


class TestChangePasswordContention:
    def test_contended_change_answers_409_and_keeps_sessions(
        self, app, registered_user, monkeypatch
    ):
        from pathlib import Path

        from local_deep_research.database.encrypted_db import db_manager
        from local_deep_research.database.rekey_lock import (
            RekeyLockContentionError,
        )

        user, pw = registered_user
        helper = TestChangePasswordIndeterminateOutcome()
        client_a, client_b, _sid_b = helper._two_sessions(app, user, pw)

        def contended(*_a, **_k):
            raise RekeyLockContentionError(
                Path("/nonexistent/user.db.rekey.lock")
            )

        monkeypatch.setattr(db_manager, "change_password", contended)
        helper._forbid_backup_purge(monkeypatch)

        resp = helper._post_change(client_a, pw)

        assert resp.status_code == 409, resp.text[:300]
        assert "already in progress" in resp.text
        assert client_a.get("/auth/check").status_code == 200
        assert client_b.get("/auth/check").status_code == 200


class TestRevocationStepsAreIndependent:
    """One failing revocation step must not skip the ones after it."""

    def test_a_failing_early_step_does_not_skip_later_ones(self, monkeypatch):
        from types import SimpleNamespace

        import local_deep_research.database.session_passwords as sp
        import local_deep_research.database.thread_local_session as tls
        from local_deep_research.web.routers import auth as auth_router

        calls: list = []

        def failing_scheduler():
            raise RuntimeError("scheduler down")

        monkeypatch.setattr(
            "local_deep_research.scheduler.background."
            "get_background_job_scheduler",
            failing_scheduler,
        )

        def failing_destroy(username):
            calls.append(("destroy", username))
            raise RuntimeError("session store down")

        monkeypatch.setattr(
            auth_router.session_manager,
            "destroy_all_user_sessions",
            failing_destroy,
        )
        monkeypatch.setattr(
            sp.session_password_store,
            "clear_all_for_user",
            lambda username: calls.append(("passwords", username)),
        )
        monkeypatch.setattr(
            tls,
            "clear_user_credentials",
            lambda username: calls.append(("thread", username)),
        )
        monkeypatch.setattr(
            auth_router,
            "_disconnect_user_sockets",
            lambda username: calls.append(("sockets", username)),
        )
        request = SimpleNamespace(session={"username": "u", "session_id": "s"})

        auth_router._revoke_user_credentials_after_rekey(request, "u")

        assert calls == [
            ("destroy", "u"),
            ("passwords", "u"),
            ("thread", "u"),
            ("sockets", "u"),
        ]
        assert request.session == {}