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.

12.2 KB
Raw Blame
"""
Comprehensive endpoint coverage tests.

Tests every major API endpoint when authenticated. Endpoints that currently
return 500 (runtime bugs from migration) are marked with xfail — they'll
automatically start passing as bugs are fixed.
"""

import uuid

import pytest
from fastapi.testclient import TestClient


@pytest.fixture(scope="module")
def auth_client():
    """Authenticated test client."""
    from local_deep_research.web.fastapi_app import app

    c = TestClient(app, raise_server_exceptions=False)

    user = f"test_cov_{uuid.uuid4().hex[:8]}"
    pw = "TestPassword123!"  # noqa: S105

    # Fetch CSRF tokens before register/login — Wave 9 made these
    # endpoints fail-closed on missing tokens.
    def _csrf():
        c.get("/auth/login")
        r = c.get("/auth/csrf-token")
        return r.json().get("csrf_token", "") if r.status_code == 200 else ""

    c.post(
        "/auth/register",
        data={
            "username": user,
            "password": pw,
            "confirm_password": pw,
            "acknowledge": "true",
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    resp = c.post(
        "/auth/login",
        data={"username": user, "password": pw, "csrf_token": _csrf()},
        follow_redirects=False,
    )
    if resp.status_code != 302:
        pytest.fail(
            f"Auth bootstrap broken: login returned {resp.status_code} "
            f"(expected 302): {resp.text[:300]}"
        )

    # Attach CSRF token for any state-changing requests in this suite.
    csrf_resp = c.get("/auth/csrf-token")
    if csrf_resp.status_code == 200:
        token = csrf_resp.json().get("csrf_token")
        if token:
            c.headers.update({"X-CSRFToken": token})

    yield c

    c.post("/auth/logout", follow_redirects=False)


# ============================================================================
# Endpoints that WORK (assert 200)
# ============================================================================


class TestWorkingEndpoints:
    """Endpoints that return 200 when authenticated."""

    # Auth
    def test_auth_check(self, auth_client):
        assert auth_client.get("/auth/check").status_code == 200

    def test_csrf_token(self, auth_client):
        resp = auth_client.get("/auth/csrf-token")
        assert resp.status_code == 200
        assert resp.json().get("csrf_token")

    def test_integrity_check(self, auth_client):
        assert auth_client.get("/auth/integrity-check").status_code == 200

    def test_change_password_page(self, auth_client):
        assert auth_client.get("/auth/change-password").status_code == 200

    def test_validate_password(self, auth_client):
        resp = auth_client.post(
            "/auth/validate-password", data={"password": "Str0ng!Pass"}
        )
        assert resp.status_code == 200
        assert resp.json()["valid"] is True

    # Pages
    def test_history_page(self, auth_client):
        assert auth_client.get("/history/").status_code == 200

    def test_settings_page(self, auth_client):
        assert auth_client.get("/settings/").status_code == 200

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

    # Research API
    def test_ollama_status(self, auth_client):
        assert (
            auth_client.get("/research/api/check/ollama_status").status_code
            == 200
        )

    def test_ollama_model(self, auth_client):
        # A model name is required: the endpoint returns 400 "model required"
        # when neither ?model= nor a configured llm.model is present (the
        # default llm.model is empty). Supply one so the endpoint runs its
        # availability check — with no Ollama reachable it still responds 200
        # (available: false), which is the "endpoint is wired" signal this
        # smoke test asserts.
        assert (
            auth_client.get(
                "/research/api/check/ollama_model?model=llama3"
            ).status_code
            == 200
        )

    def test_config_limits(self, auth_client):
        assert auth_client.get("/api/config/limits").status_code == 200

    # History API
    def test_history_api(self, auth_client):
        resp = auth_client.get("/history/api")
        assert resp.status_code == 200

    # Metrics API
    def test_metrics(self, auth_client):
        assert auth_client.get("/metrics/api/metrics").status_code == 200

    def test_cost_analytics(self, auth_client):
        assert auth_client.get("/metrics/api/cost-analytics").status_code == 200

    def test_pricing(self, auth_client):
        assert auth_client.get("/metrics/api/pricing").status_code == 200

    def test_rate_limiting_metrics(self, auth_client):
        assert auth_client.get("/metrics/api/rate-limiting").status_code == 200

    # Library API
    def test_supported_formats(self, auth_client):
        assert (
            auth_client.get("/library/api/config/supported-formats").status_code
            == 200
        )

    # Benchmark API
    def test_benchmark_configs(self, auth_client):
        assert auth_client.get("/benchmark/api/configs").status_code == 200

    def test_search_quality(self, auth_client):
        assert (
            auth_client.get("/benchmark/api/search-quality").status_code == 200
        )

    # Health (no auth needed)
    def test_health(self, auth_client):
        assert auth_client.get("/api/v1/health").status_code == 200

    def test_news_health(self, auth_client):
        assert auth_client.get("/news/health").status_code == 200

    # Pages (previously xfail, now passing)
    def test_root_page(self, auth_client):
        assert auth_client.get("/", follow_redirects=True).status_code == 200

    def test_benchmark_page(self, auth_client):
        assert auth_client.get("/benchmark/").status_code == 200

    def test_library_page(self, auth_client):
        assert auth_client.get("/library/").status_code == 200

    # Settings API (previously xfail, now passing)
    def test_settings_all(self, auth_client):
        assert auth_client.get("/settings/api").status_code == 200

    def test_settings_categories(self, auth_client):
        assert auth_client.get("/settings/api/categories").status_code == 200

    def test_settings_types(self, auth_client):
        assert auth_client.get("/settings/api/types").status_code == 200

    def test_settings_ui_elements(self, auth_client):
        assert auth_client.get("/settings/api/ui_elements").status_code == 200

    def test_settings_available_search_engines(self, auth_client):
        assert (
            auth_client.get(
                "/settings/api/available-search-engines"
            ).status_code
            == 200
        )

    def test_settings_warnings(self, auth_client):
        assert auth_client.get("/settings/api/warnings").status_code == 200

    def test_settings_llm_provider(self, auth_client):
        assert auth_client.get("/settings/api/llm.provider").status_code == 200

    def test_settings_llm_model(self, auth_client):
        assert auth_client.get("/settings/api/llm.model").status_code == 200

    def test_settings_search_tool(self, auth_client):
        assert auth_client.get("/settings/api/search.tool").status_code == 200

    # Research API (previously xfail, now passing)
    def test_research_config(self, auth_client):
        # /research/api/config never existed (on Flask main either); the
        # old assertion only passed when an earlier test had mutated the
        # shared app. The real endpoint is settings/current-config.
        assert (
            auth_client.get("/research/api/settings/current-config").status_code
            == 200
        )

    def test_queue_status(self, auth_client):
        assert auth_client.get("/api/queue/status").status_code == 200

    # News API (previously xfail, now passing)
    def test_news_subscriptions(self, auth_client):
        assert (
            auth_client.get("/news/api/subscriptions/current").status_code
            == 200
        )

    # Library API (previously xfail, now passing)
    def test_library_stats(self, auth_client):
        assert auth_client.get("/library/api/stats").status_code == 200

    def test_library_collections(self, auth_client):
        assert auth_client.get("/library/api/collections").status_code == 200

    def test_library_collections_list(self, auth_client):
        assert (
            auth_client.get("/library/api/collections/list").status_code == 200
        )

    # Benchmark API (previously xfail, now passing)
    def test_benchmark_history(self, auth_client):
        assert auth_client.get("/benchmark/api/history").status_code == 200

    def test_benchmark_running(self, auth_client):
        assert auth_client.get("/benchmark/api/running").status_code == 200

    # Settings API (previously xfail, now passing — db session deps fixed)
    def test_settings_available_models(self, auth_client):
        assert (
            auth_client.get("/settings/api/available-models").status_code == 200
        )

    def test_settings_ollama_status(self, auth_client):
        assert auth_client.get("/settings/api/ollama-status").status_code == 200

    def test_settings_backup_status(self, auth_client):
        assert auth_client.get("/settings/api/backup-status").status_code == 200

    def test_news_feed(self, auth_client):
        assert auth_client.get("/news/api/feed").status_code == 200

    def test_research_settings(self, auth_client):
        assert (
            auth_client.get("/research/api/settings/current-config").status_code
            == 200
        )


# ============================================================================
# Response format tests
# ============================================================================


class TestResponseFormats:
    """Verify API responses have correct format."""

    def test_auth_check_format(self, auth_client):
        resp = auth_client.get("/auth/check")
        data = resp.json()
        assert "authenticated" in data
        assert "username" in data

    def test_csrf_token_format(self, auth_client):
        resp = auth_client.get("/auth/csrf-token")
        data = resp.json()
        assert "csrf_token" in data
        assert isinstance(data["csrf_token"], str)
        assert len(data["csrf_token"]) > 0

    def test_health_format(self, auth_client):
        resp = auth_client.get("/api/v1/health")
        data = resp.json()
        assert data["status"] == "ok"
        assert "timestamp" in data

    def test_integrity_check_format(self, auth_client):
        resp = auth_client.get("/auth/integrity-check")
        data = resp.json()
        assert "integrity" in data
        assert "username" in data
        assert "timestamp" in data

    def test_history_format(self, auth_client):
        resp = auth_client.get("/history/api")
        # For a freshly authenticated user hitting a dependency-free
        # endpoint, this is always 200 — asserting only conditionally
        # (the previous `if resp.status_code == 200:` guard) let the
        # test pass vacuously if the route ever started 500ing.
        assert resp.status_code == 200, resp.text[:300]
        data = resp.json()
        assert isinstance(data, (dict, list))

    def test_metrics_format(self, auth_client):
        resp = auth_client.get("/metrics/api/metrics")
        assert resp.status_code == 200
        data = resp.json()
        # May be dict (success) or list (Flask tuple response not fully converted)
        assert isinstance(data, (dict, list))

    def test_benchmark_configs_format(self, auth_client):
        resp = auth_client.get("/benchmark/api/configs")
        assert resp.status_code == 200
        data = resp.json()
        # May be list of configs or error response
        assert isinstance(data, (dict, list))

    def test_supported_formats_format(self, auth_client):
        resp = auth_client.get("/library/api/config/supported-formats")
        # Same reasoning as test_history_format: this route has no
        # external dependency for an authenticated user, so it should
        # always be 200 — assert that unconditionally rather than
        # hiding the body check behind a status-code guard that could
        # silently pass on a regression to 4xx/5xx.
        assert resp.status_code == 200, resp.text[:300]
        data = resp.json()
        assert isinstance(data, (dict, list))