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.

8.7 KB
Raw Blame
"""
Comprehensive smoke test: every GET endpoint must respond cleanly when
authenticated.

The PR shipped >100 routes — endpoint-by-endpoint hand-written tests
caught some 500s, but several only show up when you exercise the entire
surface (e.g. a dead Flask import in a service is invisible until that
specific route is hit). This test enumerates every GET route via the
FastAPI app, fires a request, and asserts the response is not a 5xx.

Acceptable codes:
- 200/204: normal success
- 302: redirect (login pages, dashboards)
- 400: route requires extra body/query (e.g. /library/api/rag/index-local)
- 404: documented missing-resource (e.g. /favicon.ico)
- 501: feature explicitly NotImplemented (e.g. /api/news/categories)
- 405: route is method-only-other-verb (defensive)

Anything 5xx fails the test with the path and body so the regression is
obvious.
"""

import os
import uuid

import pytest
from fastapi.testclient import TestClient

# The app's docs_url toggle reads PYTEST_CURRENT_TEST at app-import time.
# Pytest only sets that env var per-test, not at collection — but our
# parametrize decorator imports the app at collection time. Force the
# flag now so /api/docs is correctly disabled if any other test inspects
# it after this module loads.
os.environ.setdefault("TESTING", "1")


# Endpoints we expect to fail in the test environment for documented
# reasons (not bugs). Map of path -> reason. Comment, don't silently skip.
KNOWN_NON_2XX = {
    "/favicon.ico": "static file not in test data dir",
    "/library/api/rag/index-local": "requires POST body with `path`",
    "/api/news/categories": "intentional NotImplementedException",
    "/news/api/categories": "intentional NotImplementedException",
}

# Routes whose GET handlers explicitly need request body — they return 400
# without it. We accept 400 as not-a-server-bug.
ALLOW_400 = {"/library/api/rag/index-local"}

# Anything in /openapi.json/docs etc. is just FastAPI built-ins — let through.
# /api/docs in particular: whether it returns 200 or 404 depends on whether
# PYTEST_CURRENT_TEST was set at app-import time. We have a dedicated test for
# that in test_fastapi_migration; don't double-test it here and risk a
# collection-order dependency.
SKIP_PATHS = {
    "/docs",
    "/redoc",
    "/docs/oauth2-redirect",
    "/openapi.json",
    "/api/docs",
    # These two GET endpoints call get_rag_service(), which eagerly builds
    # LibraryRAGService and loads the ~400MB sentence-transformers embedding
    # model — even though /stats and /info only read metadata (verified with
    # a per-endpoint fresh-process probe: exactly these pull the ML
    # stack; the other rag GETs do not). Under the whole-suite `-n auto` run,
    # each xdist worker that hits one loads the model into its own process;
    # several loading concurrently OOM-kill the CI runner, and the OOM-killer
    # takes out unrelated workers (observed as "worker gwN crashed" on trivial
    # endpoints like / and /favicon.ico, plus a journal-warm 503 race). Both
    # are covered by dedicated tests in tests/research_library/routes/.
    # (That a GET /stats spins up a 400MB model is a minor product
    # inefficiency worth a separate lazy-load follow-up.)
    #
    # The bulk re-index trigger shared this hazard and used to be listed
    # here too; it is a POST now, so this GET-only enumerator never reaches
    # it. Its entry lives in test_full_surface_smoke.py's
    # MUTATING_DENY_LIST instead.
    "/library/api/rag/info",
    "/library/api/rag/stats",
    # Still skipped, but for a different and much smaller reason than before,
    # so the old rationale is not left standing.
    #
    # It used to be excluded because the handler's
    # get_journal_reference_db().available fell through to a real
    # download_journal_data() fetch inside the request; timeout_method="thread"
    # cannot abort a blocked socket, so the xdist worker was killed rather than
    # the test failing. That hole is now closed in production code — the
    # implicit auto-download triggers pass auto_download=False under
    # LDR_TESTING_WITH_MOCKS. Measured on the seam: 0 connect attempts in 0.40s
    # with the gate, 18 without.
    #
    # What remains is not a hang and not a defect. With no gz snapshots on disk
    # — the normal state for a mock-mode run, since only the dedicated
    # journal-data-integration workflow fetches them — the endpoint answers
    # 503 {"status":"error","message":"Journal reference database not
    # available."}. That is the designed response to absent reference data, but
    # it is still a 5xx, and this sweep asserts non-5xx for every GET. Verified
    # by removing this entry and running the file: 109 passed, 1 failed, the
    # failure being exactly that 503.
    #
    # Coverage is unaffected, as before: the same non-5xx contract is asserted
    # in test_metrics_benchmark_hostile_input.py, and the success/sort/injection
    # /IDOR cases in tests/security/test_metrics_hostile_input_fastapi.py seed
    # reference_db directly rather than racing the lazy build.
    "/metrics/api/journals",
}


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

    c = TestClient(app, raise_server_exceptions=False)

    user = f"test_all_{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]}"
        )

    # Warm the bundled journal reference DB. Its first access lazily compiles
    # five gzipped snapshots into the queryable ~217K-row file (~25s); if that
    # build runs mid-sweep inside test_get_endpoint_no_500[/metrics/api/journals],
    # the project's thread-method per-test timeout cannot interrupt the blocked
    # request and the whole run hangs. Building it once here (the file persists
    # on disk) keeps every endpoint request fast. `.available` is fail-safe —
    # it returns False rather than raising when the DB can't be built.
    from local_deep_research.journal_quality.db import get_journal_reference_db

    get_journal_reference_db().available

    yield c


def _enumerate_get_routes() -> list[str]:
    """Pull every concrete GET path out of the assembled app."""
    from local_deep_research.web.fastapi_app import app

    paths: set[str] = set()
    for route in app.routes:
        methods = getattr(route, "methods", None) or set()
        path = getattr(route, "path", None)
        if not path or "GET" not in methods:
            continue
        if "{" in path:
            # Skip parameterized routes — they need real IDs.
            continue
        if path.startswith("/__"):
            # Test-only probe routes. `tests/web/test_middleware_order_and_
            # headers.py` registers `/__mw_order_probe__/*` on the live `app`
            # singleton at import time, and one of them exists precisely to
            # raise. Whether it lands in this sweep depends purely on whether
            # that module was imported first, which today is decided by
            # pytest's alphabetical file ordering — so running these two
            # files together, or adding pytest-randomly, or renaming either
            # file, turns this suite red for a route no product code serves.
            continue
        if path in SKIP_PATHS:
            continue
        paths.add(path)
    return sorted(paths)


@pytest.mark.parametrize("path", _enumerate_get_routes())
def test_get_endpoint_no_500(auth_client, path: str):
    """Every GET endpoint must return non-5xx when authenticated."""
    resp = auth_client.get(path)

    if path in ALLOW_400 and resp.status_code == 400:
        return

    if path in KNOWN_NON_2XX:
        # Accept any documented non-bug status, including 501 NotImplemented.
        assert resp.status_code in (400, 404, 501), (
            f"{path} (known edge case: {KNOWN_NON_2XX[path]}) returned "
            f"{resp.status_code}: {resp.text[:200]}"
        )
        return

    assert resp.status_code < 500, (
        f"{path} returned {resp.status_code}: {resp.text[:300]}"
    )