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.

17.8 KB
Raw Blame
"""Regression tests for BC-1d: metrics.py / benchmark.py routes returning a
hardcoded 500 for malformed input instead of a clean 4xx.

Two bug shapes, fixed in ``web/routers/metrics.py`` and
``web/routers/benchmark.py``:

Shape A — ``data = await request.json()`` was called *inside* the route's
outer ``try: ... except Exception: return ...(500)`` block. A body that
isn't valid JSON at all makes ``request.json()`` raise
``json.JSONDecodeError``. The app has a registered handler for that
exception (``fastapi_app.py::handle_json_decode_error``) which answers a
clean ``400 {"error": "Invalid JSON body"}`` — but only if the exception is
allowed to propagate there uncaught. The route's own broad
``except Exception`` intercepted it first and returned a hardcoded 500
instead. Fixed by moving ``await request.json()`` (and, where present, its
``isinstance(data, dict)`` guard) outside the try block, so a malformed body
is never caught locally and reaches the app-level handler.

  - POST /metrics/api/ratings/{research_id}
  - POST /metrics/api/cost-calculation
  - POST /benchmark/api/validate-config (malformed-bytes case only — a
    well-formed-but-non-dict body, e.g. ``null`` or ``[]``, is a
    *documented*, pre-existing 200 ``{"valid": False, "errors": [...]}"``
    response for this specific endpoint per its own docstring, not a bug.
    That behaviour is intentionally left unchanged; pinned by
    ``test_validate_config_non_dict_body_stays_200_by_design`` below so a
    future edit that "fixes" it to 400 gets caught as an unintended
    behaviour change.)

Shape B — ``data = await request.json() or {}`` guards only FALSY bodies
(``None``, ``[]``, ``""``). A *truthy* non-dict body (a bare string, a bare
int, or a non-empty list) survives the ``or {}`` and reaches
``data.get(...)``, raising ``AttributeError`` into the outer
``except Exception`` -> 500.

  - POST /metrics/api/domain-classifications/classify

Every "must be 4xx" assertion below was confirmed to fail against the
pre-fix code on this branch (git show HEAD~1 for the two files, replayed by
hand): each Shape-A route returned 500 for malformed JSON bytes, and the
Shape-B route returned 500 for a bare string/int/non-empty-list body. See
the docstring on each test for the exact pre-fix status observed.
"""

import json as _json
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 per-test, not at collection, and TestClient(app)
# below imports the app at module-collection time. Force it now (mirrors
# test_full_surface_smoke.py / test_all_endpoints.py).
os.environ.setdefault("TESTING", "1")

# Imported so this file fails loudly if a route is renamed/removed rather
# than silently testing a path that 404s for the wrong reason.
from local_deep_research.web.routers.metrics import router as metrics_router
from local_deep_research.web.routers.benchmark import (
    router as benchmark_router,
)

_METRICS_PATHS = {r.path for r in metrics_router.routes if hasattr(r, "path")}
_BENCHMARK_PATHS = {
    r.path for r in benchmark_router.routes if hasattr(r, "path")
}


def test_routes_under_test_still_exist():
    """Guards the premise: if these routes move, the cases below would
    silently pass against 404s instead of exercising the fix.

    Router-relative paths already include the router's own prefix
    (``APIRouter(prefix="/metrics", ...)`` / ``prefix="/benchmark"``), so
    these match the full paths used by the tests below.
    """
    assert "/metrics/api/ratings/{research_id}" in _METRICS_PATHS
    assert "/metrics/api/cost-calculation" in _METRICS_PATHS
    assert "/metrics/api/domain-classifications/classify" in _METRICS_PATHS
    assert "/benchmark/api/validate-config" in _BENCHMARK_PATHS


@pytest.fixture(scope="module")
def auth_client():
    """Authenticated TestClient for a freshly-created throwaway user, with
    the CSRF header pre-attached. Reuses the module-scoped pattern from
    test_full_surface_smoke.py::auth_client so the whole file runs against
    one registered user instead of one per test.
    """
    from local_deep_research.web.fastapi_app import app

    c = TestClient(app, raise_server_exceptions=False)

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

    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]}"
        )

    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)


MALFORMED_JSON_BYTES = b"{not valid json"

# Well-formed JSON, but not an object.
NON_DICT_BODIES = [
    pytest.param(None, id="json-null"),
    pytest.param([], id="json-empty-list"),
    pytest.param("a string", id="json-string"),
    pytest.param(7, id="json-number"),
]

# Truthy non-dict bodies specifically — the ones that survive a bare
# `or {}` guard (Shape B).
TRUTHY_NON_DICT_BODIES = [
    pytest.param("a string", id="json-string"),
    pytest.param(7, id="json-number"),
    pytest.param([1, 2, 3], id="json-non-empty-list"),
]


def _post_json_value(client, route, value):
    """POST *value* serialized as an actual JSON body — including the
    JSON literal ``null`` for ``value=None``.

    httpx's ``json=`` kwarg treats ``None`` specially: passing ``json=None``
    omits the body entirely (empty request) rather than sending the bytes
    ``null``, which would silently turn a "JSON null body" test case into a
    "no body at all" test case (still worth asserting on, but a different
    bug than the one being pinned). Sending raw ``content=`` bytes exercises
    the exact wire payload we mean.
    """
    return client.post(
        route,
        content=_json.dumps(value).encode(),
        headers={"Content-Type": "application/json"},
    )


SHAPE_A_ROUTES = [
    pytest.param(
        "/metrics/api/ratings/00000000-0000-4000-8000-000000000000",
        {"status": "error"},
        id="ratings",
    ),
    pytest.param(
        "/metrics/api/cost-calculation",
        {"error": "..."},
        id="cost-calculation",
    ),
]


@pytest.mark.parametrize("route,_shape", SHAPE_A_ROUTES)
def test_malformed_json_bytes_is_400_not_500(auth_client, route, _shape):
    """Shape A. Before the fix: `await request.json()` was inside the
    route's own `try/except Exception`, so json.JSONDecodeError never
    reached the app's registered 400 handler and the route answered a
    hardcoded 500 ({"status": "error", "message": "An internal error..."}
    or {"error": "An internal error occurred"}) instead.
    """
    resp = auth_client.post(
        route,
        content=MALFORMED_JSON_BYTES,
        headers={"Content-Type": "application/json"},
    )
    assert resp.status_code == 400, (
        f"POST {route} returned {resp.status_code} for malformed JSON "
        f"bytes; expected 400 from the app's json.JSONDecodeError handler. "
        f"Body: {resp.text[:300]}"
    )
    assert resp.status_code < 500


def test_validate_config_malformed_json_bytes_is_400_not_500(auth_client):
    """Shape A. Before the fix this returned 500
    {"valid": False, "errors": ["An internal error has occurred."]}."""
    resp = auth_client.post(
        "/benchmark/api/validate-config",
        content=MALFORMED_JSON_BYTES,
        headers={"Content-Type": "application/json"},
    )
    assert resp.status_code == 400, (
        f"POST /benchmark/api/validate-config returned {resp.status_code} "
        f"for malformed JSON bytes; expected 400. Body: {resp.text[:300]}"
    )


def test_validate_config_non_dict_body_stays_200_by_design(auth_client):
    """Pin the pre-existing, documented behaviour for a well-formed but
    non-dict body: this endpoint intentionally returns 200
    {"valid": False, "errors": [...]} rather than a 4xx (its own docstring:
    "not using @require_json_body because this endpoint returns
    {'valid': False, 'errors': [...]}"). The malformed-bytes fix above must
    not change this — only the crash-on-unparseable-bytes case was a bug.
    """
    resp = _post_json_value(auth_client, "/benchmark/api/validate-config", None)
    assert resp.status_code == 200, (
        f"non-dict body handling changed: got {resp.status_code}, "
        f"expected the documented 200 valid:false response: {resp.text[:300]}"
    )
    body = resp.json()
    assert body["valid"] is False
    assert body["errors"] == ["No data provided"]


@pytest.mark.parametrize("body", NON_DICT_BODIES)
def test_ratings_non_dict_body_is_400(auth_client, body):
    """The pre-existing isinstance guard (json_body_error("status", ...))
    for a well-formed-but-non-dict body must keep working after moving the
    parse call out of the try block."""
    resp = _post_json_value(
        auth_client,
        "/metrics/api/ratings/00000000-0000-4000-8000-000000000000",
        body,
    )
    assert resp.status_code == 400, (
        f"expected 400 for non-dict body {body!r}, got {resp.status_code}: "
        f"{resp.text[:300]}"
    )
    payload = resp.json()
    assert payload.get("status") == "error"


@pytest.mark.parametrize("body", NON_DICT_BODIES)
def test_cost_calculation_non_dict_body_is_400(auth_client, body):
    resp = _post_json_value(auth_client, "/metrics/api/cost-calculation", body)
    assert resp.status_code == 400, (
        f"expected 400 for non-dict body {body!r}, got {resp.status_code}: "
        f"{resp.text[:300]}"
    )
    payload = resp.json()
    assert "error" in payload


@pytest.mark.parametrize("body", TRUTHY_NON_DICT_BODIES)
def test_classify_domains_truthy_non_dict_body_is_400_not_500(
    auth_client, body
):
    """Shape B. Before the fix: `data = await request.json() or {}` only
    guards FALSY bodies. A truthy non-dict body (string/int/non-empty list)
    survived the `or {}` and reached `data.get("domain")`, raising
    AttributeError -> a bare 500 (no try/except at all wrapped this route,
    so it fell through to fastapi_app.py's catch-all Exception handler).
    """
    resp = _post_json_value(
        auth_client, "/metrics/api/domain-classifications/classify", body
    )
    assert resp.status_code == 400, (
        f"POST /metrics/api/domain-classifications/classify returned "
        f"{resp.status_code} for truthy non-dict body {body!r}; expected "
        f"400. Body: {resp.text[:300]}"
    )
    payload = resp.json()
    assert payload.get("status") == "error"


@pytest.mark.parametrize(
    "body",
    [
        pytest.param(None, id="json-null"),
        pytest.param([], id="json-empty-list"),
    ],
)
def test_classify_domains_falsy_non_dict_body_still_400(auth_client, body):
    """The pre-existing `or {}` fallback handled FALSY non-dict bodies
    (None, []) fine before the fix too (they silently became {}, which then
    400s downstream for "must provide domain or batch"). Confirm the new
    isinstance guard still answers 4xx for these, just via the explicit
    json_body_error path now instead of falling through to the
    domain/batch validation."""
    resp = _post_json_value(
        auth_client, "/metrics/api/domain-classifications/classify", body
    )
    assert resp.status_code == 400, (
        f"expected 400 for falsy non-dict body {body!r}, got "
        f"{resp.status_code}: {resp.text[:300]}"
    )


def test_cost_calculation_well_formed_request_unchanged(auth_client):
    """A well-formed request must still succeed exactly as before — the
    fix must not change behaviour for valid input."""
    resp = auth_client.post(
        "/metrics/api/cost-calculation",
        json={
            "model_name": "gpt-4o-mini",
            "provider": "openai",
            "prompt_tokens": 1000,
            "completion_tokens": 500,
        },
    )
    assert resp.status_code == 200, (
        f"well-formed cost-calculation request regressed: "
        f"{resp.status_code}: {resp.text[:300]}"
    )
    payload = resp.json()
    assert payload["status"] == "success"
    assert payload["model_name"] == "gpt-4o-mini"


def test_ratings_well_formed_request_unchanged(auth_client):
    """A well-formed rating body must still be accepted and saved."""
    research_id = f"hostile-input-test-{uuid.uuid4().hex[:8]}"
    resp = auth_client.post(
        f"/metrics/api/ratings/{research_id}",
        json={"rating": 4, "feedback": "solid"},
    )
    assert resp.status_code == 200, (
        f"well-formed rating request regressed: {resp.status_code}: "
        f"{resp.text[:300]}"
    )
    payload = resp.json()
    assert payload["status"] == "success"
    assert payload["rating"] == 4


def test_validate_config_well_formed_request_unchanged(auth_client):
    """A well-formed (if incomplete) config must still get the normal
    valid/errors report, not an error."""
    resp = auth_client.post(
        "/benchmark/api/validate-config",
        json={
            "search_config": {
                "search_tool": "searxng",
                "search_strategy": "focused_iteration",
            },
            "datasets_config": {"simpleqa": {"count": 5}},
        },
    )
    assert resp.status_code == 200
    payload = resp.json()
    assert payload["valid"] is True
    assert payload["errors"] == []
    assert payload["total_examples"] == 5


def test_classify_domains_well_formed_request_unchanged(auth_client):
    """A well-formed dict body without domain/batch must still reach the
    existing domain/batch validation (400, distinct message) rather than
    being rejected by the new isinstance guard."""
    resp = auth_client.post(
        "/metrics/api/domain-classifications/classify", json={}
    )
    assert resp.status_code == 400
    payload = resp.json()
    assert payload.get("status") == "error"
    assert "domain" in payload.get("message", "").lower()


@pytest.fixture
def seeded_journal_reference_db(tmp_path, monkeypatch):
    """A real JournalQualityDB over a tiny seeded file.

    Needed because the route under test answers 503 "Journal reference database
    not available" before it ever reaches pagination when no reference data
    exists — and under the test suite's mock-only mode it never does, since the
    implicit auto-download is deliberately gated off (see
    ``journal_quality/db.py::_build_or_raise``). Without seeding, this test
    asserts `< 500` against a 503 and tells you nothing about the clamp it is
    named for.

    Mirrors ``tests/security/test_metrics_hostile_input_fastapi.py``'s
    ``reference_db``: production models and the production accessor class, only
    the *file* substituted, so ``get_journals_page`` is genuinely the code under
    test. ``PRAGMA user_version`` stays 0, which ``_validate_existing_db``
    grandfathers in rather than rebuilding, so nothing tries to download.
    """
    from sqlalchemy import create_engine
    from sqlalchemy.orm import Session as SASession

    from local_deep_research.journal_quality import db as jq_db
    from local_deep_research.journal_quality.models import (
        JournalQualityBase,
        Source,
    )
    from local_deep_research.journal_quality.scoring import normalize_name

    path = tmp_path / "journal_quality.db"
    engine = create_engine(f"sqlite:///{path}")
    JournalQualityBase.metadata.create_all(engine)
    with SASession(engine) as session:
        for name in ("Journal of Testing", "Review of Fixtures"):
            session.add(Source(name=name, name_lower=normalize_name(name)))
        session.commit()
    engine.dispose()

    ref = jq_db.JournalQualityDB()
    monkeypatch.setattr(ref, "_resolve_db_path", lambda: path)
    monkeypatch.setattr(jq_db, "get_journal_reference_db", lambda: ref)
    assert ref.available, "seeded reference DB did not open"
    return path


def test_journals_page_overflow_still_clamped(
    auth_client, seeded_journal_reference_db
):
    """Not a regression from this slice's edits (metrics.py's other Shape
    A/B fixes don't touch /api/journals), but confirms the one paginated
    route in this file already has the correct upper-bound clamp this slice
    was asked to check for. An unbounded page would reach `.offset()` and
    raise OverflowError from SQLite's 64-bit conversion -> 500.
    """
    resp = auth_client.get(
        "/metrics/api/journals", params={"page": str(10**40)}
    )
    assert resp.status_code < 500, (
        f"GET /metrics/api/journals?page=10**40 returned "
        f"{resp.status_code}: {resp.text[:300]}"
    )
    # The clamp is the subject, so the route must have actually REACHED it.
    # Before the reference DB was seeded above, this route answered 503
    # "Journal reference database not available" long before any pagination
    # ran — which satisfies `< 500` and proves nothing about clamping.
    #
    # 400 is the clamp firing, not a failure: an absurd page is rejected with
    # an explicit ceiling rather than reaching `.offset()` and raising
    # OverflowError from SQLite's 64-bit conversion (which is the 500 this
    # test exists to prevent). Asserting the message pins that it was the
    # PAGE bound that rejected it, not some other 400.
    assert resp.status_code == 400, (
        f"expected the page clamp to reject page=10**40 with 400; got "
        f"{resp.status_code}: {resp.text[:300]}"
    )
    assert "page exceeds maximum" in resp.text, (
        f"got a 400, but not from the page clamp: {resp.text[:300]}"
    )