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.

16.4 KB
Raw Blame
"""
Tests verifying that the FastAPI ``require_auth`` dependency rejects
unauthenticated requests correctly across the URL shapes the app actually
exposes.

Guards two things:

* Page routes (e.g. ``/news/``, ``/news/subscriptions``) redirect
  unauthenticated callers to the login page.
* API routes — including nested paths like ``/news/api/...`` —
  return a JSON ``401`` instead of an HTML redirect. This is the
  regression case that motivated ``_is_api_request`` matching ``/api/``
  anywhere in the path, not only as a top-level prefix.

Ported from the pre-FastAPI Flask ``@login_required`` test. The decorator
was replaced by the ``require_auth`` dependency
(``local_deep_research.web.dependencies.auth``); the JSON-vs-redirect
negotiation moved into the global HTTPException handler registered by
``local_deep_research.web.fastapi_app._register_exception_handlers``
(``_is_api_request`` is the successor of the old ``_is_api_path``). We wire
both REAL pieces here against synthetic routes that mirror production URL
shapes, so a regression in either the dependency or the handler surfaces.

The real news/research routers pull in heavy optional dependencies
(langchain, pdfplumber, etc.) and are exercised elsewhere. Here we only
care about the auth gate's behavior at each URL shape.

Note: the FastAPI/Starlette HTTPException JSON body uses the ``detail`` key
(``{"detail": "Authentication required"}``), not Flask's ``{"error": ...}``.
"""

from unittest.mock import patch

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

from local_deep_research.web.dependencies.auth import require_auth
from local_deep_research.web.fastapi_app import _register_exception_handlers


@pytest.fixture
def app():
    app = FastAPI()
    # SessionMiddleware backs request.session, which require_auth reads.
    app.add_middleware(SessionMiddleware, secret_key="test-secret-key")
    # Register the REAL exception handlers so the JSON-401-vs-login-redirect
    # negotiation under test is production behaviour, not a re-impl.
    _register_exception_handlers(app)

    # Helper login route: seeds the session the way the real /auth/login
    # handler does after verifying credentials — BOTH the username and a real
    # server-side session id (routers/auth.py stores them on adjacent lines).
    # Used by the authenticated_client fixture so the happy path is exercised
    # end-to-end against the real require_auth dependency.
    #
    # The session id is load-bearing: require_auth validates it against
    # session_manager so that logout/password-change actually revoke access.
    # Seeding only the username would authenticate a session the server has no
    # record of — exactly what that check exists to reject.
    @app.post("/_test_login")
    def _test_login(request: Request):
        from local_deep_research.web.auth.session_manager import (
            session_manager,
        )

        request.session["username"] = "test-user"
        request.session["session_id"] = session_manager.create_session(
            "test-user"
        )
        return {"ok": True}

    # Mirror production: news pages at /news with a nested news API at
    # /news/api/..., the same nesting that broke JSON-vs-HTML detection
    # before _is_api_request() used a substring match.
    @app.get("/news/")
    def news_page(user: str = Depends(require_auth)):
        return JSONResponse("News")

    @app.get("/news/subscriptions")
    def subscriptions_page(user: str = Depends(require_auth)):
        return JSONResponse("Subscriptions")

    @app.get("/news/subscriptions/new")
    def new_subscription_page(user: str = Depends(require_auth)):
        return JSONResponse("New")

    @app.get("/news/subscriptions/{sid}/edit")
    def edit_subscription_page(sid: str, user: str = Depends(require_auth)):
        return JSONResponse(f"Edit {sid}")

    @app.get("/news/health")
    def news_health():
        return {"status": "ok"}

    @app.get("/news/api/categories")
    def get_categories(user: str = Depends(require_auth)):
        return {"categories": []}

    @app.post("/news/api/subscribe")
    def subscribe(user: str = Depends(require_auth)):
        return {"ok": True}

    # research router: /api/config/limits is a top-level API path.
    @app.get("/api/config/limits")
    def get_upload_limits(user: str = Depends(require_auth)):
        return {"limit": 0}

    return app


@pytest.fixture
def client(app):
    # raise_server_exceptions=False so the registered exception handlers run
    # instead of TestClient re-raising.
    return TestClient(app, raise_server_exceptions=False)


@pytest.fixture(autouse=True)
def patch_db_manager():
    """Stub the auth db_manager so we don't touch a real database.

    Default is_user_connected=False so the unauthenticated-rejection
    tests don't need to opt in. Tests that want to exercise the
    authenticated 200 path use the `authenticated_client` fixture
    below, which flips this to True and seeds the session.
    """
    with patch(
        "local_deep_research.web.dependencies.auth.db_manager"
    ) as mock_dm:
        mock_dm.is_user_connected.return_value = False
        yield mock_dm


@pytest.fixture
def authenticated_client(client, patch_db_manager):
    """Test client that passes all three checks in `require_auth`: a
    `username` in the session, `db_manager.is_user_connected` True, AND a
    `session_id` the server-side session store still recognises.

    Without this, every test in the file is one-sided (rejection-only)
    and a regression that breaks the *allow* path of the gate — e.g. the
    post-rejection branch swallowing valid requests — would not be caught
    by any existing test.
    """
    patch_db_manager.is_user_connected.return_value = True
    # Seed the session via the helper login route (sets the signed cookie
    # on the client's cookie jar, the same as a real login).
    resp = client.post("/_test_login")
    assert resp.status_code == 200
    return client


class TestNewsPageRoutesRequireAuth:
    """The four news page routes added in PR #3129 must redirect to login
    when the caller has no session."""

    @pytest.mark.parametrize(
        "path",
        [
            "/news/",
            "/news/subscriptions",
            "/news/subscriptions/new",
            "/news/subscriptions/abc-123/edit",
        ],
    )
    def test_unauthenticated_page_redirects_to_login(self, client, path):
        response = client.get(path, follow_redirects=False)
        assert response.status_code == 302
        assert "/auth/login" in response.headers["location"]


class TestNewsApiRoutesRequireAuth:
    """/news/api/categories must return JSON 401, not an HTML redirect.
    This is the regression case for nested-path API detection."""

    def test_unauthenticated_categories_returns_json_401(self, client):
        response = client.get("/news/api/categories", follow_redirects=False)
        assert response.status_code == 401
        assert "application/json" in response.headers["content-type"]
        assert response.json()["detail"] == "Authentication required"
        # Must not be an HTML redirect to /auth/login.
        location = response.headers.get("location") or ""
        assert "/auth/login" not in location


class TestResearchApiRoutesRequireAuth:
    """/api/config/limits added in PR #3129 must return JSON 401 for
    unauthenticated callers."""

    def test_unauthenticated_upload_limits_returns_json_401(self, client):
        response = client.get("/api/config/limits", follow_redirects=False)
        assert response.status_code == 401
        assert "application/json" in response.headers["content-type"]
        assert response.json()["detail"] == "Authentication required"


class TestNewsApiNonGetMethodsRequireAuth:
    """The dependency runs before the route, so it should reject POST/PUT/
    DELETE the same way as GET. Probe one non-GET API endpoint to lock
    that in."""

    def test_unauthenticated_post_returns_json_401(self, client):
        response = client.post(
            "/news/api/subscribe", json={}, follow_redirects=False
        )
        assert response.status_code == 401
        assert "application/json" in response.headers["content-type"]
        assert response.json()["detail"] == "Authentication required"


# TestNewsHealthRouteIsPublic was deleted here. It asserted /news/health
# returns 200 unauthenticated, but this migration moved that route BEHIND auth
# (see changelog.d/3299.breaking.md), so the class documented the opposite of
# shipped behaviour while claiming to "lock that contract in".
#
# It could not have caught the change either way: the `client` fixture above
# builds a synthetic app whose /news/health stub carries no auth dependency, so
# the assertion held regardless of the real router. The live contract is pinned
# where it can actually fail -- tests/web/test_route_table_parity.py maps
# ("GET", "/news/health") to [401], and tests/web/routers/
# test_fastapi_migration.py exercises it against the real app.


class TestAuthenticatedRequestsArePassedThrough:
    """Happy-path coverage. Without these tests, the entire file only
    verifies that the gate REJECTS — a regression where it stopped
    ALLOWING valid requests would slip through. Mirrors each rejection
    test class with a positive case at the same URL shape.
    """

    @pytest.mark.parametrize(
        "path",
        [
            "/news/",
            "/news/subscriptions",
            "/news/subscriptions/new",
            "/news/subscriptions/abc-123/edit",
        ],
    )
    def test_authenticated_page_returns_200(self, authenticated_client, path):
        response = authenticated_client.get(path)
        assert response.status_code == 200

    def test_authenticated_news_api_categories_returns_200(
        self, authenticated_client
    ):
        """Counterpart to test_unauthenticated_categories_returns_json_401."""
        response = authenticated_client.get("/news/api/categories")
        assert response.status_code == 200
        assert "application/json" in response.headers["content-type"]
        assert response.json() == {"categories": []}

    def test_authenticated_research_api_limits_returns_200(
        self, authenticated_client
    ):
        """Counterpart to test_unauthenticated_upload_limits_returns_json_401."""
        response = authenticated_client.get("/api/config/limits")
        assert response.status_code == 200
        assert "application/json" in response.headers["content-type"]
        assert response.json() == {"limit": 0}

    def test_authenticated_post_returns_200(self, authenticated_client):
        """Counterpart to test_unauthenticated_post_returns_json_401."""
        response = authenticated_client.post("/news/api/subscribe", json={})
        assert response.status_code == 200
        assert "application/json" in response.headers["content-type"]
        assert response.json() == {"ok": True}


class TestAuthenticatedButDisconnectedDb:
    """Second branch of the gate: `username` is in session but
    `db_manager.is_user_connected` is False. This happens when the
    session outlives the encrypted-DB connection (server restart with
    encrypted databases enabled). API paths must get JSON 401 with
    a different error message ("Database connection required") and
    page paths must redirect.
    """

    @pytest.fixture
    def session_without_db(self, client, patch_db_manager):
        # is_user_connected stays False (autouse default), but seed the
        # session so we hit the second branch, not the first.
        patch_db_manager.is_user_connected.return_value = False
        resp = client.post("/_test_login")
        assert resp.status_code == 200
        return client

    def test_api_path_returns_json_401_db_required(self, session_without_db):
        response = session_without_db.get(
            "/news/api/categories", follow_redirects=False
        )
        assert response.status_code == 401
        assert "application/json" in response.headers["content-type"]
        assert response.json()["detail"] == "Database connection required"

    def test_page_path_redirects_to_login(self, session_without_db):
        response = session_without_db.get(
            "/news/subscriptions", follow_redirects=False
        )
        assert response.status_code == 302
        assert "/auth/login" in response.headers["location"]


class TestUnauthorized401HandlerNegotiation:
    """The global HTTPException handler in fastapi_app must use the same
    _is_api_request detection for both the dependency's 401 and any
    explicit raise HTTPException(401), so that a 401 on a nested API path
    returns JSON, not an HTML redirect, while a 401 on a page path
    redirects to login."""

    def _build_app(self):
        from fastapi import FastAPI, HTTPException

        app = FastAPI()
        app.add_middleware(SessionMiddleware, secret_key="test")

        @app.get("/news/api/raises")
        def raises_nested_api():
            raise HTTPException(
                status_code=401, detail="Authentication required"
            )

        @app.get("/dashboard/raises")
        def raises_page():
            raise HTTPException(
                status_code=401, detail="Authentication required"
            )

        _register_exception_handlers(app)
        return app

    def test_raise_401_on_nested_api_returns_json(self):
        app = self._build_app()
        client = TestClient(app, raise_server_exceptions=False)
        response = client.get("/news/api/raises", follow_redirects=False)
        assert response.status_code == 401
        assert "application/json" in response.headers["content-type"]
        assert response.json()["detail"] == "Authentication required"

    def test_raise_401_on_page_redirects(self):
        app = self._build_app()
        client = TestClient(app, raise_server_exceptions=False)
        response = client.get("/dashboard/raises", follow_redirects=False)
        assert response.status_code == 302
        assert "/auth/login" in response.headers["location"]


class TestRealAppAuthBoundaries:
    """Fences against the REAL app's routes losing their auth dependency.

    The synthetic-app tests above verify require_auth's *behavior*; they
    cannot catch a production route that simply dropped the dependency
    (which happened to /api/config/limits in the FastAPI migration).
    """

    def test_config_limits_requires_auth_on_real_app(self):
        from local_deep_research.web.fastapi_app import app

        client = TestClient(app, raise_server_exceptions=False)
        response = client.get("/api/config/limits", follow_redirects=False)
        assert response.status_code == 401, (
            "GET /api/config/limits must require authentication "
            "(regression: the FastAPI migration dropped @login_required)"
        )

    @pytest.mark.parametrize(
        "path",
        [
            "/api/history",
            "/settings/api",
            "/history/api",
            "/metrics/api/metrics",
        ],
    )
    def test_legacy_api_paths_answer_json_401_not_a_login_redirect(self, path):
        """Ported from main's ``tests/auth_tests/test_auth_integration.py``
        (``test_protected_api_endpoints``), deleted by the migration.

        Two of these paths — ``/settings/api`` and ``/history/api`` — carry
        the ``api`` segment at the END, which is the case a naive
        ``"/api/" in path`` check gets wrong. ``_is_api_request``'s
        ``path.endswith("/api")`` arm is unit-tested in
        ``tests/web/test_exception_handler_contract.py``; nothing drove
        these real routes end to end, so a routing change that made one of
        them a page path would flip an XHR caller's 401 into a 302 to the
        login HTML and nothing would notice.

        Asserted with ``follow_redirects=False``: httpx follows redirects by
        default where Flask's client did not, so a regression to a 302 would
        otherwise be observed as the login page's 200.
        """
        from local_deep_research.web.fastapi_app import app

        client = TestClient(app, raise_server_exceptions=False)
        response = client.get(path, follow_redirects=False)

        assert response.status_code == 401, (
            f"GET {path} unauthenticated returned {response.status_code}; "
            "an api-segment path must answer 401, not bounce to login"
        )
        assert "application/json" in response.headers["content-type"]
        assert response.json()["detail"] == "Authentication required"