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.

24.6 KB
Raw Blame
# allow: no-sut-import - black-box HTTP test; drives real routes through the
# FastAPI test client.
"""Rate-limiting, Ollama-status and notification endpoints of the settings router.

Ports main's ``tests/web/routes/test_settings_routes_rate_limiting.py``
(``git show origin/main:tests/web/routes/test_settings_routes_rate_limiting.py``),
deleted by the FastAPI migration. All 24 originals are ported.

Plumbing translation
--------------------
* Flask blueprint ``settings_bp`` -> ``web/routers/settings.py``; the patch
  target for the per-user DB session moves from
  ``web.routes.settings_routes.get_user_db_session`` to
  ``web.routers.settings.get_user_db_session``.
* ``_get_setting_from_session`` grew an explicit ``username`` parameter
  (settings.py:352) now that Flask's ``session`` global is gone, so the
  stub signature is ``(key, username, default=None)``.
* ``POST /settings/api/rate-limiting/cleanup`` is now ``async`` and does its
  delete through ``run_db_sync(_cleanup_rate_limit_estimates_sync, ...)``
  (settings.py:2878-2917). ``_cleanup_rate_limit_estimates_sync`` opens the
  session through the same module-level ``get_user_db_session`` name, so the
  original's structural assertions on the mock session still apply unchanged
  -- which is the point: they pin ``.filter(...).delete()`` + ``.commit()``,
  properties invisible in the response body.
* ``authenticated_client`` / ``client`` come from ``tests/conftest.py`` rather
  than the deleted ``tests/web/routes/_settings_route_helpers.py``.
* No assertion in the original depended on a redirect, so no
  ``follow_redirects`` translation was needed; the two page-less API 401
  checks are asserted on the status code directly.

Regression pinned by this file
------------------------------
``TestNotificationUrlStoredFallbackRegression`` below -- #5958, the
stored-URL fallback on the test-url endpoint. It was red on this branch
until the fallback was ported back; see its docstring.
"""

import time
from contextlib import contextmanager
from types import SimpleNamespace
from unittest.mock import MagicMock, patch

import pytest
from sqlalchemy.exc import OperationalError

SETTINGS_PREFIX = "/settings"
ROUTER = "local_deep_research.web.routers.settings"


def _unauth_post(client, path, **kwargs):
    """POST as an *unauthenticated* caller, past the CSRF gate.

    Main's ``client`` fixture built the Flask app with
    ``WTF_CSRF_ENABLED = False``, so an anonymous POST reached the
    ``@login_required`` check and returned 401. On this branch
    ``CSRFMiddleware`` is unconditionally active (``fastapi_app.py``) and
    runs *before* the auth dependency, so a token-less anonymous POST is
    rejected with 403 "CSRF token missing" and never reaches the auth gate
    -- which would make an unmodified port assert the CSRF middleware
    rather than the route's authentication requirement.

    Fetching an anonymous CSRF token first restores exactly what main's
    fixture arranged: CSRF satisfied, authentication not. (CSRF
    enforcement itself is covered by
    ``tests/web/test_csrf_lifecycle_contracts.py`` and
    ``test_csrf_middleware_edges.py``.)
    """
    client.get("/auth/login")
    token = client.get("/auth/csrf-token").json().get("csrf_token", "")
    kwargs.setdefault("headers", {})["X-CSRFToken"] = token
    kwargs.setdefault("follow_redirects", False)
    return client.post(path, **kwargs)


def _fake_estimate(engine_type, success_rate, total_attempts=10):
    """Build a RateLimitEstimate-shaped row for the DB-backed route."""
    return SimpleNamespace(
        engine_type=engine_type,
        base_wait_seconds=2.0,
        min_wait_seconds=1.0,
        max_wait_seconds=5.0,
        last_updated=time.time(),
        total_attempts=total_attempts,
        success_rate=success_rate,
    )


@contextmanager
def _patch_estimates(estimates=None, query_error=None):
    """Patch the settings router's ``get_user_db_session`` so the
    rate-limiting routes read *estimates* (or raise *query_error*)."""
    session = MagicMock()
    if query_error is not None:
        session.query.side_effect = query_error
    else:
        session.query.return_value.order_by.return_value.all.return_value = (
            estimates or []
        )

    @contextmanager
    def _ctx(username, password=None):
        yield session

    with patch(f"{ROUTER}.get_user_db_session", side_effect=_ctx):
        yield session


class TestApiGetRateLimitingStatus:
    """GET /settings/api/rate-limiting/status."""

    def test_requires_authentication(self, client):
        response = client.get(f"{SETTINGS_PREFIX}/api/rate-limiting/status")
        assert response.status_code == 401, response.status_code

    def test_returns_status_and_engines(self, authenticated_client):
        # The route reads engine rows from persisted RateLimitEstimate
        # records (DB-backed), and the status block from the user's
        # settings -- no get_tracker call anymore (#4721).
        estimates = [
            _fake_estimate("bing", 0.8, total_attempts=30),
            _fake_estimate("google", 0.95, total_attempts=50),
        ]
        with _patch_estimates(estimates):
            response = authenticated_client.get(
                f"{SETTINGS_PREFIX}/api/rate-limiting/status"
            )
        assert response.status_code == 200
        data = response.get_json()
        # status carries the rate-limiting settings block (exact key set)
        assert set(data["status"].keys()) == {
            "enabled",
            "profile",
            "exploration_rate",
            "learning_rate",
            "memory_window",
        }
        assert len(data["engines"]) == 2
        # ordered by engine_type -> bing first
        assert data["engines"][0]["engine_type"] == "bing"
        assert data["engines"][1]["engine_type"] == "google"
        assert data["engines"][1]["success_rate"] == 95.0

    def test_status_block_surfaces_settings_and_defaults(
        self, authenticated_client
    ):
        """Guard the rate_limiting.* status block: the ``profile`` key, the
        ``enabled: True`` fallback default, and that configured values flow
        through rather than being hardcoded.

        ``enabled`` and ``profile`` are omitted from the patched settings so
        the route's own fallback defaults (True / "balanced") must apply;
        the rest are configured to non-defaults to prove flow-through.
        """

        configured = {
            "rate_limiting.exploration_rate": 0.2,
            "rate_limiting.learning_rate": 0.5,
            "rate_limiting.memory_window": 50,
        }

        def fake_get(key, username, default=None):
            return configured.get(key, default)

        with (
            _patch_estimates([]),
            patch(
                f"{ROUTER}._get_setting_from_session",
                side_effect=fake_get,
            ),
        ):
            response = authenticated_client.get(
                f"{SETTINGS_PREFIX}/api/rate-limiting/status"
            )

        assert response.status_code == 200
        status = response.get_json()["status"]
        assert status == {
            "enabled": True,  # fallback default (was False before the fix)
            "profile": "balanced",  # fallback default
            "exploration_rate": 0.2,  # configured -> flows through
            "learning_rate": 0.5,
            "memory_window": 50,
        }

    def test_handles_zero_attempt_engine(self, authenticated_client):
        # A freshly-tracked engine has success_rate 0.0 (the column is
        # NOT NULL with default 0.0), which renders as 0.0 -- there is no
        # None case to handle now that the route is DB-backed.
        estimates = [_fake_estimate("new_engine", 0.0, total_attempts=0)]
        with _patch_estimates(estimates):
            response = authenticated_client.get(
                f"{SETTINGS_PREFIX}/api/rate-limiting/status"
            )
        data = response.get_json()
        assert data["engines"][0]["success_rate"] == 0.0

    def test_error_returns_500(self, authenticated_client):
        with _patch_estimates(query_error=RuntimeError("db error")):
            response = authenticated_client.get(
                f"{SETTINGS_PREFIX}/api/rate-limiting/status"
            )
        assert response.status_code == 500
        data = response.get_json()
        assert "error" in data


class TestApiResetEngineRateLimiting:
    """POST /settings/api/rate-limiting/engines/{engine_type}/reset."""

    def test_requires_authentication(self, client):
        response = _unauth_post(
            client, f"{SETTINGS_PREFIX}/api/rate-limiting/engines/google/reset"
        )
        assert response.status_code == 401, response.status_code

    def test_resets_engine(self, authenticated_client):
        # Deletes the persisted RateLimitEstimate row for the engine
        # (DB-backed, no get_tracker) and commits. These are
        # output-invisible properties: the response body is the same
        # message whether or not the delete/commit actually happened,
        # so they are asserted structurally on the session mock.
        with _patch_estimates() as session:
            response = authenticated_client.post(
                f"{SETTINGS_PREFIX}/api/rate-limiting/engines/google/reset"
            )
        assert response.status_code == 200
        assert "google" in response.get_json()["message"]
        session.query.return_value.filter_by.assert_called_once_with(
            engine_type="google"
        )
        session.query.return_value.filter_by.return_value.delete.assert_called_once()
        session.commit.assert_called_once()

    def test_error_returns_500(self, authenticated_client):
        with _patch_estimates(query_error=RuntimeError("db fail")):
            response = authenticated_client.post(
                f"{SETTINGS_PREFIX}/api/rate-limiting/engines/google/reset"
            )
        assert response.status_code == 500


class TestApiCleanupRateLimiting:
    """POST /settings/api/rate-limiting/cleanup."""

    def test_requires_authentication(self, client):
        response = _unauth_post(
            client, f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup"
        )
        assert response.status_code == 401, response.status_code

    def test_cleanup_default_days(self, authenticated_client):
        # DB-backed: deletes old RateLimitEstimate rows and commits (no
        # tracker). Structural, for the same reason as test_resets_engine.
        with _patch_estimates() as session:
            response = authenticated_client.post(
                f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup"
            )
        assert response.status_code == 200
        assert "30 days" in response.get_json()["message"]
        session.query.return_value.filter.return_value.delete.assert_called_once()
        session.commit.assert_called_once()

    def test_cleanup_custom_days(self, authenticated_client):
        with _patch_estimates() as session:
            response = authenticated_client.post(
                f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup",
                json={"days": 7},
            )
        assert response.status_code == 200
        assert "7 days" in response.get_json()["message"]
        session.query.return_value.filter.return_value.delete.assert_called_once()
        session.commit.assert_called_once()

    def test_error_returns_500(self, authenticated_client):
        with _patch_estimates(query_error=RuntimeError("fail")):
            response = authenticated_client.post(
                f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup"
            )
        assert response.status_code == 500

    @pytest.mark.parametrize("days_value", [0, -1, 366, 1000])
    def test_rejects_out_of_range_days(self, days_value, authenticated_client):
        response = authenticated_client.post(
            f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup",
            json={"days": days_value},
        )
        assert response.status_code == 400

    @pytest.mark.parametrize("days_value", ["not-a-number", None, [1, 2]])
    def test_rejects_non_integer_days(self, days_value, authenticated_client):
        response = authenticated_client.post(
            f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup",
            json={"days": days_value},
        )
        assert response.status_code == 400


def _locked_commit_error():
    """SQLAlchemy-wrapped SQLITE_BUSY, as a locked commit raises it."""
    return OperationalError(
        "DELETE FROM rate_limit_estimates",
        {},
        Exception("database is locked"),
    )


@contextmanager
def _patch_sessions_lock_then_succeed(sessions, first_error):
    """Patch ``get_user_db_session`` to yield one FRESH mock session per
    entry, the first entry's commit raising *first_error* -- the shape a
    transient writer-lock blip produces on these routes (mirrors the
    tracker's retry tests in
    ``tests/web_search_engines/rate_limiting/test_tracker_coverage.py``)."""

    def _factory(username, password=None):
        @contextmanager
        def _cm():
            ms = MagicMock()
            if not sessions and first_error is not None:
                ms.commit.side_effect = first_error
            sessions.append(ms)
            yield ms

        return _cm()

    with patch(f"{ROUTER}.get_user_db_session", side_effect=_factory):
        yield sessions


class TestRateLimitingRoutesLockRetry:
    """#6945: the live reset/cleanup routes must retry a SQLite
    writer-lock blip (the same wrap the tracker's reset_engine /
    cleanup_old_data got), opening a FRESH session per attempt instead
    of surfacing the first ``database is locked`` as a 500."""

    def test_reset_retries_locked_delete(self, authenticated_client):
        sessions = []
        with _patch_sessions_lock_then_succeed(
            sessions, _locked_commit_error()
        ):
            response = authenticated_client.post(
                f"{SETTINGS_PREFIX}/api/rate-limiting/engines/google/reset"
            )
        assert response.status_code == 200
        assert "google" in response.get_json()["message"]
        assert len(sessions) >= 2, (
            "the first 'database is locked' surfaced as a 500 instead of "
            "being retried on a fresh session"
        )
        sessions[
            -1
        ].query.return_value.filter_by.return_value.delete.assert_called_once()
        assert sessions[-1].commit.call_count == 1

    def test_cleanup_retries_locked_delete(self, authenticated_client):
        sessions = []
        with _patch_sessions_lock_then_succeed(
            sessions, _locked_commit_error()
        ):
            response = authenticated_client.post(
                f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup"
            )
        assert response.status_code == 200
        assert "30 days" in response.get_json()["message"]
        assert len(sessions) >= 2, (
            "the first 'database is locked' surfaced as a 500 instead of "
            "being retried on a fresh session"
        )
        sessions[
            -1
        ].query.return_value.filter.return_value.delete.assert_called_once()
        assert sessions[-1].commit.call_count == 1


class TestCheckOllamaStatusSettings:
    """GET /settings/api/ollama-status."""

    def test_requires_authentication(self, client):
        response = client.get(f"{SETTINGS_PREFIX}/api/ollama-status")
        assert response.status_code == 401, response.status_code

    def test_ollama_running(self, authenticated_client):
        mock_resp = MagicMock()
        mock_resp.status_code = 200
        mock_resp.json.return_value = {"version": "0.3.0"}

        with (
            patch(
                f"{ROUTER}._get_setting_from_session",
                return_value="http://localhost:11434",
            ),
            patch(f"{ROUTER}.safe_get", return_value=mock_resp),
        ):
            response = authenticated_client.get(
                f"{SETTINGS_PREFIX}/api/ollama-status"
            )
        assert response.status_code == 200
        data = response.get_json()
        assert data["running"] is True
        assert data["version"] == "0.3.0"

    def test_ollama_non_200(self, authenticated_client):
        mock_resp = MagicMock()
        mock_resp.status_code = 500

        with (
            patch(
                f"{ROUTER}._get_setting_from_session",
                return_value="http://localhost:11434",
            ),
            patch(f"{ROUTER}.safe_get", return_value=mock_resp),
        ):
            response = authenticated_client.get(
                f"{SETTINGS_PREFIX}/api/ollama-status"
            )
        data = response.get_json()
        assert data["running"] is False

    def test_ollama_connection_error(self, authenticated_client):
        import requests

        with (
            patch(
                f"{ROUTER}._get_setting_from_session",
                return_value="http://localhost:11434",
            ),
            patch(
                f"{ROUTER}.safe_get",
                side_effect=requests.exceptions.ConnectionError("refused"),
            ),
        ):
            response = authenticated_client.get(
                f"{SETTINGS_PREFIX}/api/ollama-status"
            )
        data = response.get_json()
        assert data["running"] is False


class TestApiTestNotificationUrl:
    """POST /settings/api/notifications/test-url."""

    def test_requires_authentication(self, client):
        response = _unauth_post(
            client,
            f"{SETTINGS_PREFIX}/api/notifications/test-url",
            json={"service_url": "http://example.com"},
        )
        assert response.status_code == 401, response.status_code

    def test_successful_test(self, authenticated_client):
        mock_ns = MagicMock()
        mock_ns.test_service.return_value = {
            "success": True,
            "message": "Notification sent",
            "error": "",
        }

        with patch(
            "local_deep_research.notifications.service.NotificationService",
            return_value=mock_ns,
        ):
            response = authenticated_client.post(
                f"{SETTINGS_PREFIX}/api/notifications/test-url",
                json={"service_url": "tgram://token/chat_id"},
            )
        assert response.status_code == 200
        data = response.get_json()
        assert data["success"] is True

    def test_failed_test(self, authenticated_client):
        mock_ns = MagicMock()
        mock_ns.test_service.return_value = {
            "success": False,
            "message": "",
            "error": "Invalid URL",
        }

        with patch(
            "local_deep_research.notifications.service.NotificationService",
            return_value=mock_ns,
        ):
            response = authenticated_client.post(
                f"{SETTINGS_PREFIX}/api/notifications/test-url",
                json={"service_url": "invalid://url"},
            )
        data = response.get_json()
        assert data["success"] is False

    def test_missing_service_url(self, authenticated_client):
        response = authenticated_client.post(
            f"{SETTINGS_PREFIX}/api/notifications/test-url",
            json={},
        )
        assert response.status_code == 400
        data = response.get_json()
        assert data["success"] is False

    def test_no_json_body(self, authenticated_client):
        response = authenticated_client.post(
            f"{SETTINGS_PREFIX}/api/notifications/test-url",
        )
        # No JSON body must not surface as a 500: main returned 400 after
        # falling back to the (unconfigured) stored URL; this branch returns
        # 400 from the registered JSONDecodeError handler. Different reason,
        # same contract -- and a 500 here would be the regression.
        assert response.status_code == 400, response.status_code

    def test_does_not_leak_internal_details(self, authenticated_client):
        """Response should only contain expected safe fields."""
        mock_ns = MagicMock()
        mock_ns.test_service.return_value = {
            "success": True,
            "message": "OK",
            "error": "",
            "internal_debug": "SECRET_TOKEN_123",
        }

        with patch(
            "local_deep_research.notifications.service.NotificationService",
            return_value=mock_ns,
        ):
            response = authenticated_client.post(
                f"{SETTINGS_PREFIX}/api/notifications/test-url",
                json={"service_url": "tgram://token/chat_id"},
            )
        data = response.get_json()
        assert "internal_debug" not in data
        assert "SECRET_TOKEN_123" not in str(data)


class TestNotificationUrlStoredFallbackRegression:
    """Regression for #5958 -- these were RED until the fallback was ported.

    ``origin/main``'s ``api_test_notification_url``
    (``web/routes/settings_routes.py``, helpers at :3127 ``_is_blank_service_url``
    and :3140 ``_caller_supplied_notification_url``, added by 69eca236c
    "fix(security): redact notifications.service_url on settings read paths"
    (#5602), which IS inside the merge base b67ddb681) did::

        service_url = data.get("service_url")
        if _is_blank_service_url(service_url) or (
            service_url == DataSanitizer.REDACTION_TEXT
        ):
            service_url = _get_setting_from_session(
                "notifications.service_url", default=""
            )
        if _is_blank_service_url(service_url):
            return jsonify(
                {"success": False, "error": "No notification URL configured"}
            ), 400

    The FastAPI port had replaced all of that with::

        data = await request.json()
        if not isinstance(data, dict) or "service_url" not in data:
            return json_body_error("success", "service_url is required")
        service_url = data["service_url"]

    Neither ``_is_blank_service_url`` nor ``_caller_supplied_notification_url``
    existed anywhere in ``src/``. Consequences:

    1. The stored-URL fallback was gone: "test my configured notification
       URL" (a body with a blank/absent ``service_url``, which is what the
       settings UI sends for the redacted field) could not be done at all.
    2. A blank, whitespace-only, or ``"[REDACTED]"`` ``service_url`` was
       handed straight to ``NotificationService.test_service`` instead of
       being resolved or rejected -- the sentinel case in particular is the
       exact value ``GET /settings/api`` returns for this setting, so a
       round-tripped read tested the literal string ``"[REDACTED]"``.
    3. main's dedicated rate-limit bucket for the zero-argument stored-URL
       trigger went with it.

    All three are restored in ``web/routers/settings.py``; the bucket is
    ``notification_test_limit``, whose exemption predicate reads the body
    stashed by the ``_notification_test_body`` route dependency (the only
    point at which a FastAPI request body is available before slowapi's
    decorator runs).
    """

    @pytest.mark.parametrize(
        "submitted",
        ["", "   ", "[REDACTED]"],
        ids=["empty", "whitespace-only", "redaction-sentinel"],
    )
    def test_blank_or_sentinel_url_falls_back_to_stored_url(
        self, submitted, authenticated_client
    ):
        stored = "tgram://stored-token/stored-chat"
        mock_ns = MagicMock()
        mock_ns.test_service.return_value = {
            "success": True,
            "message": "ok",
            "error": "",
        }

        def fake_get(key, username, default=None):
            if key == "notifications.service_url":
                return stored
            return default

        with (
            patch(f"{ROUTER}._get_setting_from_session", side_effect=fake_get),
            patch(
                "local_deep_research.notifications.service.NotificationService",
                return_value=mock_ns,
            ),
        ):
            authenticated_client.post(
                f"{SETTINGS_PREFIX}/api/notifications/test-url",
                json={"service_url": submitted},
            )

        mock_ns.test_service.assert_called_once_with(stored)

    def test_unconfigured_stored_url_returns_no_url_configured_400(
        self, authenticated_client
    ):
        mock_ns = MagicMock()

        def fake_get(key, username, default=None):
            return "" if key == "notifications.service_url" else default

        with (
            patch(f"{ROUTER}._get_setting_from_session", side_effect=fake_get),
            patch(
                "local_deep_research.notifications.service.NotificationService",
                return_value=mock_ns,
            ),
        ):
            response = authenticated_client.post(
                f"{SETTINGS_PREFIX}/api/notifications/test-url",
                json={"service_url": "   "},
            )

        assert response.status_code == 400
        assert response.get_json() == {
            "success": False,
            "error": "No notification URL configured",
        }
        mock_ns.test_service.assert_not_called()