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.

48.3 KB
Raw Blame
"""Persistence contracts for ``web/routers/settings.py``: what a settings
write is allowed to change, and what must survive it.

WHY THIS FILE EXISTS

Four invariants of the settings router are only partly pinned by the
existing suite. Each one is a "the write must not happen" rule, and each
one is currently proved for *some* of the routes that can write:

1. ENVIRONMENT LOCK. A setting pinned by an ``LDR_*`` env var is operator
   policy; no request may change its stored value.
   ``tests/web/routers/test_settings_env_lock_403.py`` covers the two
   single-key routes (``PUT``/``DELETE /settings/api/{key}``), which are
   the two that carry an explicit ``_is_environment_locked`` guard. The
   *bulk* writers -- ``POST /settings/save_all_settings``,
   ``POST /settings/save_settings`` (the no-JS form), the search-favorites
   routes, and ``POST /settings/reset_to_defaults`` -- have no such guard
   and rely entirely on ``SettingsManager`` refusing underneath them, or
   (for reset) on a single ``preserve_environment_locked=True`` keyword.
   ``tests/security/test_env_locked_survives_reset_and_import.py`` asserts
   that keyword is *passed* by patching the manager; nothing asserts that
   an env-locked value actually survives a real reset. That is the gap
   ``TestEnvLockedValuesSurviveEveryWriteRoute`` fills.

2. SETTINGS LOCK (``app.lock_settings``). #5659 added route-level 403
   guards to ``reset_to_defaults``, ``api_import_settings`` and
   ``api_delete_setting``, and
   ``tests/web/routers/test_settings_lock_enforcement.py`` pins those
   three thoroughly. The other mutating routes never got one.
   ``TestSettingsLockOnMutatingRoutes`` proves both halves of the contract:
   the manager leaves data untouched and each JSON mutator answers 403.

3. TYPE COERCION. Every write route funnels through
   ``coerce_setting_for_write`` -> ``get_typed_setting_value``, so the
   Python type that lands in the JSON column is decided by the row's
   ``ui_element``, not by the JSON type the client sent. The suite checks
   individual cases (``embeddings.openai.chunk_size`` bool/float in
   ``test_settings_port_regressions.py``, out-of-bounds and out-of-options
   rejection in ``test_settings_save_validation_and_env_overlay.py``);
   ``TestTypeCoercionOnWrite`` pins the whole table, per ui_element,
   reading the raw stored value back so a wrong-but-equal type (``"7"``
   vs ``7``) cannot pass.

4. ATOMICITY. ``_save_all_settings_sync`` deliberately saves with
   ``commit=False`` and rolls the whole batch back if any key fails
   validation, so a rejected batch is never half-applied.
   ``test_settings_namespace_guard.py`` asserts ``session.rollback()`` was
   *called* on a mocked session; ``TestBulkSaveIsAllOrNothing`` asserts
   the sibling key's value in a real database is unchanged, which is the
   property users actually have.

5. CENSUS. Probing routes one at a time says nothing about the route
   somebody adds next month. ``TestLockGuardCensusOverTheWholeRouter``
   parses the router with ``ast`` and enumerates *every* handler that can
   reach a settings write, recording whether a ``settings_locked`` check
   appears anywhere in its call graph, then compares that against a
   written-down inventory. It needs no app, no database and no login, so
   it is also the part of this file that still runs when the HTTP harness
   cannot.

DELIBERATELY NOT COVERED (filed upstream, do not duplicate): #5737
(``import_settings``' lock refusal is a silent no-op at the manager),
#5739 (``settings_locked`` fails open / latent recursion), #5740
(inconsistent lock-vs-env ordering). ``reset_to_defaults`` preserving
*shipped* password rows is already pinned by
``test_settings_lock_enforcement.py::TestResetPreserveInteraction``; what
is added here is the two cases that file does not reach -- a
user-created secret, and a secret that is simultaneously env-locked.

HOW THESE TESTS DRIVE THE CODE. Real HTTP through ``TestClient`` against
a real registered user and a real (unencrypted-mode) database, with every
"nothing was written" claim read back out of the ``settings`` table
directly rather than through ``GET /settings/api`` -- that endpoint
redacts secrets and overlays env values, so it cannot tell a preserved
value apart from a blanked one. Every negative assertion is paired with a
positive control issuing the *same* request without the lock/env var, so
a 403-shaped or unchanged-shaped result can never come from a broken
endpoint.

The settings lock is flipped by writing ``app.lock_settings`` straight
into the database because no API path can set it: it ships
``editable: false, visible: false`` and its own description says it must
be changed in the database.

A fresh client logs in inside every test rather than once per module:
``tests/conftest.py``'s autouse ``cleanup_database_connections`` closes
every open user database before each test function, so a session opened
by an outer-scoped fixture is already disconnected by the time a test
body runs (same reasoning as the two sibling files above).
"""

import ast
import uuid
from pathlib import Path

import pytest
from fastapi.testclient import TestClient

import local_deep_research
from local_deep_research.database.models import Setting
from local_deep_research.database.session_context import get_user_db_session

LOCK_KEY = "app.lock_settings"

# One representative shipped setting per ui_element, chosen so a write has
# no side effects beyond the row itself: none is in
# ``WARNING_AFFECTING_KEYS`` (which would trigger a warnings recalculation)
# and none is under ``document_scheduler.``/``zotero.`` (which would
# trigger a job reschedule).
NUMBER_KEY = "search.max_results"  # number, min 1, max 50, default 50
NUMBER_ENV = "LDR_SEARCH_MAX_RESULTS"
SELECT_KEY = "app.timezone"  # select, static options, default "UTC"
CHECKBOX_KEY = "app.enable_notifications"  # checkbox, default True
RANGE_KEY = "llm.temperature"  # range, 0.0..1.0, default 0.7
JSON_KEY = "search.engine.web.arxiv.strengths"  # json (list)
MULTISELECT_KEY = "report.export_formats"  # multiselect (list)
TEXT_KEY = "search.engine.web.mojeek.default_params.language"  # text
PASSWORD_KEY = "llm.openai.api_key"  # password, default ""
PASSWORD_ENV = "LDR_LLM_OPENAI_API_KEY"
FAVORITES_KEY = "search.favorites"  # json (list), written by two routes
FAVORITES_ENV = "LDR_SEARCH_FAVORITES"

# Keys that do not exist in the shipped defaults; the tests create them.
NEW_KEY = "llm.env_locked_probe"
NEW_KEY_ENV = "LDR_LLM_ENV_LOCKED_PROBE"
CUSTOM_API_KEY_SETTING = "llm.probeprovider.api_key"

# Every key any test in this file may disturb. Snapshotted and restored
# around each test so the module-scoped user database cannot carry state
# (or a reset) from one test into the next.
TOUCHED_KEYS = (
    LOCK_KEY,
    NUMBER_KEY,
    SELECT_KEY,
    CHECKBOX_KEY,
    RANGE_KEY,
    JSON_KEY,
    MULTISELECT_KEY,
    TEXT_KEY,
    PASSWORD_KEY,
    FAVORITES_KEY,
    NEW_KEY,
    CUSTOM_API_KEY_SETTING,
)

_MISSING = object()


# ---------------------------------------------------------------------------
# HTTP harness
# ---------------------------------------------------------------------------


@pytest.fixture(scope="module")
def app():
    from local_deep_research.web.fastapi_app import app

    return app


def _unique_ip() -> str:
    """A distinct client IP per TestClient, so the ``@settings_limit``
    rate limiter cannot make one test fail because of another's requests."""
    return f"10.{uuid.uuid4().int % 254 + 1}.{uuid.uuid4().int % 254 + 1}.7"


def _new_client(app) -> TestClient:
    client = TestClient(app, raise_server_exceptions=False)
    client.headers.update({"X-Forwarded-For": _unique_ip()})
    return client


def _csrf(client: TestClient) -> str:
    client.get("/auth/login")
    response = client.get("/auth/csrf-token")
    return (
        response.json().get("csrf_token", "")
        if response.status_code == 200
        else ""
    )


@pytest.fixture(scope="module")
def registered_user(app):
    """Register one real user for the module; yield ``(username, password)``."""
    client = _new_client(app)
    user = f"test_persist_{uuid.uuid4().hex[:8]}"
    password = "TestPassword123!"  # noqa: S105

    response = client.post(
        "/auth/register",
        data={
            "username": user,
            "password": password,
            "confirm_password": password,
            "acknowledge": "true",
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    if response.status_code != 302:
        pytest.fail(
            f"Registration bootstrap failed: expected 302, got "
            f"{response.status_code}: {response.text[:500]}"
        )

    token = client.get("/auth/csrf-token").json().get("csrf_token", "")
    client.post(
        "/auth/logout",
        headers={"X-CSRFToken": token},
        follow_redirects=False,
    )
    return user, password


@pytest.fixture
def auth_client(app, registered_user):
    """A freshly logged-in client carrying a CSRF header for write verbs."""
    user, password = registered_user
    client = _new_client(app)
    response = client.post(
        "/auth/login",
        data={
            "username": user,
            "password": password,
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    assert response.status_code == 302, (
        f"Login failed: {response.status_code} {response.text[:300]}"
    )

    token = client.get("/auth/csrf-token").json().get("csrf_token")
    assert token, "Could not obtain CSRF token for authenticated client"
    client.headers.update({"X-CSRFToken": token})
    return client


@pytest.fixture
def username(registered_user):
    return registered_user[0]


# ---------------------------------------------------------------------------
# Direct database access -- how "nothing was written" is established
# ---------------------------------------------------------------------------


def _stored(username: str, key: str):
    """The value of ``key`` exactly as it sits in the user's database.

    Deliberately not ``GET /settings/api/{key}``: that route redacts
    secrets to the ``[REDACTED]`` sentinel and overlays the env value, so
    a preserved secret and a blanked one look identical through it, and an
    env-locked key reports the env value no matter what is stored.
    """
    with get_user_db_session(username) as session:
        row = session.query(Setting).filter(Setting.key == key).first()
        return _MISSING if row is None else row.value


def _write_direct(username: str, key: str, value) -> None:
    """Set a row's value bypassing every route guard (test setup only)."""
    with get_user_db_session(username) as session:
        row = session.query(Setting).filter(Setting.key == key).first()
        assert row is not None, (
            f"{key} row is missing from the user database -- the defaults "
            "were not loaded, so this test would be vacuous"
        )
        row.value = value
        session.commit()


class _SettingsHttpTest:
    """Base for the HTTP-driven classes below.

    Carries the snapshot/restore fixture as a *class*-scoped autouse
    fixture rather than a module-level one so the static census class at
    the end of this file never pulls in the app, a login, or a database.

    The fixture depends on ``auth_client`` for ordering, not for use: the
    per-user database can only be opened once a login has put the user's
    key in the session password store, so the snapshot has to happen
    after the client logs in. Teardown runs in reverse order, so the
    session is still live when the restore runs.
    """

    @pytest.fixture(autouse=True)
    def _restore_touched_settings(self, auth_client, registered_user):
        """Snapshot/restore every key this module writes.

        Four tests call ``reset_to_defaults``, which rewrites every row;
        without this the module-scoped database would leak that reset into
        later tests and a "value unchanged" assertion could pass or fail
        for reasons that have nothing to do with the route under test.
        """
        user = registered_user[0]
        before = {key: _stored(user, key) for key in TOUCHED_KEYS}
        yield
        with get_user_db_session(user) as session:
            for key, value in before.items():
                row = session.query(Setting).filter(Setting.key == key).first()
                if value is _MISSING:
                    if row is not None:
                        session.delete(row)
                elif row is not None:
                    row.value = value
            session.commit()


@pytest.fixture
def lock_settings(registered_user):
    """Lock the instance on demand.

    The autouse ``restore_touched_settings`` fixture puts
    ``app.lock_settings`` back afterwards, so a lock can never leak into a
    later test as a false refusal.
    """
    user = registered_user[0]

    def _lock():
        _write_direct(user, LOCK_KEY, True)
        assert _stored(user, LOCK_KEY) is True, (
            "the lock was not persisted -- every refusal assertion below "
            "would be testing nothing"
        )

    return _lock


def _seed(client: TestClient, key: str, value) -> None:
    """Write a marker value through the public API and confirm it landed."""
    response = client.put(f"/settings/api/{key}", json={"value": value})
    assert response.status_code in (200, 201), (
        f"seeding {key} failed: {response.status_code} {response.text[:300]}"
    )


def _form_save(client: TestClient, payload: dict):
    """Submit the no-JS form-POST save route with a fresh CSRF token."""
    csrf = client.get("/auth/csrf-token").json()["csrf_token"]
    data = dict(payload)
    data["csrf_token"] = csrf
    return client.post(
        "/settings/save_settings", data=data, follow_redirects=False
    )


# ---------------------------------------------------------------------------
# 1. Environment lock
# ---------------------------------------------------------------------------


class TestEnvLockedValuesSurviveEveryWriteRoute(_SettingsHttpTest):
    """An ``LDR_*``-pinned setting is operator policy. Its *stored* value
    must be unreachable from every write route, not just the two that
    carry an explicit ``_is_environment_locked`` guard.

    The stored value matters even though reads prefer the env var: the
    damage from a clobbered row is latent while the variable is set and
    surfaces the moment the operator removes it.
    """

    def test_bulk_json_save_cannot_write_an_env_locked_key(
        self, auth_client, username, monkeypatch
    ):
        _seed(auth_client, NUMBER_KEY, 7)
        monkeypatch.setenv(NUMBER_ENV, "42")

        response = auth_client.post(
            "/settings/save_all_settings", json={NUMBER_KEY: 9}
        )

        assert response.status_code == 200, response.text
        body = response.json()
        assert NUMBER_KEY not in body.get("updated", []), (
            "save_all_settings reported an env-locked key as updated; the "
            "refusal must be visible to the caller, not swallowed"
        )
        assert _stored(username, NUMBER_KEY) == 7, (
            "an env-locked setting was overwritten through "
            "POST /settings/save_all_settings"
        )

    def test_bulk_json_save_writes_the_same_key_without_the_env_var(
        self, auth_client, username
    ):
        """Positive control for the test above: identical request, no env
        var. Without this the assertion could pass because the bulk route
        never writes anything at all."""
        _seed(auth_client, NUMBER_KEY, 7)

        response = auth_client.post(
            "/settings/save_all_settings", json={NUMBER_KEY: 9}
        )

        assert response.status_code == 200, response.text
        assert NUMBER_KEY in response.json().get("updated", [])
        assert _stored(username, NUMBER_KEY) == 9

    def test_no_js_form_save_cannot_write_an_env_locked_key(
        self, auth_client, username, monkeypatch
    ):
        _seed(auth_client, NUMBER_KEY, 7)
        monkeypatch.setenv(NUMBER_ENV, "42")

        response = _form_save(auth_client, {NUMBER_KEY: "9"})

        assert response.status_code in (302, 303), response.text
        assert _stored(username, NUMBER_KEY) == 7, (
            "an env-locked setting was overwritten through "
            "POST /settings/save_settings (the no-JS form fallback)"
        )

    def test_no_js_form_save_writes_the_same_key_without_the_env_var(
        self, auth_client, username
    ):
        """Positive control for the test above."""
        _seed(auth_client, NUMBER_KEY, 7)

        response = _form_save(auth_client, {NUMBER_KEY: "9"})

        assert response.status_code in (302, 303), response.text
        assert _stored(username, NUMBER_KEY) == 9

    def test_search_favorites_route_cannot_write_an_env_locked_key(
        self, auth_client, username, monkeypatch
    ):
        _seed(auth_client, FAVORITES_KEY, ["arxiv"])
        monkeypatch.setenv(FAVORITES_ENV, '["pubmed"]')

        response = auth_client.put(
            "/settings/api/search-favorites", json={"favorites": ["github"]}
        )

        assert response.status_code != 200, (
            "PUT /settings/api/search-favorites reported success for an "
            f"env-locked key: {response.text[:200]}"
        )
        assert _stored(username, FAVORITES_KEY) == ["arxiv"], (
            "an env-locked setting was overwritten through "
            "PUT /settings/api/search-favorites"
        )

    def test_search_favorites_route_writes_without_the_env_var(
        self, auth_client, username
    ):
        """Positive control for the test above."""
        _seed(auth_client, FAVORITES_KEY, ["arxiv"])

        response = auth_client.put(
            "/settings/api/search-favorites", json={"favorites": ["github"]}
        )

        assert response.status_code == 200, response.text
        assert _stored(username, FAVORITES_KEY) == ["github"]

    def test_bulk_save_cannot_mint_a_new_row_for_an_env_locked_key(
        self, auth_client, username, monkeypatch
    ):
        """The creation branch is a separate code path from the update
        branch (``create_or_update_setting`` rather than ``set_setting``)
        and has to refuse too, or an env-locked key with no row yet could
        be materialised with an attacker-chosen stored value.

        The refusal is a silent skip, not an error: ``_filter_editable_settings``
        drops every ``check_env_setting`` key from the batch before the write
        loop runs (main's behaviour, restored by #5978), so the key never
        reaches ``create_or_update_setting`` at all. That is a stronger
        guarantee than the 400 this branch used to return from the creation
        branch's own refusal, and it is what makes the no-JS form usable at
        all on an instance with any ``LDR_*`` variable set -- the settings
        form posts every field, so counting operator-locked keys as failures
        made every save look partially broken."""
        assert _stored(username, NEW_KEY) is _MISSING, (
            f"{NEW_KEY} already exists; this test needs a key with no row"
        )
        monkeypatch.setenv(NEW_KEY_ENV, "from-the-environment")

        response = auth_client.post(
            "/settings/save_all_settings", json={NEW_KEY: "from-the-browser"}
        )

        assert response.status_code == 200, response.text
        body = response.json()
        assert NEW_KEY not in body.get("created", [])
        assert NEW_KEY not in body.get("updated", [])
        assert _stored(username, NEW_KEY) is _MISSING, (
            "save_all_settings created a database row for an env-locked key"
        )

    def test_bulk_save_creates_the_same_new_row_without_the_env_var(
        self, auth_client, username
    ):
        """Positive control: the creation branch does work, so the refusal
        above is the env lock and not the namespace guard."""
        assert _stored(username, NEW_KEY) is _MISSING

        response = auth_client.post(
            "/settings/save_all_settings", json={NEW_KEY: "from-the-browser"}
        )

        assert response.status_code == 200, response.text
        assert NEW_KEY in response.json().get("created", [])
        assert _stored(username, NEW_KEY) == "from-the-browser"

    def test_reset_to_defaults_keeps_the_stored_env_locked_value(
        self, auth_client, username, monkeypatch
    ):
        """``reset_to_defaults`` bulk-writes every row through
        ``import_settings``, which bypasses the per-key
        ``_is_environment_locked`` setters entirely. Only the
        ``preserve_environment_locked=True`` keyword stops it clobbering
        an operator-pinned row. ``test_env_locked_survives_reset_and_import``
        asserts the keyword is passed (with a patched manager); this
        asserts the value actually survives a real reset.
        """
        _seed(auth_client, NUMBER_KEY, 7)
        _seed(auth_client, SELECT_KEY, "America/New_York")
        monkeypatch.setenv(NUMBER_ENV, "42")

        response = auth_client.post("/settings/reset_to_defaults")
        assert response.status_code == 200, response.text

        assert _stored(username, SELECT_KEY) == "UTC", (
            "the reset did not actually run -- the env-locked assertion "
            "below would be vacuous"
        )
        assert _stored(username, NUMBER_KEY) == 7, (
            "reset_to_defaults overwrote the stored value of an "
            "environment-locked setting"
        )

    def test_reset_to_defaults_resets_the_same_key_without_the_env_var(
        self, auth_client, username
    ):
        """Positive control: the key is not immune to reset in general."""
        _seed(auth_client, NUMBER_KEY, 7)

        response = auth_client.post("/settings/reset_to_defaults")
        assert response.status_code == 200, response.text

        assert _stored(username, NUMBER_KEY) == 50, (
            "reset did not restore the shipped default for an unlocked key"
        )


# ---------------------------------------------------------------------------
# 2. Settings lock on every mutating JSON route
# ---------------------------------------------------------------------------


class TestSettingsLockOnMutatingRoutes(_SettingsHttpTest):
    """#5659 gave ``reset_to_defaults``, ``api_import_settings`` and
    ``api_delete_setting`` a route-level 403. The same explicit contract now
    covers the JSON bulk save, single-key PUT, and both favorites mutators.

    The data assertions remain defense in depth: a route must both answer 403
    and leave every stored value untouched.
    """

    def test_single_key_put_writes_nothing_while_locked(
        self, auth_client, username, lock_settings
    ):
        _seed(auth_client, NUMBER_KEY, 7)
        lock_settings()

        response = auth_client.put(
            f"/settings/api/{NUMBER_KEY}", json={"value": 9}
        )

        assert response.status_code != 200, (
            "PUT reported success while settings were locked: "
            f"{response.text[:200]}"
        )
        assert _stored(username, NUMBER_KEY) == 7

    def test_single_key_put_writes_when_unlocked(self, auth_client, username):
        """Positive control for the test above."""
        _seed(auth_client, NUMBER_KEY, 7)

        response = auth_client.put(
            f"/settings/api/{NUMBER_KEY}", json={"value": 9}
        )

        assert response.status_code == 200, response.text
        assert _stored(username, NUMBER_KEY) == 9

    def test_bulk_json_save_writes_nothing_while_locked(
        self, auth_client, username, lock_settings
    ):
        _seed(auth_client, NUMBER_KEY, 7)
        _seed(auth_client, SELECT_KEY, "UTC")
        lock_settings()

        response = auth_client.post(
            "/settings/save_all_settings",
            json={NUMBER_KEY: 9, SELECT_KEY: "America/New_York"},
        )

        assert _stored(username, NUMBER_KEY) == 7
        assert _stored(username, SELECT_KEY) == "UTC"
        assert response.status_code == 403, response.text
        assert response.json() == {
            "status": "error",
            "message": "Settings are locked and cannot be changed",
        }

    def test_bulk_json_save_writes_both_keys_when_unlocked(
        self, auth_client, username
    ):
        """Positive control for the test above."""
        _seed(auth_client, NUMBER_KEY, 7)
        _seed(auth_client, SELECT_KEY, "UTC")

        response = auth_client.post(
            "/settings/save_all_settings",
            json={NUMBER_KEY: 9, SELECT_KEY: "America/New_York"},
        )

        assert response.status_code == 200, response.text
        assert sorted(response.json().get("updated", [])) == sorted(
            [NUMBER_KEY, SELECT_KEY]
        )
        assert _stored(username, NUMBER_KEY) == 9
        assert _stored(username, SELECT_KEY) == "America/New_York"

    def test_no_js_form_save_writes_nothing_while_locked(
        self, auth_client, username, lock_settings
    ):
        _seed(auth_client, NUMBER_KEY, 7)
        lock_settings()

        response = _form_save(auth_client, {NUMBER_KEY: "9"})

        assert response.status_code in (302, 303), response.text
        assert _stored(username, NUMBER_KEY) == 7
        page = auth_client.get("/settings/")
        assert page.status_code == 200
        assert "setting(s) failing" in page.text, (
            "the no-JS form flashed no failure feedback for a save the "
            "lock refused"
        )

    def test_no_js_form_save_flashes_success_when_unlocked(
        self, auth_client, username
    ):
        """Positive control: the same submission succeeds and says so, so
        the failure flash above is caused by the lock."""
        _seed(auth_client, NUMBER_KEY, 7)

        response = _form_save(auth_client, {NUMBER_KEY: "9"})

        assert response.status_code in (302, 303), response.text
        assert _stored(username, NUMBER_KEY) == 9
        page = auth_client.get("/settings/")
        assert "setting(s) failing" not in page.text
        assert "Settings saved" in page.text

    def test_search_favorites_toggle_writes_nothing_while_locked(
        self, auth_client, username, lock_settings
    ):
        _seed(auth_client, FAVORITES_KEY, ["arxiv"])
        lock_settings()

        response = auth_client.post(
            "/settings/api/search-favorites/toggle",
            json={"engine_id": "github"},
        )

        assert response.status_code != 200, (
            "the toggle route reported success while settings were locked: "
            f"{response.text[:200]}"
        )
        assert _stored(username, FAVORITES_KEY) == ["arxiv"]

    def test_search_favorites_toggle_writes_when_unlocked(
        self, auth_client, username
    ):
        """Positive control for the test above."""
        _seed(auth_client, FAVORITES_KEY, ["arxiv"])

        response = auth_client.post(
            "/settings/api/search-favorites/toggle",
            json={"engine_id": "github"},
        )

        assert response.status_code == 200, response.text
        assert _stored(username, FAVORITES_KEY) == ["arxiv", "github"]

    @pytest.mark.parametrize(
        "method,path,payload",
        [
            ("put", f"/settings/api/{NUMBER_KEY}", {"value": 9}),
            ("post", "/settings/save_all_settings", {NUMBER_KEY: 9}),
            (
                "put",
                "/settings/api/search-favorites",
                {"favorites": ["github"]},
            ),
            (
                "post",
                "/settings/api/search-favorites/toggle",
                {"engine_id": "github"},
            ),
        ],
    )
    def test_locked_mutating_route_answers_403(
        self, auth_client, lock_settings, method, path, payload
    ):
        lock_settings()

        response = getattr(auth_client, method)(path, json=payload)

        assert response.status_code == 403, (
            f"{method.upper()} {path} answered {response.status_code} while "
            "settings were locked; #5659's contract for a locked instance "
            "is 403"
        )


# ---------------------------------------------------------------------------
# 3. Type coercion and per-type validation
# ---------------------------------------------------------------------------


class TestTypeCoercionOnWrite(_SettingsHttpTest):
    """``coerce_setting_for_write`` decides the stored Python type from the
    row's ``ui_element``, not from the JSON type the client sent. Values
    are read back raw, and the *type* is asserted with ``type(x) is``
    rather than ``==``: ``"7" == 7`` is False but ``True == 1`` is True and
    ``7 == 7.0`` is True, so equality alone would let a wrong type through.
    """

    @pytest.mark.parametrize(
        "key,sent,expected,expected_type",
        [
            # checkbox -- HTML checkbox semantics (parse_boolean)
            (CHECKBOX_KEY, "false", False, bool),
            (CHECKBOX_KEY, "off", False, bool),
            (CHECKBOX_KEY, "0", False, bool),
            (CHECKBOX_KEY, "true", True, bool),
            (CHECKBOX_KEY, "on", True, bool),
            (CHECKBOX_KEY, 1, True, bool),
            # number -- whole floats collapse to int
            (NUMBER_KEY, "7", 7, int),
            (NUMBER_KEY, 7.0, 7, int),
            (NUMBER_KEY, "7.0", 7, int),
            # range -- fractional values stay float
            (RANGE_KEY, "0.25", 0.25, float),
            (RANGE_KEY, 0.25, 0.25, float),
            # select -- stringified
            (SELECT_KEY, "America/New_York", "America/New_York", str),
            # json -- a JSON string is decoded
            (JSON_KEY, '["alpha", "beta"]', ["alpha", "beta"], list),
            # multiselect -- comma-separated and JSON forms both decode
            (MULTISELECT_KEY, "markdown,latex", ["markdown", "latex"], list),
            (MULTISELECT_KEY, '["ris"]', ["ris"], list),
            # text -- explicitly NOT parsed, even when it looks like JSON
            # (coerce_setting_for_write's docstring: pre-parsing would turn
            # it into a dict and str() would then store a Python repr)
            (TEXT_KEY, '{"k": "v"}', '{"k": "v"}', str),
        ],
    )
    def test_value_is_stored_with_the_type_its_ui_element_implies(
        self, auth_client, username, key, sent, expected, expected_type
    ):
        response = auth_client.put(f"/settings/api/{key}", json={"value": sent})
        assert response.status_code == 200, response.text

        stored = _stored(username, key)
        assert stored == expected, (
            f"{key}: sent {sent!r}, expected {expected!r}, stored {stored!r}"
        )
        assert type(stored) is expected_type, (
            f"{key}: sent {sent!r}, stored {stored!r} of type "
            f"{type(stored).__name__}, expected {expected_type.__name__}"
        )

    @pytest.mark.parametrize(
        "key,seed,rejected",
        [
            # number: below min_value (1) and above max_value (50)
            (NUMBER_KEY, 7, 0),
            (NUMBER_KEY, 7, 999),
            # range: outside 0.0..1.0
            (RANGE_KEY, 0.5, 5),
            # select: not one of the shipped options
            (SELECT_KEY, "UTC", "Mars/Olympus_Mons"),
        ],
    )
    def test_out_of_contract_value_is_rejected_and_not_persisted(
        self, auth_client, username, key, seed, rejected
    ):
        _seed(auth_client, key, seed)

        response = auth_client.put(
            f"/settings/api/{key}", json={"value": rejected}
        )

        assert response.status_code == 400, (
            f"{key}: {rejected!r} was accepted: {response.text[:200]}"
        )
        assert _stored(username, key) == seed, (
            f"{key}: a rejected value still changed the stored value"
        )

    @pytest.mark.parametrize(
        "key,seed,accepted",
        [
            # Boundary values are inside the contract; without these the
            # rejection cases above could pass on a route that rejected
            # everything.
            (NUMBER_KEY, 7, 1),
            (NUMBER_KEY, 7, 50),
            (RANGE_KEY, 0.5, 1),
            (SELECT_KEY, "UTC", "America/Denver"),
        ],
    )
    def test_in_contract_value_is_accepted_and_persisted(
        self, auth_client, username, key, seed, accepted
    ):
        """Positive control for the rejection cases above."""
        _seed(auth_client, key, seed)

        response = auth_client.put(
            f"/settings/api/{key}", json={"value": accepted}
        )

        assert response.status_code == 200, response.text
        assert _stored(username, key) == accepted

    def test_non_numeric_input_is_rejected_rather_than_stored_as_null(
        self, auth_client, username
    ):
        _seed(auth_client, NUMBER_KEY, 7)

        response = auth_client.put(
            f"/settings/api/{NUMBER_KEY}", json={"value": "not-a-number"}
        )

        assert response.status_code == 400, (
            f"a non-numeric value was accepted for {NUMBER_KEY}: "
            f"{response.status_code} {response.text[:200]}"
        )
        assert _stored(username, NUMBER_KEY) == 7

    @pytest.mark.parametrize(
        "rejected", [True, False, "NaN", "Infinity", "-Infinity"]
    )
    def test_boolean_and_non_finite_numbers_are_rejected(
        self, auth_client, username, rejected
    ):
        _seed(auth_client, NUMBER_KEY, 7)

        response = auth_client.put(
            f"/settings/api/{NUMBER_KEY}", json={"value": rejected}
        )

        assert response.status_code == 400, (
            f"a hostile numeric value was accepted for {NUMBER_KEY}: "
            f"{rejected!r} -> {response.status_code} {response.text[:200]}"
        )
        assert _stored(username, NUMBER_KEY) == 7


# ---------------------------------------------------------------------------
# 4. Atomicity of the bulk save
# ---------------------------------------------------------------------------


class TestBulkSaveIsAllOrNothing(_SettingsHttpTest):
    """``_save_all_settings_sync`` writes every key with ``commit=False``
    and rolls the session back if *any* key fails, so a rejected batch
    leaves the database exactly as it was. ``test_settings_namespace_guard``
    asserts ``rollback()`` was called on a mock; these assert the value a
    user would actually find afterwards.
    """

    def test_a_validation_failure_rolls_back_its_batch_mates(
        self, auth_client, username
    ):
        _seed(auth_client, SELECT_KEY, "UTC")
        _seed(auth_client, NUMBER_KEY, 7)

        response = auth_client.post(
            "/settings/save_all_settings",
            json={
                SELECT_KEY: "America/New_York",  # valid, written first
                NUMBER_KEY: 999,  # above max_value -> batch fails
            },
        )

        assert response.status_code == 400, response.text
        assert _stored(username, SELECT_KEY) == "UTC", (
            "a valid key in a rejected batch was persisted -- the save is "
            "not atomic"
        )
        assert _stored(username, NUMBER_KEY) == 7

    def test_a_rejected_new_key_rolls_back_its_batch_mates(
        self, auth_client, username
    ):
        """The creation branch's namespace rejection has to roll back too:
        it appends to the same ``validation_errors`` list, but the valid
        keys ahead of it have already been written to the session."""
        _seed(auth_client, SELECT_KEY, "UTC")

        response = auth_client.post(
            "/settings/save_all_settings",
            json={
                SELECT_KEY: "America/New_York",  # valid, written first
                "security.evil": "x",  # blocked namespace -> batch fails
            },
        )

        assert response.status_code == 400, response.text
        assert _stored(username, SELECT_KEY) == "UTC", (
            "a valid key in a batch rejected for a namespace violation was "
            "persisted -- the save is not atomic"
        )
        assert _stored(username, "security.evil") is _MISSING

    def test_a_fully_valid_batch_commits_every_key(self, auth_client, username):
        """Positive control for both tests above: the same two-key shape
        does persist when nothing in it is rejected, so the unchanged
        values above are the rollback and not an inert endpoint."""
        _seed(auth_client, SELECT_KEY, "UTC")
        _seed(auth_client, NUMBER_KEY, 7)

        response = auth_client.post(
            "/settings/save_all_settings",
            json={SELECT_KEY: "America/New_York", NUMBER_KEY: 9},
        )

        assert response.status_code == 200, response.text
        assert _stored(username, SELECT_KEY) == "America/New_York"
        assert _stored(username, NUMBER_KEY) == 9


# ---------------------------------------------------------------------------
# 5. Reset preserves secrets
# ---------------------------------------------------------------------------


class TestResetToDefaultsPreservesSecrets(_SettingsHttpTest):
    """``reset_to_defaults`` must not cost the user their credentials.

    ``test_settings_lock_enforcement.py::TestResetPreserveInteraction``
    already pins the shipped ``ui_element == "password"`` rows, which the
    route snapshots and restores explicitly. These are the two cases that
    block of code does *not* reach.
    """

    def test_a_user_created_api_key_row_survives_a_reset(
        self, auth_client, username
    ):
        """A key created through the API gets ``ui_element`` "text", so the
        route's ``Setting.ui_element == "password"`` snapshot never sees
        it. It survives only because the defaults import runs with
        ``delete_extra=False`` and leaves rows outside the defaults file
        alone. Flipping that flag would destroy every custom credential.
        """
        response = auth_client.put(
            f"/settings/api/{CUSTOM_API_KEY_SETTING}",
            json={"value": "sk-custom-provider-secret"},
        )
        assert response.status_code in (200, 201), response.text
        assert _stored(username, CUSTOM_API_KEY_SETTING) == (
            "sk-custom-provider-secret"
        )
        _seed(auth_client, SELECT_KEY, "America/New_York")

        reset = auth_client.post("/settings/reset_to_defaults")
        assert reset.status_code == 200, reset.text

        assert _stored(username, SELECT_KEY) == "UTC", (
            "the reset did not actually run -- the survival assertion "
            "below would be vacuous"
        )
        assert _stored(username, CUSTOM_API_KEY_SETTING) == (
            "sk-custom-provider-secret"
        ), "reset_to_defaults destroyed a user-created API key"

    def test_an_env_locked_password_row_survives_a_reset(
        self, auth_client, username, monkeypatch
    ):
        """The two mechanisms overlap here and only one of them works.

        The route's restore loop calls ``settings_manager.set_setting``,
        which refuses env-locked keys -- so for a secret that is *also*
        pinned by ``LDR_*`` the snapshot-and-restore block cannot put the
        value back. Preservation rests entirely on
        ``preserve_environment_locked=True`` inside the import. If that
        keyword were ever dropped, the stored key would be blanked and the
        loss would stay invisible until the operator unset the variable.
        """
        _seed(auth_client, PASSWORD_KEY, "sk-operator-secret")
        _seed(auth_client, SELECT_KEY, "America/New_York")
        monkeypatch.setenv(PASSWORD_ENV, "sk-from-the-environment")

        reset = auth_client.post("/settings/reset_to_defaults")
        assert reset.status_code == 200, reset.text

        assert _stored(username, SELECT_KEY) == "UTC", (
            "the reset did not actually run -- the survival assertion "
            "below would be vacuous"
        )
        assert _stored(username, PASSWORD_KEY) == "sk-operator-secret", (
            "reset_to_defaults blanked the stored value of an "
            "environment-locked password setting"
        )


# ---------------------------------------------------------------------------
# 6. Static census: which settings-writing routes carry a lock guard
# ---------------------------------------------------------------------------
#
# The tests above probe five routes over HTTP. That answers "is this route
# guarded?" one route at a time and says nothing about the route somebody
# adds next month. This section answers the whole question at once, by
# parsing the router instead of running it: it enumerates EVERY route
# handler that can reach a settings write and records whether a
# ``settings_locked`` check appears anywhere in its call graph.
#
# It needs no app, no database and no login, which also makes it the part
# of this file that keeps working when the HTTP harness cannot run.


def _referenced_names(node: ast.AST) -> set:
    """Every bare name and attribute name appearing under *node*.

    Attributes are collected by their final segment, so
    ``settings_manager.settings_locked`` registers as ``settings_locked``
    and ``settings_manager.set_setting(...)`` as ``set_setting``, without
    needing to know what the receiver is bound to.
    """
    found = set()
    for child in ast.walk(node):
        if isinstance(child, ast.Name):
            found.add(child.id)
        elif isinstance(child, ast.Attribute):
            found.add(child.attr)
    return found


def _assigns_a_dot_value(node: ast.AST) -> bool:
    """True if *node* assigns to something's ``.value`` attribute.

    Catches the routes that edit ``Setting`` rows through the ORM session
    directly (``setting.value = ...; db_session.commit()``) instead of
    going through ``SettingsManager``, which is exactly how a route ends
    up bypassing every manager-level guard. Attribute assignment, not
    subscript, so the many ``some_dict["value"] = ...`` lines in the
    response-shaping helpers do not match.
    """
    for child in ast.walk(node):
        if isinstance(child, ast.Assign):
            for target in child.targets:
                if isinstance(target, ast.Attribute) and target.attr == "value":
                    return True
    return False


# Module-level helpers whose presence means "this handler can write to the
# settings table".
_SETTINGS_WRITE_CALLS = frozenset(
    {
        "set_setting",
        "create_or_update_setting",
        "delete_setting",
        "load_from_defaults_file",
        "import_settings",
    }
)
_WRITE_VERBS = frozenset({"post", "put", "delete", "patch"})


def _lock_guard_census(source: str) -> dict:
    """Map ``(VERB, path)`` -> "is there a lock check in its call graph?".

    Only routes that can write settings are included. Handlers in this
    router are thin: the real work sits in a module-level ``_*_sync``
    helper handed to ``run_db_sync``, so the walk follows references to
    other module-level functions (transitively, to a depth of three) and
    treats the union as one call graph. Nested ``def _impl()`` bodies come
    along automatically because ``ast.walk`` descends into them.
    """
    tree = ast.parse(source)
    module_functions = {
        node.name: node
        for node in tree.body
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
    }

    census = {}
    for name, function in module_functions.items():
        routes = [
            (decorator.func.attr.upper(), decorator.args[0].value)
            for decorator in function.decorator_list
            if isinstance(decorator, ast.Call)
            and isinstance(decorator.func, ast.Attribute)
            and isinstance(decorator.func.value, ast.Name)
            and decorator.func.value.id == "router"
            and decorator.func.attr in _WRITE_VERBS
            and decorator.args
            and isinstance(decorator.args[0], ast.Constant)
        ]
        if not routes:
            continue

        seen = {name}
        frontier = [function]
        names_in_graph = set()
        writes_directly = False
        for _ in range(3):
            next_frontier = []
            for node in frontier:
                referenced = _referenced_names(node)
                names_in_graph |= referenced
                writes_directly = writes_directly or _assigns_a_dot_value(node)
                for referenced_name in referenced:
                    if referenced_name in module_functions:
                        if referenced_name not in seen:
                            seen.add(referenced_name)
                            next_frontier.append(
                                module_functions[referenced_name]
                            )
            frontier = next_frontier
            if not frontier:
                break

        writes = bool(names_in_graph & _SETTINGS_WRITE_CALLS) or writes_directly
        if not writes:
            continue
        for route in routes:
            census[route] = "settings_locked" in names_in_graph
    return census


# The complete inventory of settings-writing routes and whether each one
# checks ``app.lock_settings`` at the route. The original three ``True``
# entries are #5659's guards; the JSON save/favorites/update entries extend
# the same explicit-403 contract. Every ``False`` is a route where a locked
# instance still runs the handler and only stops at the manager -- safe for
# the data, wrong in the answer unless the route carries an explicit guard.
# The former manual corruption-repair endpoint is replaced by migration 0031.
#
# This mapping is a tripwire, not an endorsement: adding a settings-writing
# route, or adding/removing a guard, fails this test until the inventory is
# updated deliberately.
EXPECTED_LOCK_GUARD_CENSUS = {
    ("POST", "/save_all_settings"): True,
    ("POST", "/reset_to_defaults"): True,
    ("POST", "/save_settings"): False,
    ("POST", "/api/import"): True,
    ("PUT", "/api/search-favorites"): True,
    ("POST", "/api/search-favorites/toggle"): True,
    ("PUT", "/api/{key}"): True,
    ("DELETE", "/api/{key}"): True,
}


class TestLockGuardCensusOverTheWholeRouter:
    """No settings-writing route may appear, or change its guard status,
    without someone updating the inventory above."""

    def test_router_matches_the_documented_inventory(self):
        router_path = (
            Path(local_deep_research.__file__).parent
            / "web"
            / "routers"
            / "settings.py"
        )
        assert router_path.is_file(), (
            f"settings router not found at {router_path}; the census would "
            "silently cover nothing"
        )

        census = _lock_guard_census(router_path.read_text(encoding="utf-8"))

        assert census == EXPECTED_LOCK_GUARD_CENSUS, (
            "the settings-writing routes, or their route-level "
            f"app.lock_settings guards, have changed.\n"
            f"  added/changed: {sorted(census.items() - EXPECTED_LOCK_GUARD_CENSUS.items())}\n"
            f"  missing/changed: {sorted(EXPECTED_LOCK_GUARD_CENSUS.items() - census.items())}\n"
            "If a guard was added, flip its entry to True. If a route was "
            "added, decide which list it belongs in."
        )

    def test_the_census_distinguishes_a_guarded_route_from_an_unguarded_one(
        self,
    ):
        """Negative control for the analyser itself.

        Without this, ``_lock_guard_census`` could be returning ``False``
        for everything -- or ``True`` for everything -- and the inventory
        test above would still pass on a copy of whatever it produced. Two
        synthetic routers differing only by the guard line must be
        classified differently.
        """
        unguarded = """
router = APIRouter()

@router.post("/thing")
def do_thing(request):
    manager.set_setting("k", 1)
"""
        guarded = """
router = APIRouter()

@router.post("/thing")
def do_thing(request):
    if manager.settings_locked:
        return 403
    manager.set_setting("k", 1)
"""
        no_write = """
router = APIRouter()

@router.post("/thing")
def do_thing(request):
    return manager.get_setting("k")
"""
        assert _lock_guard_census(unguarded) == {("POST", "/thing"): False}
        assert _lock_guard_census(guarded) == {("POST", "/thing"): True}
        assert _lock_guard_census(no_write) == {}, (
            "a read-only route was counted as a settings writer; the "
            "inventory would fill up with routes that cannot write"
        )

    def test_the_census_follows_delegation_into_a_sync_helper(self):
        """Second negative control: the handlers in this router delegate
        the write to a module-level ``_*_sync`` helper, so an analyser that
        only looked at the decorated function's own body would report every
        one of them as a non-writer and the inventory would be empty."""
        delegating = """
router = APIRouter()

def _do_thing_sync(username):
    manager.set_setting("k", 1)

@router.post("/thing")
async def do_thing(request):
    return await run_db_sync(_do_thing_sync, username)
"""
        assert _lock_guard_census(delegating) == {("POST", "/thing"): False}, (
            "delegation to a module-level sync helper was not followed"
        )