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.

4.0 KB
Raw Blame
"""Follow-up endpoints must reject a non-object JSON body with 400, not 500.

Flask guarded both follow-up routes with ``@require_json_body``, which
returned **400** whenever the parsed body was not a ``dict``. The FastAPI port
kept the malformed-JSON branch but dropped the ``isinstance(data, dict)``
check, so a body that is valid JSON yet not an object — ``[1, 2]``, ``"x"``,
``3`` — parsed successfully and then hit ``data.get(...)``, raising
``AttributeError`` into the module's outer ``except Exception`` and surfacing
as a 500 with a logged stack trace.

Both sibling routers kept the gate (``chat._json_object_body``,
``notes._notes_json_body``); follow-up was the only one that lost it, which
makes this the last member of the ``except Exception -> 500`` family swept
earlier in this branch.

Impact is a wrong status code and log noise rather than a security or
data-integrity problem — the endpoints require an authenticated session and a
valid CSRF token. Pinned because a 500 on malformed input is exactly the
signal that hides real 500s in the logs.
"""

import pytest

# Imported so this file fails loudly if the routes are renamed or removed,
# rather than silently testing paths that no longer exist.
from local_deep_research.web.routers.followup import router as followup_router

FOLLOWUP_ROUTE_PATHS = {
    route.path for route in followup_router.routes if hasattr(route, "path")
}

BAD_BODIES = [
    pytest.param([1, 2], id="json-array"),
    pytest.param("a string", id="json-string"),
    pytest.param(3, id="json-number"),
    pytest.param(True, id="json-bool"),
    pytest.param(None, id="json-null"),
]

FOLLOWUP_ROUTES = ["/api/followup/prepare", "/api/followup/start"]


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 body guard."""
    for full_path in FOLLOWUP_ROUTES:
        suffix = full_path.rsplit("/", 1)[-1]
        assert any(p.endswith(suffix) for p in FOLLOWUP_ROUTE_PATHS), (
            f"{full_path} is no longer registered on the follow-up router "
            f"(known paths: {sorted(FOLLOWUP_ROUTE_PATHS)})"
        )


@pytest.mark.parametrize("route", FOLLOWUP_ROUTES)
@pytest.mark.parametrize("body", BAD_BODIES)
def test_non_object_body_is_client_error(authenticated_client, route, body):
    """Valid JSON that is not an object must be 400, never 5xx."""
    resp = authenticated_client.post(route, json=body)

    assert resp.status_code < 500, (
        f"{route} returned {resp.status_code} for a non-object JSON body "
        f"({body!r}). Flask returned 400 here; a 500 means the isinstance "
        f"guard is missing and AttributeError reached the outer handler."
    )
    assert resp.status_code == 400, (
        f"{route} returned {resp.status_code}; expected 400 to match Flask "
        f"and the sibling chat/notes routers"
    )


@pytest.mark.parametrize("route", FOLLOWUP_ROUTES)
def test_malformed_json_is_still_400(authenticated_client, route):
    """The pre-existing malformed-body branch must keep working."""
    resp = authenticated_client.post(
        route,
        content=b"{not valid json",
        headers={"Content-Type": "application/json"},
    )
    assert resp.status_code == 400, (
        f"{route} returned {resp.status_code} for malformed JSON; expected 400"
    )


@pytest.mark.parametrize("route", FOLLOWUP_ROUTES)
def test_object_body_is_not_rejected_by_the_guard(authenticated_client, route):
    """A dict body must get past the shape check.

    It may still fail validation for missing fields — that is the handler's
    job — but it must not be refused as a malformed shape, which would mean
    the guard is too broad.
    """
    resp = authenticated_client.post(route, json={})

    if resp.status_code == 400:
        body = resp.json()
        assert "must be a JSON object" not in str(body), (
            f"{route} rejected a dict body as a non-object; the isinstance "
            f"guard is inverted or too broad: {body}"
        )