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.

5.9 KB
Raw Blame
"""Rate-limit contract tests for research log exports.

Ported from main's Flask-Limiter version (added by #5369, "fix(logs):
exclude HEAD preflight from export quota") onto the FastAPI + slowapi
wiring in ``web/routers/research.py`` + ``web/dependencies/rate_limit.py``.

Main's ``_is_log_export_rate_limit_exempt`` read Flask's ``request``
global directly. slowapi has no such global: it inspects the
``exempt_when`` callable's signature and passes the Starlette ``Request``
only when the callable declares exactly one parameter (see
``slowapi/wrappers.py``'s ``_exempt_when_takes_request`` /
``Limit.is_exempt``), so the port — ``_log_export_exempt`` — takes
``request`` explicitly instead of reading a global.

Both properties main pinned still matter here and are asserted against
the REAL slowapi objects (no re-declared limits):

* ``test_log_export_exemption_preserves_api_rules`` — the HEAD carve-out
  is ADDED on top of the pre-existing ``_api_exempt`` rule (the
  ``app.api_rate_limit = 0`` escape hatch), not a replacement for it.
* ``test_log_export_exempt_is_wired_as_the_routes_exempt_when`` — the
  route's registered limit actually uses ``_log_export_exempt`` (not the
  bare ``_api_exempt``) as its ``exempt_when``.
* ``TestHeadPreflightsDoNotConsumeGetExportQuota`` — end-to-end: ten HEAD
  pre-flights must not burn any of the ten per-minute GET slots.

The enforcement test drives ``limiter._check_request_limit`` directly —
the exact call slowapi's decorator wrapper makes on every request — with
a uuid-unique username, rather than round-tripping through
``fastapi.testclient.TestClient``. A full TestClient round trip would
additionally require mocking the DB-session plumbing the endpoint body
uses (irrelevant to rate-limiting) and forcing the *global*
``limiter.enabled`` flag for the request, without adding any coverage
over the real check the decorator performs. This mirrors the established
idiom in ``tests/web/routers/test_notes_rate_limit_keys.py``'s
``TestEnforcement``, which solves the same "storage is process-global"
concern the same way: a uuid-unique per-user key isolates each test's
bucket from every other test hitting the same scope in this process.
"""

import uuid
from unittest.mock import patch

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

from local_deep_research.web.dependencies import rate_limit as rl
from local_deep_research.web.routers import research as research_mod

_RR = "local_deep_research.web.routers.research"


def _request(method, *, username=None, ip="10.204.5.1"):
    """Minimal Starlette ``Request`` from a raw ASGI scope (idiom from
    ``tests/web/routers/test_notes_rate_limit_keys.py``'s
    ``make_request``). ``username=None`` omits the session key entirely,
    matching an unauthenticated/no-SessionMiddleware scope.
    """
    scope = {
        "type": "http",
        "method": method,
        "path": "/api/research/test-rid/logs/export",
        "query_string": b"",
        "headers": [],
        "client": (ip, 51234),
    }
    if username is not None:
        scope["session"] = {"username": username}
    return Request(scope)


@pytest.mark.parametrize(
    ("method", "base_exempt", "expected", "base_calls"),
    [
        ("HEAD", False, True, 0),
        ("GET", False, False, 1),
        ("GET", True, True, 1),
    ],
)
def test_log_export_exemption_preserves_api_rules(
    method, base_exempt, expected, base_calls
):
    """Only HEAD bypasses the existing API exemption decision.

    HEAD must short-circuit ``_api_exempt`` entirely (0 calls) — proving
    the carve-out doesn't merely happen to agree with the base rule but
    actually skips it. Every GET must defer to ``_api_exempt`` verbatim
    (1 call, its return value passed straight through) — proving the
    base rule (``app.api_rate_limit = 0``) still governs non-HEAD
    requests unchanged.
    """
    request = _request(method)
    with patch(f"{_RR}._api_exempt", return_value=base_exempt) as base:
        assert research_mod._log_export_exempt(request) is expected
        assert base.call_count == base_calls


def test_log_export_exempt_is_wired_as_the_routes_exempt_when():
    """The registered route limit must use ``_log_export_exempt`` — not
    the bare ``_api_exempt`` — as its ``exempt_when``. Catches a revert
    of the decorator argument even without making a single request."""
    name = f"{_RR}.export_research_logs"
    (lim,) = research_mod.limiter._route_limits[name]
    assert lim.scope == "log_export"
    assert lim.exempt_when is research_mod._log_export_exempt
    assert lim.exempt_when is not research_mod._api_exempt


class TestHeadPreflightsDoNotConsumeGetExportQuota:
    """Ten HEAD checks must leave all ten per-minute GET slots
    available — the end-to-end property main's second test pinned."""

    def test_ten_head_checks_leave_all_ten_get_slots(self, monkeypatch):
        monkeypatch.setattr(research_mod.limiter, "enabled", True)
        # Pin the api_rate_limit contextvar to a non-zero (non-exempt)
        # value so this test exercises the HEAD carve-out specifically,
        # not the separate app.api_rate_limit=0 escape hatch.
        token = rl._api_rate_limit_ctx.set(rl.API_RATE_LIMIT_DEFAULT)
        username = f"log-export-quota-{uuid.uuid4().hex[:12]}"

        def hit(method):
            research_mod.limiter._check_request_limit(
                _request(method, username=username),
                research_mod.export_research_logs,
                False,
            )

        try:
            for request_number in range(1, 11):
                hit("HEAD")  # pre-flights: must never be rejected

            for request_number in range(1, 11):
                hit("GET")  # all ten GET slots must still be intact

            with pytest.raises(RateLimitExceeded):
                hit("GET")  # the real 10/minute quota is exhausted here
        finally:
            rl._api_rate_limit_ctx.reset(token)