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.

6.8 KB
Raw Blame
"""``DatabaseMiddleware`` must not open a database for requests that cannot
need one.

Ported from ``TestShouldSkipDatabaseMiddleware`` in
``tests/web/auth/test_middleware.py`` and
``tests/web/auth/test_middleware_optimizer.py`` on main, which tested
``web/auth/middleware_optimizer.py::should_skip_database_middleware()`` —
the predicate Flask's ``ensure_user_database`` before_request handler
consulted first. Both the module and the function were deleted by the
FastAPI migration. The successor is ``DatabaseMiddleware._skip_prefixes``
in ``web/fastapi_app.py``, which gates the CALL from the outside instead of
returning early from inside it.

Main pinned the predicate's return value; there is nothing equivalent to
call here, so this pins the observable consequence instead: whether
``ensure_user_database`` runs at all for a given path. That is the property
the skip list exists for. ``ensure_user_database`` opens a SQLCipher
connection — PBKDF2 key derivation plus file I/O, offloaded to a thread —
so losing an entry means every static asset on a page load pays for a key
derivation. Nothing on the branch asserted the list's contents or its
effect.

Translated, not dropped: main skipped ``/socket.io/`` (Flask-SocketIO's
mount point); the ASGI server mounts Socket.IO under ``/ws`` instead
(``web/services/socketio_asgi.py``), and ``_skip_prefixes`` carries
``"/ws/"``. Same entry, new path.

Two of main's entries have NO counterpart on this branch and are
deliberately not asserted here, because each is a throttle choice the
migration appears to have made on purpose rather than a behaviour a caller
can observe:

* ``/robots.txt`` — the branch serves no such route, so the request 404s.
* ``/auth/logout`` — logout closes the user's connection, so running the
  opener first is at worst wasted work; skipping it was arguably the odder
  choice.
They are reported as divergences rather than pinned as failures.
"""

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient

from local_deep_research.web.fastapi_app import DatabaseMiddleware


class _SessionInjector:
    """Stands in for ``SessionMiddleware``, which needs a signed cookie.

    ``DatabaseMiddleware``'s entire contract with the layer above it is
    ``scope["session"]`` being a non-empty dict, so populating it directly
    exercises the real middleware without a login round trip. Ordering
    matches production: session outside, database inside.

    A non-empty session is essential: ``DatabaseMiddleware`` also skips the
    call when the session is empty, so an anonymous probe would report
    "skipped" for every path and this file would assert nothing. It must
    also be a LIVE server-side session — ``_enforce_session_revocation``
    runs first and empties the dict for a session id the manager does not
    know, which would silently produce the same false "skipped" result.
    """

    def __init__(self, app, username, session_id):
        self.app = app
        self.username = username
        self.session_id = session_id

    async def __call__(self, scope, receive, send):
        if scope["type"] == "http":
            scope["session"] = {
                "username": self.username,
                "session_id": self.session_id,
            }
        await self.app(scope, receive, send)


@pytest.fixture
def probe(monkeypatch):
    """(client, calls) — every path answers 200 and records whether
    ``ensure_user_database`` was invoked for it."""
    from local_deep_research.web.auth.session_manager import session_manager

    calls = []

    username = "skip_prefix_probe"
    session_id = session_manager.create_session(username)

    monkeypatch.setattr(
        "local_deep_research.web.dependencies.auth.ensure_user_database",
        lambda request: calls.append(dict(request.session)),
    )

    inner = FastAPI()

    @inner.api_route("/{full_path:path}", methods=["GET", "POST", "OPTIONS"])
    def catch_all(full_path: str):
        return {"ok": True}

    client = TestClient(
        _SessionInjector(DatabaseMiddleware(inner), username, session_id),
        raise_server_exceptions=False,
    )
    try:
        yield client, calls
    finally:
        session_manager.destroy_session(session_id)


@pytest.mark.parametrize(
    "path",
    [
        "/static/js/app.js",
        "/static/css/styles.css",
        "/static/images/logo.png",
        "/static/deeply/nested/path/file.js",
        "/favicon.ico",
        "/api/v1/health",
        # Socket.IO's ASGI mount point (main: "/socket.io/").
        "/ws/socket.io/?EIO=4&transport=polling",
        # Public auth routes: reached before any database exists.
        "/auth/login",
        "/auth/register",
        "/auth/csrf-token",
    ],
)
def test_skipped_paths_do_not_open_a_database(probe, path):
    client, calls = probe

    assert client.get(path, follow_redirects=False).status_code == 200
    assert calls == [], (
        f"{path} ran ensure_user_database — a SQLCipher open (PBKDF2 key "
        "derivation + file I/O) on a request that cannot need the database"
    )


@pytest.mark.parametrize(
    "path",
    [
        "/api/research/start",
        "/settings/",
        "/",
        "/dashboard",
        "/news/subscriptions",
    ],
)
def test_normal_paths_do_open_a_database(probe, path):
    """The negative control.

    Without it, every assertion above would pass for a ``_skip_prefixes``
    that matched everything — or for a middleware that had stopped calling
    ``ensure_user_database`` at all.
    """
    client, calls = probe

    assert client.get(path, follow_redirects=False).status_code == 200
    assert len(calls) == 1, (
        f"{path} did not run ensure_user_database, so an authenticated "
        "request would reach its route with no open database"
    )


def test_post_to_a_normal_path_also_opens(probe):
    """Main's predicate was method-sensitive only for OPTIONS; every other
    method on a non-skipped path must still open."""
    client, calls = probe

    assert client.post("/api/research/start").status_code == 200
    assert len(calls) == 1


def test_skip_applies_to_static_regardless_of_method(probe):
    client, calls = probe

    assert client.post("/static/js/app.js").status_code == 200
    assert calls == []


@pytest.mark.parametrize(
    "path",
    [
        "/settings/",
        "/api/research/start",
        "/news/subscriptions",
    ],
)
def test_options_never_opens_an_authenticated_database(probe, path):
    """CORS probes must not pay for SQLCipher/PBKDF2 initialization.

    The Flask predecessor skipped every OPTIONS request before consulting
    its path rules. Preserve that method-level boundary even when a browser
    includes a valid session cookie and CORS middleware does not short-circuit
    the request itself.
    """
    client, calls = probe

    assert client.options(path).status_code == 200
    assert calls == []