~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
"""FastAPI re-port of the deleted ``tests/web/routes/test_settings_routes_deep_coverage.py``.
The Flask original drove ``web/routes/settings_routes.py`` through a
blueprint test client. On this branch the same handlers live in
``web/routers/settings.py``; the three bulk-write handlers were split into
an ``async`` wrapper plus a plain ``_*_sync(..., username)`` helper, so the
ports drive the sync helpers directly (the pattern already established by
``tests/web/routers/test_settings_namespace_guard.py`` and
``test_settings_cache_invalidation.py``).
Deviations from a literal transcription, each deliberate:
* Several originals asserted only ``resp.status_code == 200`` on a branch
whose whole point was an *invisible* side effect (which repair value was
written, which ``category`` a new row is created with). Deleting the guard
leaves such an assertion green — PORT.md Lesson 9. Those ports keep the
original's scenario but assert the side effect the original was named
after, so a mutation of the guard turns them red. Each such test says so.
* ``inject_csrf_token`` was a Flask context processor with no FastAPI
equivalent function; its successor is the Jinja env global installed in
``web/fastapi_app.py``. The assertion (a callable named ``csrf_token`` is
available to templates) is ported to that surface.
* ``TestGetBulkSettingsOuterException`` patched ``flask.jsonify`` to force
the *outer* except. The branch returns a plain dict, so the outer except is
reached by making ``request.query_params.getlist`` raise instead — the same
structural property (an error outside the per-key try still yields 500).
"""
import json
from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch
S = "local_deep_research.web.routers.settings"
# ---------------------------------------------------------------------------
# Plumbing (translated from the deleted tests/web/routes/_settings_route_helpers.py)
# ---------------------------------------------------------------------------
def _make_setting(
key="test.key",
value="val",
ui_element="text",
name="Test Key",
description="desc",
category="general",
setting_type="app",
editable=True,
visible=True,
options=None,
min_value=None,
max_value=None,
step=None,
updated_at=None,
):
"""Build a mock Setting ORM row (port of ``_make_setting``)."""
s = MagicMock()
s.key = key
s.value = value
s.ui_element = ui_element
s.name = name
s.description = description
s.category = category
s.type = setting_type
s.editable = editable
s.visible = visible
s.options = options
s.min_value = min_value
s.max_value = max_value
s.step = step
s.updated_at = updated_at
return s
def _patched_db(all_settings=None, first=None, session=None):
"""Patch ``settings.get_user_db_session`` with a mock session whose
``query(Setting).all()`` returns *all_settings*.
Replaces the Flask original's ``_authenticated_client`` (session cookie +
``@login_required`` mocking): on the branch the username is an explicit
parameter, so authentication plumbing is not part of these tests.
"""
query = MagicMock()
query.all.return_value = list(all_settings or [])
query.first.return_value = first
query.filter.return_value = query
query.filter_by.return_value = query
query.distinct.return_value = query
query.group_by.return_value = query
query.having.return_value = query
query.order_by.return_value = query
if session is None:
session = MagicMock()
session.query.return_value = query
@contextmanager
def fake_db_session(*a, **kw):
yield session
return session, patch(
f"{S}.get_user_db_session", side_effect=fake_db_session
)
@contextmanager
def _quiet_side_effects():
"""Silence the post-commit fan-out the originals never reached."""
with (
patch(f"{S}.invalidate_settings_caches"),
patch(f"{S}.reschedule_document_jobs_if_needed"),
patch(f"{S}.reschedule_zotero_jobs_if_needed"),
):
yield
def _body(resp):
"""Decode a JSONResponse body (handlers return dicts on success)."""
if isinstance(resp, dict):
return resp
return json.loads(resp.body)
def _fake_request(query_params=None):
"""Minimal stand-in for starlette's Request for the sync GET handlers."""
req = MagicMock()
params = dict(query_params or {})
req.query_params.get.side_effect = lambda k, d=None: params.get(k, d)
req.query_params.getlist.side_effect = lambda k: params.get(k, [])
return req
# ---------------------------------------------------------------------------
# _get_setting_from_session: db_session is None branch
# ---------------------------------------------------------------------------
class TestGetSettingFromSession:
"""_get_setting_from_session: db_session is None branch."""
def test_returns_default_when_db_session_is_none(self):
from local_deep_research.web.routers.settings import (
_get_setting_from_session,
)
@contextmanager
def _fake_ctx(*a, **kw):
yield None
with (
patch(f"{S}.get_user_db_session", side_effect=_fake_ctx),
patch(f"{S}.get_settings_manager") as mock_get_sm,
):
result = _get_setting_from_session(
"any.key", "testuser", "fallback"
)
assert result == "fallback"
# A None session must short-circuit: building a SettingsManager on it
# is what the guard exists to prevent. Asserting only the return value
# is not enough -- get_settings_manager(None, user).get_setting()
# also answers the default for an unknown key, so the guard could be
# deleted with the return-value assertion still green.
mock_get_sm.assert_not_called()
# ---------------------------------------------------------------------------
# inject_csrf_token context processor -> Jinja env global
# ---------------------------------------------------------------------------
class TestInjectCsrfToken:
"""Templates must still be handed a callable ``csrf_token``.
Flask's ``inject_csrf_token`` context processor returned
``{"csrf_token": <callable>}``. FastAPI has no context processors; the
equivalent is the Jinja environment global installed by
``web/fastapi_app.py`` (``env.globals["csrf_token"] = ...``). The
assertion is unchanged: a *callable* named ``csrf_token`` is reachable
from templates.
"""
def test_injects_callable(self):
import local_deep_research.web.fastapi_app # noqa: F401 (installs the global)
from local_deep_research.web.template_config import templates
assert "csrf_token" in templates.env.globals
assert callable(templates.env.globals["csrf_token"])
# ---------------------------------------------------------------------------
# save_all_settings: new-setting UI element detection
# ---------------------------------------------------------------------------
class TestSaveAllSettingsNewSettingUIDetection:
"""save_all_settings: new setting creation with different value types."""
def _create(self, form_data, created_type="app"):
from local_deep_research.web.routers.settings import (
_save_all_settings_sync,
)
created = _make_setting(key=next(iter(form_data)))
created.type = created_type
_, db_patch = _patched_db()
with (
db_patch,
_quiet_side_effects(),
patch(
f"{S}.create_or_update_setting", return_value=created
) as mock_create,
):
resp = _save_all_settings_sync(dict(form_data), "testuser")
return resp, mock_create
def test_new_bool_setting_gets_checkbox(self):
resp, mock_create = self._create({"app.flag": True})
assert not hasattr(resp, "status_code"), _body(resp)
assert mock_create.call_args[0][0]["ui_element"] == "checkbox"
def test_new_int_setting_gets_number(self):
resp, mock_create = self._create({"app.count": 42})
assert not hasattr(resp, "status_code"), _body(resp)
assert mock_create.call_args[0][0]["ui_element"] == "number"
def test_new_dict_setting_gets_textarea(self):
resp, mock_create = self._create(
{"report.structure": {"a": 1}}, created_type="report"
)
assert not hasattr(resp, "status_code"), _body(resp)
assert mock_create.call_args[0][0]["ui_element"] == "textarea"
def test_unknown_prefix_rejected_with_validation_error(self):
"""Unknown prefix is rejected by the namespace gate with 400."""
from local_deep_research.web.routers.settings import (
_save_all_settings_sync,
)
_, db_patch = _patched_db()
with (
db_patch,
_quiet_side_effects(),
patch(f"{S}.create_or_update_setting") as mock_create,
):
resp = _save_all_settings_sync(
{"custom.param": "value"}, "testuser"
)
assert resp.status_code == 400
data = _body(resp)
assert any(
e["key"] == "custom.param" and "not allowed" in e["error"]
for e in data["errors"]
)
mock_create.assert_not_called()
# ---------------------------------------------------------------------------
# save_all_settings: empty string keys skipped
# ---------------------------------------------------------------------------
class TestSaveAllSettingsSkipInvalidKeys:
"""save_all_settings: empty string keys are skipped."""
def test_empty_key_skipped(self):
from local_deep_research.web.routers.settings import (
_save_all_settings_sync,
)
setting = _make_setting(key="llm.model", value="gpt-4", editable=True)
setting.type = "llm"
_, db_patch = _patched_db([setting])
settings_manager = MagicMock(settings_locked=False)
with (
db_patch,
patch(f"{S}.get_settings_manager", return_value=settings_manager),
_quiet_side_effects(),
patch(f"{S}.validate_setting", return_value=(True, None)),
patch(
f"{S}.coerce_setting_for_write",
side_effect=lambda key, value, ui_element: value,
),
patch(f"{S}.set_setting", return_value=True) as mock_set,
patch(f"{S}.create_or_update_setting") as mock_create,
):
resp = _save_all_settings_sync(
{"": "ignored", "llm.model": "gpt-4"}, "testuser"
)
assert not hasattr(resp, "status_code"), _body(resp)
assert mock_set.call_count == 1
# The blank key must not fall through to the creation branch either.
mock_create.assert_not_called()
# ---------------------------------------------------------------------------
# save_all_settings: corrupted-value repair branches
# ---------------------------------------------------------------------------
class TestSaveAllSettingsCorruptedBranches:
"""save_all_settings: additional corrupted value correction branches.
The originals asserted only ``status_code == 200``, which stays green if
the whole repair block is deleted. These ports keep the same scenarios
and additionally pin the value actually handed to ``set_setting`` — the
thing each test is named after.
"""
def _save(self, setting, form_data):
from local_deep_research.web.routers.settings import (
_save_all_settings_sync,
)
_, db_patch = _patched_db([setting])
settings_manager = MagicMock(settings_locked=False)
with (
db_patch,
patch(f"{S}.get_settings_manager", return_value=settings_manager),
_quiet_side_effects(),
patch(f"{S}.validate_setting", return_value=(True, None)),
patch(
f"{S}.coerce_setting_for_write",
side_effect=lambda key, value, ui_element: value,
),
patch(f"{S}.set_setting", return_value=True) as mock_set,
):
resp = _save_all_settings_sync(dict(form_data), "testuser")
return resp, mock_set
def test_corrupted_llm_provider(self):
setting = _make_setting(
key="llm.provider", value="openai", ui_element="text", editable=True
)
setting.type = "llm"
resp, mock_set = self._save(
setting, {"llm.provider": "[object Object]"}
)
assert not hasattr(resp, "status_code"), _body(resp)
# "[object Object]" is corruption, repaired to the local-only default.
assert mock_set.call_args[0][1] == "ollama"
def test_corrupted_unknown_key_becomes_none(self):
setting = _make_setting(
key="database.name", value="test", ui_element="text", editable=True
)
setting.type = "database"
resp, mock_set = self._save(
setting, {"database.name": "[object Object]"}
)
assert not hasattr(resp, "status_code"), _body(resp)
assert mock_set.call_args[0][1] is None
def test_corrupted_bracket_char(self):
"""Value '{' (single bracket) is detected as corrupted."""
setting = _make_setting(
key="llm.model", value="gpt-4", ui_element="text", editable=True
)
setting.type = "llm"
resp, mock_set = self._save(setting, {"llm.model": "{"})
assert not hasattr(resp, "status_code"), _body(resp)
# llm.model repairs to "" (let the provider pick), never to "{".
assert mock_set.call_args[0][1] == ""
# ---------------------------------------------------------------------------
# save_all_settings: success message formats
# ---------------------------------------------------------------------------
class TestSaveAllSettingsSuccessMessages:
"""save_all_settings: different success message formats."""
def _save(self, setting, form_data):
from local_deep_research.web.routers.settings import (
_save_all_settings_sync,
)
_, db_patch = _patched_db([setting])
settings_manager = MagicMock(settings_locked=False)
with (
db_patch,
patch(f"{S}.get_settings_manager", return_value=settings_manager),
_quiet_side_effects(),
patch(f"{S}.validate_setting", return_value=(True, None)),
patch(
f"{S}.coerce_setting_for_write",
side_effect=lambda key, value, ui_element: value,
),
patch(f"{S}.set_setting", return_value=True),
):
return _save_all_settings_sync(dict(form_data), "testuser")
def test_single_bool_enabled_message(self):
setting = _make_setting(
key="app.dark_mode",
value=True,
ui_element="checkbox",
editable=True,
)
setting.type = "app"
data = _body(self._save(setting, {"app.dark_mode": True}))
assert "enabled" in data["message"] or "disabled" in data["message"]
def test_single_non_bool_updated_message(self):
setting = _make_setting(
key="llm.temperature",
value=0.7,
ui_element="number",
name="Temperature",
editable=True,
)
setting.type = "llm"
data = _body(self._save(setting, {"llm.temperature": 0.5}))
assert "updated" in data["message"]
# Pin the whole message: "updated" also appears in the multi-setting
# message, so the substring alone survives the single-update branch
# being deleted.
assert data["message"] == "Temperature updated"
# ---------------------------------------------------------------------------
# save_all_settings: validation failure -> 400
# ---------------------------------------------------------------------------
class TestSaveAllSettingsValidationError:
"""save_all_settings: validation failure returns 400."""
def test_validation_error_on_existing(self):
from local_deep_research.web.routers.settings import (
_save_all_settings_sync,
)
setting = _make_setting(
key="search.iterations",
value=3,
ui_element="number",
editable=True,
name="Iterations",
)
setting.type = "search"
session, db_patch = _patched_db([setting])
settings_manager = MagicMock(settings_locked=False)
with (
db_patch,
patch(f"{S}.get_settings_manager", return_value=settings_manager),
_quiet_side_effects(),
patch(f"{S}.coerce_setting_for_write", return_value=-1),
patch(
f"{S}.validate_setting",
return_value=(False, "Value must be at least 0"),
),
patch(f"{S}.set_setting", return_value=True) as mock_set,
):
resp = _save_all_settings_sync(
{"search.iterations": -1}, "testuser"
)
assert resp.status_code == 400
data = _body(resp)
assert data["status"] == "error"
assert len(data["errors"]) == 1
# The rejected batch must not have been written or committed.
mock_set.assert_not_called()
session.commit.assert_not_called()
# ---------------------------------------------------------------------------
# save_settings (no-JS form POST) edge cases
# ---------------------------------------------------------------------------
class TestSaveSettingsFormPost:
"""save_settings: traditional POST edge cases.
Flask returned a 302 for every one of these; the branch's async wrapper
still does, so the original ``status_code == 302`` assertion would pass
even if the whole per-setting body were deleted. The branch factored the
body into ``_save_settings_sync``, which returns an outcome dict — these
ports pin that outcome (the property the 302 was standing in for) and,
where the original did, the rollback call.
"""
def _run(self, setting, form_data, sm=None, session=None):
from local_deep_research.web.routers.settings import (
_save_settings_sync,
)
if sm is None:
sm = MagicMock()
sm.set_setting.return_value = True
session, db_patch = _patched_db(
[setting] if setting is not None else [], session=session
)
with (
db_patch,
_quiet_side_effects(),
patch(f"{S}.get_settings_manager", return_value=sm),
patch(f"{S}.validate_setting", return_value=(True, None)),
patch(f"{S}.coerce_setting_for_write", return_value="gpt-4"),
):
outcome = _save_settings_sync(dict(form_data), "testuser")
return outcome, sm, session
def test_commit_failure_rollback(self):
setting = _make_setting(
key="llm.model", value="gpt-4", ui_element="text", editable=True
)
session = MagicMock()
session.commit.side_effect = RuntimeError("commit failed")
outcome, _sm, session = self._run(
setting, {"llm.model": "gpt-4"}, session=session
)
assert outcome["ok"] is False
session.rollback.assert_called_once()
def test_set_setting_returns_false(self):
setting = _make_setting(
key="llm.model", value="gpt-4", ui_element="text", editable=True
)
sm = MagicMock()
sm.set_setting.return_value = False
outcome, _sm, _session = self._run(
setting, {"llm.model": "gpt-4"}, sm=sm
)
assert outcome["failed"] == 1
assert outcome["ok"] is False
def test_setting_exception_in_loop(self):
setting = _make_setting(
key="llm.model", value="gpt-4", ui_element="text", editable=True
)
sm = MagicMock()
sm.set_setting.side_effect = RuntimeError("unexpected")
outcome, _sm, session = self._run(
setting, {"llm.model": "gpt-4"}, sm=sm
)
# The exception is swallowed per key and counted, not propagated.
assert outcome["failed"] == 1
session.commit.assert_called_once()
def test_non_editable_skipped(self):
setting = _make_setting(
key="app.locked", value="v", ui_element="text", editable=False
)
outcome, sm, _session = self._run(setting, {"app.locked": "new_val"})
# _filter_editable_settings strips the key before the write loop.
assert outcome["ok"] is True
assert all(
call.args and call.args[0] != "app.locked"
for call in sm.set_setting.call_args_list
)
# ---------------------------------------------------------------------------
# Exception -> 500 on every read/write API route
# ---------------------------------------------------------------------------
def _boom(*a, **kw):
raise RuntimeError("db fail")
class TestApiGetAllSettingsException:
"""api_get_all_settings: exception -> 500."""
def test_exception_returns_500(self):
from local_deep_research.web.routers.settings import (
api_get_all_settings,
)
with patch(f"{S}.get_user_db_session", side_effect=_boom):
resp = api_get_all_settings(_fake_request(), username="testuser")
assert resp.status_code == 500
assert "error" in _body(resp)
class TestApiGetDbSettingException:
"""api_get_db_setting: exception -> 500."""
def test_exception_returns_500(self):
from local_deep_research.web.routers.settings import api_get_db_setting
with patch(f"{S}.get_user_db_session", side_effect=_boom):
resp = api_get_db_setting(
_fake_request(), "llm.model", username="testuser"
)
assert resp.status_code == 500
assert "error" in _body(resp)
class TestApiUpdateSettingException:
"""api_update_setting: unhandled exception -> 500."""
def test_exception_returns_500(self):
from local_deep_research.web.routers.settings import (
_api_update_setting_sync,
)
with patch(f"{S}.get_user_db_session", side_effect=_boom):
resp = _api_update_setting_sync(
{"value": "test"}, "llm.model", "testuser"
)
assert resp.status_code == 500
assert "error" in _body(resp)
class TestApiDeleteSettingException:
"""api_delete_setting: unhandled exception -> 500."""
def test_exception_returns_500(self):
from local_deep_research.web.routers.settings import api_delete_setting
with patch(f"{S}.get_user_db_session", side_effect=_boom):
resp = api_delete_setting.__wrapped__(
Mock(), "llm.model", username="testuser"
)
assert resp.status_code == 500
assert "error" in _body(resp)
class TestApiGetCategoriesException:
"""api_get_categories: exception -> 500."""
def test_exception_returns_500(self):
from local_deep_research.web.routers.settings import api_get_categories
with patch(f"{S}.get_user_db_session", side_effect=_boom):
resp = api_get_categories(_fake_request(), username="testuser")
assert resp.status_code == 500
assert "error" in _body(resp)
class TestApiImportSettingsException:
"""api_import_settings: exception -> 500."""
def test_exception_returns_500(self):
from local_deep_research.web.routers.settings import api_import_settings
with patch(f"{S}.get_user_db_session", side_effect=_boom):
resp = api_import_settings.__wrapped__(
_fake_request(), username="testuser"
)
assert resp.status_code == 500
assert "error" in _body(resp)
# ---------------------------------------------------------------------------
# search favorites: edge cases + exception paths
# ---------------------------------------------------------------------------
class TestApiGetSearchFavoritesEdge:
"""api_get_search_favorites: edge cases."""
def test_non_list_favorites_reset(self):
from local_deep_research.web.routers.settings import (
api_get_search_favorites,
)
sm = MagicMock()
sm.get_setting.return_value = "not_a_list"
_, db_patch = _patched_db()
with db_patch, patch(f"{S}.get_settings_manager", return_value=sm):
resp = api_get_search_favorites(
_fake_request(), username="testuser"
)
assert _body(resp)["favorites"] == []
def test_exception_returns_500(self):
from local_deep_research.web.routers.settings import (
api_get_search_favorites,
)
with patch(f"{S}.get_user_db_session", side_effect=_boom):
resp = api_get_search_favorites(
_fake_request(), username="testuser"
)
assert resp.status_code == 500
class TestApiUpdateSearchFavoritesException:
"""api_update_search_favorites: exception -> 500."""
def test_exception_returns_500(self):
resp = _run_favorites_impl(
"api_update_search_favorites",
{"favorites": ["google"]},
db_side_effect=_boom,
)
assert resp.status_code == 500
class TestApiToggleSearchFavoriteException:
"""api_toggle_search_favorite: exception -> 500."""
def test_exception_returns_500(self):
resp = _run_favorites_impl(
"api_toggle_search_favorite",
{"engine_id": "google"},
db_side_effect=_boom,
)
assert resp.status_code == 500
def _run_favorites_impl(
route_name,
body,
db_side_effect=None,
settings_manager=None,
all_settings=None,
):
"""Drive one of the two async favorites routes.
Both are ``async def`` wrappers that read the JSON body and then offload a
nested ``_impl()`` to ``run_db_sync``. We call the (slowapi-unwrapped)
coroutine with a fake Request and drive it to completion synchronously,
since ``run_db_sync`` is the only await point.
"""
import asyncio
import local_deep_research.web.routers.settings as mod
route = getattr(mod, route_name)
route = getattr(route, "__wrapped__", route)
req = MagicMock()
async def _json():
return body
req.json = _json
if db_side_effect is not None:
db_patch = patch(f"{S}.get_user_db_session", side_effect=db_side_effect)
else:
_, db_patch = _patched_db(all_settings)
sm_patch = (
patch(f"{S}.get_settings_manager", return_value=settings_manager)
if settings_manager is not None
else patch(f"{S}.get_settings_manager")
)
with db_patch, sm_patch, _quiet_side_effects():
return asyncio.run(route(req, username="testuser"))
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# save_all_settings: setting type/category derivation
# ---------------------------------------------------------------------------
class TestSaveAllSettingsTypeCategorization:
"""save_all_settings: setting type categorization for various prefixes.
The originals passed an EXISTING setting and asserted ``200``. On both
main and this branch ``setting_type``/``category`` are only consumed on
the *creation* branch, so those originals were green with the entire
prefix ladder deleted. These ports keep the same keys but exercise the
branch that actually consumes the derivation.
"""
def _create(self, key, value):
from local_deep_research.web.routers.settings import (
_save_all_settings_sync,
)
created = _make_setting(key=key)
_, db_patch = _patched_db()
with (
db_patch,
_quiet_side_effects(),
patch(
f"{S}.create_or_update_setting", return_value=created
) as create,
):
resp = _save_all_settings_sync({key: value}, "testuser")
return resp, create.call_args[0][0]
def test_database_prefix(self):
resp, new_setting = self._create("database.path", "/new")
assert not hasattr(resp, "status_code"), _body(resp)
assert new_setting["type"] == "database"
assert new_setting["category"] == "database_parameters"
def test_llm_parameters_category(self):
"""llm.temperature -> category=llm_parameters."""
resp, new_setting = self._create("llm.temperature", 0.9)
assert not hasattr(resp, "status_code"), _body(resp)
assert new_setting["type"] == "llm"
assert new_setting["category"] == "llm_parameters"
# ---------------------------------------------------------------------------
# get_bulk_settings: outer exception -> 500
# ---------------------------------------------------------------------------
class TestGetBulkSettingsOuterException:
"""get_bulk_settings: outer exception -> 500.
The Flask original forced this by patching ``flask.jsonify`` to raise on
the success return. The branch builds a plain dict, so the equivalent
"raise outside the per-key try/except" is the query-parameter read.
"""
def test_outer_exception(self):
from local_deep_research.web.routers.settings import get_bulk_settings
req = MagicMock()
req.query_params.getlist.side_effect = RuntimeError("boom")
resp = get_bulk_settings(req, username="testuser")
assert resp.status_code == 500
assert _body(resp)["success"] is False