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.

5.0 KB
Raw Blame
# allow: no-sut-import — drives the real FastAPI app only through the shared
# `authenticated_client` fixture (tests/conftest.py); no direct SUT import.
"""Port-fidelity pins for `routers/research.py` and `routers/history.py`.

Two behaviours that `main`'s Flask blueprints had and the FastAPI port
initially lost. The HEAD difference remains an ``xfail(strict=True)``;
the malformed-body response has been restored and is a passing regression.

1. HEAD on a GET route.
   ``werkzeug.routing.Rule.__init__`` adds ``HEAD`` to ``methods``
   whenever ``GET`` is present, so every ``@research_bp.route(...)`` /
   ``@history_bp.route(...)`` GET rule answered HEAD. FastAPI's
   ``APIRoute.__init__`` sets ``self.methods = {m.upper() for m in
   methods}`` and never adds HEAD, so the ported routes reply
   ``405 Allow: GET``.

   This is load-bearing inside research.py itself:
   ``_log_export_exempt`` (the port of main's #5369
   ``_is_log_export_rate_limit_exempt``) exists to keep HEAD
   pre-flights out of the 10/min ``/logs/export`` quota, and its
   docstring asserts "Starlette answers HEAD on a route registered with
   ``@router.get``". It does not — the ``request.method == "HEAD"``
   branch is unreachable, because the request 405s before the limiter's
   ``exempt_when`` is ever consulted.

2. ``POST /api/save_raw_config`` with a body that will not parse.
   main gated the route with ``@require_json_body(error_format=
   "success")``, whose ``request.get_json(silent=True)`` collapsed BOTH
   failure modes — unparseable body and parsed-but-not-an-object — into
   one ``400 {"success": false, "error": "Request body must be valid
   JSON"}``. The route now uses ``read_json_dict`` so both invalid-body
   cases return the same envelope, including the ``success`` key used by
   callers.
"""

import pytest


@pytest.fixture
def client(authenticated_client):
    return authenticated_client


# Two GET routes, one from each router in scope, that a browser or proxy
# would realistically pre-flight or that an uptime check would probe.
@pytest.mark.parametrize(
    "url",
    [
        "/api/history",  # routers/research.py::get_history
        "/history/api",  # routers/history.py::get_history
        "/api/config/limits",  # routers/research.py::get_upload_limits
    ],
)
@pytest.mark.xfail(
    strict=True,
    reason=(
        "fastapi.routing.APIRoute.__init__ does not add HEAD to .methods "
        "the way werkzeug.routing.Rule.__init__ does, so every ported GET "
        "route answers 405 where main answered the GET's status"
    ),
)
def test_head_is_answered_like_main(client, url):
    """HEAD must reach the handler and report the same status as GET.

    Body stripping is the ASGI/WSGI server's job (uvicorn's h11 layer,
    Werkzeug's) and is not asserted here — the regression is that the
    request never reaches the handler at all.
    """
    get_resp = client.get(url)
    assert get_resp.status_code == 200, (
        f"precondition: GET {url} should be 200 for an authenticated "
        f"client, got {get_resp.status_code}"
    )

    head_resp = client.head(url)
    assert head_resp.status_code == get_resp.status_code, (
        f"HEAD {url} returned {head_resp.status_code} "
        f"(Allow: {head_resp.headers.get('allow')!r}) but GET returned "
        f"{get_resp.status_code}; Werkzeug served HEAD from the GET rule"
    )


def test_save_raw_config_unparseable_body_keeps_success_envelope(client):
    """An unparseable body gets main's ``success`` envelope, not the generic one."""
    resp = client.post(
        "/api/save_raw_config",
        content=b"this is not json",
        headers={"Content-Type": "application/json"},
    )

    assert resp.status_code == 400, (
        f"expected 400, got {resp.status_code}: {resp.text[:200]}"
    )
    body = resp.json()
    assert body.get("success") is False, (
        "main's require_json_body(error_format='success') returned "
        f"{{'success': False, 'error': ...}}; port returned {body!r}"
    )
    assert body.get("error") == "Request body must be valid JSON", (
        f"error text drifted: {body!r}"
    )


def test_save_raw_config_non_object_body_keeps_success_envelope(client):
    """The sibling failure mode — a body that parses but is not an object.

    Control for the xfail above: this branch WAS ported (the handler's
    ``isinstance(data, dict)`` check calls ``json_body_error("success",
    ...)``), so it still matches main exactly. It shows the envelope the
    unparseable-body case is missing, and that the assertion above is
    checking a shape the route is genuinely capable of producing.
    """
    resp = client.post(
        "/api/save_raw_config",
        content=b"[1, 2, 3]",
        headers={"Content-Type": "application/json"},
    )

    assert resp.status_code == 400, (
        f"expected 400, got {resp.status_code}: {resp.text[:200]}"
    )
    body = resp.json()
    assert body == {
        "success": False,
        "error": "Request body must be valid JSON",
    }, f"envelope drifted from main's require_json_body(...): {body!r}"