~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
Whole-surface smoke test: parameterized GET routes and mutating routes
(POST/PUT/PATCH/DELETE) must not 500 for a syntactically valid but
nonexistent resource, and must speak the right content-type back to the
caller.
Companion to ``test_all_endpoints.py``, which sweeps the ~110
non-parameterized authenticated GETs and explicitly ``continue``s on any
path containing ``{`` — this is a separate module (so it shards
independently) covering exactly what that one skips:
(a) PARAMETERIZED GET routes — every ``{param}`` filled with a
nonexistent-but-syntactically-valid value: ``int``-annotated params
(checked against the endpoint's own signature) get a large integer;
everything else gets a UUID that will never collide with a real row.
(b) MUTATING routes (POST/PUT/PATCH/DELETE) — authenticated, a valid
CSRF header, and ``json={}``. A hand-reviewed, commented deny-list
(MUTATING_DENY_LIST below) excludes routes that would start real
background work (research, benchmarks, RAG indexing, background
threads), hit a real network, or are the auth flow itself. Every
route that IS swept was verified by reading its handler (and, where
relevant, the service layer under it) to return a fast 4xx from a
missing-field or missing-resource check, before doing anything
expensive — not assumed safe by name. Several routes that *look*
dangerous by name (rag/index-document, rag/test-embedding,
rag/configure, most of the download-* family) validate a required
field and 400 before doing anything expensive, so they are NOT
denied; they're swept normally.
Two assertions per route, not one:
* ``status_code < 500`` — a missing/absent resource is a 4xx, never a
5xx. The dominant bug class this catches: a router wraps its handler
in a bare ``except Exception: return JSONResponse(..., status_code=500)``.
When the service layer raises a domain exception that already carries
the right status (e.g. ``SubscriptionNotFoundException``,
status_code=404), that bare except swallows it and downgrades it to
500 — shadowing the ``NewsAPIException`` handler registered in
``fastapi_app.py`` that would otherwise have rendered it correctly. A
narrower variant of the same observable bug shows up without any
exception at all: a handler that collapses every
``(success=False, reason)`` outcome from its service layer to 500,
even when the reason is "not found" (see
``POST /library/api/download/{resource_id}``). Two routes are
documented exceptions to the "< 500" rule (``KNOWN_5XX`` below): their
correct, intended answer is a deliberate, typed 501 (feature not
implemented) — the same convention test_all_endpoints.py's
KNOWN_NON_2XX already uses for the sibling GET /news/api/categories.
* Content-type negotiation. This PR shipped a real regression where a
404 carried the right status but the wrong content-type (raw JSON to
a browser navigation instead of HTML — fixed in "fix(errors): return
HTML, not raw JSON, for browser 404s"). A status-only assertion would
never have caught that. API-shaped paths must answer
``application/json``; the small, verified set of genuine full-page
routes (``HTML_PAGE_ROUTES`` below) must answer ``text/html`` for a
browser-style ``Accept: text/html`` request. Redirects (3xx) and
empty (204) bodies are exempt — there's no user-visible body
content-type to check. One route (``STREAMING_CONTENT_TYPE_EXEMPT``)
always answers with an SSE stream by design and is exempt for the
same reason.
"""
import inspect
import os
import re
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 our parametrize
# decorators import the app at collection time. Force it now (mirrors
# test_all_endpoints.py).
os.environ.setdefault("TESTING", "1")
VALID_BUT_MISSING_INT = "999999999"
VALID_BUT_MISSING_UUID = "00000000-0000-4000-8000-000000000000"
_PATH_PARAM_RE = re.compile(r"\{([a-zA-Z_][a-zA-Z0-9_]*)(?::[^}]*)?\}")
# Test-only probe routes registered on the live `app` singleton by
# tests/web/test_middleware_order_and_headers.py at import time — collection
# order dependent, not product code. Same exclusion test_all_endpoints.py
# applies to its own sweep.
_PROBE_PREFIX = "/__"
# ----------------------------------------------------------------------------
# Genuine full-page routes: server-rendered HTML shells that ignore the path
# param's validity entirely (the real data is fetched client-side via JS),
# verified by reading every handler body — none of them perform a DB lookup
# that could fail before rendering the template. A browser navigating here
# with `Accept: text/html`, missing id or not, must get text/html back.
#
# The "document viewer" family in library.py/rag.py (`/library/document/{id}`,
# `.../pdf`, `.../txt`, `.../chunks`) IS included below. All four are ordinary
# <a href> links out of library.html, so a missing/unavailable document must
# render an HTML page, not a raw JSON body in the browser's JSON viewer — see
# library.py's "Browser navigation: return text/html, not JSON" comments on
# each handler, landed in 9ccd27e39 (fix(library): finish the HTML-vs-JSON
# rule on the two viewer page routes) and pinned by
# test_library_port_fidelity.py. Their /api/ siblings
# (`/library/api/document/{id}/pdf`, `.../text`) are NOT in this set — they
# keep the JSON contract every other /api/ route has and fall through to the
# JSON-default branch below.
# ----------------------------------------------------------------------------
HTML_PAGE_ROUTES = {
("GET", "/progress/{research_id}"),
("GET", "/details/{research_id}"),
("GET", "/results/{research_id}"),
("GET", "/chat/{session_id}"),
("GET", "/notes/{note_id}"),
("GET", "/library/collections/{collection_id}"),
("GET", "/library/collections/{collection_id}/upload"),
("GET", "/news/subscriptions/{subscription_id}/edit"),
("GET", "/library/document/{document_id}"),
("GET", "/library/document/{document_id}/pdf"),
("GET", "/library/document/{document_id}/txt"),
("GET", "/library/document/{document_id}/chunks"),
}
# One swept mutating route always answers with a Server-Sent-Events stream
# (`media_type="text/event-stream"`) by design — it takes no request body at
# all, so there is no validation branch that would 400 before the stream
# starts. Not a bug (the SSE body itself reports "0 files" cleanly); just
# not the JSON contract every other route in the default bucket has.
STREAMING_CONTENT_TYPE_EXEMPT = {
("POST", "/library/api/download-all-text"),
}
# ----------------------------------------------------------------------------
# Mutating routes whose correct, intended response is a >=500 status,
# because the underlying feature is deliberately not implemented — not
# because a domain exception got swallowed. Same convention as
# test_all_endpoints.py's KNOWN_NON_2XX, which allows 501 for the sibling
# GET /news/api/categories for the identical reason (both call into
# news/api.py functions that unconditionally raise NotImplementedException,
# status_code=501). Once the NewsAPIException re-raise fix landed, these two
# routes now correctly answer 501 instead of the 500 they used to leak —
# that's the fix working, not a route this sweep should still flag.
# ----------------------------------------------------------------------------
KNOWN_5XX = {
("POST", "/news/api/preferences"): (
501,
"api.save_news_preferences() always raises NotImplementedException",
),
("POST", "/news/api/research/{card_id}"): (
501,
"api.research_news_item() always raises NotImplementedException",
),
}
# ----------------------------------------------------------------------------
# Parameterized GET routes excluded from the sweep. Same reasoning as
# test_all_endpoints.py's SKIP_PATHS for /library/api/rag/info et al. (see
# the full postmortem comment there). Currently empty: the one
# parameterized GET route that used to need its own entry here,
# /library/api/collections/{collection_id}/index, is now a POST and is
# covered by MUTATING_DENY_LIST instead (see its entry there — "moved here
# with the verb change"). Kept as a placeholder for the next parameterized
# GET route with the same hazard, so it lands here rather than in that
# file's (non-parameterized-only) skip list.
# ----------------------------------------------------------------------------
GET_DENY_LIST = {}
# ----------------------------------------------------------------------------
# Mutating routes excluded from the sweep. Every entry starts real
# background work, makes a real network call, or is the auth flow itself.
# Each one was verified by reading the actual handler body (and the service
# layer it calls, where relevant) rather than assumed from the route name.
# ----------------------------------------------------------------------------
MUTATING_DENY_LIST = {
("POST", "/api/start_research"): (
"starts a real background research run (spawns a worker thread "
"that calls the configured LLM/search backend)."
),
("POST", "/research/api/start"): (
"thin alias — api.api_start_research() calls "
"research.start_research() directly; same background research "
"run as /api/start_research."
),
("POST", "/api/followup/start"): (
"same family as /api/start_research: on success it commits a "
"ResearchHistory row and spawns a background research thread "
"(_start_followup_sync -> start_research_process). Today an "
"empty body only short-circuits at the 'llm.model is not "
"configured' 400 gate because a fresh throwaway test user has "
"no llm.model set — that's an environment default, not a code "
"guarantee, so this is denied on the same basis as the two "
"routes above rather than relied on to fail closed forever."
),
("POST", "/api/v1/analyze_documents"): (
"/api/v1/* is a versioned external-integration surface with its "
"own contract tests (test_api_v1_fastapi.py); once past body "
"validation it does real LLM/search work."
),
("POST", "/api/v1/generate_report"): (
"/api/v1/* — real LLM work once validated; see analyze_documents."
),
("POST", "/api/v1/quick_summary"): (
"/api/v1/* — real LLM work once validated; see analyze_documents."
),
("POST", "/api/v1/research/{research_id}/export/{format}"): (
"path lives under /api/v1/ (defined in research.py, not "
"api_v1.py, but same blanket exclusion — part of the versioned "
"external surface)."
),
("POST", "/auth/login"): (
"auth flow — explicit deny category; has its own dedicated tests."
),
("POST", "/auth/register"): (
"auth flow — explicit deny category; has its own dedicated tests."
),
("POST", "/auth/logout"): (
"auth flow — explicit deny category; also practically required: "
"it would deauthenticate the shared module-scoped sweep client "
"mid-run and fail every test collected after it."
),
("POST", "/auth/change-password"): (
"auth flow — explicit deny category; has its own dedicated tests."
),
("POST", "/library/api/collections/{collection_id}/index/start"): (
"spawns a real background threading.Thread running "
"_background_index_worker (embedding indexing) unconditionally "
"— the handler creates the TaskMetadata row and starts the "
"thread before ever checking whether collection_id refers to a "
"real collection."
),
("POST", "/library/api/collections/{collection_id}/index"): (
"the parameterized sibling of /rag/index-all: same embedding/"
"index pipeline (force_reindex rebuilds vectors), so it both "
"starts real work and eagerly loads the embedding model. Was a "
"GET with the same hazard — moved here with the verb change."
),
("POST", "/library/api/rag/index-all"): (
"calls get_rag_service() before any validation, which eagerly "
"loads the ~400MB sentence-transformers embedding model — the "
"same OOM hazard test_all_endpoints.py's SKIP_PATHS documents "
"for the sibling /rag/info and /rag/stats GETs — and with no "
"collection_id in the URL it resolves the caller's default "
"library and runs the real bulk embedding pipeline over it. It "
"also always answers text/event-stream, so it could not meet "
"this sweep's application/json contract either."
),
("POST", "/library/api/zotero/sync"): (
"spawns a real threading.Thread that calls the live Zotero API "
"when Zotero is configured for the user. Today a fresh "
"throwaway test user has no Zotero config, so "
"`cfg.is_configured` gates it to a 400 before the thread "
"starts — but that's an environment default, not a code "
"guarantee, so it's denied rather than relied upon."
),
("POST", "/metrics/api/journal-data/download"): (
"downloads real journal reference data — its own docstring says "
"'several hundred MB' and it's rate-limited to 2/hour."
),
("POST", "/api/scheduler/run-now"): (
"schedules a real APScheduler background job "
"(_process_user_documents) for the calling user; unlike the "
"news scheduler routes it isn't gated behind a settings flag."
),
("POST", "/benchmark/api/start"): (
"starts a real benchmark run (background thread doing "
"search/LLM calls)."
),
("POST", "/benchmark/api/start-simple"): (
"same as /benchmark/api/start — simplified-config variant, same "
"background run."
),
}
@pytest.fixture(scope="module")
def auth_client():
"""Authenticated TestClient for a freshly-created throwaway user, with
the CSRF header pre-attached so every mutating request in this module
passes CSRFMiddleware without repeating the handshake per-test.
Reuses the auth_client pattern from test_all_endpoints.py (~lines
79-125): a fresh registered+logged-in user against the live app via
TestClient. No mocking, no network, no LLM.
"""
from local_deep_research.web.fastapi_app import app
c = TestClient(app, raise_server_exceptions=False)
user = f"test_surface_{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]}"
)
# Attach the CSRF header once for every subsequent state-changing
# request this client makes (mirrors test_state_changing_flows.py's
# _attach_csrf_header).
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)
def _iter_app_routes():
from local_deep_research.web.fastapi_app import app
for route in app.routes:
methods = getattr(route, "methods", None) or set()
path = getattr(route, "path", None)
endpoint = getattr(route, "endpoint", None)
if not path or path.startswith(_PROBE_PREFIX):
continue
yield path, methods, endpoint
def _fill_path_params(path: str, endpoint) -> str:
"""Replace every ``{param}`` in *path* with a nonexistent-but-valid
value: the big integer for params annotated ``int`` on the endpoint's
own signature, a UUID otherwise (per spec: untyped/str params, and
anything we can't introspect, default to the UUID)."""
try:
sig = inspect.signature(endpoint) if endpoint else None
except (TypeError, ValueError):
sig = None
def _replace(match: "re.Match[str]") -> str:
name = match.group(1)
param = sig.parameters.get(name) if sig else None
annotation = param.annotation if param is not None else inspect._empty
if annotation is int:
return VALID_BUT_MISSING_INT
return VALID_BUT_MISSING_UUID
return _PATH_PARAM_RE.sub(_replace, path)
def _enumerate_parameterized_get_routes() -> list[tuple[str, str]]:
"""Every concrete (filled_url, path) for a GET route with a path
param, minus GET_DENY_LIST."""
cases: dict[str, str] = {}
for path, methods, endpoint in _iter_app_routes():
if "GET" not in methods or "{" not in path:
continue
if ("GET", path) in GET_DENY_LIST:
continue
cases[path] = _fill_path_params(path, endpoint)
return sorted((url, path) for path, url in cases.items())
def _enumerate_mutating_routes() -> list[tuple[str, str, str]]:
"""Every (method, filled_url, path) for POST/PUT/PATCH/DELETE routes,
minus MUTATING_DENY_LIST."""
cases: dict[tuple[str, str], str] = {}
for path, methods, endpoint in _iter_app_routes():
for method in methods & {"POST", "PUT", "PATCH", "DELETE"}:
if (method, path) in MUTATING_DENY_LIST:
continue
cases[(method, path)] = _fill_path_params(path, endpoint)
return sorted((method, url, path) for (method, path), url in cases.items())
def _assert_content_type(resp, method: str, path: str) -> None:
"""Content-type negotiation, not just status. Redirects and empty
(204) bodies carry no meaningful content-type contract and are
skipped, as is the one route known to always stream SSE."""
if resp.status_code in (301, 302, 303, 307, 308, 204):
return
if (method, path) in STREAMING_CONTENT_TYPE_EXEMPT:
return
content_type = resp.headers.get("content-type", "")
if (method, path) in HTML_PAGE_ROUTES:
assert content_type.startswith("text/html"), (
f"{method} {path}: browser navigation (Accept: text/html) "
f"got content-type {content_type!r} (status "
f"{resp.status_code}), expected text/html"
)
else:
assert content_type.startswith("application/json"), (
f"{method} {path}: expected application/json, got "
f"{content_type!r} (status {resp.status_code}): "
f"{resp.text[:200]}"
)
_GET_CASES = _enumerate_parameterized_get_routes()
_MUTATING_CASES = _enumerate_mutating_routes()
@pytest.mark.parametrize(
"url,path", _GET_CASES, ids=[f"GET {p}" for _u, p in _GET_CASES]
)
def test_parameterized_get_no_500(auth_client, url: str, path: str):
"""A parameterized GET against a nonexistent resource must be a clean
4xx (never 5xx) with the right content-type."""
is_html_page = ("GET", path) in HTML_PAGE_ROUTES
headers = {"Accept": "text/html" if is_html_page else "application/json"}
# follow_redirects=False: assert on this route's OWN response, not on
# whatever a followed 3xx lands on (which would hide a misbehaving
# redirect, or mask a downstream page's content-type as this route's).
resp = auth_client.get(url, headers=headers, follow_redirects=False)
assert resp.status_code < 500, (
f"GET {path} ({url}) returned {resp.status_code}: {resp.text[:300]}"
)
_assert_content_type(resp, "GET", path)
@pytest.mark.parametrize(
"method,url,path",
_MUTATING_CASES,
ids=[f"{m} {p}" for m, _u, p in _MUTATING_CASES],
)
def test_mutating_route_no_500(auth_client, method: str, url: str, path: str):
"""A mutating request against a nonexistent resource (or an empty
body) must be a clean 4xx (never 5xx) with the right content-type —
except the couple of routes in KNOWN_5XX, whose correct answer is a
deliberate, typed 5xx (feature not implemented), not a swallowed 500."""
resp = auth_client.request(
method,
url,
json={},
headers={"Accept": "application/json"},
follow_redirects=False,
)
known = KNOWN_5XX.get((method, path))
if known is not None:
expected_status, reason = known
assert resp.status_code == expected_status, (
f"{method} {path} ({url}) returned {resp.status_code}, "
f"expected the documented {expected_status} ({reason}): "
f"{resp.text[:300]}"
)
else:
assert resp.status_code < 500, (
f"{method} {path} ({url}) returned {resp.status_code}: "
f"{resp.text[:300]}"
)
_assert_content_type(resp, method, path)
def test_deny_lists_reference_live_routes():
"""Guard against deny-list rot: every GET_DENY_LIST / MUTATING_DENY_LIST
/ HTML_PAGE_ROUTES / STREAMING_CONTENT_TYPE_EXEMPT / KNOWN_5XX entry must
name a route that still exists — a stale entry silently stops excluding
(or special-casing) anything and nobody notices."""
live = {(m, p) for p, methods, _e in _iter_app_routes() for m in methods}
for label, table in (
("GET_DENY_LIST", GET_DENY_LIST),
("MUTATING_DENY_LIST", MUTATING_DENY_LIST),
("HTML_PAGE_ROUTES", {k: None for k in HTML_PAGE_ROUTES}),
(
"STREAMING_CONTENT_TYPE_EXEMPT",
{k: None for k in STREAMING_CONTENT_TYPE_EXEMPT},
),
("KNOWN_5XX", KNOWN_5XX),
):
stale = set(table) - live
assert not stale, (
f"{label} entries with no matching live route: {stale}"
)