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.

7.3 KB
Raw Blame
"""The two halves of the settings lock (#5531/#5659) that survived the port
untested.

``tests/web/routers/test_settings_lock_enforcement.py`` already carries most
of the Flask-era ``tests/web/routes/test_settings_lock_enforcement.py``: the
three route guards (403 on reset / import / delete while locked), the
manager-level refusal, and the source-level pin that all three bootstrap call
sites pass ``override_locked=True``. Two of that file's tests have no
successor anywhere on the branch, and this file recovers them.

1. ``delete_extra`` pruning must keep working on a LOCKED account.
   ``import_settings`` prunes with a direct query rather than through the
   public ``delete_setting()`` helper, deliberately: ``delete_setting()``
   defaults to ``override_locked=False`` and swallows SQL errors, so routing
   the prune through it would silently keep stale rows on every locked
   account. Nothing else pins that -- the branch's ``delete_extra`` tests
   (tests/settings/test_settings_manager.py, tests/api/test_settings_utils.py
   and friends) all run against an UNLOCKED manager, where the distinction
   does not exist.

2. The post-login migration must actually reach a locked account.
   ``test_settings_lock_enforcement.py::test_bootstrap_call_sites_pass_override_locked``
   pins the keyword by reading the source, and
   ``TestStartupIsUnaffectedByTheLock`` drives the override-locked half
   through a direct ``import_settings(..., override_locked=True)`` call
   (``load_from_defaults_file`` no longer accepts the kwarg, #5841) -- but
   nothing drives the real call site. A refactor that moved step 1 of
   ``_perform_post_login_tasks_body`` behind a differently-named helper, or
   dropped it, would leave both of those green while a locked account
   silently stopped receiving settings an upgrade shipped.

Both tests run against a real in-memory database and read the value back out,
so the assertion is never made against a mock of itself.
"""

from contextlib import contextmanager
from unittest.mock import patch

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import Base
from local_deep_research.database.models.settings import Setting, SettingType
from local_deep_research.settings.manager import SettingsManager

LOCK_KEY = "app.lock_settings"


@pytest.fixture
def locked_session():
    """A real settings database whose ``app.lock_settings`` row is true."""
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()
    session.add(
        Setting(
            key=LOCK_KEY,
            value=True,
            type=SettingType.APP,
            name="Lock Settings",
            editable=False,
            visible=False,
            # ui_element drives the read path's type coercion, so a lock row
            # without it reads back as the string "False" and is truthy.
            ui_element="checkbox",
        )
    )
    session.add(
        Setting(
            key="llm.temperature",
            value="0.7",
            type=SettingType.LLM,
            name="Temperature",
            editable=True,
            ui_element="number",
        )
    )
    session.commit()
    yield session
    session.close()
    engine.dispose()


def _value_of(session, key):
    row = session.query(Setting).filter(Setting.key == key).first()
    return row.value if row else None


def test_delete_extra_still_prunes_while_locked(locked_session):
    """``delete_extra`` is not routed through the ``delete_setting`` guard.

    The database initializer calls ``import_settings(..., overwrite=False,
    delete_extra=True, override_locked=True)`` directly (the trusted
    bootstrap bypass, spent at the import call site since #5841). If the
    pruning branch went through the public ``delete_setting()`` helper it
    would hit that helper's own ``override_locked=False`` default and
    silently keep stale rows on locked accounts. It does not:
    ``import_settings`` prunes with a direct query, because import needs
    strict failure semantics and ``delete_setting()`` swallows SQL errors.
    This pins that.
    """
    locked_session.add(
        Setting(
            key="search.removed_in_this_release",
            value="stale",
            type=SettingType.SEARCH,
            name="Removed In This Release",
            editable=True,
            ui_element="text",
        )
    )
    locked_session.commit()
    assert _value_of(locked_session, "search.removed_in_this_release")

    manager = SettingsManager(db_session=locked_session)

    # The lock row itself is carried in the payload: it is a real setting and
    # omitting it would make ``delete_extra`` prune the lock under test.
    manager.import_settings(
        {
            LOCK_KEY: {
                "value": True,
                "type": "APP",
                "name": "Lock Settings",
                "editable": False,
                "visible": False,
                "ui_element": "checkbox",
            },
            "llm.temperature": {
                "value": "0.7",
                "type": "LLM",
                "name": "Temperature",
                "ui_element": "number",
            },
        },
        overwrite=False,
        delete_extra=True,
        override_locked=True,
    )

    assert _value_of(locked_session, "search.removed_in_this_release") is None
    assert _value_of(locked_session, LOCK_KEY) is not None


def test_post_login_migration_adds_upgrade_settings_to_a_locked_account(
    locked_session,
):
    """Drive ``override_locked`` at the place it is actually spent.

    Only step 1 of ``_perform_post_login_tasks_body`` is under test; the
    later steps have their own try/except and are expected to fail against
    this fixture.
    """
    from local_deep_research.web.routers import auth as auth_router

    @contextmanager
    def _locked(*_args, **_kwargs):
        yield locked_session

    # No app.version row, so db_version_matches_package() is False and the
    # migration branch runs. Assert that first: the branch is inside a
    # try/except that logs, so a test that never entered it would pass.
    assert not SettingsManager(
        db_session=locked_session
    ).db_version_matches_package()

    with patch(
        "local_deep_research.database.session_context.get_user_db_session",
        _locked,
    ):
        auth_router._perform_post_login_tasks_body("testuser", "pw", "sess-1")

    # The migration imports the whole defaults file with overwrite=False, so
    # a locked account ends up holding keys it did not have before. Assert on
    # a key the fixture does not seed: ``update_db_version`` writes
    # ``app.version`` whether or not the import was refused, so a row count
    # alone passes even when the guard rejected everything.
    assert _value_of(locked_session, "app.debug") is not None, (
        "the locked account did not receive app.debug, so the bootstrap "
        "import was refused: override_locked is missing at the call site"
    )
    assert locked_session.query(Setting).count() > 100, (
        "the defaults file holds ~340 keys; a locked account that received "
        "only a handful got a refused import, not a completed one"
    )

    # ...without the import clearing or overwriting the lock.
    assert _value_of(locked_session, LOCK_KEY) in (True, "True", 1)