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.

8.3 KB
Raw Blame
"""GET /auth/check and GET /auth/change-password must honor the same
``db_manager.is_user_connected()`` staleness check as their siblings.

Regression: the Flask->FastAPI migration upgraded ``change_password`` (POST)
and ``integrity_check`` from a manual ``request.session.get("username")``
check to ``Depends(require_auth)``, which additionally verifies
``db_manager.is_user_connected(username)`` -- but left their two GET
siblings, ``check_auth`` and ``change_password_page``, on the old manual
check. A session whose DB connection is gone (``is_user_connected()``
False -- exactly the state ``clear_session_if_unrecoverable()`` exists to
handle) was therefore reported as valid by the two GETs: ``/auth/check``
returned 200 ``{"authenticated": true}`` and ``GET /auth/change-password``
rendered the page (200), while ``POST /auth/change-password`` correctly
302'd a caller in the same state to login.

Fixed in ``src/local_deep_research/web/routers/auth.py``:
``change_password_page`` now depends on ``require_auth`` like its POST
sibling and ``integrity_check``. ``check_auth`` keeps its bespoke raw-JSON
response contract on purpose -- it must never redirect a non-``/api/``,
non-JSON-``Accept`` caller the way ``Depends(require_auth)``'s 401 does
(see ``_is_api_request`` / ``handle_http_exception`` in
``fastapi_app.py``) -- so it calls ``require_auth`` directly and converts
its 401 into the JSON answer.

A fresh client logs in from scratch inside EVERY test (rather than once in
a shared fixture) because ``tests/conftest.py``'s autouse
``cleanup_database_connections`` fixture closes every open user database
before each test function runs -- a connection established by an
outer-scoped fixture would already be closed by the time the test body
executed.

That fixture used to clear ``session_manager``'s server-side sessions too,
which is what an earlier version of this docstring described. It no longer
does: ``require_auth`` now validates the session id against
``session_manager``, so clearing it between tests logged out every
module-scoped client that had legitimately logged in during module setup.
Logging in per test remains correct regardless -- the connection close
alone is enough to require it.
"""

import uuid

import pytest
from fastapi.testclient import TestClient


@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
    default, while this local guard keeps the module's isolation explicit.
    """
    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 with other login/register-heavy tests."""
    return f"10.{uuid.uuid4().int % 254 + 1}.{uuid.uuid4().int % 254 + 1}.3"


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,
    )


@pytest.fixture(scope="module")
def registered_user(app):
    """Register one real user for the whole module; yield (username, pw).

    Registration's own transient DB connection is fine to lose to the
    autouse cleanup between tests -- only the per-test fresh login below
    needs a live connection.
    """
    c = _new_client(app)
    user = f"stale_gate_{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]}"
        )

    token = c.get("/auth/csrf-token").json().get("csrf_token", "")
    c.post(
        "/auth/logout",
        headers={"X-CSRFToken": token},
        follow_redirects=False,
    )
    return user, pw


@pytest.fixture
def fresh_client(app, registered_user):
    """A newly logged-in client, established fresh inside this test's own
    execution window (see module docstring for why that matters here)."""
    user, pw = registered_user
    client = _new_client(app)
    resp = _login(client, user, pw)
    assert resp.status_code == 302, f"Login failed: {resp.status_code}"
    return client


@pytest.fixture
def disconnected_db(monkeypatch):
    """Simulate the exact stale-session state ``clear_session_if_unrecoverable``
    exists to handle: the session cookie is still valid, but the encrypted
    database connection behind it is gone (e.g. evicted after idle timeout
    or a server restart). This is the PoC from the regression report.
    """
    from local_deep_research.database.encrypted_db import (
        UserConnectionStatus,
        db_manager,
    )

    monkeypatch.setattr(db_manager, "is_user_connected", lambda username: False)
    # require_auth asks probe_user_connection to classify a negative answer
    # (a kept engine behind a sidecar fault is a 503, not a 401) and takes
    # its answer. The real probe would still find this test's live engine,
    # so report what the real manager says once the connection is gone.
    monkeypatch.setattr(
        db_manager,
        "probe_user_connection",
        lambda username: UserConnectionStatus.NOT_CONNECTED,
    )


class TestAuthCheckHonorsConnectivity:
    def test_disconnected_session_is_not_reported_authenticated(
        self, fresh_client, disconnected_db
    ):
        resp = fresh_client.get("/auth/check", follow_redirects=False)

        assert resp.status_code == 401, (
            f"stale session reported as authenticated by /auth/check: "
            f"{resp.status_code} {resp.text[:200]}"
        )
        # Must stay raw JSON -- never an HTML redirect, regardless of the
        # caller's Accept header. This is the response-contract caveat:
        # Depends(require_auth)'s 401 gets converted to a 302 login
        # redirect for non-/api/, non-JSON-Accept requests by the global
        # exception handler, which /auth/check (an AJAX polling endpoint)
        # must not do.
        assert "application/json" in resp.headers["content-type"]
        assert "location" not in resp.headers
        assert resp.json().get("authenticated") is False

    def test_connected_session_still_authenticates(self, fresh_client):
        """Counterpart/sanity check: without the fault injected, a live
        session is still reported authenticated by /auth/check."""
        resp = fresh_client.get("/auth/check", follow_redirects=False)
        assert resp.status_code == 200
        body = resp.json()
        assert body.get("authenticated") is True
        assert "username" in body


class TestChangePasswordPageHonorsConnectivity:
    def test_disconnected_session_redirects_to_login(
        self, fresh_client, disconnected_db
    ):
        resp = fresh_client.get("/auth/change-password", follow_redirects=False)
        assert resp.status_code == 302, (
            f"stale session was allowed to view /auth/change-password: "
            f"{resp.status_code}"
        )
        assert "/auth/login" in resp.headers.get("location", "")

    def test_connected_session_still_renders_page(self, fresh_client):
        """Counterpart/sanity check: without the fault injected, a live
        session still gets the change-password page."""
        resp = fresh_client.get("/auth/change-password", follow_redirects=False)
        assert resp.status_code == 200