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.1 KB
Raw Blame
"""Readable 422 responses through the application's real exception handlers."""

import pytest
from fastapi import FastAPI, HTTPException, Query
from fastapi.testclient import TestClient
from pydantic import BaseModel, Field, field_validator

from local_deep_research.web.fastapi_app import _register_exception_handlers


class _Payload(BaseModel):
    title: str
    count: int = Field(gt=0)


class _SensitivePayload(BaseModel):
    password: str

    @field_validator("password")
    @classmethod
    def reject_password(cls, value: str) -> str:
        raise ValueError(f"rejected {value}")


@pytest.fixture
def client():
    app = FastAPI()
    _register_exception_handlers(app)

    @app.post("/api/example")
    @app.post("/api/v1/example")
    def example(payload: _Payload):
        return payload.model_dump()

    @app.get("/api/example")
    def query_example(limit: int = Query(gt=0)):
        return {"limit": limit}

    @app.get("/api/denied")
    def denied():
        raise HTTPException(status_code=403, detail="Action unavailable")

    @app.post("/api/sensitive")
    def sensitive(payload: _SensitivePayload):
        return payload.model_dump()

    with TestClient(app) as test_client:
        yield test_client


def test_body_errors_have_a_readable_summary_and_structured_fields(client):
    response = client.post("/api/example", json={"count": 0})
    assert response.status_code == 422
    body = response.json()
    assert isinstance(body["error"], str)
    assert "title: Field required" in body["error"]
    assert "count: Value is too small" in body["error"]
    assert {tuple(error["loc"]) for error in body["detail"]} == {
        ("body", "title"),
        ("body", "count"),
    }
    assert {error["type"] for error in body["detail"]} == {
        "missing",
        "greater_than",
    }


def test_query_error_names_the_field(client):
    response = client.get("/api/example", params={"limit": "many"})
    assert response.status_code == 422
    assert response.json()["error"].startswith("Invalid request: limit:")
    assert response.json()["detail"][0]["type"] == "int_parsing"


def test_validation_response_does_not_echo_submitted_values(client):
    secret = "private-value-for-validation-test"
    response = client.post(
        "/api/example", json={"title": {"credential": secret}, "count": 0}
    )
    assert response.status_code == 422
    assert secret not in response.text
    for error in response.json()["detail"]:
        assert set(error) == {"loc", "msg", "type"}


def test_custom_validator_message_cannot_echo_submitted_secret(client):
    secret = "synthetic-secret-for-validator-test"
    response = client.post("/api/sensitive", json={"password": secret})

    assert response.status_code == 422
    assert secret not in response.text
    assert (
        response.json()["error"] == "Invalid request: password: Invalid value"
    )
    assert response.json()["detail"] == [
        {
            "loc": ["body", "password"],
            "msg": "Invalid value",
            "type": "value_error",
        }
    ]


def test_invalid_json_has_a_readable_error(client):
    response = client.post(
        "/api/example",
        content="{",
        headers={"Content-Type": "application/json"},
    )
    assert response.status_code == 422
    assert isinstance(response.json()["error"], str)
    assert response.json()["detail"][0]["type"] == "json_invalid"
    assert response.json()["error"] == (
        "Invalid request: position 1: Invalid JSON"
    )


def test_programmatic_api_keeps_its_error_key(client):
    response = client.post("/api/v1/example", json={})
    assert response.status_code == 422
    assert response.json()["error"].startswith("Invalid request:")
    assert isinstance(response.json()["detail"], list)


def test_valid_payload_and_http_exception_keep_their_contracts(client):
    payload = {"title": "A valid title", "count": 2}
    response = client.post("/api/example", json=payload)
    assert response.status_code == 200
    assert response.json() == payload
    denied = client.get("/api/denied")
    assert denied.status_code == 403
    assert denied.json() == {"detail": "Action unavailable"}