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.

4.8 KB
Raw Blame
"""Rate-limit scope tests for the notification test-url endpoint.

``api_test_notification_url`` (web/routers/settings.py) carries
``@notification_test_limit`` — a ``shared_limit`` with its own
``notification_test`` bucket (per-user key), whose ``exempt_when`` is
``_caller_supplied_notification_url``. The split that matters:

* a caller who names a URL in the body is exempt (the case the endpoint
  exists to serve — testing a URL while configuring notifications);
* the STORED-URL fallback (blank/sentinel body) consumes the bucket —
  that path is a zero-argument send trigger to the caller's own
  configured webhook (#5958 follow-up).

Like ``tests/web/routes/test_research_log_export_rate_limit.py`` (the
idiom this file follows), enforcement is driven via
``limiter._check_request_limit`` — the exact call slowapi's decorator
wrapper makes per request — rather than a TestClient round trip: the
limiter and its storage are process-global (other test modules import
the app with ``LDR_DISABLE_RATE_LIMITING=true``), and the raw check
needs none of the endpoint's DB plumbing. A uuid-unique username per
test isolates each bucket, so no ``limiter.reset()`` is required.

The payload is stashed on ``request.state`` by the real
``_notification_test_body`` route dependency before slowapi evaluates
``exempt_when``; the tests reproduce that stash directly.
"""

import uuid

import pytest
from slowapi.errors import RateLimitExceeded
from starlette.requests import Request

from local_deep_research.web.routers import settings as settings_mod

_ENDPOINT = f"{settings_mod.__name__}.api_test_notification_url"


def _request(username):
    """Minimal Starlette ``Request`` from a raw ASGI scope (idiom from
    ``test_research_log_export_rate_limit.py``). The session key makes
    ``_user_key`` resolve to a per-user bucket, like a logged-in request.
    """
    scope = {
        "type": "http",
        "method": "POST",
        "path": "/settings/api/notifications/test-url",
        "query_string": b"",
        "headers": [],
        "client": ("10.204.5.2", 51234),
        "session": {"username": username},
    }
    return Request(scope)


def _route_limit():
    """The endpoint's ``notification_test`` limit.

    Selected by scope rather than unpacked as the route's only entry:
    ``_route_limits`` holds one parsed ``Limit`` per limit string, and a
    ``;``-joined multi-limit configuration -- the shape ``DEFAULT_RATE_LIMIT``
    already uses -- would make a one-element unpack raise instead of testing
    anything. With several, the smallest quota is the one a burst hits first,
    so that is the one the enforcement test has to exhaust.
    """
    limits = [
        limit
        for limit in settings_mod.limiter._route_limits[_ENDPOINT]
        if limit.scope == "notification_test"
    ]
    assert limits, (
        f"no notification_test limit registered for {_ENDPOINT}; "
        f"got {settings_mod.limiter._route_limits[_ENDPOINT]!r}"
    )
    return min(limits, key=lambda limit: limit.limit.amount)


def test_stored_url_fallback_consumes_the_notification_test_bucket(
    monkeypatch,
):
    monkeypatch.setattr(settings_mod.limiter, "enabled", True)
    limit = _route_limit()
    # Wiring: own scope (not the shared "settings" bucket) with the
    # notification-specific exemption predicate.
    assert limit.scope == "notification_test"
    assert limit.exempt_when is settings_mod._caller_supplied_notification_url

    quota = limit.limit.amount  # 30 per minute via rate_limit_settings
    username = f"notif-test-bucket-{uuid.uuid4().hex[:12]}"

    def hit():
        request = _request(username)
        # Blank body: not caller-supplied, so the stored-URL fallback is
        # what this request would take — it must consume the bucket.
        request.state.notification_test_payload = {"service_url": ""}
        settings_mod.limiter._check_request_limit(
            request, settings_mod.api_test_notification_url, False
        )

    for _ in range(quota):
        hit()  # within the quota: must not raise

    with pytest.raises(RateLimitExceeded):
        hit()  # one past the quota: the fallback path is cut off


def test_caller_supplied_url_is_exempt_from_the_notification_test_bucket(
    monkeypatch,
):
    monkeypatch.setattr(settings_mod.limiter, "enabled", True)
    quota = _route_limit().limit.amount
    username = f"notif-test-exempt-{uuid.uuid4().hex[:12]}"

    # Well past the stored-URL quota: a caller who names their own
    # destination must never be throttled by the fallback bucket.
    for _ in range(quota + 10):
        request = _request(username)
        request.state.notification_test_payload = {
            "service_url": "discord://HOOKID/TOKEN"
        }
        settings_mod.limiter._check_request_limit(
            request, settings_mod.api_test_notification_url, False
        )