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.

2.2 KB
Raw Blame
"""The settings update path must not log the value it wrote (#6529).

``_api_update_setting_sync`` logged ``Setting {key} changed to {value}`` at
debug level for every key in ``WARNING_AFFECTING_KEYS``. #6201 established
that a settings value must never be interpolated into a log line, since any
key may carry a secret; this sink was the one it did not reach. No key in the
set carries a secret today, so the test writes a distinct sentinel under one
of them.
"""

from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch

S = "local_deep_research.web.routers.settings"


def test_warning_affecting_update_never_logs_the_value(loguru_caplog):
    from local_deep_research.web.routers.settings import (
        WARNING_AFFECTING_KEYS,
        _api_update_setting_sync,
    )

    key = "search.iterations"
    assert key in WARNING_AFFECTING_KEYS
    sentinel = "sk-live-update-must-not-reach-the-log-6529"

    existing = Mock()
    existing.key = key
    existing.editable = True
    existing.ui_element = "text"
    query = MagicMock()
    query.first.return_value = existing
    query.filter.return_value = query
    query.filter_by.return_value = query
    session = MagicMock()
    session.query.return_value = query

    @contextmanager
    def fake_db_session(*_args, **_kwargs):
        yield session

    with (
        patch(f"{S}.get_user_db_session", side_effect=fake_db_session),
        patch(f"{S}.validate_setting", return_value=(True, None)),
        patch(f"{S}.set_setting", return_value=True),
        patch(f"{S}.invalidate_settings_caches"),
        patch(f"{S}.reschedule_document_jobs_if_needed"),
        patch(f"{S}.reschedule_zotero_jobs_if_needed"),
        patch(f"{S}.calculate_warnings", return_value=[]),
        loguru_caplog.at_level("DEBUG"),
    ):
        response = _api_update_setting_sync({"value": sentinel}, key, "alice")

    assert response == {
        "message": f"Setting {key} updated successfully",
        "warnings": [],
    }
    # POSITIVE CONTROL: the debug line fired, reached the sink, and names
    # the key and the warning count.
    assert f"Setting {key} changed" in loguru_caplog.text
    assert "calculated 0 warnings" in loguru_caplog.text
    assert sentinel not in loguru_caplog.text