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.5 KB
Raw Blame
"""Pins Retry-After / X-RateLimit-* headers on a REAL 429 from a REAL
rate-limited route, through the full app stack.

Regression: ``dependencies/rate_limit.py`` sets ``headers_enabled=True``
on the slowapi ``Limiter`` (restoring main's Flask-Limiter parity), but
that flag alone does not add the headers to a live response — slowapi
only injects them when something calls ``Limiter._inject_headers``, and
slowapi's OWN default ``_rate_limit_exceeded_handler`` (``slowapi/
extension.py``) is what calls it. ``fastapi_app.py``'s
``_setup_rate_limiting`` registers a CUSTOM handler instead (for the
audit-log line and main's ``{"error", "message"}`` body contract) that,
before this fix, built a bare ``JSONResponse`` and never called
``_inject_headers`` — so live 429s carried none of these headers even
with the flag on.

Fix: the custom handler now calls
``request.app.state.limiter._inject_headers(response,
request.state.view_rate_limit)`` itself, guarded with ``getattr(...,
None)`` so a request that somehow reaches the handler without
``view_rate_limit`` set doesn't turn into a 500.

Both ``view_rate_limit`` and ``_inject_headers`` are private slowapi
API (leading underscore / dunder-mangled), verified against the
installed slowapi source rather than assumed. This test exists so a
future slowapi upgrade that renames either one fails loudly here
instead of silently dropping the headers again.
"""

import uuid

import pytest
from fastapi.testclient import TestClient

from local_deep_research.web.dependencies.rate_limit import (
    REGISTRATION_RATE_LIMIT,
    limiter,
)


def _first_amount(limit_value) -> int:
    """``"5 per 15 minutes"`` -> 5; ``"60 per minute;1000 per hour"`` -> 60."""
    return int(str(limit_value).split(";")[0].strip().split()[0])


REGISTER_ATTEMPTS = _first_amount(REGISTRATION_RATE_LIMIT)


@pytest.fixture(autouse=True)
def rate_limiting_enforced():
    """Force slowapi enforcement ON for each test, then restore — mirrors
    tests/web/routers/test_auth_rate_limits.py's fixture of the same
    name/purpose (CI runs with LDR_DISABLE_RATE_LIMITING=true)."""
    original = limiter.enabled
    limiter.enabled = True
    yield
    limiter.enabled = original
    try:
        limiter.reset()
    except Exception:
        pass


def _unique_ip() -> str:
    parts = [uuid.uuid4().int % 254 + 1 for _ in range(3)]
    return f"10.{parts[0]}.{parts[1]}.{parts[2]}"


def _make_client(app, ip: str) -> TestClient:
    client = TestClient(app, raise_server_exceptions=False)
    client.headers.update({"X-Forwarded-For": ip})
    client.get("/auth/login")
    token = client.get("/auth/csrf-token").json()["csrf_token"]
    client.headers.update({"X-CSRFToken": token})
    return client


# Invalid but cheap: fails validation with 400 before any DB access,
# while still counting toward the registration bucket (slowapi's route
# decorator increments before the endpoint body runs).
_BAD_REGISTER = {
    "username": "x",
    "password": "short",
    "confirm_password": "short",
    "acknowledge": "false",
}


def _require_small(n: int) -> None:
    if n > 15:
        pytest.skip(
            f"registration limit is {n} in this environment — too many "
            "requests to exercise the threshold quickly"
        )


def test_429_carries_retry_after_and_ratelimit_headers(app):
    _require_small(REGISTER_ATTEMPTS)
    client = _make_client(app, _unique_ip())

    resp = None
    for i in range(REGISTER_ATTEMPTS):
        resp = client.post(
            "/auth/register", data=_BAD_REGISTER, follow_redirects=False
        )
        assert resp.status_code == 400, (
            f"attempt {i + 1}/{REGISTER_ATTEMPTS} must be a validation "
            f"400, not rate-limited yet; got {resp.status_code}"
        )

    resp = client.post(
        "/auth/register", data=_BAD_REGISTER, follow_redirects=False
    )
    assert resp.status_code == 429, (
        f"registration never rate-limited after {REGISTER_ATTEMPTS} "
        f"attempts (last status {resp.status_code})"
    )

    # The actual regression: these must be present on the live 429.
    assert "retry-after" in resp.headers, (
        f"429 missing Retry-After: {dict(resp.headers)}"
    )
    assert int(resp.headers["retry-after"]) >= 0

    assert "x-ratelimit-limit" in resp.headers
    assert "x-ratelimit-remaining" in resp.headers
    assert "x-ratelimit-reset" in resp.headers

    # Body contract must be unchanged by this fix.
    assert resp.json() == {
        "error": "Too many requests",
        "message": "Too many attempts. Please try again later.",
    }