~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
"""The settings lock (``app.lock_settings``) must be enforced at the route.
WHY THIS FILE EXISTS -- a merge hazard, not a new feature.
``main`` landed #5659, "enforce the settings lock on delete, import and
reset". Half of it was manager-level (``SettingsManager.delete_setting`` /
``import_settings`` refuse while locked); the other half was three route
guards in ``web/routes/settings_routes.py`` -- a **Flask file this FastAPI
migration deletes**. Git surfaced that as a modify/delete conflict. Taking
the deletion is right, but it silently reverts the route half of the fix,
so those three guards were hand-ported into
``src/local_deep_research/web/routers/settings.py``:
* ``POST /settings/reset_to_defaults`` (``reset_to_defaults``)
* ``POST /settings/api/import`` (``api_import_settings``)
* ``DELETE /settings/api/{key}`` (``api_delete_setting``)
The manager half auto-merged and is covered by
``tests/settings/test_settings_manager.py``. The route half arrived with no
tests at all, which is what this file is for.
THE GUARDS LOOK REDUNDANT AND ARE NOT. The manager already refuses to
write while locked, so the *data* is safe either way. What is not safe is
the *answer*: without the route check the endpoint runs the manager call,
the manager quietly returns without writing, and the route reports
``200 {"status": "success"}``. The caller is told the reset happened. That
silent no-op is precisely the reason main duplicates the check, and
``test_the_route_answers_403_even_when_the_manager_cannot_refuse`` below
exists so a future "this is already handled in the manager" cleanup fails
here rather than quietly restoring the 200.
The reset endpoint additionally differs from main on this branch: it now
PRESERVES password-typed settings across a reset (so a reset does not wipe
stored API keys). The lock check runs first and short-circuits, so a locked
instance must never reach the snapshot-and-restore block at all --
``TestResetPreserveInteraction`` pins both halves of that.
HOW THESE TESTS DRIVE THE CODE. Real HTTP through ``TestClient`` against a
real registered user with a real (unencrypted-mode) database, because the
thing under test is a route's status code, and because "nothing was
written" is only meaningful when read back out of an actual database.
The lock itself is flipped by writing the ``app.lock_settings`` row
directly: it is ``editable: false`` and ``visible: false`` in
``default_settings.json``, so no API path can set it -- its description
says so outright ("Must be changed directly in the database to re-enable
editing").
A fresh client logs in inside every test rather than once in a shared
fixture: ``tests/conftest.py``'s autouse ``cleanup_database_connections``
closes every open user database before each test function, so a session
established by an outer-scoped fixture is already disconnected by the time
the test body runs (same reasoning as ``test_settings_env_lock_403.py``).
"""
import uuid
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from local_deep_research.database.models import Base, Setting
from local_deep_research.database.session_context import get_user_db_session
from local_deep_research.settings.manager import SettingsManager
LOCK_KEY = "app.lock_settings"
# A plain, editable, non-password setting whose bundled default is the empty
# string. Setting it to a marker value makes "did a reset/import actually
# run?" a two-valued question with no ambiguity: marker means untouched,
# "" means the defaults were re-imported over it.
PLAIN_KEY = "llm.model"
PLAIN_DEFAULT = ""
# A password-typed setting (``ui_element == "password"`` in
# default_settings.json), which is what reset_to_defaults' preserve block
# selects on. Its bundled default is also "", which is the whole point:
# an unguarded reset would blank a stored API key.
PASSWORD_KEY = "llm.openai.api_key"
# ---------------------------------------------------------------------------
# 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 on these routes cannot make one test fail because of
requests issued by another."""
return f"10.{uuid.uuid4().int % 254 + 1}.{uuid.uuid4().int % 254 + 1}.4"
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_lock_{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 POST/DELETE."""
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
# ---------------------------------------------------------------------------
# Direct database access -- how "nothing was written" is established
# ---------------------------------------------------------------------------
_MISSING = object()
def _stored_value(username: str, key: str):
"""The value of ``key`` as it actually sits in the user's database.
Deliberately not ``GET /settings/api``: that endpoint runs the response
through ``DataSanitizer.redact_settings_snapshot``, so a preserved API
key comes back as ``[REDACTED]`` and could not be told apart from a
blanked one.
"""
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 _row_exists(username: str, key: str) -> bool:
with get_user_db_session(username) as session:
return (
session.query(Setting).filter(Setting.key == key).first()
is not None
)
def _write_lock(username: str, locked: bool) -> None:
"""Flip ``app.lock_settings`` in the database (no API path can)."""
with get_user_db_session(username) as session:
row = session.query(Setting).filter(Setting.key == LOCK_KEY).first()
assert row is not None, (
f"{LOCK_KEY} row is missing from the user database -- the "
"defaults were not loaded, so this test would be vacuous"
)
row.value = locked
session.commit()
@pytest.fixture
def lock_settings(registered_user):
"""Lock the instance on demand; always unlock again on teardown.
Teardown matters: the user database is module-scoped, so a lock left
set would leak into every later test as a false 403.
"""
username, _ = registered_user
def _lock():
_write_lock(username, True)
assert _stored_value(username, LOCK_KEY) is True, (
"the lock was not persisted -- the 403 assertions below would "
"be testing nothing"
)
yield _lock
_write_lock(username, False)
def _lock_message(body: dict) -> str:
"""The human-readable half of a lock refusal, whichever key holds it.
Deliberately shape-tolerant. main's Flask originals answered
``{"status": "error", "message": ...}`` for reset but plain
``{"error": ...}`` for import and delete; the hand-port gave all three
the reset shape. The browser cannot tell the difference --
``fetchWithErrorHandling`` in ``static/js/services/api.js`` reads
``message || error || detail`` -- so this is a contract drift worth
recording, not a bug worth freezing into an assertion. Pinning either
spelling here would make a later reconciliation with main fail for
cosmetic reasons; the status code is the part that must not move.
"""
return str(body.get("message") or body.get("error") or "")
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]}"
)
# ---------------------------------------------------------------------------
# Positive controls: every guarded endpoint works while unlocked
# ---------------------------------------------------------------------------
class TestUnlockedEndpointsStillWork:
"""Without these, a 403 assertion cannot tell a working guard apart
from a broken endpoint."""
def test_reset_to_defaults_succeeds_and_actually_resets(
self, auth_client, registered_user
):
username, _ = registered_user
_seed(auth_client, PLAIN_KEY, "marker-reset-unlocked")
response = auth_client.post("/settings/reset_to_defaults")
assert response.status_code == 200, (
f"unlocked reset must succeed: {response.status_code} "
f"{response.text[:300]}"
)
assert response.json()["status"] == "success"
assert _stored_value(username, PLAIN_KEY) == PLAIN_DEFAULT, (
"an unlocked reset must really re-import the bundled defaults; "
"if it does not, the 'locked leaves it untouched' test below "
"passes for the wrong reason"
)
def test_api_import_succeeds_and_actually_imports(
self, auth_client, registered_user
):
username, _ = registered_user
_seed(auth_client, PLAIN_KEY, "marker-import-unlocked")
response = auth_client.post("/settings/api/import")
assert response.status_code == 200, (
f"unlocked import must succeed: {response.status_code} "
f"{response.text[:300]}"
)
assert _stored_value(username, PLAIN_KEY) == PLAIN_DEFAULT, (
"an unlocked import must really overwrite stored values with "
"the bundled defaults"
)
def test_api_delete_succeeds_and_actually_deletes(
self, auth_client, registered_user
):
username, _ = registered_user
key = f"llm.lock_probe_{uuid.uuid4().hex[:8]}"
_seed(auth_client, key, "delete-me")
assert _row_exists(username, key), "probe setting was not created"
response = auth_client.delete(f"/settings/api/{key}")
assert response.status_code == 200, (
f"unlocked delete must succeed: {response.status_code} "
f"{response.text[:300]}"
)
assert not _row_exists(username, key), (
"an unlocked delete must really remove the row"
)
# ---------------------------------------------------------------------------
# The guards themselves: 403 AND no write
# ---------------------------------------------------------------------------
class TestLockedEndpointsReturn403AndWriteNothing:
"""A 403 that still mutated state would be worse than no guard at all,
so every case reads the database back."""
def test_reset_to_defaults_is_refused_and_changes_nothing(
self, auth_client, registered_user, lock_settings
):
username, _ = registered_user
_seed(auth_client, PLAIN_KEY, "marker-reset-locked")
lock_settings()
response = auth_client.post("/settings/reset_to_defaults")
assert response.status_code == 403, (
f"locked reset must 403: {response.status_code} "
f"{response.text[:300]}"
)
assert "locked" in _lock_message(response.json()).lower(), (
f"the refusal must say why: {response.text[:300]}"
)
assert _stored_value(username, PLAIN_KEY) == "marker-reset-locked", (
"a refused reset must leave stored values exactly as they were"
)
def test_api_import_is_refused_and_changes_nothing(
self, auth_client, registered_user, lock_settings
):
username, _ = registered_user
_seed(auth_client, PLAIN_KEY, "marker-import-locked")
lock_settings()
response = auth_client.post("/settings/api/import")
assert response.status_code == 403, (
f"locked import must 403: {response.status_code} "
f"{response.text[:300]}"
)
assert "locked" in _lock_message(response.json()).lower(), (
f"the refusal must say why: {response.text[:300]}"
)
assert _stored_value(username, PLAIN_KEY) == "marker-import-locked", (
"a refused import must leave stored values exactly as they were"
)
def test_api_delete_is_refused_and_the_row_survives(
self, auth_client, registered_user, lock_settings
):
username, _ = registered_user
key = f"llm.lock_probe_{uuid.uuid4().hex[:8]}"
_seed(auth_client, key, "survive-me")
lock_settings()
response = auth_client.delete(f"/settings/api/{key}")
assert response.status_code == 403, (
f"locked delete must 403: {response.status_code} "
f"{response.text[:300]}"
)
assert "locked" in _lock_message(response.json()).lower(), (
f"the refusal must say why: {response.text[:300]}"
)
assert _stored_value(username, key) == "survive-me", (
"a refused delete must leave the row -- and its value -- intact"
)
def test_reading_settings_is_not_blocked_by_the_lock(
self, auth_client, lock_settings
):
"""Scope check: the lock is about writes. If it started rejecting
reads, a locked instance's settings page would break, and the
403s above would no longer be evidence about the write path."""
lock_settings()
response = auth_client.get("/settings/api")
assert response.status_code == 200, (
f"locked instances must still be readable: "
f"{response.status_code} {response.text[:300]}"
)
# ---------------------------------------------------------------------------
# The whole point of duplicating the check at the route
# ---------------------------------------------------------------------------
class TestGuardLivesAtTheRouteNotOnlyInTheManager:
"""Each test here removes the manager's own refusal, so the only thing
that can still produce a 403 is the route guard.
If someone deletes a route guard as "already handled by the manager",
the endpoint reverts to ``200`` with nothing written -- the exact
silent no-op #5659 was filed about -- and these fail.
Every test also runs the unlocked case with the same patch in place.
That is the control: it proves the patched manager method is genuinely
the one the route calls, so "not called" below means short-circuited
rather than mis-patched.
"""
@pytest.fixture
def import_spy(self, monkeypatch):
"""Replace the manager's import with one that can never refuse."""
calls = []
def _spy(self, commit=True, **kwargs):
calls.append(kwargs)
monkeypatch.setattr(
SettingsManager, "load_from_defaults_file", _spy, raising=True
)
return calls
@pytest.fixture
def delete_spy(self, monkeypatch):
"""Replace the manager's delete with one that always reports success."""
calls = []
def _spy(self, key, commit=True, override_locked=False):
calls.append(key)
return True
monkeypatch.setattr(
SettingsManager, "delete_setting", _spy, raising=True
)
return calls
def test_reset_answers_403_even_when_the_manager_cannot_refuse(
self, auth_client, lock_settings, import_spy
):
control = auth_client.post("/settings/reset_to_defaults")
assert control.status_code == 200, (
f"control: unlocked reset should reach the patched manager, got "
f"{control.status_code} {control.text[:300]}"
)
assert len(import_spy) == 1, (
"control: the patched import was not the method the route calls "
f"-- recorded {import_spy}"
)
lock_settings()
response = auth_client.post("/settings/reset_to_defaults")
assert response.status_code == 403, (
"with the manager's refusal removed, the ROUTE must still 403; "
f"got {response.status_code} {response.text[:300]} -- this is "
"the 200-with-nothing-written regression from #5659"
)
assert len(import_spy) == 1, (
"the route guard must short-circuit BEFORE the manager import "
f"-- it was reached again: {import_spy}"
)
def test_import_answers_403_even_when_the_manager_cannot_refuse(
self, auth_client, lock_settings, import_spy
):
control = auth_client.post("/settings/api/import")
assert control.status_code == 200, (
f"control: unlocked import should reach the patched manager, got "
f"{control.status_code} {control.text[:300]}"
)
assert len(import_spy) == 1, (
"control: the patched import was not the method the route calls "
f"-- recorded {import_spy}"
)
lock_settings()
response = auth_client.post("/settings/api/import")
assert response.status_code == 403, (
"with the manager's refusal removed, the ROUTE must still 403; "
f"got {response.status_code} {response.text[:300]}"
)
assert len(import_spy) == 1, (
"the route guard must short-circuit BEFORE the manager import "
f"-- it was reached again: {import_spy}"
)
def test_delete_answers_403_even_when_the_manager_cannot_refuse(
self, auth_client, registered_user, lock_settings, delete_spy
):
username, _ = registered_user
control_key = f"llm.lock_probe_{uuid.uuid4().hex[:8]}"
locked_key = f"llm.lock_probe_{uuid.uuid4().hex[:8]}"
_seed(auth_client, control_key, "control")
_seed(auth_client, locked_key, "locked")
control = auth_client.delete(f"/settings/api/{control_key}")
assert control.status_code == 200, (
f"control: unlocked delete should reach the patched manager, got "
f"{control.status_code} {control.text[:300]}"
)
assert delete_spy == [control_key], (
"control: the patched delete was not the method the route calls "
f"-- recorded {delete_spy}"
)
lock_settings()
response = auth_client.delete(f"/settings/api/{locked_key}")
assert response.status_code == 403, (
"with the manager's refusal removed, the ROUTE must still 403; "
f"got {response.status_code} {response.text[:300]}"
)
assert delete_spy == [control_key], (
"the route guard must short-circuit BEFORE the manager delete "
f"-- it was reached again: {delete_spy}"
)
assert _stored_value(username, locked_key) == "locked", (
"the refused delete must have left the row untouched"
)
# ---------------------------------------------------------------------------
# Lock x password-preservation -- the interaction this branch introduced
# ---------------------------------------------------------------------------
class TestResetPreserveInteraction:
"""``reset_to_defaults`` on this branch snapshots password-typed rows
and restores them after the re-import, so a reset does not wipe stored
API keys. The lock check is ahead of that block, so a locked instance
must never enter it."""
def test_unlocked_reset_keeps_api_keys_but_resets_everything_else(
self, auth_client, registered_user
):
username, _ = registered_user
_seed(auth_client, PASSWORD_KEY, "sk-preserve-me")
_seed(auth_client, PLAIN_KEY, "marker-should-be-reset")
response = auth_client.post("/settings/reset_to_defaults")
assert response.status_code == 200, (
f"{response.status_code} {response.text[:300]}"
)
assert _stored_value(username, PASSWORD_KEY) == "sk-preserve-me", (
"a reset must not blank password-typed settings -- the bundled "
f"default for {PASSWORD_KEY} is the empty string, so an "
"unguarded re-import silently destroys the user's API key"
)
assert _stored_value(username, PLAIN_KEY) == PLAIN_DEFAULT, (
"non-password settings must still be reset -- otherwise the "
"preservation above proves nothing about reset running at all"
)
def test_locked_reset_never_reaches_the_preserve_and_restore_path(
self, auth_client, registered_user, lock_settings
):
"""The subtle case: the lock check short-circuits ahead of the
snapshot, so a locked reset is a no-op for password settings for
the boring reason (it never ran), not because preservation saved
them. Both the password row and its neighbour must be untouched.
"""
username, _ = registered_user
_seed(auth_client, PASSWORD_KEY, "sk-locked-untouched")
_seed(auth_client, PLAIN_KEY, "marker-locked-untouched")
lock_settings()
response = auth_client.post("/settings/reset_to_defaults")
assert response.status_code == 403, (
f"{response.status_code} {response.text[:300]}"
)
assert _stored_value(username, PASSWORD_KEY) == "sk-locked-untouched", (
"a refused reset must not touch password-typed settings"
)
assert (
_stored_value(username, PLAIN_KEY) == "marker-locked-untouched"
), (
"a refused reset must not touch ordinary settings either -- if "
"this changed, the reset ran and only the restore step saved "
"the password row"
)
def test_locked_reset_does_not_even_snapshot_the_password_rows(
self, auth_client, lock_settings, monkeypatch
):
"""Executable proof of the ordering claim above.
``set_setting`` is the restore step. If the guard were placed after
the snapshot/restore block instead of before it, this would fire.
"""
restored = []
original = SettingsManager.set_setting
def _spy(self, key, value, commit=True, *args, **kwargs):
restored.append(key)
return original(self, key, value, commit, *args, **kwargs)
monkeypatch.setattr(SettingsManager, "set_setting", _spy, raising=True)
_seed(auth_client, PASSWORD_KEY, "sk-never-restored")
lock_settings()
restored.clear()
response = auth_client.post("/settings/reset_to_defaults")
assert response.status_code == 403, (
f"{response.status_code} {response.text[:300]}"
)
assert restored == [], (
"a locked reset must return before the preserve-and-restore "
f"block runs; it restored {restored}"
)
# ---------------------------------------------------------------------------
# Startup must survive the lock
# ---------------------------------------------------------------------------
class TestStartupIsUnaffectedByTheLock:
"""#5659's changelog promises a locked instance still boots and still
receives defaults shipped by an upgrade. ``import_settings`` takes
``override_locked``, and the bootstrap callers pass it; these tests are
what stop that from being quietly dropped."""
def _fresh_db(self, tmp_path, request, name="settings"):
engine = create_engine(f"sqlite:///{tmp_path}/{name}.db")
request.addfinalizer(engine.dispose)
Base.metadata.create_all(engine)
return sessionmaker(bind=engine)
def test_a_locked_instance_can_still_be_initialised_from_empty(
self, tmp_path, request, monkeypatch
):
"""The lock set before the database exists -- an operator shipping
``LDR_APP_LOCK_SETTINGS=true`` in their container config -- must
not leave the instance with zero settings.
``SettingsManager.__init__`` -> ``_ensure_settings_initialized``
passes ``override_locked=True`` for exactly this. Drop that and the
instance boots empty and unusable.
"""
monkeypatch.setenv("LDR_APP_LOCK_SETTINGS", "true")
Session = self._fresh_db(tmp_path, request, name="bootstrap")
with Session() as session:
manager = SettingsManager(session)
assert manager.settings_locked is True, (
"premise: the environment override must make this manager "
"see a locked instance, or the test proves nothing"
)
assert session.query(Setting).count() > 0, (
"a locked instance must still bootstrap its settings -- "
"_ensure_settings_initialized has to pass override_locked"
)
assert (
session.query(Setting).filter(Setting.key == PLAIN_KEY).first()
is not None
)
def test_newly_shipped_defaults_still_land_on_a_locked_instance(
self, tmp_path, request
):
"""The upgrade path: a locked account still has to receive settings
introduced by a new release. ``override_locked=True`` is what makes
that work, and the default (no override) must still refuse -- both
halves asserted, so neither can be satisfied by accident."""
Session = self._fresh_db(tmp_path, request, name="upgrade")
with Session() as session:
SettingsManager(session)
assert session.query(Setting).count() > 0, "defaults not loaded"
# Lock the instance, then remove a row to stand in for a setting
# this release ships and the stored database has never seen.
with Session() as session:
session.query(Setting).filter(
Setting.key == LOCK_KEY
).one().value = True
session.query(Setting).filter(Setting.key == PLAIN_KEY).delete()
session.commit()
with Session() as session:
manager = SettingsManager(session)
assert manager.settings_locked is True
manager.load_from_defaults_file()
assert (
session.query(Setting).filter(Setting.key == PLAIN_KEY).first()
is None
), (
"without override_locked a locked import must be refused -- "
"this is the manager-level half of #5659"
)
manager.import_settings(
manager.default_settings, override_locked=True
)
assert (
session.query(Setting).filter(Setting.key == PLAIN_KEY).first()
is not None
), (
"override_locked=True must let newly shipped defaults land "
"on a locked instance, as #5659's changelog promises"
)
def test_bootstrap_call_sites_pass_override_locked():
"""Pin ``override_locked=True`` at all three trusted call sites.
PORTED from tests/web/routes/test_settings_lock_enforcement.py, the Flask
file this migration deletes. That file could not even be COLLECTED on
this branch -- it did `from ._settings_route_helpers import ...` and
neither that helper nor `tests/web/routes/__init__.py` survives here, so
pytest raised `ImportError: attempted relative import with no known
parent package` and aborted the whole run before any test executed. Its
route-level assertions are covered by the classes above; this call-site
pin was the one thing it had that nothing else did.
Retargeted for the port: `_perform_post_login_tasks_body` moved from
`web/auth/routes.py` to `web/routers/auth.py`. Retargeted again for
#5841: the trusted bootstrap call sites spend the bypass through direct
``import_settings(..., override_locked=True)`` calls because the
``load_from_defaults_file`` wrapper no longer accepts the kwarg (it is
the wrapper's job to be structurally incapable of smuggling it).
A behavioural test covers the post-login path; the initializer and the
manager's own seeding path are awkward to drive end to end, and this is
what stops a refactor from quietly dropping the keyword there. Without
it a LOCKED account silently misses every setting a later release ships,
which shows up much later as a missing-key crash rather than as a lock
error.
"""
import ast
import inspect
import textwrap
from local_deep_research.database import initialize as db_initialize
from local_deep_research.web.routers import auth as auth_router
call_sites = [
(
auth_router._perform_post_login_tasks_body,
"post-login settings migration",
),
(
db_initialize._initialize_default_settings,
"database initializer",
),
(
SettingsManager._ensure_settings_initialized,
"manager first-run seeding",
),
]
for func, label in call_sites:
# A char-window check here (e.g. "override_locked=True" in
# src[i:i+200]) can be satisfied by an unrelated call further down
# the source -- parse the call instead of pattern-matching nearby
# text.
src = textwrap.dedent(inspect.getsource(func))
tree = ast.parse(src)
calls = [
node
for node in ast.walk(tree)
if isinstance(node, ast.Call)
and (
(
isinstance(node.func, ast.Name)
and node.func.id == "import_settings"
)
or (
isinstance(node.func, ast.Attribute)
and node.func.attr == "import_settings"
)
)
]
assert len(calls) == 1, (
f"expected exactly one import_settings(...) call in {label}, "
f"found {len(calls)}"
)
call = calls[0]
override_locked_true = any(
kw.arg == "override_locked"
and isinstance(kw.value, ast.Constant)
and kw.value.value is True
for kw in call.keywords
)
assert override_locked_true, (
f"the {label} calls import_settings without "
"override_locked=True, so a locked account will silently miss "
"settings the upgrade shipped"
)