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.

15.1 KB
Raw Blame
"""
Comprehensive tests for FastAPI migration.

These tests verify that the FastAPI app provides the same behavior as
the Flask app for all critical user flows. They are the safety net
ensuring the migration is a drop-in replacement.
"""

import pytest
from fastapi.testclient import TestClient


@pytest.fixture()
def client(request):
    """Create a FastAPI test client.

    This overrides the Flask 'client' fixture from conftest.py.
    Uses raise_server_exceptions=False to prevent lifespan conflicts
    when Flask's conftest.py has already initialized SocketIOService.
    """
    from local_deep_research.web.fastapi_app import app

    return TestClient(app, raise_server_exceptions=False)


@pytest.fixture()
def strict_client():
    """FastAPI test client that raises on 500 errors."""
    from local_deep_research.web.fastapi_app import app

    return TestClient(app)


# ============================================================================
# Router Loading Tests — verify all 18 routers import and have routes
# ============================================================================

ROUTER_MODULES = [
    (".routers.chat", 9),
    (".routers.auth", 10),
    (".routers.research", 8),
    (".routers.history", 7),
    (".routers.settings", 30),
    (".routers.metrics", 20),
    (".routers.api", 8),
    (".routers.context_overflow_api", 2),
    (".routers.news_flask_api", 25),
    (".routers.news_pages", 4),
    (".routers.benchmark", 12),
    (".routers.followup", 2),
    (".routers.library", 20),
    (".routers.rag", 25),
    (".routers.library_delete", 8),
    (".routers.library_search", 3),
    (".routers.scheduler", 2),
    (".routers.api_v1", 4),
]


def test_total_route_count(client):
    """The app has at least 250 routes (264 at time of migration)."""
    from local_deep_research.web.fastapi_app import app

    routes = [r for r in app.routes if hasattr(r, "path")]
    assert len(routes) >= 250, f"Only {len(routes)} routes, expected >= 250"


# ============================================================================
# Health & Infrastructure Tests
# ============================================================================


def test_health_endpoint(client):
    """Health check returns 200 with expected fields."""
    resp = client.get("/api/v1/health")
    assert resp.status_code == 200
    data = resp.json()
    assert data["status"] == "ok"
    assert "timestamp" in data


def test_swagger_docs_disabled_by_default(client):
    """Swagger UI requires the explicit LDR_EXPOSE_DOCS operator switch."""
    resp = client.get("/api/docs")
    # Docs are disabled by default — 404 is expected
    assert resp.status_code == 404


def test_openapi_schema_endpoint_disabled_in_testing(client):
    """The /openapi.json HTTP endpoint is gated with the Swagger UI: it is
    the full API schema, so it must not be served unauthenticated when
    docs are disabled (the default for multi-user deployments)."""
    resp = client.get("/openapi.json")
    assert resp.status_code == 404


def test_openapi_schema_still_generates_in_process():
    """Gating the endpoint must not break programmatic schema generation."""
    from local_deep_research.web.fastapi_app import app

    schema = app.openapi()
    assert "paths" in schema
    assert len(schema["paths"]) > 50


def test_socketio_polling(client):
    """Socket.IO polling endpoint responds (ASGI integration works)."""
    resp = client.get("/ws/socket.io/?EIO=4&transport=polling")
    assert resp.status_code == 200


# ============================================================================
# Auth Flow Tests — login, register, logout, session management
# ============================================================================


def test_login_page_renders(client):
    """Login page renders HTML with form fields."""
    resp = client.get("/auth/login")
    assert resp.status_code == 200
    assert "password" in resp.text.lower()
    assert "username" in resp.text.lower()


def test_login_page_has_csrf_token(client):
    """Login page includes a CSRF token in the form."""
    resp = client.get("/auth/login")
    assert 'name="csrf_token"' in resp.text


def test_register_page_renders(client):
    """Register page renders with password requirements."""
    resp = client.get("/auth/register")
    assert resp.status_code == 200
    assert "password" in resp.text.lower()


def test_login_redirects_when_authenticated(client):
    """Login page redirects to / if already logged in (session has username)."""
    # Set session cookie via login attempt first
    # Since we can't actually authenticate, verify the redirect behavior
    # for an unauthenticated user (should show login page)
    resp = client.get("/auth/login", follow_redirects=False)
    assert resp.status_code == 200  # Not redirect — not authenticated


def _login_with_csrf(client, username: str, password: str):
    """POST /auth/login with a real CSRF token. The middleware now
    requires login forms to carry a token (was exempt before — see
    Wave 9 fix); rendering the GET first stamps the session with one.
    """
    client.get("/auth/login")
    csrf = client.get("/auth/csrf-token").json()["csrf_token"]
    return client.post(
        "/auth/login",
        data={
            "username": username,
            "password": password,
            "csrf_token": csrf,
        },
    )


def test_login_post_bad_credentials(client):
    """Login POST with bad credentials returns 401 with login page."""
    resp = _login_with_csrf(client, "nonexistent", "wrong")
    assert resp.status_code == 401
    assert "login" in resp.text.lower()


def test_login_post_empty_fields(client):
    """Login POST with empty fields returns 400."""
    resp = _login_with_csrf(client, "", "")
    assert resp.status_code == 400


def test_auth_check_unauthenticated(client):
    """Auth check returns 401 when not logged in."""
    resp = client.get("/auth/check")
    assert resp.status_code == 401
    data = resp.json()
    assert data["authenticated"] is False


def test_csrf_token_endpoint(client):
    """CSRF token endpoint returns a token."""
    resp = client.get("/auth/csrf-token")
    assert resp.status_code == 200
    data = resp.json()
    assert "csrf_token" in data


def test_validate_password_weak(client):
    """Password validation rejects weak passwords."""
    # /auth/validate-password is CSRF-protected (it was briefly exempt; the
    # shipped caller in static/js/security/auth-validation.js sends the token
    # in both the header and the form field).
    token = client.get("/auth/csrf-token").json()["csrf_token"]
    resp = client.post(
        "/auth/validate-password",
        data={"password": "abc", "csrf_token": token},
        headers={"X-CSRFToken": token},
    )
    assert resp.status_code == 200
    data = resp.json()
    assert data["valid"] is False
    assert len(data["errors"]) > 0


def test_validate_password_strong(client):
    """Password validation accepts strong passwords."""
    token = client.get("/auth/csrf-token").json()["csrf_token"]
    resp = client.post(
        "/auth/validate-password",
        data={"password": "StrongPass123!", "csrf_token": token},
        headers={"X-CSRFToken": token},
    )
    assert resp.status_code == 200
    data = resp.json()
    assert data["valid"] is True


def test_logout_requires_post(client):
    """Logout only accepts POST (prevents CSRF via GET)."""
    resp = client.get("/auth/logout", follow_redirects=False)
    # Should be 405 Method Not Allowed (GET not allowed on POST-only route)
    assert resp.status_code == 405


def test_change_password_requires_auth(client):
    """Change password page redirects unauthenticated users."""
    resp = client.get("/auth/change-password", follow_redirects=False)
    assert resp.status_code == 302
    assert "/auth/login" in resp.headers.get("location", "")


# ============================================================================
# Unauthenticated Access Tests — verify auth is required
# ============================================================================


PROTECTED_GET_ENDPOINTS = [
    # API v1
    "/api/v1/",
    # Research API (frontend JS calls these)
    "/api/research/test123/status",
    "/api/research/test123",
    "/api/research/test123/logs",
    "/api/report/test123",
    # History
    "/history/api",
    # Settings
    "/settings/api/all",
    "/settings/api/llm.provider",
    # Metrics
    "/metrics/api/metrics",
    "/metrics/api/cost-analytics",
    # News
    "/news/api/feed",
    # Library
    "/library/api/stats",
    "/library/api/collections",
    # Research config
    "/research/api/settings/current-config",
    "/research/api/check/ollama_status",
    # Benchmark
    "/benchmark/api/history",
]

PROTECTED_POST_ENDPOINTS = [
    "/api/v1/quick_summary",
    "/api/start_research",
    "/api/terminate/test123",
]


@pytest.mark.parametrize("path", PROTECTED_GET_ENDPOINTS)
def test_protected_get_endpoint_requires_auth(client, path):
    """Protected GET endpoints return 401 when not authenticated."""
    resp = client.get(path)
    assert resp.status_code == 401, (
        f"GET {path} returned {resp.status_code}, expected 401"
    )


@pytest.mark.parametrize("path", PROTECTED_POST_ENDPOINTS)
def test_protected_post_endpoint_requires_auth(client, path):
    """Protected POST endpoints reject unauthenticated calls.

    CSRF runs before auth in the middleware order, so an unauth
    request without a session token gets 403 (CSRF missing). Either
    401 or 403 means rejected — both are acceptable for this test.
    """
    resp = client.post(path, json={"query": "test"})
    assert resp.status_code in (401, 403), (
        f"POST {path} returned {resp.status_code}, expected 401/403"
    )


# ============================================================================
# Root Route Tests
# ============================================================================


def test_root_redirects_to_login(client):
    """Root route redirects unauthenticated users to login."""
    resp = client.get("/", follow_redirects=False)
    assert resp.status_code == 302
    assert "/auth/login" in resp.headers.get("location", "")


# ============================================================================
# Static File Tests
# ============================================================================


def test_favicon(client):
    """Favicon is served."""
    resp = client.get("/favicon.ico")
    # Either 200 (found) or 404 (not built yet) — not 500
    assert resp.status_code in (200, 404)


def test_static_css(client):
    """Static CSS files are served."""
    resp = client.get("/static/css/styles.css")
    assert resp.status_code in (200, 404)


# ============================================================================
# Template Rendering Tests — verify Jinja2 works correctly
# ============================================================================


def test_login_template_has_vite_assets(client):
    """Login page includes Vite-built CSS/JS assets."""
    resp = client.get("/auth/login")
    assert (
        "app." in resp.text
    )  # Vite-built asset reference (e.g. app.O-lNHxZ7.js)


def test_news_page_renders(client):
    """News page renders without errors."""
    resp = client.get("/news/")
    assert resp.status_code == 200


def test_news_health(client):
    """News health check requires auth (no longer public after Wave 12).

    `/api/v1/health` is the public liveness probe; this one was
    leaking infrastructure state to unauthenticated callers and is
    now gated behind `require_auth`. Browser path → 302 to login;
    JSON-Accept path → 401.
    """
    resp = client.get(
        "/news/health",
        headers={"Accept": "application/json"},
        follow_redirects=False,
    )
    assert resp.status_code in (302, 401)


# ============================================================================
# Middleware Tests
# ============================================================================


def test_security_headers_present(client):
    """Security headers are set on responses."""
    resp = client.get("/api/v1/health")
    headers = resp.headers
    assert "x-content-type-options" in headers
    assert headers["x-content-type-options"] == "nosniff"
    assert "x-frame-options" in headers
    assert "content-security-policy" in headers


def test_server_header_removed(client):
    """Server header is stripped from responses."""
    resp = client.get("/api/v1/health")
    assert "server" not in resp.headers


def test_session_cookie_set(client):
    """Session middleware sets a session cookie."""
    resp = client.get("/auth/login")
    cookies = resp.cookies
    # Starlette SessionMiddleware sets a 'session' cookie
    assert "session" in cookies or any(
        "session" in str(c) for c in resp.headers.getlist("set-cookie")
    )


# ============================================================================
# Socket.IO Integration Tests
# ============================================================================


def test_socketio_mount_path(client):
    """Socket.IO is mounted at /ws/socket.io (not /socket.io)."""
    # The old Flask path must not work. This previously fired the request and
    # discarded the result, so the half of the contract that actually matters
    # to an upgrading operator -- that the legacy path is gone -- was never
    # asserted at all.
    resp_old = client.get("/socket.io/?EIO=4&transport=polling")
    assert resp_old.status_code != 200, (
        "the legacy /socket.io path still answers 200; the documented "
        "breaking change (path moved to /ws/socket.io) has regressed"
    )
    # The new path should work
    resp_new = client.get("/ws/socket.io/?EIO=4&transport=polling")
    assert resp_new.status_code == 200


# ============================================================================
# API v1 Tests
# ============================================================================


def test_api_v1_docs(client):
    """API v1 documentation endpoint requires auth."""
    resp = client.get("/api/v1/")
    assert resp.status_code == 401


def test_api_v1_quick_summary_requires_auth(client):
    """Quick summary endpoint requires authentication.

    Either 401 (auth missing) or 403 (CSRF missing, since CSRF runs
    before auth in the middleware order) is acceptable — both mean
    "rejected, retry with credentials/token". The exact code depends
    on whether the caller has a session token at all.
    """
    resp = client.post("/api/v1/quick_summary", json={"query": "test"})
    assert resp.status_code in (401, 403)


# ============================================================================
# Error Handling Tests
# ============================================================================


def test_404_returns_json_for_api(client):
    """API 404s return JSON error."""
    resp = client.get("/api/v1/nonexistent")
    assert resp.status_code in (404, 405)


def test_no_500_on_common_pages(client):
    """Common pages don't return 500 errors."""
    pages = [
        "/auth/login",
        "/auth/register",
        "/auth/csrf-token",
        "/api/v1/health",
        "/news/",
    ]
    for page in pages:
        resp = client.get(page)
        assert resp.status_code != 500, f"{page} returned 500"