"""BUG-2274: the private API's limiter must not treat the whole site as one user.""" import importlib import pytest @pytest.fixture() def api(monkeypatch): monkeypatch.delenv("JYOTISH_API_RATE_LIMIT_PER_MINUTE", raising=False) module = importlib.import_module("scripts.jyotish_api_server") module._RATE_LIMIT_BUCKETS.clear() yield module module._RATE_LIMIT_BUCKETS.clear() def test_default_allows_a_busy_minute_from_the_single_web_client(api): # 原值:默认 120 次/分钟,网页容器一个来源 IP 就是全站;新值 3000;原因:生产上线当晚 jyotish_api_429。 assert api._rate_limit_per_minute() == 3000 for second in range(1500): api.enforce_rate_limit("172.19.0.3", now=second / 100) def test_the_limit_is_still_bounded(api): for _ in range(3000): api.enforce_rate_limit("172.19.0.3", now=1.0) with pytest.raises(api.RateLimited): api.enforce_rate_limit("172.19.0.3", now=2.0) api.enforce_rate_limit("172.19.0.3", now=61.5) def test_environment_still_overrides(api, monkeypatch): monkeypatch.setenv("JYOTISH_API_RATE_LIMIT_PER_MINUTE", "5") assert api._rate_limit_per_minute() == 5 monkeypatch.setenv("JYOTISH_API_RATE_LIMIT_PER_MINUTE", "not-a-number") assert api._rate_limit_per_minute() == 3000