~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Config wiring tests for ``web/dependencies/rate_limit.py``.
The module resolves ALL of its configuration at import time:
* ``_RATE_LIMITING_ENABLED`` <- ``is_rate_limiting_enabled()`` (env flags)
* ``_RATE_LIMIT_STORAGE_URI`` <- ``RATE_LIMIT_STORAGE_URI`` env var
* ``DEFAULT_RATE_LIMIT`` (and the named route limits) <- ``load_server_config()``
and threads them into ``Limiter(**_limiter_kwargs)``. Because resolution
happens at import, changing an input requires re-executing the module:
where a test needs different inputs it uses ``importlib.reload`` (the same
pattern as the sibling ``test_rate_limit_storage.py``), with an autouse
fixture that snapshots the module namespace beforehand and writes the
ORIGINAL objects back afterwards, so every test — and whatever runs after
this file — sees the module exactly as it was. Reload cannot be avoided
here without subprocesses (see ``test_rate_limit_disable_env.py`` for that
style); in-process reloads keep these wiring checks fast.
What this file adds over its siblings:
* the ``enabled`` flag is not just resolved but actually PASSED to the
Limiter via ``_limiter_kwargs`` (the sibling storage test asserts only
module globals; the subprocess test only ``limiter.enabled``),
* ``RATE_LIMIT_STORAGE_URI`` reaches the Limiter instance itself,
* ``DEFAULT_RATE_LIMIT`` flows from ``server_config`` (env var end-to-end
and unit-level with a stubbed ``load_server_config``),
* characterization of the Flask-era env names after the rename: the
MODULE ignores ``RATELIMIT_STORAGE_URL``, but slowapi itself still
consults it (and ``RATELIMIT_ENABLED``) via starlette Config inside
``Limiter.__init__`` — see the class docstrings below for the observed
consequences.
"""
import importlib
import importlib.util
from contextlib import contextmanager
import pytest
from limits import parse_many
from local_deep_research.settings.env_registry import is_rate_limiting_enabled
def _rl():
from local_deep_research.web.dependencies import rate_limit
return rate_limit
def _reload_rate_limit():
return importlib.reload(_rl())
@contextmanager
def _rate_limit_module_restored():
"""Snapshot the module namespace and put the ORIGINAL objects back.
``importlib.reload`` rebinds ``limiter`` to a brand-new ``Limiter``
with EMPTY ``_Limiter__marked_for_limiting`` / ``_route_limits``
registries, while every router keeps the object it was decorated
against at first import. Reloading again in teardown (what this file
used to do) only produces yet another unmarked instance, so the
leaked state outlived the file and broke later registry-inspecting
tests in the same worker — see the full explanation and the identity
fence in the sibling ``test_rate_limit_storage.py``.
"""
rate_limit = _rl()
snapshot = dict(rate_limit.__dict__)
try:
yield rate_limit
finally:
# Wholesale replace, not update: a reload that raised part-way
# (see TestLegacyStorageEnvName) leaves a half-rewritten namespace.
rate_limit.__dict__.clear()
rate_limit.__dict__.update(snapshot)
# Defined FIRST so it is set up before (and torn down after) the restore
# fixture below, making it a real check that the restore happened rather
# than a tautology (same idiom as the limiter state guard in
# ``tests/web/routes/test_research_log_export_rate_limit.py``).
@pytest.fixture(autouse=True)
def _rate_limit_state_guard():
rate_limit = _rl()
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_module_state():
with _rate_limit_module_restored():
yield
@pytest.fixture()
def clean_env(monkeypatch):
"""Clear every env input the module reads, so each reload test states
its inputs explicitly (CI sets LDR_DISABLE_RATE_LIMITING=true
container-wide, which would otherwise leak into assertions here)."""
for name in (
"LDR_DISABLE_RATE_LIMITING",
"DISABLE_RATE_LIMITING",
"RATE_LIMIT_STORAGE_URI",
"RATELIMIT_STORAGE_URL",
"RATELIMIT_ENABLED",
"LDR_SECURITY_RATE_LIMIT_DEFAULT",
):
monkeypatch.delenv(name, raising=False)
return monkeypatch
class TestEnabledFlagWiring:
"""is_rate_limiting_enabled() must be honored: limiter.enabled mirrors
it, and the resolved flag is what _limiter_kwargs hands to Limiter."""
def test_kwargs_and_limiter_mirror_resolved_flag(self):
"""No reload: internal consistency of the currently loaded module.
Catches two regressions independent of the ambient env value:
hardcoding ``enabled`` in ``_limiter_kwargs`` instead of using the
resolved flag, and constructing ``Limiter`` without threading the
kwargs through (slowapi would default to enabled=True).
"""
mod = _rl()
assert mod._limiter_kwargs["enabled"] is mod._RATE_LIMITING_ENABLED
assert mod.limiter.enabled == mod._RATE_LIMITING_ENABLED
def test_disable_flag_turns_limiter_off(self, clean_env):
clean_env.setenv("LDR_DISABLE_RATE_LIMITING", "true")
mod = _reload_rate_limit()
# The canonical helper and the limiter must agree under the SAME env.
assert is_rate_limiting_enabled() is False
assert mod._limiter_kwargs["enabled"] is False
assert mod.limiter.enabled is False
def test_default_is_enabled(self, clean_env):
mod = _reload_rate_limit()
assert is_rate_limiting_enabled() is True
assert mod._limiter_kwargs["enabled"] is True
assert mod.limiter.enabled is True
def test_stale_flask_ratelimit_enabled_cannot_reenable(self, clean_env):
"""Regression fence for a bug found while writing these tests:
slowapi's Limiter.__init__ consults the Flask-era RATELIMIT_ENABLED
env var (starlette Config) and OVERRODE our `enabled` kwarg —
assigning the raw string "true", which silently re-enabled rate
limiting despite LDR_DISABLE_RATE_LIMITING=true. rate_limit.py now
re-asserts the resolved flag after construction."""
clean_env.setenv("LDR_DISABLE_RATE_LIMITING", "true")
clean_env.setenv("RATELIMIT_ENABLED", "true")
mod = _reload_rate_limit()
assert mod.limiter.enabled is False
def test_stale_flask_ratelimit_enabled_cannot_disable(self, clean_env):
"""The other direction is the dangerous one in production: a stale
RATELIMIT_ENABLED=false must not silently drop the login/register
brute-force protection when the canonical flag says enabled."""
clean_env.setenv("RATELIMIT_ENABLED", "false")
mod = _reload_rate_limit()
assert is_rate_limiting_enabled() is True
assert mod.limiter.enabled is True
class TestStorageUriWiring:
"""RATE_LIMIT_STORAGE_URI must reach the Limiter INSTANCE, not just a
module global (the sibling test only checks _RATE_LIMIT_STORAGE_URI, so
dropping the `_limiter_kwargs["storage_uri"] = ...` line would still
pass it)."""
def test_storage_uri_reaches_limiter_instance(self, clean_env):
# `memory://` is a real limits-library URI, so this proves the
# pass-through without needing a Redis server.
clean_env.setenv("RATE_LIMIT_STORAGE_URI", "memory://")
mod = _reload_rate_limit()
assert mod._limiter_kwargs["storage_uri"] == "memory://"
assert mod.limiter._storage_uri == "memory://"
def test_unset_uri_omits_kwarg_so_slowapi_default_applies(self, clean_env):
mod = _reload_rate_limit()
assert "storage_uri" not in mod._limiter_kwargs
# slowapi's own default (None -> in-memory) must be what applies.
assert mod.limiter._storage_uri is None
assert "memory" in type(mod.limiter._storage).__module__.lower()
def test_whitespace_only_uri_treated_as_unset(self, clean_env):
"""Compose/YAML files routinely produce padded or blank values; the
module strips them rather than configuring a bogus storage URI."""
clean_env.setenv("RATE_LIMIT_STORAGE_URI", " ")
mod = _reload_rate_limit()
assert mod._RATE_LIMIT_STORAGE_URI == ""
assert "storage_uri" not in mod._limiter_kwargs
class TestDefaultRateLimitFromServerConfig:
"""DEFAULT_RATE_LIMIT must flow from load_server_config() into the
Limiter's default_limits (Flask-Limiter default_limits parity —
enforced globally by SlowAPIMiddleware)."""
def _stub_config(self, monkeypatch, cfg):
from local_deep_research.web import server_config
# rate_limit.py does `from ..server_config import load_server_config`
# at module level, so patching the server_config attribute BEFORE
# the reload makes the re-import bind the stub.
monkeypatch.setattr(
server_config, "load_server_config", lambda: dict(cfg)
)
def test_config_value_becomes_default_limit(self, clean_env):
self._stub_config(clean_env, {"rate_limit_default": "7 per minute"})
mod = _reload_rate_limit()
assert mod.DEFAULT_RATE_LIMIT == "7 per minute"
assert mod._limiter_kwargs["default_limits"] == ["7 per minute"]
# And it must actually govern the Limiter: compare the parsed
# limit items rather than string formatting.
limiter_items = [
limit.limit for limit in mod.limiter._default_limits[0]
]
assert limiter_items == parse_many("7 per minute")
def test_missing_key_falls_back_to_documented_default(self, clean_env):
self._stub_config(clean_env, {})
mod = _reload_rate_limit()
assert mod.DEFAULT_RATE_LIMIT == "5000 per hour;50000 per day"
assert mod._limiter_kwargs["default_limits"] == [
"5000 per hour;50000 per day"
]
def test_env_var_flows_end_to_end(self, clean_env):
"""The REAL load_server_config path: LDR_SECURITY_RATE_LIMIT_DEFAULT
(env > legacy JSON > default) must land in DEFAULT_RATE_LIMIT."""
clean_env.setenv("LDR_SECURITY_RATE_LIMIT_DEFAULT", "9 per hour")
mod = _reload_rate_limit()
assert mod.DEFAULT_RATE_LIMIT == "9 per hour"
assert mod._limiter_kwargs["default_limits"] == ["9 per hour"]
def test_named_route_limits_flow_from_config(self, clean_env):
"""The named limits read from the same config dict must flow too —
they parameterize the auth/settings/upload decorators."""
self._stub_config(
clean_env,
{
"rate_limit_login": "2 per hour",
"rate_limit_registration": "1 per day",
"rate_limit_settings": "4 per minute",
},
)
mod = _reload_rate_limit()
assert mod.LOGIN_RATE_LIMIT == "2 per hour"
assert mod.REGISTRATION_RATE_LIMIT == "1 per day"
assert mod.SETTINGS_RATE_LIMIT == "4 per minute"
# Password-change intentionally follows rate_limit_login when its
# own key is absent (documented in the module).
assert mod.PASSWORD_CHANGE_RATE_LIMIT == "2 per hour"
class TestLegacyStorageEnvName:
"""CHARACTERIZATION of the env-var rename the migration audit flagged.
On main (Flask-Limiter era) the storage backend was configured via
``RATELIMIT_STORAGE_URL`` — honored and validated at startup since
PR #3277 (see docs/release_notes/1.10.0.md, "Rate limiter: a broken
RATELIMIT_STORAGE_URL fails fast"). The FastAPI branch renamed the
operator-facing variable to ``RATE_LIMIT_STORAGE_URI``.
The audit assumed the old name is now silently ignored. Writing these
tests showed that is only HALF true: rate_limit.py never reads it, but
slowapi's ``Limiter.__init__`` still consults ``RATELIMIT_STORAGE_URL``
through starlette Config as a fallback whenever our module passes no
``storage_uri``. Consequences pinned below:
* an upgrading operator's legacy variable still takes effect when the
new one is unset — but if it points at redis:// and the ``redis``
client package is missing (it is not a declared dependency), the app
CRASHES at import with a message naming neither the variable nor the
remedy (a regression vs main's actionable fail-fast from PR #3277);
* the canonical ``RATE_LIMIT_STORAGE_URI`` wins when both are set.
If this is ever made deliberate (honor + validate, or reject loudly),
update this class and the migration guide together.
"""
def test_module_resolution_ignores_legacy_name(self, clean_env):
"""rate_limit.py's own config resolution must not read the legacy
name: the module global stays empty and no storage_uri kwarg is
passed (what happens INSIDE slowapi is pinned separately below)."""
clean_env.setenv("RATELIMIT_STORAGE_URL", "memory://")
mod = _reload_rate_limit()
assert mod._RATE_LIMIT_STORAGE_URI == ""
assert "storage_uri" not in mod._limiter_kwargs
def test_slowapi_still_consults_legacy_name_when_uri_unset(self, clean_env):
"""Proof the legacy variable is NOT dead: slowapi's internal
fallback reads it at Limiter construction. With the redis client
package installed the legacy URI is honored; without it (this venv)
construction fails.
The failure is now the ACTIONABLE one main had: a RuntimeError
naming the variable that supplied the URI and the remedy, with any
embedded credential redacted. Previously the bare
``ConfigurationError`` propagated — a message naming neither the
variable nor the fix, and echoing the raw URI (password included)
into the logs and the exit traceback. See the class docstring: this
is the "honor + validate" resolution it asked a future change to
make deliberate.
"""
clean_env.setenv(
"RATELIMIT_STORAGE_URL", "redis://legacy-host.invalid:6379"
)
if importlib.util.find_spec("redis") is None:
with pytest.raises(RuntimeError) as exc_info:
_reload_rate_limit()
message = str(exc_info.value)
# Names the variable the operator actually set, not ours.
assert "RATELIMIT_STORAGE_URL" in message
assert "legacy-host.invalid" in message
else: # pragma: no cover - depends on optional dependency
mod = _reload_rate_limit()
assert "redis" in type(mod.limiter._storage).__module__.lower()
def test_legacy_name_credentials_are_redacted(self, clean_env):
"""A password in the legacy variable must not reach the message.
This is the regression that motivated the change: the handler
deliberately skipped redaction when the URI came from the legacy
name, on the reasoning that a URI we did not configure had nothing
to redact — leaving the credential exposed for the ONE variable
name an installation upgrading from main will actually have set.
"""
if importlib.util.find_spec("redis") is not None:
pytest.skip("redis client installed — construction would succeed")
clean_env.setenv(
"RATELIMIT_STORAGE_URL",
"redis://admin:[email protected]:6379",
)
with pytest.raises(RuntimeError) as exc_info:
_reload_rate_limit()
message = str(exc_info.value)
assert "hunter2" not in message
assert "***" in message
def test_canonical_name_wins_when_both_set(self, clean_env):
"""Passing storage_uri short-circuits slowapi's legacy-env fallback
— this construction would crash on the redis:// value otherwise
(no redis client installed), so success itself proves precedence."""
clean_env.setenv(
"RATELIMIT_STORAGE_URL", "redis://legacy-host.invalid:6379"
)
clean_env.setenv("RATE_LIMIT_STORAGE_URI", "memory://")
mod = _reload_rate_limit()
assert mod._limiter_kwargs["storage_uri"] == "memory://"
assert mod.limiter._storage_uri == "memory://"