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.

7.7 KB
Raw Blame
"""``ensure_user_database``'s password-source fallback chain.

Ported from ``tests/web/auth/test_database_middleware.py`` on main, which
tested the Flask ``before_request`` handler
``web/auth/database_middleware.py::ensure_user_database``. That module was
deleted by the FastAPI migration; the successor is
``web/dependencies/auth.py::ensure_user_database``, driven by
``DatabaseMiddleware`` in ``web/fastapi_app.py`` rather than by Flask.

Its three password sources are unchanged in intent:

1. the one-time ``temp_auth_token`` minted at login/registration,
2. the persistent ``session_password_store`` entry keyed by ``session_id``,
3. the ``"dummy"`` placeholder for unencrypted deployments.

Source 1 (and its ``stored_username == username`` binding) is already pinned
by ``tests/security/test_auth_credential_lifetime_fastapi.py`` and
``tests/web/test_ensure_user_database_token_ordering.py``. Sources 2 and 3,
the no-username early return, the already-connected fast path and the two
failure paths of ``open_user_database`` had no successor, so they are ported
here.

The Flask-only assertions of the original are dropped, and why:

* ``test_skips_when_middleware_should_skip`` — ``should_skip_database_
  middleware()`` was a ``before_request`` throttle (``web/auth/
  middleware_optimizer.py``, deleted). Its successor is
  ``DatabaseMiddleware._skip_prefixes``, which gates the CALL rather than
  living inside it; pinned in ``tests/web/test_database_middleware_skip_
  prefixes.py``.
* ``test_skips_when_already_initialized``, ``test_sets_db_initialized_flag``,
  ``test_sets_g_username`` — all assert Flask ``g`` attributes
  (``g._db_initialized``, ``g.user_password``, ``g.username``). There is no
  ``g`` under FastAPI and the successor sets no equivalent request-scoped
  state, so they have no meaning to port.
"""

import types
from unittest.mock import MagicMock

import pytest

from local_deep_research.web.dependencies import auth as auth_dep


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

    Mirrors ``DatabaseMiddleware``'s own inline ``_MinimalRequest`` shim,
    which is how production calls this function.
    """
    return types.SimpleNamespace(session=session)


@pytest.fixture
def db_manager(monkeypatch):
    """A mock ``db_manager`` in place of the module-level singleton."""
    mock = MagicMock()
    mock.has_encryption = False
    mock.is_user_connected.return_value = False
    mock.open_user_database.return_value = MagicMock()
    monkeypatch.setattr(auth_dep, "db_manager", mock)
    return mock


@pytest.fixture
def password_store(monkeypatch):
    """A mock ``session_password_store`` holding no password by default."""
    mock = MagicMock()
    mock.get_session_password.return_value = None
    monkeypatch.setattr(auth_dep, "session_password_store", mock)
    return mock


def test_skips_when_no_username(db_manager, password_store):
    """No username in the session means there is nothing to open."""
    session = {}

    assert auth_dep.ensure_user_database(_fake_request(session)) is None

    db_manager.is_user_connected.assert_not_called()
    db_manager.open_user_database.assert_not_called()
    password_store.get_session_password.assert_not_called()


def test_retrieves_password_from_session_password_store(
    db_manager, password_store
):
    """Source 2: the store is consulted with this session's own session_id.

    Passing the request's ``session_id`` rather than widening to "any
    session for this user" is what keeps one session's credential out of
    another's request.
    """
    db_manager.has_encryption = True
    password_store.get_session_password.return_value = "stored_password"

    session = {"username": "testuser", "session_id": "session_456"}
    auth_dep.ensure_user_database(_fake_request(session))

    password_store.get_session_password.assert_called_with(
        "testuser", "session_456"
    )
    db_manager.open_user_database.assert_called_with(
        "testuser", "stored_password"
    )


def test_uses_dummy_password_for_unencrypted_db(db_manager, password_store):
    """Source 3: an unencrypted database opens with the placeholder key."""
    db_manager.has_encryption = False

    session = {"username": "testuser"}
    auth_dep.ensure_user_database(_fake_request(session))

    db_manager.open_user_database.assert_called_with("testuser", "dummy")


def test_no_dummy_password_when_database_is_encrypted(
    db_manager, password_store
):
    """Negative control for the source-3 fallback.

    Without this, ``test_uses_dummy_password_for_unencrypted_db`` would still
    pass if the ``not db_manager.has_encryption`` guard were dropped and
    "dummy" handed to SQLCipher for every user.
    """
    db_manager.has_encryption = True

    session = {"username": "testuser", "session_id": "session_456"}
    auth_dep.ensure_user_database(_fake_request(session))

    db_manager.open_user_database.assert_not_called()


def test_skips_open_when_already_connected(db_manager, password_store):
    """The fast path: an open connection needs no password resolution."""
    db_manager.is_user_connected.return_value = True

    session = {"username": "testuser"}
    auth_dep.ensure_user_database(_fake_request(session))

    db_manager.open_user_database.assert_not_called()


def test_returns_when_open_user_database_returns_none(
    db_manager, password_store
):
    """A failed open is logged, not raised — the request still proceeds and
    the auth gate rejects it downstream."""
    db_manager.open_user_database.return_value = None

    session = {"username": "testuser"}
    auth_dep.ensure_user_database(_fake_request(session))

    db_manager.open_user_database.assert_called_once()


def test_handles_exception_gracefully(db_manager, password_store):
    """A raising ``db_manager`` must not take the request down with it.

    Main wrapped the whole ``is_user_connected`` / ``open_user_database``
    block in ``try/except Exception``, so a database-manager fault degraded
    to "no connection opened" and the auth gate returned 401. The port
    narrowed the ``try`` to ``open_user_database`` alone, leaving both
    ``is_user_connected`` and the temp-auth-token block unguarded; the guard
    now covers the whole body again.

    ``ensure_user_database`` runs from ``DatabaseMiddleware``, before any
    route, so an escaping exception 500s EVERY authenticated request for as
    long as the fault lasts — a 401 telling the client to log in again is
    the far better degradation.

    Correction to an earlier version of this docstring (and to issue #5971,
    which quoted it): the claim that the app's catch-all "is never reached",
    yielding a bare 500 with an empty body and no logged traceback, is
    WRONG. ``fastapi_app.py`` registers its catch-all for the bare
    ``Exception`` class, and Starlette wires such a handler into
    ``ServerErrorMiddleware`` — installed by ``build_middleware_stack``
    itself, OUTSIDE every ``add_middleware`` layer and therefore outside
    ``DatabaseMiddleware``, not inside ``ExceptionMiddleware``. Re-measured
    against the real stack: the handler runs, logs "Unhandled exception:
    GET /" with a full traceback, and returns ``{"error": "Server error"}``
    with the security headers. (The original "no traceback" reading is what
    you get from a log sink installed without
    ``logger.enable("local_deep_research")`` — the package disables its own
    loguru namespace in ``__init__.py``.) The narrowing is a real
    regression either way; only its consequence was overstated.

    Fix by widening the ``try`` back to cover the whole body, not by
    weakening this test.
    """
    db_manager.is_user_connected.side_effect = Exception("DB error")

    session = {"username": "testuser"}
    auth_dep.ensure_user_database(_fake_request(session))