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.

2.4 KB
Raw Blame
# allow: no-sut-import — the flag is evaluated at MODULE IMPORT time, so
# the SUT must be imported in a subprocess with a controlled environment;
# an in-process import here would freeze the flag for the whole test run.
"""The slowapi limiter must honor the CANONICAL disable flag.

Regression fence: after the FastAPI migration, web/dependencies/rate_limit.py
read only the legacy ``DISABLE_RATE_LIMITING`` name, silently ignoring the
canonical ``LDR_DISABLE_RATE_LIMITING=true`` that CI passes to the UI-test
server. Rate limiting therefore stayed ON in CI — the 3/hour register limit
then broke every UI suite that registered more than a couple of users, with
failures that looked unrelated ("Registration failed", stale egress scope,
missing create forms after login redirects).

The flag is evaluated at import time, so each case runs in a subprocess with
a controlled environment.
"""

import os
import subprocess
import sys

import pytest

_SNIPPET = (
    "from local_deep_research.web.dependencies.rate_limit import limiter;"
    "print('enabled=%s' % limiter.enabled)"
)


def _limiter_enabled(env_overrides: dict) -> bool:
    env = {
        k: v
        for k, v in os.environ.items()
        # Start from a clean slate for both spellings.
        if k not in ("DISABLE_RATE_LIMITING", "LDR_DISABLE_RATE_LIMITING")
    }
    env.update(env_overrides)
    out = subprocess.run(
        [sys.executable, "-c", _SNIPPET],
        capture_output=True,
        text=True,
        env=env,
        timeout=120,
        check=True,
    ).stdout
    assert "enabled=" in out, out
    return "enabled=True" in out


@pytest.mark.timeout(300)
def test_canonical_ldr_prefixed_flag_disables_limiter():
    assert _limiter_enabled({"LDR_DISABLE_RATE_LIMITING": "true"}) is False


@pytest.mark.timeout(300)
def test_legacy_unprefixed_flag_still_disables_limiter():
    assert _limiter_enabled({"DISABLE_RATE_LIMITING": "true"}) is False


@pytest.mark.timeout(300)
def test_default_is_enabled():
    assert _limiter_enabled({}) is True


@pytest.mark.timeout(300)
def test_canonical_false_overrides_legacy_true():
    # An explicit canonical "false" must beat a stale legacy "true"
    # (the documented precedence of is_rate_limiting_enabled).
    assert (
        _limiter_enabled(
            {
                "LDR_DISABLE_RATE_LIMITING": "false",
                "DISABLE_RATE_LIMITING": "true",
            }
        )
        is True
    )