~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
"""Regression tests for V2-R1-3 strict-snapshot coverage at
``/library/api/rag/models`` (``get_available_models``).
Ported (with FastAPI adaptation) from the Flask-era
``tests/research_library/routes/test_rag_routes_strict_snapshot.py`` added
by commit 87537d9ec ("fix(security): operator-gate unprotected egress and
harden policy-sensitive consumers (#5148)"). That file imported ``flask``
(not installed on this branch) via
``tests/research_library/routes/_route_helpers_rag.py``, which broke
collection of this entire directory. The SOURCE fix it exercises landed
intact in this branch's FastAPI router, in ``get_available_models``
(``src/local_deep_research/web/routers/rag.py:686-853``) -- only the
Flask-era test scaffolding needed porting.
A non-strict ``get_all_settings`` silently falls back to JSON defaults when
the underlying settings query fails (SQLAlchemyError / stale enum row). A
defaults-only snapshot can lack the operator-selected scope and provider
classification inputs, and thereby admit a cloud embedder's model-list probe
under a local-only posture. The route must therefore read settings with
``strict=True`` and convert any query failure into
``PolicyDeniedError("settings_unavailable")`` BEFORE any provider discovery,
credential read, or network probe (rag.py:702-724).
Follows the direct-call idiom established by
``test_rag_routes_cancel_and_worker_wiring.py`` /
``test_rag_routes_collections.py``: ``get_available_models`` is a plain
(non-async) callable, called directly with ``username`` passed as a keyword
(bypassing ``Depends(require_auth)`` resolution). ``get_user_db_session``
and ``get_settings_manager`` are imported function-locally inside the route
(rag.py:692-693), so -- matching the original Flask helper's "patch at
source for function-local imports" comment -- both are patched at their
SOURCE modules rather than on ``local_deep_research.web.routers.rag``.
Error paths return a starlette ``JSONResponse``, asserted via
``.status_code`` / ``json.loads(.body)`` rather than the Flask original's
``response.get_json()``.
"""
import json
from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch
import pytest
from sqlalchemy.exc import SQLAlchemyError
from local_deep_research.security.egress.policy import (
Decision,
PolicyDeniedError,
)
from local_deep_research.settings.manager import SettingsManager
from local_deep_research.web.routers.rag import get_available_models
_DB_CTX = "local_deep_research.database.session_context"
_DB_UTILS = "local_deep_research.utilities.db_utils"
_EMBEDDINGS = "local_deep_research.embeddings.embeddings_config"
_POLICY = "local_deep_research.security.egress.policy"
def _fake_request():
"""``get_available_models`` never reads anything off the request
object itself (only ``username``), so an empty stub is sufficient."""
return Mock()
@contextmanager
def _ctx_yielding(session):
yield session
def _settings_manager_with_query_failure(query_error):
"""Build a real ``SettingsManager`` whose settings-enumeration query
raises.
Mirrors the production read sequence: an initialization-count query
(1) followed by the settings-enumeration query (2) which we fail.
"""
database_session = MagicMock()
initialization_query = MagicMock()
initialization_query.count.return_value = 1
settings_query = MagicMock()
settings_query.all.side_effect = query_error
database_session.query.side_effect = [initialization_query, settings_query]
return SettingsManager(database_session), database_session
@pytest.mark.parametrize(
"query_error",
[
SQLAlchemyError("connection lost"),
LookupError("unknown setting type"),
],
ids=["sqlalchemy_error", "stale_enum_row"],
)
def test_get_available_models_refuses_before_any_probe_when_settings_query_fails(
query_error,
):
"""``/api/rag/models`` must refuse with ``settings_unavailable`` BEFORE
importing provider classes, reading credentials, or opening any
network probe when the underlying settings query fails.
Regression for V2-R1-3: a non-strict snapshot fell back to JSON
defaults, then handed the permissive snapshot to provider discovery
-- opening the door to a cloud embedder probe the operator had not
authorized.
"""
manager, database_session = _settings_manager_with_query_failure(
query_error
)
# Provider discovery surface -- none of these may run when the
# settings snapshot is unevaluable. Patched at the source modules so
# the route's function-local imports are caught.
with (
patch(f"{_EMBEDDINGS}._get_provider_classes") as mock_get_classes,
patch(f"{_POLICY}.evaluate_embeddings") as mock_evaluate,
patch(f"{_POLICY}.context_from_snapshot") as mock_context,
patch(f"{_DB_UTILS}.get_settings_manager", return_value=manager),
patch(
f"{_DB_CTX}.get_user_db_session",
side_effect=lambda *a, **k: _ctx_yielding(database_session),
),
):
result = get_available_models(_fake_request(), username="testuser")
# Then: a 503 with no provider construction, no policy evaluation,
# no network probe -- the refusal happens at the snapshot gate.
assert result.status_code == 503
body = json.loads(result.body)
assert body["success"] is False
assert "unavailable" in body["error"].lower()
# The settings-enumeration query was issued once (proving strict=True
# ran); no further DB reads occurred downstream.
assert database_session.query.call_count == 2
mock_get_classes.assert_not_called()
mock_evaluate.assert_not_called()
mock_context.assert_not_called()
def test_get_available_models_400_for_non_settings_policy_denial():
"""A non-``settings_unavailable`` ``PolicyDeniedError`` (e.g. a real
scope-mismatch raised after the snapshot was successfully read) must
surface as 400, distinct from the 503 used for ``settings_unavailable``
and the 500 used for unrelated server crashes.
Locks the route's exception-handler contract so a future refactor
cannot collapse these branches back to the generic 500 path.
"""
good_session = MagicMock()
initialization_query = MagicMock()
initialization_query.count.return_value = 1
settings_query = MagicMock()
settings_query.all.return_value = []
good_session.query.side_effect = [initialization_query, settings_query]
good_manager = SettingsManager(good_session)
non_settings_denial = PolicyDeniedError(
Decision(False, "scope_mismatch_private_only"),
target="available_rag_models",
)
with (
patch(
f"{_EMBEDDINGS}._get_provider_classes",
side_effect=non_settings_denial,
),
patch(f"{_DB_UTILS}.get_settings_manager", return_value=good_manager),
patch(
f"{_DB_CTX}.get_user_db_session",
side_effect=lambda *a, **k: _ctx_yielding(good_session),
),
):
result = get_available_models(_fake_request(), username="testuser")
assert result.status_code == 400
body = json.loads(result.body)
assert body["success"] is False
# Distinct from the settings_unavailable 503 branch -- this denial
# reason is NOT double-wrapped into a generic settings-unavailable
# message.
assert "unavailable" not in body["error"].lower()