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.

10.4 KB
Raw Blame
"""The login route's post-login background dispatch.

Ported from ``tests/web/auth/test_auth_routes.py`` on ``origin/main``
(classes ``TestPostLoginWorkerDispatch`` and
``TestPostLoginTasksPasswordForwarding``, added by #5217), which the
FastAPI migration deleted along with the rest of that Flask file.

Why this file has to exist separately from the rest of the router
tests: the root test policy stubs
``web.routers.auth._perform_post_login_tasks`` by default so that real
background work never races a test's DB teardown. That is the right
default for ordinary request tests — and it is also exactly what can
blind the tree to ``web/routers/auth.py``'s dispatch block. With only
the default stub in place, deleting the four dispatch lines from the
login handler leaves the rest of the suite green. #5217 added the Flask
compensating control for precisely this reason; these are its FastAPI
successors. The direct wrapper test below receives the captured production
callable without restoring the global; the route tests replace the stub with a
recorder for only as long as their own request is in flight.

The tests below deliberately re-monkeypatch the same attribute (after
the fixture, so this wins) with a *recording* stub, which restores the
observability the no-op removed without reintroducing the teardown
race: the recorder touches no database.
"""

import threading
import uuid
from contextlib import suppress
from unittest.mock import MagicMock, patch

import pytest
from fastapi.testclient import TestClient

TEST_PASSWORD = "PostLoginPass123"  # noqa: S105


@pytest.fixture(autouse=True)
def _slowapi_off():
    """Disable the per-IP HTTP limiter; these tests are not about it."""
    from local_deep_research.web.dependencies.rate_limit import limiter

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


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


def _set_unique_peer(client: TestClient) -> None:
    client.headers.update(
        {
            "X-Forwarded-For": (
                f"10.{uuid.uuid4().int % 254 + 1}."
                f"{uuid.uuid4().int % 254 + 1}.12"
            )
        }
    )


def _csrf(client: TestClient) -> str:
    client.get("/auth/login")
    return client.get("/auth/csrf-token").json()["csrf_token"]


def _register(client: TestClient, username: str, password: str):
    return client.post(
        "/auth/register",
        data={
            "username": username,
            "password": password,
            "confirm_password": password,
            "acknowledge": "true",
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )


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


def _cleanup_client(client, username):
    try:
        with suppress(Exception):
            client.post("/auth/logout", follow_redirects=False)
        with suppress(Exception):
            from local_deep_research.web.auth.session_manager import (
                session_manager,
            )

            session_manager.destroy_all_user_sessions(username)
        with suppress(Exception):
            from local_deep_research.database.session_passwords import (
                session_password_store,
            )

            session_password_store.clear_all_for_user(username)
        with suppress(Exception):
            from local_deep_research.database.thread_local_session import (
                clear_user_credentials,
            )

            clear_user_credentials(username)
    finally:
        client.close()


class TestPostLoginWorkerDispatch:
    """A successful login must hand the credentials to a background worker.

    Flask original asserted the exact ``threading.Thread(target=...,
    args=(app_ctx, username, password), daemon=True)`` construction plus
    ``thread.start()``. The FastAPI handler builds the worker differently
    (``contextvars.copy_context()`` + a zero-arg closure), so the
    construction details are not portable — but the observable property
    they existed to pin is, and it is the stronger assertion anyway:
    ``_perform_post_login_tasks`` is actually *called*, off the request
    thread, on a daemon thread, with this login's credentials.
    """

    def test_successful_login_starts_post_login_worker(
        self, app, monkeypatch, request
    ):
        import local_deep_research.web.routers.auth as auth_mod

        username = f"postlogin_{uuid.uuid4().hex[:10]}"
        seen = {}
        ran = threading.Event()

        def _record(recorded_username, password, session_id):
            # A login thread starved in an earlier test may only resolve this
            # global after our recorder is installed. Ignore foreign users so
            # it cannot satisfy or overwrite this test's signal.
            if recorded_username != username:
                return
            seen["args"] = (recorded_username, password, session_id)
            seen["off_request_thread"] = (
                threading.current_thread() is not threading.main_thread()
            )
            seen["daemon"] = threading.current_thread().daemon
            ran.set()

        # Records the dispatch without running the database-heavy worker body.
        monkeypatch.setattr(auth_mod, "_perform_post_login_tasks", _record)

        client = _client(app)
        request.addfinalizer(lambda: _cleanup_client(client, username))
        _set_unique_peer(client)
        assert _register(client, username, TEST_PASSWORD).status_code == 302

        response = _login(client, username, TEST_PASSWORD)
        assert response.status_code == 302

        assert ran.wait(15), (
            "login returned 302 but never dispatched the post-login worker"
        )
        got_user, got_password, got_session_id = seen["args"]
        assert got_user == username
        assert got_password == TEST_PASSWORD
        # The worker needs the live session id to reach the session
        # password store / queue processor for this login.
        assert isinstance(got_session_id, str) and got_session_id
        assert seen["off_request_thread"], (
            "post-login work ran inline on the request thread; the redirect "
            "no longer returns immediately"
        )
        assert seen["daemon"] is True, (
            "post-login worker thread is not a daemon; it would block "
            "interpreter shutdown"
        )

    def test_failed_login_starts_no_post_login_worker(
        self, app, monkeypatch, request
    ):
        """A rejected login must not run post-login work.

        The Flask file pinned dispatch only on the success path; the
        negative is the half that keeps a wrong-password POST from
        warming caches (and from being an oracle). Nothing on this
        branch asserted it.
        """
        import local_deep_research.web.routers.auth as auth_mod

        username = f"postlogin_{uuid.uuid4().hex[:10]}"
        ran = threading.Event()

        def _record(recorded_username, password, session_id):
            if recorded_username == username:
                ran.set()

        monkeypatch.setattr(auth_mod, "_perform_post_login_tasks", _record)

        client = _client(app)
        request.addfinalizer(lambda: _cleanup_client(client, username))
        _set_unique_peer(client)
        assert _register(client, username, TEST_PASSWORD).status_code == 302

        response = _login(client, username, "WrongPassword456")
        assert response.status_code == 401
        assert not ran.wait(2), (
            "a failed login dispatched the post-login worker"
        )


class TestPostLoginTasksPasswordForwarding:
    """``_perform_post_login_tasks`` must forward the password.

    Ported verbatim in intent from main. The user database is SQLCipher
    encrypted, so every ``get_user_db_session`` call inside the worker
    has to carry the plaintext password; dropping it turns each step
    into a silent, logged-and-swallowed failure (every step of the body
    is wrapped in its own ``except``), so nothing else would go red.
    The FastAPI signature gained a ``session_id`` parameter.
    """

    def test_post_login_tasks_forwards_password(self, real_post_login_tasks):
        with (
            patch(
                "local_deep_research.settings.manager.SettingsManager"
            ) as mock_settings_cls,
            patch(
                "local_deep_research.database.library_init.initialize_library_for_user"
            ) as mock_init_lib,
            patch(
                "local_deep_research.web.routers.auth.auth_db_session"
            ) as mock_auth_db,
            patch(
                "local_deep_research.scheduler.background.get_background_job_scheduler"
            ),
            patch("local_deep_research.database.models.ProviderModel"),
            patch(
                "local_deep_research.database.session_context.get_user_db_session"
            ) as mock_get_session,
        ):
            mock_settings_mgr = MagicMock()
            mock_settings_mgr.db_version_matches_package.return_value = True
            mock_settings_cls.return_value = mock_settings_mgr
            mock_init_lib.return_value = {"success": True}
            mock_auth_db.return_value.__enter__ = MagicMock(
                return_value=MagicMock()
            )
            mock_auth_db.return_value.__exit__ = MagicMock(return_value=False)

            mock_session = MagicMock()
            mock_get_session.return_value.__enter__ = MagicMock(
                return_value=mock_session
            )
            mock_get_session.return_value.__exit__ = MagicMock(
                return_value=False
            )

            # Call directly (synchronous — @thread_cleanup is transparent).
            real_post_login_tasks.__wrapped__("testuser", "testpass", "sess-1")

            calls = mock_get_session.call_args_list
            password_calls = [
                c
                for c in calls
                if c.args == ("testuser", "testpass")
                or c.kwargs.get("password") == "testpass"
            ]
            assert len(password_calls) >= 2, (
                f"Expected get_user_db_session called with password at least "
                f"twice, got {len(password_calls)}: {calls}"
            )