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.

18.1 KB
Raw Blame
"""
Tests for authenticated user flows.

These tests register a test user, log in, and verify that
authenticated endpoints work correctly under FastAPI.
"""

import pytest
from fastapi.testclient import TestClient


@pytest.fixture(scope="module")
def app():
    """Get the FastAPI app."""
    from local_deep_research.web.fastapi_app import app

    return app


@pytest.fixture(scope="module")
def client(app):
    """Session-scoped client for authenticated tests."""
    return TestClient(app, raise_server_exceptions=False)


@pytest.fixture(scope="module")
def auth_client(client):
    """Client with an authenticated session.

    Registers a test user and logs in, returning a client
    with session cookies that can access protected endpoints.

    Post-Wave-9 the register/login endpoints are no longer CSRF-exempt,
    so we fetch a real session-bound CSRF token before each POST.
    """
    import uuid

    test_user = f"test_fastapi_{uuid.uuid4().hex[:8]}"
    test_pass = "TestPassword123!"  # noqa: S105

    def _fetch_csrf():
        # GET /auth/login stamps the session with a token; the token
        # endpoint then returns it for use in form POSTs.
        client.get("/auth/login")
        r = client.get("/auth/csrf-token")
        return r.json().get("csrf_token", "") if r.status_code == 200 else ""

    # Register
    resp = client.post(
        "/auth/register",
        data={
            "username": test_user,
            "password": test_pass,
            "confirm_password": test_pass,
            "acknowledge": "true",
            "csrf_token": _fetch_csrf(),
        },
        follow_redirects=False,
    )
    # Should redirect to / on success, or 400/500 on failure
    assert resp.status_code in (302, 200, 400), (
        f"Register returned {resp.status_code}"
    )

    if resp.status_code != 302:
        # Registration may fail if user exists — try login instead
        pass

    # Login
    resp = client.post(
        "/auth/login",
        data={
            "username": test_user,
            "password": test_pass,
            "csrf_token": _fetch_csrf(),
        },
        follow_redirects=False,
    )

    if resp.status_code == 302:
        # Success — session cookies are set on the client.
        # Attach CSRF token for any state-changing requests.
        csrf_resp = client.get("/auth/csrf-token")
        if csrf_resp.status_code == 200:
            token = csrf_resp.json().get("csrf_token")
            if token:
                client.headers.update({"X-CSRFToken": token})
        yield client
    else:
        pytest.fail(
            f"Auth bootstrap broken: login returned {resp.status_code} "
            f"(expected 302): {resp.text[:300]}"
        )

    # Cleanup: logout
    client.post("/auth/logout", follow_redirects=False)


# ============================================================================
# Authenticated Page Tests
# ============================================================================


class TestAuthenticatedPages:
    """Test that main pages render for authenticated users."""

    def test_root_page_renders(self, auth_client):
        """Root page renders research form for authenticated users."""
        resp = auth_client.get("/", follow_redirects=True)
        assert resp.status_code == 200

    def test_history_page(self, auth_client):
        """History page is accessible."""
        resp = auth_client.get("/history/")
        assert resp.status_code in (200, 401)

    def test_benchmark_page(self, auth_client):
        """Benchmark page is accessible."""
        resp = auth_client.get("/benchmark/")
        assert resp.status_code in (200, 401)

    def test_news_page(self, auth_client):
        """News page renders."""
        resp = auth_client.get("/news/")
        assert resp.status_code == 200


# ============================================================================
# Settings API Tests
# ============================================================================


class TestSettingsAPI:
    """Test settings API endpoints."""

    def test_get_all_settings(self, auth_client):
        """Get all settings returns JSON."""
        resp = auth_client.get("/settings/api")
        # auth_client is guaranteed authenticated (the fixture pytest.fail()s
        # otherwise), so 401 is a failure, not an acceptable outcome. Tolerating
        # it made this test pass silently whenever auth broke.
        assert resp.status_code == 200, resp.text[:300]
        assert isinstance(resp.json(), (dict, list))

    def test_get_setting_by_key(self, auth_client):
        """Get a specific setting by key."""
        resp = auth_client.get("/settings/api/llm.provider")
        # 401 dropped: the client is authenticated by construction.
        assert resp.status_code in (200, 404), resp.text[:300]

    def test_get_categories(self, auth_client):
        """Get settings categories."""
        resp = auth_client.get("/settings/api/categories")
        assert resp.status_code in (200, 401)

    def test_get_available_models(self, auth_client):
        """Get available LLM models."""
        resp = auth_client.get("/settings/api/available-models")
        assert resp.status_code in (200, 401)

    def test_get_available_search_engines(self, auth_client):
        """Get available search engines."""
        resp = auth_client.get("/settings/api/available-search-engines")
        assert resp.status_code in (200, 401)


# ============================================================================
# Research API Tests
# ============================================================================


class TestResearchAPI:
    """Test research API endpoints."""

    def test_research_settings(self, auth_client):
        """Get research settings (search engines, models config)."""
        resp = auth_client.get("/research/api/settings/current-config")
        assert resp.status_code in (200, 401)

    def test_queue_status(self, auth_client):
        """Get research queue status."""
        resp = auth_client.get("/api/queue/status")
        assert resp.status_code in (200, 401)

    def test_config_limits(self, auth_client):
        """Get upload config limits."""
        resp = auth_client.get("/api/config/limits")
        assert resp.status_code in (200, 401)


# ============================================================================
# History API Tests
# ============================================================================


class TestHistoryAPI:
    """Test history API endpoints."""

    def test_get_history(self, auth_client):
        """Get research history."""
        resp = auth_client.get("/history/api")
        assert resp.status_code == 200, resp.text[:300]
        assert isinstance(resp.json(), (dict, list))


# ============================================================================
# News API Tests
# ============================================================================


class TestNewsAPI:
    """Test news API endpoints."""

    def test_get_feed(self, auth_client):
        """Get news feed."""
        resp = auth_client.get("/news/api/feed")
        assert resp.status_code in (200, 401)

    def test_get_subscriptions(self, auth_client):
        """Get news subscriptions."""
        resp = auth_client.get("/news/api/subscriptions/current")
        assert resp.status_code in (200, 401)

    def test_get_categories(self, auth_client):
        """Get news categories."""
        resp = auth_client.get("/news/api/categories")
        assert resp.status_code in (200, 401, 501)


# ============================================================================
# Library API Tests
# ============================================================================


class TestLibraryAPI:
    """Test library API endpoints."""

    def test_get_stats(self, auth_client):
        """Get library stats."""
        resp = auth_client.get("/library/api/stats")
        assert resp.status_code in (200, 401)

    def test_get_collections(self, auth_client):
        """Get collections list."""
        resp = auth_client.get("/library/api/collections")
        assert resp.status_code in (200, 401)

    def test_get_supported_formats(self, auth_client):
        """Get supported file formats."""
        resp = auth_client.get("/library/api/config/supported-formats")
        assert resp.status_code in (200, 401)


# ============================================================================
# Metrics API Tests
# ============================================================================


class TestMetricsAPI:
    """Test metrics API endpoints."""

    def test_get_metrics(self, auth_client):
        """Get research metrics."""
        resp = auth_client.get("/metrics/api/metrics")
        assert resp.status_code in (200, 401)

    def test_get_cost_analytics(self, auth_client):
        """Get cost analytics."""
        resp = auth_client.get("/metrics/api/cost-analytics")
        assert resp.status_code in (200, 401)

    def test_get_pricing(self, auth_client):
        """Get pricing info."""
        resp = auth_client.get("/metrics/api/pricing")
        assert resp.status_code in (200, 401)


# ============================================================================
# Benchmark API Tests
# ============================================================================


class TestBenchmarkAPI:
    """Test benchmark API endpoints."""

    def test_get_history(self, auth_client):
        """Get benchmark history."""
        resp = auth_client.get("/benchmark/api/history")
        assert resp.status_code in (200, 401)

    def test_get_configs(self, auth_client):
        """Get benchmark configs."""
        resp = auth_client.get("/benchmark/api/configs")
        assert resp.status_code in (200, 401)

    def test_get_running(self, auth_client):
        """Get running benchmarks."""
        resp = auth_client.get("/benchmark/api/running")
        assert resp.status_code in (200, 401)


# ============================================================================
# CSRF Token Tests
# ============================================================================


class TestCSRF:
    """Test CSRF token generation and persistence."""

    def test_csrf_token_generated(self, auth_client):
        """CSRF token endpoint returns a non-empty token."""
        resp = auth_client.get("/auth/csrf-token")
        assert resp.status_code == 200
        data = resp.json()
        assert data.get("csrf_token"), "CSRF token should not be empty"

    def test_csrf_token_in_login_page(self, client):
        """Login page includes a CSRF token value."""
        resp = client.get("/auth/login")
        assert resp.status_code == 200
        # CSRF token may be in a meta tag or hidden input
        assert "csrf" in resp.text.lower()


# ============================================================================
# Auth Check After Login
# ============================================================================


class TestAuthCheck:
    """Test auth state after login."""

    def test_auth_check_authenticated(self, auth_client):
        """Auth check returns authenticated=True after login."""
        resp = auth_client.get("/auth/check")
        # Previously the body checks sat behind `if resp.status_code == 200`
        # with no status assertion, so a 401 (auth entirely broken) passed.
        assert resp.status_code == 200, resp.text[:300]
        data = resp.json()
        assert data["authenticated"] is True
        assert data.get("username"), "authenticated response must name the user"

    def test_integrity_check(self, auth_client):
        """Database integrity check works."""
        resp = auth_client.get("/auth/integrity-check")
        assert resp.status_code == 200, resp.text[:300]
        assert "integrity" in resp.json()


class TestSecurityHeadersOnExceptionResponses:
    """
    Regression: ``handle_http_exception``, ``not_found``, and the
    catch-all 500 handler all build their own ``Response`` / ``JSONResponse``.
    The review (H2) flagged that on some FastAPI versions exception-handler
    responses can bypass user middleware — meaning error pages would ship
    without CSP / X-Frame-Options / Referrer-Policy / Permissions-Policy.

    Pin the expected behaviour: the same security headers that apply to
    200 responses must also apply to 401, 404, and 500 responses.
    """

    REQUIRED_HEADERS = (
        "content-security-policy",
        "x-frame-options",
        "x-content-type-options",
        "referrer-policy",
        "permissions-policy",
    )

    def _assert_headers_present(self, response, label: str):
        missing = [
            h for h in self.REQUIRED_HEADERS if h not in response.headers
        ]
        assert not missing, (
            f"{label} response missing security headers: {missing}. "
            f"Got headers: {list(response.headers.keys())}"
        )

    def test_404_response_has_security_headers(self, app):
        from fastapi.testclient import TestClient

        client = TestClient(app, raise_server_exceptions=False)
        resp = client.get("/this-path-does-not-exist-9876")
        assert resp.status_code == 404
        self._assert_headers_present(resp, "404")

    def test_401_redirect_has_security_headers(self, app):
        from fastapi.testclient import TestClient

        client = TestClient(app, raise_server_exceptions=False)
        # Browser-style 401 → 302 redirect to login; still goes through
        # the exception handler and SecurityHeadersMiddleware.
        resp = client.get(
            "/history",
            headers={"Accept": "text/html"},
            follow_redirects=False,
        )
        assert resp.status_code in (302, 401)
        self._assert_headers_present(resp, "401-redirect")

    def test_api_401_json_has_security_headers(self, app):
        from fastapi.testclient import TestClient

        client = TestClient(app, raise_server_exceptions=False)
        # API-style 401 returns JSON without redirect. /settings/api is an
        # authenticated JSON API endpoint that returns a JSON 401 to an
        # unauthenticated caller (the old /research/api/config was removed in
        # main #4551). The same SecurityHeadersMiddleware must apply.
        resp = client.get("/settings/api")
        assert resp.status_code in (401, 200)
        self._assert_headers_present(resp, "401-json")


class TestUnauthenticatedRedirectNextEncoding:
    """
    Regression: ``handle_http_exception`` builds the login redirect with
    ``?next=<request.url.path>`` for browser 401s. The path was being
    appended raw — characters like ``?`` or ``&`` (from a request whose
    path contained percent-encoded forms) would break the URL. Now it's
    URL-encoded via urllib.parse.quote.
    """

    def test_browser_401_redirect_encodes_next_param(self, app):
        from urllib.parse import quote

        from fastapi.testclient import TestClient

        client = TestClient(app, raise_server_exceptions=False)
        # /history requires auth — a browser-accept GET with no session
        # gets redirected to /auth/login?next=/history.
        resp = client.get(
            "/history",
            headers={"Accept": "text/html"},
            follow_redirects=False,
        )
        assert resp.status_code == 302, (
            f"Expected redirect, got {resp.status_code}"
        )
        location = resp.headers.get("location", "")
        # The path /history has no special chars, but the encoding must
        # still preserve the / in the path (safe='/'), and the value must
        # match what quote() produces — proving we ran it through quote
        # rather than concatenating raw.
        assert location == f"/auth/login?next={quote('/history', safe='/')}", (
            f"Unexpected redirect location: {location!r}"
        )


class TestLoginDatabaseInitFailure:
    """
    Regression test: when ``db_manager.open_user_database`` raises
    ``DatabaseInitializationError`` during /auth/login, the handler must
    respond with 503 and not crash with a 500.

    Pre-fix the handler called ``render_template(name, **kwargs), 503``
    (Flask-style) which raised ``TypeError`` at runtime under FastAPI.
    """

    def test_returns_503_on_db_init_error(self, app, monkeypatch):
        import uuid

        from fastapi.testclient import TestClient

        from local_deep_research.database.encrypted_db import (
            DatabaseInitializationError,
            db_manager,
        )

        # Dedicated TestClient with its own cookie jar — the module-scoped
        # `client` fixture has session state from earlier tests that breaks
        # CSRF token issuance for this fresh register/login flow.
        client = TestClient(app, raise_server_exceptions=False)
        user = f"db_init_fail_{uuid.uuid4().hex[:8]}"
        pw = "TestPassword123!"  # noqa: S105

        def _csrf():
            client.get("/auth/login")
            r = client.get("/auth/csrf-token")
            return (
                r.json().get("csrf_token", "") if r.status_code == 200 else ""
            )

        # Register a user so credentials are valid (the handler distinguishes
        # invalid creds → 401 from broken DB init → 503).
        resp = client.post(
            "/auth/register",
            data={
                "username": user,
                "password": pw,
                "confirm_password": pw,
                "acknowledge": "true",
                "csrf_token": _csrf(),
            },
            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]}"
            )

        def _raise(*args, **kwargs):
            raise DatabaseInitializationError("simulated migrations failure")

        monkeypatch.setattr(db_manager, "open_user_database", _raise)

        resp = client.post(
            "/auth/login",
            data={
                "username": user,
                "password": pw,
                "csrf_token": _csrf(),
            },
            follow_redirects=False,
        )
        assert resp.status_code == 503, (
            f"Expected 503 on DatabaseInitializationError; got {resp.status_code}. "
            f"Body: {resp.text[:500]}"
        )