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.0 KB
Raw Blame
"""
Tests for the slowapi storage_uri wiring in
``web/dependencies/rate_limit.py``.

Pre-fix the module instantiated ``Limiter(key_func=...)`` with no
storage_uri, so all rate-limit counters lived in per-worker memory.
A multi-worker uvicorn deploy effectively multiplied the per-IP login
brute-force limit by the worker count, and a restart wiped lockout
state.

Now the module reads ``RATE_LIMIT_STORAGE_URI`` from env and threads
it through to ``Limiter`` when set. When unset, a single startup-time
WARNING surfaces the in-memory caveat.
"""

import importlib
from contextlib import contextmanager

import pytest


def _reload_rate_limit():
    """Reload the module so it picks up env-var changes from monkeypatch."""
    from local_deep_research.web.dependencies import rate_limit

    importlib.reload(rate_limit)
    return rate_limit


@contextmanager
def _rate_limit_module_restored():
    """Snapshot the module's globals and put the ORIGINAL objects back.

    THE PROBLEM (cross-file pollution that only bites in the full suite):
    ``importlib.reload`` re-executes the module into the SAME namespace,
    rebinding ``limiter`` to a brand-new ``Limiter`` whose
    ``_Limiter__marked_for_limiting`` / ``_route_limits`` registries are
    EMPTY. Every router (``routers/research.py``, ``routers/settings.py``,
    …) was decorated against the ORIGINAL limiter object at first import
    and is never re-imported, so the leaked fresh instance permanently
    disagrees with the limiter the app actually enforces. Anything that
    later looks the limiter up through the module — e.g.
    ``test_export_research_logs.py::test_log_export_rate_limit_decorator_is_attached``
    inspecting the registry, or the autouse ``limiter.reset()`` in
    ``tests/conftest.py`` (which would then reset a throwaway object and
    let real counters accumulate) — is broken for the rest of the worker.

    Reloading a SECOND time in teardown does not fix it: that just yields
    yet another unmarked instance. Only restoring the original objects
    does, so the namespace is snapshotted before the test and written back
    verbatim afterwards.

    Subprocess isolation (``tests/test_settings_service_no_import_cycle.py``)
    is the other way to avoid the leak; it is not used here because these
    are cheap in-process assertions over many module globals and each
    probe would pay a fresh interpreter + package import.
    """
    from local_deep_research.web.dependencies import rate_limit

    snapshot = dict(rate_limit.__dict__)
    try:
        yield rate_limit
    finally:
        # Wholesale replace, not update: a reload that raised part-way
        # leaves a half-rewritten namespace (see the ConfigurationError
        # case in test_rate_limit_startup_validation_gap.py).
        rate_limit.__dict__.clear()
        rate_limit.__dict__.update(snapshot)


@pytest.fixture(autouse=True)
def _rate_limit_state_guard():
    """Prove every test leaves the rate_limit module exactly as it found it.

    Declared FIRST so it is set up before — and therefore torn down after
    — ``_restore_rate_limit_module`` below, making this a real check of
    that fixture rather than a tautology. Same idiom as the limiter state
    guard in ``tests/web/routes/test_research_log_export_rate_limit.py``.
    """
    from local_deep_research.web.dependencies import rate_limit

    original = dict(rate_limit.__dict__)
    yield
    assert rate_limit.__dict__ == original, (
        "rate_limit module globals leaked out of this test — a reload was "
        "not restored; later tests would see a fresh, unmarked Limiter."
    )


@pytest.fixture(autouse=True)
def _restore_rate_limit_module():
    """Undo the reload(s) a test performs, restoring the ORIGINAL objects.

    Replaces an earlier teardown that simply reloaded the module again:
    that left behind yet another fresh Limiter instead of the one the
    routers are decorated against. See ``_rate_limit_module_restored``.
    """
    with _rate_limit_module_restored():
        yield


@pytest.fixture(autouse=True)
def _clear_disable_flags(monkeypatch):
    """The limiter now resolves its enabled state through the canonical
    LDR_DISABLE_RATE_LIMITING (falling back to the legacy unprefixed
    name). CI sets LDR_DISABLE_RATE_LIMITING=true container-wide, which
    would otherwise disable the limiter and break the "enabled" assertions
    here. Clear BOTH spellings so each test controls the flag explicitly.
    """
    monkeypatch.delenv("LDR_DISABLE_RATE_LIMITING", raising=False)
    monkeypatch.delenv("DISABLE_RATE_LIMITING", raising=False)


def test_no_storage_uri_keeps_inmemory_default(monkeypatch):
    monkeypatch.delenv("RATE_LIMIT_STORAGE_URI", raising=False)
    mod = _reload_rate_limit()
    # The Limiter is constructed; we just verify it's wired without
    # storage_uri. slowapi keeps the storage instance on `_storage`
    # (memory variant when nothing else configured).
    assert mod.limiter is not None
    storage = getattr(mod.limiter, "_storage", None)
    if storage is not None:
        # MemoryStorage class name varies between limits versions, but
        # always contains "memory" in its module path.
        assert "memory" in type(storage).__module__.lower(), (
            f"Expected memory storage backend, got {type(storage)!r}"
        )


def test_storage_uri_threaded_through_to_limiter(monkeypatch):
    """Setting RATE_LIMIT_STORAGE_URI passes it to Limiter."""
    monkeypatch.setenv("RATE_LIMIT_STORAGE_URI", "memory://")
    mod = _reload_rate_limit()
    # ``memory://`` is the explicit in-memory URI accepted by the
    # `limits` library — proves the URI was passed through without
    # requiring an actual Redis/Memcached server in the test env.
    assert mod._RATE_LIMIT_STORAGE_URI == "memory://"


def test_warning_logged_when_no_storage_and_enabled(
    monkeypatch, loguru_caplog_full
):
    monkeypatch.delenv("RATE_LIMIT_STORAGE_URI", raising=False)
    # Bare ``caplog`` captures stdlib logging only; this module logs via
    # direct loguru calls, which do NOT propagate to it. Use
    # ``loguru_caplog_full`` (tests/conftest.py), which bridges loguru into
    # caplog via a PropagateHandler, so the warning text is actually
    # observable — a bare-``caplog`` assertion here would false-pass no
    # matter what (or whether) anything was logged.
    with loguru_caplog_full.at_level("WARNING"):
        mod = _reload_rate_limit()

    assert mod._RATE_LIMIT_STORAGE_URI == ""
    assert mod._RATE_LIMITING_ENABLED is True
    assert "Rate-limit storage is in-memory" in loguru_caplog_full.text, (
        "expected the in-memory storage warning to be logged when "
        "RATE_LIMIT_STORAGE_URI is unset and rate limiting is enabled"
    )
    assert "RATE_LIMIT_STORAGE_URI" in loguru_caplog_full.text, (
        "the warning must name the env var that fixes it"
    )


def test_reload_isolation_returns_the_limiter_the_routers_use():
    """Fence for the cross-file pollution these reload tests used to cause.

    A reload MUST swap the module's ``limiter`` (otherwise these tests
    would not be exercising re-resolution at all), and the restore MUST
    hand back the very object the routers were decorated against — same
    identity, and with its route registry intact, so registry-inspecting
    tests elsewhere (e.g.
    ``test_export_research_logs.py::test_log_export_rate_limit_decorator_is_attached``)
    still pass when they land after this file in an xdist worker.
    """
    from local_deep_research.web.dependencies import rate_limit
    from local_deep_research.web.routers import research

    original = rate_limit.limiter
    assert research.limiter is original

    with _rate_limit_module_restored():
        _reload_rate_limit()
        assert rate_limit.limiter is not original
        assert rate_limit.limiter._Limiter__marked_for_limiting == {}

    assert rate_limit.limiter is original
    assert rate_limit.limiter is research.limiter
    assert (
        f"{research.__name__}.export_research_logs"
        in rate_limit.limiter._Limiter__marked_for_limiting
    )