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.

22.7 KB
Raw Blame
"""Auth-route assertions the FastAPI port left behind.

Ported from ``tests/web/auth/test_auth_routes.py`` on ``origin/main``, which
the migration deleted. Most of that file IS superseded on this branch — the
open-redirect matrix by ``tests/web/test_redirect_and_url_generation.py``,
the registration-failure paths by ``tests/web/test_registration_*.py``, the
password-change lifecycle by
``tests/web/test_long_integration_flows_followup.py``, logout by
``tests/web/routers/test_auth_flow_gaps.py``. What is restored here is the
residue: assertions with no successor at all, all of which pin a guard that
still exists in ``web/routers/auth.py`` and would go unnoticed if it were
removed.

Restored, and why each matters
------------------------------
1. **Login's empty-field 400s.** Nothing asserts them. Without the guard an
   empty password reaches ``open_user_database``, where "no password" and
   "wrong password" are the same failure — the 400 is what keeps a blank
   submit from being counted as a credential attempt.
2. **The 503 DB-init path must not record a lockout failure.** Branch tests
   cover the 503; none cover ``record_failure.assert_not_called()``. The
   whole point of that branch is that the user's password was fine and the
   server is misconfigured — charging them a lockout strike for it locks a
   valid user out of a server-side bug.
3. **Logout's active-research carve-out.** The DB must NOT be closed while
   the user still has research running, or the log-queue drain silently
   drops that job's rows. Only a static call-site census covers it.
4. **Change-password's four validation 400s**, including "new password must
   differ from current" — the one a naive validator forgets.
5. **The 409 while research is active**, and that ``db_manager.change_password``
   is never reached. A static census proves the gate is *called*; nothing
   proved the rejection actually happens or that the destructive rekey is
   skipped. Note the branch source comment on this very branch: main's
   ``#5538`` hunk arrived with Flask signatures that raised under FastAPI,
   so the rejection 500'd instead of 409'ing — an already-realised
   regression on exactly this line, and still untested.
6. **The rekey's TOCTOU gate, in both directions.** These are the two tests
   worth the most: the acting user's gate must block a research start that
   races the rekey, and it must NOT be process-global (which would freeze
   every other user's research for the rekey's duration). Both are
   concurrency properties that no static check can see.
7. **integrity-check's response shape.** The branch test asserts only that
   an ``"integrity"`` key exists; the ternary that decides
   ``"valid"``/``"corrupted"`` and the ``username`` field are unpinned.

Harness
-------
Main built a bare Flask app, registered ``auth_bp``, and poked
``session_transaction()``. The FastAPI analogue is a bare app with the auth
``APIRouter`` plus ``SessionMiddleware`` and a stamping route (there is no
``session_transaction``), and ``dependency_overrides[require_auth]`` where
the route authenticates through the dependency rather than the session dict.
Building a private app rather than using the shared ``app`` fixture is
deliberate: these tests patch ``db_manager`` and drive real concurrency, and
must not do that to an app other tests share.

``render_template`` is replaced by a stub that returns a ``JSONResponse``
carrying the SAME status code, because on this branch the status is a
keyword argument to ``render_template`` rather than a returned tuple — a
stub that dropped it would silently turn every 400/409 assertion into a 200.
Every request passes ``follow_redirects=False``: httpx follows redirects by
default and Flask's client did not, so a 302 assertion would otherwise see
the destination's 200.
"""

import contextlib
import threading
import time
from unittest.mock import MagicMock, patch

import pytest
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from starlette.middleware.sessions import SessionMiddleware
from starlette.testclient import TestClient

from local_deep_research.database.encrypted_db import (
    DatabaseInitializationError,
)
from local_deep_research.web.dependencies.auth import require_auth
from local_deep_research.web.routers.auth import router as auth_router

USERNAME = "testuser"
SESSION_ID = "session_123"
AUTH_MOD = "local_deep_research.web.routers.auth"
GLOBALS_MOD = "local_deep_research.web.routes.globals"


def _fake_render(request, name, context=None, status_code=200):
    """Stand-in for ``render_template`` that preserves the status code.

    Also echoes the context keys, so a test can assert what main asserted
    with ``mock_render.assert_called_with(..., password_requirements=...)``
    — the real helper returns rendered HTML, which would make that
    unobservable.
    """
    return JSONResponse(
        {"template": name, "context": sorted(context or {})},
        status_code=status_code,
    )


@pytest.fixture(autouse=True)
def _slowapi_off():
    from local_deep_research.web.dependencies.rate_limit import limiter

    original = limiter.enabled
    limiter.enabled = False
    try:
        yield
    finally:
        limiter.enabled = original


@pytest.fixture
def mini_app():
    """Bare app with the auth router — main's ``register_blueprint(auth_bp)``."""
    from local_deep_research.web.dependencies.rate_limit import limiter

    app = FastAPI()
    app.state.limiter = limiter
    app.include_router(auth_router)
    app.add_middleware(SessionMiddleware, secret_key="port-gap-test-secret")

    @app.post("/_stamp_session")
    def _stamp(request: Request):
        """The FastAPI analogue of Flask's ``session_transaction()``."""
        request.session["username"] = USERNAME
        request.session["session_id"] = SESSION_ID
        return {"ok": True}

    return app


@pytest.fixture
def authed_app(mini_app):
    """``mini_app`` where ``require_auth`` resolves to ``USERNAME``.

    The routes below authenticate through the dependency, not the session
    dict, so this is what "logged in" means to them.
    """
    mini_app.dependency_overrides[require_auth] = lambda: USERNAME
    return mini_app


def _client(app) -> TestClient:
    return TestClient(app, raise_server_exceptions=False)


def _stamped_client(app) -> TestClient:
    client = _client(app)
    client.post("/_stamp_session")
    return client


# ---------------------------------------------------------------------------
# 1-2. POST /auth/login field validation and the 503 lockout carve-out
# ---------------------------------------------------------------------------
class TestLogin:
    @pytest.mark.parametrize(
        ("label", "form"),
        [
            ("missing username", {"username": "", "password": "password123"}),
            ("missing password", {"username": "testuser", "password": ""}),
        ],
    )
    def test_returns_400_for_missing_credentials_field(
        self, mini_app, label, form
    ):
        client = _client(mini_app)
        with (
            patch(f"{AUTH_MOD}.render_template", _fake_render),
            patch(
                f"{AUTH_MOD}.load_server_config",
                return_value={"allow_registrations": True},
            ),
        ):
            response = client.post(
                "/auth/login", data=form, follow_redirects=False
            )

        assert response.status_code == 400, (
            f"{label}: an empty field must be rejected before it reaches "
            f"open_user_database, got {response.status_code}"
        )

    def test_returns_503_for_database_init_failure_without_lockout(
        self, mini_app
    ):
        """A DatabaseInitializationError yields 503 and bypasses lockout.

        The user's password is valid — we got far enough to attempt schema
        init — so penalising them with a lockout strike for a server-side
        configuration problem (e.g. a world-writable migrations dir) would
        lock a legitimate user out of someone else's mistake.
        """
        client = _client(mini_app)
        with (
            patch(f"{AUTH_MOD}.render_template", _fake_render),
            patch(
                f"{AUTH_MOD}.load_server_config",
                return_value={"allow_registrations": True},
            ),
            patch(f"{AUTH_MOD}.db_manager") as mock_db,
            patch(f"{AUTH_MOD}.get_account_lockout_manager") as mock_factory,
        ):
            mock_db.open_user_database.side_effect = (
                DatabaseInitializationError("boom")
            )
            mock_lockout = mock_factory.return_value
            mock_lockout.is_locked.return_value = False

            response = client.post(
                "/auth/login",
                data={"username": "validuser", "password": "correctpassword"},
                follow_redirects=False,
            )

        assert response.status_code == 503
        mock_lockout.record_failure.assert_not_called()


# ---------------------------------------------------------------------------
# 3. POST /auth/logout — the DB close is gated on active research
# ---------------------------------------------------------------------------
class TestLogoutDatabaseClose:
    def _logout_with_active_research(self, app, active):
        client = _stamped_client(app)
        sched = MagicMock()
        sched.is_running = False
        with (
            patch(f"{AUTH_MOD}.db_manager") as mock_db,
            patch(f"{AUTH_MOD}.session_manager"),
            patch(f"{AUTH_MOD}._disconnect_session_sockets"),
            patch(
                "local_deep_research.database.session_passwords."
                "session_password_store"
            ),
            patch(
                "local_deep_research.database.thread_local_session."
                "clear_user_credentials"
            ),
            patch(
                "local_deep_research.scheduler.background."
                "get_background_job_scheduler",
                return_value=sched,
            ),
            patch(
                f"{GLOBALS_MOD}.get_usernames_with_active_research",
                return_value=active,
            ),
        ):
            response = client.post("/auth/logout", follow_redirects=False)
        assert response.status_code == 302
        return mock_db

    def test_skips_db_close_when_research_active(self, mini_app):
        """Logout must NOT close the DB while the user has research running,
        so the log-queue drain doesn't silently drop that job's logs."""
        mock_db = self._logout_with_active_research(mini_app, {USERNAME})
        mock_db.close_user_database.assert_not_called()

    def test_closes_db_when_no_active_research(self, mini_app):
        """With no active research, logout closes the DB as before."""
        mock_db = self._logout_with_active_research(mini_app, set())
        mock_db.close_user_database.assert_called_once_with(USERNAME)


# ---------------------------------------------------------------------------
# 4-5. POST /auth/change-password validation and the active-research 409
# ---------------------------------------------------------------------------
class TestChangePasswordValidation:
    @pytest.mark.parametrize(
        ("label", "form"),
        [
            (
                "missing current password",
                {
                    "current_password": "",
                    "new_password": "NewStrongP4ss!",
                    "confirm_password": "NewStrongP4ss!",
                },
            ),
            (
                "new password same as current",
                {
                    "current_password": "samepassword123",
                    "new_password": "samepassword123",
                    "confirm_password": "samepassword123",
                },
            ),
            (
                "weak new password",
                {
                    "current_password": "OldPass123",
                    "new_password": "weak",
                    "confirm_password": "weak",
                },
            ),
            (
                "confirmation mismatch",
                {
                    "current_password": "OldPass123",
                    "new_password": "NewStrongP4ss!",
                    "confirm_password": "DifferentP4ss!",
                },
            ),
        ],
    )
    def test_returns_400_for_invalid_form(self, authed_app, label, form):
        client = _stamped_client(authed_app)
        with (
            patch(f"{AUTH_MOD}.render_template", _fake_render),
            patch(f"{AUTH_MOD}.db_manager") as mock_db,
        ):
            response = client.post(
                "/auth/change-password", data=form, follow_redirects=False
            )

        assert response.status_code == 400, (
            f"{label}: expected 400, got {response.status_code}"
        )
        mock_db.change_password.assert_not_called()
        # Main pinned this via mock_render.assert_called_with(...,
        # password_requirements=...): the rejected form must be re-rendered
        # WITH the rules, or the user re-types against invisible ones.
        assert "password_requirements" in response.json()["context"], (
            f"{label}: the 400 page dropped password_requirements"
        )

    def test_blocked_while_research_active(self, authed_app):
        """Must NOT rekey the DB while the user has research running (the
        rekey can't be deferred and would break/corrupt the running job);
        returns 409 and never calls ``db_manager.change_password``."""
        client = _stamped_client(authed_app)
        with (
            patch(f"{AUTH_MOD}.render_template", _fake_render),
            patch(f"{AUTH_MOD}.db_manager") as mock_db,
            patch(
                f"{GLOBALS_MOD}.get_usernames_with_active_research",
                return_value={USERNAME},
            ),
        ):
            response = client.post(
                "/auth/change-password",
                data={
                    "current_password": "OldPass123",
                    "new_password": "NewStrongP4ss!",
                    "confirm_password": "NewStrongP4ss!",
                },
                follow_redirects=False,
            )

        assert response.status_code == 409, (
            "a password change during active research must be refused with "
            f"409, got {response.status_code}"
        )
        mock_db.change_password.assert_not_called()


# ---------------------------------------------------------------------------
# 6. The rekey's per-user TOCTOU gate, in both directions
# ---------------------------------------------------------------------------
class TestChangePasswordRekeyGate:
    """``change_password`` holds ``user_research_start_gate(username)``
    across BOTH its active-research check and the rekey itself.

    Two properties, and they pull against each other — which is why both
    have to be pinned:
      * the gate must be held long enough that a research start racing the
        rekey cannot register in the window (safety);
      * the gate must be PER-USER, not the process-global ``_lock`` that
        every progress/log update takes, or a multi-second SQLCipher
        ``PRAGMA rekey`` freezes every other user's research (availability).
    """

    #: Post-rekey cleanup steps that would touch real sockets / credential
    #: stores. Stubbed so the tests observe only the gate's ordering.
    _CLEANUP_PATCHES = (
        f"{AUTH_MOD}.session_manager",
        f"{AUTH_MOD}._disconnect_user_sockets",
        "local_deep_research.database.session_passwords.session_password_store",
        "local_deep_research.database.thread_local_session."
        "clear_user_credentials",
    )

    @classmethod
    @contextlib.contextmanager
    def _rekey_harness(cls, fake_change_password):
        """Patch the route down to just ``db_manager.change_password``."""
        with contextlib.ExitStack() as stack:
            stack.enter_context(
                patch(f"{AUTH_MOD}.render_template", _fake_render)
            )
            mock_db = stack.enter_context(patch(f"{AUTH_MOD}.db_manager"))
            mock_db.change_password.side_effect = fake_change_password
            for target in cls._CLEANUP_PATCHES:
                stack.enter_context(patch(target))
            yield mock_db

    @staticmethod
    def _research_data(owner):
        return {
            "thread": threading.Thread(target=lambda: None),
            "progress": 0,
            "status": "IN_PROGRESS",
            "log": [],
            "settings": {"username": owner},
        }

    def test_rekey_holds_user_gate_against_new_research(self, authed_app):
        """A research thread must not be able to register itself while the
        request sits between its "is research active?" check and the rekey
        completing.

        ``db_manager.change_password`` is faked to take a moment (as the
        real close -> open -> PRAGMA rekey -> close sequence would), and a
        background thread races to call the REAL ``check_and_start_research``
        for this user while that is in flight. Both acquire the same
        per-user gate, so the racer can only finish registering AFTER the
        fake rekey returns. Drop the gate from the route and the racer
        registers immediately, well before the (slow) rekey — exactly the
        corruption-risking window this guards.
        """
        from local_deep_research.web import research_state

        research_id = "toctou-regression-research-id"
        times = {}
        rekey_started = threading.Event()

        def fake_change_password(username, old_password, new_password):
            rekey_started.set()
            # Brief — just enough to give the racer a real chance. The
            # assertion is ordering-based (enforced by the gate, not by
            # this duration), so it does not need to be long.
            time.sleep(0.05)
            times["rekey_about_to_return"] = time.monotonic()
            return True

        def attempt_start_research():
            assert rekey_started.wait(timeout=5), (
                "fake change_password never started"
            )
            times["racer_started"] = research_state.check_and_start_research(
                research_id, self._research_data(USERNAME)
            )
            times["racer_registered"] = time.monotonic()

        client = _stamped_client(authed_app)
        try:
            with self._rekey_harness(fake_change_password):
                racer = threading.Thread(target=attempt_start_research)
                racer.start()
                response = client.post(
                    "/auth/change-password",
                    data={
                        "current_password": "OldPass123",
                        "new_password": "NewStrongP4ss!",
                        "confirm_password": "NewStrongP4ss!",
                    },
                    follow_redirects=False,
                )
                racer.join(timeout=5)
        finally:
            # Never leave the racer's registration behind for other tests.
            research_state.remove_active_research(research_id)

        assert response.status_code == 302
        assert not racer.is_alive()
        assert times.get("racer_started") is True
        assert times["racer_registered"] >= times["rekey_about_to_return"], (
            "a research start registered DURING the rekey; the per-user "
            "gate is no longer held across the check and the rekey"
        )

    def test_rekey_does_not_block_other_users_new_research(self, authed_app):
        """The gate must be per-user, not process-global.

        A background thread registers research for a DIFFERENT user while
        the acting user's rekey is deliberately blocked, and the rekey is
        wired to return only AFTER that registration happens. Under a
        process-global lock the other user's ``check_and_start_research``
        would block behind the rekey that is waiting for it — a deadlock —
        and the wait would time out.
        """
        from local_deep_research.web import research_state

        research_id = "other-user-research-id"
        rekey_started = threading.Event()
        allow_rekey_return = threading.Event()
        times = {}

        def fake_change_password(username, old_password, new_password):
            rekey_started.set()
            assert allow_rekey_return.wait(timeout=5), (
                "other user's research never registered during the rekey; "
                "the gate is process-global, not per-user"
            )
            times["rekey_returned"] = time.monotonic()
            return True

        def other_user_starts_research():
            assert rekey_started.wait(timeout=5), (
                "fake change_password never started"
            )
            times["other_started"] = research_state.check_and_start_research(
                research_id, self._research_data("otheruser")
            )
            times["other_registered"] = time.monotonic()
            allow_rekey_return.set()

        client = _stamped_client(authed_app)
        try:
            with self._rekey_harness(fake_change_password):
                racer = threading.Thread(target=other_user_starts_research)
                racer.start()
                response = client.post(
                    "/auth/change-password",
                    data={
                        "current_password": "OldPass123",
                        "new_password": "NewStrongP4ss!",
                        "confirm_password": "NewStrongP4ss!",
                    },
                    follow_redirects=False,
                )
                racer.join(timeout=5)
        finally:
            research_state.remove_active_research(research_id)

        assert response.status_code == 302
        assert not racer.is_alive()
        assert times.get("other_started") is True
        assert times["other_registered"] <= times["rekey_returned"], (
            "the other user's research only registered after the rekey "
            "finished; the rekey is holding a process-wide lock"
        )


# ---------------------------------------------------------------------------
# 7. GET /auth/integrity-check response shape
# ---------------------------------------------------------------------------
class TestIntegrityCheck:
    @pytest.mark.parametrize(
        ("is_valid", "expected"), [(True, "valid"), (False, "corrupted")]
    )
    def test_returns_integrity_status(self, authed_app, is_valid, expected):
        client = _stamped_client(authed_app)
        with patch(f"{AUTH_MOD}.db_manager") as mock_db:
            mock_db.check_database_integrity.return_value = is_valid
            response = client.get(
                "/auth/integrity-check", follow_redirects=False
            )

        assert response.status_code == 200
        body = response.json()
        assert body["username"] == USERNAME
        assert body["integrity"] == expected
        mock_db.check_database_integrity.assert_called_once_with(USERNAME)