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.

6.3 KB
Raw Blame
"""SlowAPI shared limits preserve enforcement when checks run in workers."""

import inspect
import threading
from contextvars import ContextVar

import pytest
from fastapi import Depends, FastAPI
from httpx import ASGITransport, AsyncClient
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from slowapi.middleware import SlowAPIMiddleware, _should_exempt
from starlette.requests import Request
from starlette.responses import JSONResponse, Response

from local_deep_research.web.dependencies.rate_limit import (
    _user_key,
    async_shared_limit,
)


@pytest.fixture()
def limiter():
    instance = Limiter(
        key_func=_user_key,
        storage_uri="memory://",
        strategy="moving-window",
        headers_enabled=True,
    )
    instance.enabled = True
    return instance


def _request(username="alice"):
    return Request(
        {
            "type": "http",
            "method": "GET",
            "path": "/limited",
            "headers": [],
            "query_string": b"",
            "session": {"username": username},
            "client": ("127.0.0.1", 1234),
        }
    )


@pytest.mark.asyncio
@pytest.mark.parametrize("return_dict", [False, True])
async def test_storage_and_headers_run_in_workers(
    limiter, monkeypatch, return_dict
):
    """Real counter writes and header reads keep the caller's context."""
    caller_thread = threading.get_ident()
    context = ContextVar("limit_request_context", default="missing")
    token = context.set("authenticated")
    storage_calls = []
    for name in ("acquire_entry", "get_moving_window"):
        original = getattr(limiter._storage, name)

        def record(*args, _original=original, _name=name, **kwargs):
            storage_calls.append((_name, threading.get_ident(), context.get()))
            return _original(*args, **kwargs)

        monkeypatch.setattr(limiter._storage, name, record)

    async def endpoint(request: Request, response: Response):
        assert threading.get_ident() == caller_thread
        assert context.get() == "authenticated"
        if return_dict:
            return {"ok": True}
        return JSONResponse({"ok": True})

    wrapped = async_shared_limit(
        limiter, "2 per hour", scope="thread-test", key_func=_user_key
    )(endpoint)
    response = Response()
    try:
        result = await wrapped(_request(), response=response)
    finally:
        context.reset(token)

    headers = response.headers if return_dict else result.headers
    assert headers["X-RateLimit-Limit"] == "2"
    assert headers["X-RateLimit-Remaining"] == "1"
    assert {name for name, _, _ in storage_calls} == {
        "acquire_entry",
        "get_moving_window",
    }
    assert all(tid != caller_thread for _, tid, _ in storage_calls)
    assert all(value == "authenticated" for _, _, value in storage_calls)
    assert inspect.iscoroutinefunction(wrapped)
    assert inspect.signature(wrapped) == inspect.signature(endpoint)
    assert inspect.unwrap(wrapped) is endpoint
    assert _should_exempt(limiter, wrapped)


@pytest.mark.asyncio
async def test_middleware_leaves_shared_quota_to_authenticated_route(limiter):
    """Shared limits run after auth and keep users on one IP independent."""
    app = FastAPI()
    app.state.limiter = limiter
    app.add_middleware(SlowAPIMiddleware)
    app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

    async def authenticate(request: Request):
        request.scope["session"] = {"username": request.headers["x-user"]}

    shared = async_shared_limit(
        limiter, "2 per hour", scope="shared-test", key_func=_user_key
    )

    @app.get("/first", dependencies=[Depends(authenticate)])
    @shared
    async def first(request: Request):
        return JSONResponse({"endpoint": "first"})

    @app.get("/second", dependencies=[Depends(authenticate)])
    @shared
    async def second(request: Request):
        return JSONResponse({"endpoint": "second"})

    async with AsyncClient(
        transport=ASGITransport(app=app), base_url="http://test"
    ) as client:
        responses = [
            await client.get(path, headers={"x-user": user})
            for path, user in (
                ("/first", "alice"),
                ("/second", "alice"),
                ("/first", "alice"),
                ("/first", "bob"),
                ("/second", "bob"),
                ("/second", "bob"),
            )
        ]
    assert [response.status_code for response in responses] == [
        200,
        200,
        429,
        200,
        200,
        429,
    ]
    assert responses[0].headers["X-RateLimit-Remaining"] == "1"
    assert responses[1].headers["X-RateLimit-Remaining"] == "0"


@pytest.mark.asyncio
async def test_stacked_limits_charge_each_bucket_once(limiter):
    """SlowAPI registers stacked limits against the same handler name."""

    def decorate(scope):
        return async_shared_limit(
            limiter, "2 per hour", scope=scope, key_func=_user_key
        )

    @decorate("outer")
    @decorate("inner")
    async def endpoint(request: Request):
        return JSONResponse({"ok": True})

    for _ in range(2):
        assert (await endpoint(request=_request())).status_code == 200
    with pytest.raises(RateLimitExceeded):
        await endpoint(request=_request())


@pytest.mark.asyncio
async def test_disabled_limiter_does_not_contact_storage(limiter, monkeypatch):
    async def endpoint(request: Request):
        return {"ok": True}

    def unexpected_storage(*args, **kwargs):
        pytest.fail("disabled limiter contacted storage")

    wrapped = async_shared_limit(
        limiter, "1 per hour", scope="disabled-test", key_func=_user_key
    )(endpoint)
    limiter.enabled = False
    monkeypatch.setattr(limiter._storage, "acquire_entry", unexpected_storage)
    for _ in range(2):
        assert await wrapped(request=_request()) == {"ok": True}


def test_rejects_sync_endpoint_and_dynamic_limit(limiter):
    def sync_endpoint(request: Request):
        return {"ok": True}

    with pytest.raises(TypeError, match="async endpoint"):
        async_shared_limit(
            limiter, "1 per hour", scope="invalid", key_func=_user_key
        )(sync_endpoint)
    with pytest.raises(TypeError, match="static limit string"):
        async_shared_limit(
            limiter, lambda: "1 per hour", scope="invalid", key_func=_user_key
        )