~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
"""Settings-router guards from the deleted Flask suite that no branch test
replaced.
Ported from ``tests/web/routes/test_settings_routes.py``, deleted by the
FastAPI migration. Most of that file IS superseded on the branch --
``validate_setting``/``coerce_setting_for_write`` behaviour by
``tests/settings/test_settings_defaults_runtime_validation.py`` and
``tests/web/routers/test_settings_persistence_contracts.py``, the
PDF-storage-mode shaping by ``test_settings_port_regressions.py``, the
non-editable protections and env locks by
``tests/security/test_settings_egress_and_secrets_fastapi.py`` and
``test_settings_env_lock_403.py``, and the "route exists" smoke tests by
``tests/security/test_unauthenticated_reachability_census.py``. What is
recovered here is what nothing on the branch asserts:
1. **The research page's environment-lock rendering.** ``research.html``
still marks the egress-scope select and the two locality checkboxes
``disabled aria-disabled data-env-locked`` (with ``data-env-value``
carrying the effective value) when the operator pins them via
``LDR_*``, driven by five flags ``fastapi_app.index()`` puts in the
template context. No test on the branch looks at any of it -- the
nearest, ``test_egress_unprotected_ui_gate.py``, only checks whether
the ``unprotected`` OPTION is present.
2. **The gate-ENABLED positive controls.** The branch pins that
``unprotected`` is refused with ``LDR_POLICY_ALLOW_UNPROTECTED_EGRESS``
unset, on both write routes and on model discovery. It never pins that
it is ACCEPTED with the gate set -- so a route that refused
``unprotected`` unconditionally, making the operator gate a no-op,
passes every existing test.
3. **``policy.egress_scope`` write canonicalisation.** See
``test_scope_write_is_canonicalized`` -- this one was a real loss on
the branch (#5975), not a missing test, and is now restored.
4. **The retired ``"both"`` scope.** Its rejection today is incidental
(it is simply absent from the shipped ``options``); nothing asserts it.
5. **``coerce_setting_for_write``'s call contract** (``default=None``,
``check_env=False``) and the **DELETE-then-PUT recreate coercion**,
where a caller-supplied ``ui_element``/``type``/``options`` must be
ignored in favour of the trusted registered defaults.
"""
import os
from contextlib import contextmanager
from unittest.mock import MagicMock, patch
import pytest
SETTINGS_PREFIX = "/settings"
_MODULE = "local_deep_research.web.routers.settings"
SCOPE_KEY = "policy.egress_scope"
@pytest.fixture(autouse=True)
def _reset_rate_limiter_storage():
"""The mutating settings routes carry ``@settings_limit``; clear the
shared bucket so these tests are independent of each other."""
try:
from local_deep_research.web.dependencies.rate_limit import limiter
storage = getattr(limiter, "_storage", None)
if storage is not None and hasattr(storage, "reset"):
storage.reset()
except Exception:
pass
yield
# ===========================================================================
# 1. Research page: environment-lock rendering
# ===========================================================================
def test_env_locked_scope_renders_normalized_and_disabled(
authenticated_client, monkeypatch
):
"""``LDR_POLICY_EGRESS_SCOPE=" UNPROTECTED "`` must reach the page as
the normalized ``unprotected``, on a control the user cannot change,
and the page has to say who locked it. Without the
``egress_scope_env_locked`` flag the select renders editable and a
user silently "changes" a setting the environment overrides."""
monkeypatch.setenv("LDR_POLICY_EGRESS_SCOPE", " UNPROTECTED ")
monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
response = authenticated_client.get("/", follow_redirects=False)
assert response.status_code == 200, response.status_code
html = response.text
assert "data-env-locked" in html
assert "Locked by LDR_POLICY_EGRESS_SCOPE" in html
assert "unprotected" in html
@pytest.mark.parametrize(
("env_key", "control_id", "raw_value", "expected_checked"),
[
(
"LDR_LLM_REQUIRE_LOCAL_ENDPOINT",
"llm_require_local_endpoint",
"true",
True,
),
(
"LDR_LLM_REQUIRE_LOCAL_ENDPOINT",
"llm_require_local_endpoint",
"false",
False,
),
(
"LDR_EMBEDDINGS_REQUIRE_LOCAL",
"embeddings_require_local",
"true",
True,
),
(
"LDR_EMBEDDINGS_REQUIRE_LOCAL",
"embeddings_require_local",
"false",
False,
),
],
)
def test_locality_env_lock_renders_an_effective_disabled_control(
authenticated_client,
monkeypatch,
env_key,
control_id,
raw_value,
expected_checked,
):
"""Both directions of the lock are asserted deliberately: a control
that rendered ``checked`` unconditionally, or never, would satisfy
half of this parametrization and fail the other half."""
from bs4 import BeautifulSoup
monkeypatch.setenv(env_key, raw_value)
response = authenticated_client.get("/", follow_redirects=False)
assert response.status_code == 200, response.status_code
control = BeautifulSoup(response.text, "html.parser").find(id=control_id)
assert control is not None, f"#{control_id} is not on the research page"
assert control.has_attr("disabled")
assert control.get("aria-disabled") == "true"
assert control.get("data-env-locked") == "true"
assert control.get("data-env-value") == raw_value
assert control.has_attr("checked") is expected_checked
# ===========================================================================
# 2. The operator gate, in the ALLOW direction
# ===========================================================================
def _stored_scope(client):
response = client.get(f"{SETTINGS_PREFIX}/api/{SCOPE_KEY}")
assert response.status_code == 200, response.text[:300]
return response.get_json()["value"]
def test_save_all_persists_unprotected_when_the_gate_is_enabled(
authenticated_client, monkeypatch
):
"""The anti-vacuity control for the branch's existing "unprotected is
refused" tests: with the operator gate set, the bulk save must let it
through and store it."""
monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
response = authenticated_client.post(
f"{SETTINGS_PREFIX}/save_all_settings",
json={SCOPE_KEY: "unprotected"},
)
assert response.status_code == 200, response.text[:300]
assert _stored_scope(authenticated_client) == "unprotected"
def test_put_persists_unprotected_when_the_gate_is_enabled(
authenticated_client, monkeypatch
):
monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
response = authenticated_client.put(
f"{SETTINGS_PREFIX}/api/{SCOPE_KEY}", json={"value": "unprotected"}
)
assert response.status_code == 200, response.text[:300]
assert _stored_scope(authenticated_client) == "unprotected"
def test_save_all_refuses_unprotected_when_the_gate_is_disabled(
authenticated_client, monkeypatch
):
"""The other half of the same pair, so neither can be satisfied by a
route that simply always allows or always refuses."""
monkeypatch.delenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", raising=False)
response = authenticated_client.post(
f"{SETTINGS_PREFIX}/save_all_settings",
json={SCOPE_KEY: "unprotected"},
)
assert response.status_code == 400, response.text[:300]
assert _stored_scope(authenticated_client) != "unprotected"
# ---------------------------------------------------------------------------
# _resolve_model_discovery_policy -- same gate, on the read path
# ---------------------------------------------------------------------------
def _discovery_settings_manager(
scope="unprotected", primary="library", require_local=False
):
manager = MagicMock()
values = {
"llm.require_local_endpoint": require_local,
SCOPE_KEY: scope,
"search.tool": primary,
}
manager.get_setting.side_effect = lambda key, default=None: values.get(
key, default
)
manager.get_settings_snapshot.return_value = dict(values)
return manager
def _require_local_llm(manager):
from local_deep_research.web.routers import settings as settings_router
@contextmanager
def _fake_db(*_args, **_kwargs):
yield True
with (
patch(f"{_MODULE}.get_user_db_session", _fake_db),
patch(f"{_MODULE}.get_settings_manager", return_value=manager),
):
context, _meta = settings_router._resolve_model_discovery_policy(
"alice"
)
return context.require_local_llm
def test_model_discovery_refuses_unprotected_when_the_gate_is_disabled():
from local_deep_research.security.egress.policy import PolicyDeniedError
os.environ.pop("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", None)
with pytest.raises(PolicyDeniedError) as excinfo:
_require_local_llm(_discovery_settings_manager(scope="unprotected"))
assert excinfo.value.decision.reason == "unprotected_egress_disabled"
def test_model_discovery_stays_permissive_when_the_gate_is_enabled(
monkeypatch,
):
"""The control for the test above. Without it, a resolver that raised
``PolicyDeniedError`` for every scope -- making model discovery
permanently unusable -- would still pass."""
monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
assert (
_require_local_llm(_discovery_settings_manager(scope="unprotected"))
is False
)
@pytest.mark.parametrize(
("scope", "primary", "require_local", "expected"),
[
("private_only", "library", False, True),
("adaptive", "library", False, True),
("public_only", "searxng", False, False),
("public_only", "searxng", True, True),
],
)
def test_model_discovery_locality_matrix(
scope, primary, require_local, expected
):
"""The four non-gated resolutions, pinned at the router's own helper
rather than at the policy layer it delegates to -- a helper that
stopped forwarding the toggle or the primary engine would leave the
policy-layer tests green."""
assert (
_require_local_llm(
_discovery_settings_manager(
scope=scope, primary=primary, require_local=require_local
)
)
is expected
)
# ===========================================================================
# 3. Write canonicalisation -- a real loss on this branch
# ===========================================================================
def test_scope_write_is_canonicalized():
"""Regression for #5975.
main's ``coerce_setting_for_write`` ended with ``if key ==
'policy.egress_scope' and isinstance(coerced, str): return
coerced.strip().lower()``. The FastAPI port dropped those two lines
and nothing else canonicalises on the write path, so a scope arriving
with surrounding whitespace or in a different case fell through to
``validate_setting``'s select-options check and was rejected with a
400 instead of being stored as the canonical value."""
from local_deep_research.web.routers.settings import (
coerce_setting_for_write,
)
assert coerce_setting_for_write(SCOPE_KEY, " STRICT ", "select") == "strict"
# ===========================================================================
# 4. The retired "both" scope
# ===========================================================================
def test_validate_setting_rejects_the_retired_both_egress_scope():
"""``both`` was retired by ADR-0007 / migration 0019: stored rows are
migrated to ``adaptive`` and residual values are coerced at read time,
so it must not be savable. The rejection is only incidental today --
``both`` is simply absent from the shipped options -- so this pins it
against an options list that re-adds it."""
from local_deep_research.web.models.settings import (
BaseSetting,
SettingType,
)
from local_deep_research.web.routers.settings import validate_setting
setting = BaseSetting(
key=SCOPE_KEY,
value="adaptive",
type=SettingType.APP,
name="Egress Scope",
ui_element="select",
options=[
{"value": "adaptive", "label": "Adaptive"},
{"value": "public_only", "label": "Public only"},
{"value": "private_only", "label": "Private only"},
{"value": "strict", "label": "Strict"},
{"value": "unprotected", "label": "Unprotected"},
],
)
valid, _ = validate_setting(setting, "both")
assert valid is False
# Control: a supported value still validates, so the assertion above
# is not just "select validation rejects everything".
valid, _ = validate_setting(setting, "private_only")
assert valid is True
def test_shipped_egress_scope_options_do_not_offer_both():
"""The other half: the retired value must stay out of the dropdown."""
import json
from pathlib import Path
import local_deep_research
defaults = json.loads(
(
Path(local_deep_research.__file__).parent
/ "defaults"
/ "default_settings.json"
).read_text()
)
options = defaults[SCOPE_KEY]["options"]
values = {
opt.get("value") if isinstance(opt, dict) else opt for opt in options
}
assert "both" not in values
assert "adaptive" in values
# ===========================================================================
# 5. coerce_setting_for_write's call contract, and recreate coercion
# ===========================================================================
def test_coerce_for_write_never_reads_the_environment_and_has_no_default():
"""``check_env=True`` on the write path would silently persist an
environment override in place of the value the user submitted, and a
non-None default would invent a value for an unparseable input. Both
kwargs are load-bearing and invisible in the result."""
from local_deep_research.web.routers.settings import (
coerce_setting_for_write,
)
with patch(f"{_MODULE}.get_typed_setting_value", return_value=42) as typed:
result = coerce_setting_for_write("some.key", "42", "number")
typed.assert_called_once_with(
key="some.key",
value="42",
ui_element="number",
default=None,
check_env=False,
)
assert result == 42
def test_recreated_checkbox_is_coerced_and_takes_its_registered_metadata(
authenticated_client,
):
"""DELETE-then-PUT recreate. The caller sends ``ui_element: "number"``
and the string ``"false"``; the route must ignore the caller's
metadata, coerce with the REGISTERED ``checkbox`` element, and store a
real boolean. Trusting the request body here is how a numeric setting
silently degrades to free text with its bounds gone.
"""
key = "llm.require_local_endpoint"
assert (
authenticated_client.delete(f"{SETTINGS_PREFIX}/api/{key}").status_code
== 200
)
response = authenticated_client.put(
f"{SETTINGS_PREFIX}/api/{key}",
json={"value": "false", "ui_element": "number"},
)
assert response.status_code == 201, response.text[:300]
assert response.get_json()["setting"]["value"] is False
detail = authenticated_client.get(f"{SETTINGS_PREFIX}/api/{key}").get_json()
assert detail["ui_element"] == "checkbox"
assert detail["type"] == "llm"
def test_recreated_policy_setting_keeps_its_type_and_options(
authenticated_client,
):
"""The same guard on a select: a caller-supplied ``type: "APP"``,
``ui_element: "text"`` and an empty ``options`` list must all be
discarded, or the recreated policy setting becomes an unvalidated
free-text field."""
key = SCOPE_KEY
assert (
authenticated_client.delete(f"{SETTINGS_PREFIX}/api/{key}").status_code
== 200
)
response = authenticated_client.put(
f"{SETTINGS_PREFIX}/api/{key}",
json={
"value": "adaptive",
"type": "APP",
"ui_element": "text",
"options": [],
},
)
assert response.status_code == 201, response.text[:300]
detail = authenticated_client.get(f"{SETTINGS_PREFIX}/api/{key}").get_json()
assert detail["value"] == "adaptive"
assert detail["type"] == "search"
assert detail["ui_element"] == "select"
assert detail["options"]