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.

8.3 KB
Raw Blame
"""Per-user keying fences for the chat router's slowapi rate-limit buckets.

``web/routers/chat.py`` decorates its seven mutating endpoints with
inline ``@limiter.limit("N per minute", key_func=_chat_user_key)``
buckets; ``_chat_user_key`` itself is unit-tested (``tests/chat/
test_chat_route_helpers.py``). Per-user keying and the exact rate for
each of these seven endpoints are already pinned app-wide by
``tests/web/test_rate_limit_census.py`` (``test_endpoint_buckets_
match_the_census``, ``test_no_unlisted_rate_limited_endpoint`` — a
dropped ``key_func`` or a changed rate shows up there as a census
mismatch), and ``tests/web/routers/test_router_sibling_consistency.py``
requires all seven routes to carry a ``@limiter.limit`` decorator at
all.

What this file adds on top of that: a chat-local view of the same
wiring, exercised directly against the imported ``chat`` module rather
than through the census's app-wide registry scan (the fixture/helper
structure — a per-test ``chat_mod`` fixture and a ``_limits_for``
lookup by qualified name — is borrowed from ``tests/web/routers/
test_notes_rate_limit_keys.py``, but without that file's exact-bucket-
set, dynamic-limit or enforcement-class coverage); and a decorator-
order guard the census cannot make. ``TestWiringIsReal`` checks that
each limit is attached to the function FastAPI actually registered as
the route endpoint, catching the case where ``@limiter.limit`` ends up
above ``@router.<method>`` and the route is registered with the
undecorated function — the limit stays recorded in the limiter but
never fires.
"""

# allow: no-sut-import — the SUT is imported dynamically via
# ``importlib.import_module(CHAT_MODULE)`` below (and accessed by
# qualified name), so there is no literal import statement for the
# shadow-test scanner to find. This file does exercise the real
# ``web/routers/chat.py`` wiring.
import importlib

import pytest
from fastapi.routing import APIRoute
from starlette.requests import Request

CHAT_MODULE = "local_deep_research.web.routers.chat"

# Endpoint -> (amount, granularity) pinned from web/routers/chat.py.
# The cost-tiering is deliberate: sending a message and kicking off
# research (send_message, generate-title, retry) are the expensive
# paths at 10/min; session bookkeeping (update/delete/delete-attempt)
# rides at 30/min; creation is 20/min.
EXPECTED_ENDPOINT_RATES = {
    "create_session": (20, "minute"),
    "generate_session_title": (10, "minute"),
    "update_session": (30, "minute"),
    "delete_session": (30, "minute"),
    "send_message": (10, "minute"),
    "delete_attempt": (30, "minute"),
    "retry_attempt": (10, "minute"),
}


@pytest.fixture()
def chat_mod():
    """The chat router module as currently loaded.

    Resolved per-test (idiom from the notes fences): a sibling test
    file may reload the rate_limit module, so all assertions use the
    limiter / _chat_user_key objects the chat module actually bound at
    ITS import — the ones its routes are registered with.
    """
    return importlib.import_module(CHAT_MODULE)


def _limits_for(chat_mod, endpoint_name):
    """The slowapi Limit objects registered for a chat endpoint."""
    qualified = f"{CHAT_MODULE}.{endpoint_name}"
    limits = chat_mod.limiter._route_limits.get(qualified, [])
    assert limits, (
        f"{qualified} has no registered rate limit — the limit decorator "
        "was removed or renamed"
    )
    return limits


def make_request(username=None, ip="10.202.7.1", method="POST", path="/x"):
    """Minimal Starlette Request from a raw ASGI scope.

    ``username=None`` means no session key at all (anonymous), which
    exercises the per-IP fallback path of the key function.
    """
    scope = {
        "type": "http",
        "method": method,
        "path": path,
        "query_string": b"",
        "headers": [],
        "client": (ip, 51234),
    }
    if username is not None:
        scope["session"] = {"username": username}
    return Request(scope)


class TestPerUserKeying:
    """Regression guard: chat buckets must key per user, not per IP."""

    @pytest.mark.parametrize("endpoint", sorted(EXPECTED_ENDPOINT_RATES))
    def test_endpoint_keys_per_user(self, chat_mod, endpoint):
        for lim in _limits_for(chat_mod, endpoint):
            assert lim.key_func is chat_mod._chat_user_key, (
                f"{endpoint} must pass key_func=_chat_user_key — got "
                f"{lim.key_func!r}. Without per-user keying, users behind "
                "a shared NAT/proxy IP starve each other's chat budgets."
            )

    def test_no_chat_limit_uses_the_limiter_default_ip_key(self, chat_mod):
        """The Limiter default key (_get_client_ip) is per-IP; no chat
        bucket may fall back to it."""
        default_key = chat_mod.limiter._key_func
        for endpoint in EXPECTED_ENDPOINT_RATES:
            for lim in _limits_for(chat_mod, endpoint):
                assert lim.key_func is not default_key, (
                    f"{endpoint} is keyed by the limiter default (client "
                    "IP) — per-user keying was dropped"
                )

    def test_user_key_separates_two_users_on_the_same_ip(self, chat_mod):
        """The key func the buckets are wired with must yield different
        bucket keys for different logged-in users on ONE IP."""
        key_func = _limits_for(chat_mod, "send_message")[0].key_func
        key_a = key_func(make_request(username="alice", ip="10.202.7.9"))
        key_b = key_func(make_request(username="bob", ip="10.202.7.9"))
        assert key_a != key_b
        assert "alice" in key_a and "bob" in key_b

    def test_unauthenticated_key_falls_back_to_ip(self, chat_mod):
        """Without a session the key degrades to the client IP (the
        documented fallback), still distinguishing peers."""
        key_func = _limits_for(chat_mod, "send_message")[0].key_func
        key_a = key_func(make_request(username=None, ip="10.202.7.10"))
        key_b = key_func(make_request(username=None, ip="10.202.7.11"))
        assert key_a != key_b


class TestPinnedRates:
    """The rate numbers are load-bearing (cost-tiered) — pin them."""

    @pytest.mark.parametrize(
        "endpoint,rate", sorted(EXPECTED_ENDPOINT_RATES.items())
    )
    def test_endpoint_rate_is_pinned(self, chat_mod, endpoint, rate):
        amount, per = rate
        limits = _limits_for(chat_mod, endpoint)
        parsed = {
            (lim.limit.amount, lim.limit.GRANULARITY.name) for lim in limits
        }
        assert parsed == {(amount, per)}, (
            f"{endpoint} must carry exactly {amount} per {per} and "
            f"nothing else, registered: {parsed} — a stacked second "
            "limit (e.g. a looser default-keyed one) would pass a "
            "membership check but still widens the budget"
        )
        for lim in limits:
            assert lim.limit.multiples == 1, (
                f"{endpoint}: {lim.limit} has multiples="
                f'{lim.limit.multiples} — "{amount} per {per}" means a '
                f'1x window; "{amount} per N {per}s" (multiples=N) '
                "matches the (amount, granularity) pair above but is a "
                "different, looser rate"
            )


class TestWiringIsReal:
    """The limit must be attached to the function FastAPI registered."""

    @pytest.mark.parametrize("endpoint", sorted(EXPECTED_ENDPOINT_RATES))
    def test_endpoint_carries_its_limit(self, chat_mod, endpoint):
        """A decorator-order slip would register the limit in the limiter
        but leave the route unwrapped — _limits_for's non-empty result
        plus this wrapper check catches both halves."""
        func = getattr(chat_mod, endpoint, None)
        assert func is not None, f"{endpoint} no longer exists in the router"
        routes = [
            route
            for route in chat_mod.router.routes
            if isinstance(route, APIRoute) and route.name == endpoint
        ]
        assert routes, f"{endpoint} has no registered FastAPI route"
        for route in routes:
            assert route.endpoint is func, (
                f"{endpoint}: FastAPI registered a different function; "
                "place @router above @limiter.limit"
            )
            assert getattr(route.endpoint, "__wrapped__", None) is not None, (
                f"{endpoint}: FastAPI registered an unwrapped endpoint"
            )