Spaces:
Running on T4
Running on T4
Download backend/tests/unit/test_auth_rate_limit.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 1.46 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/unit/test_auth_rate_limit.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/tests/unit/test_auth_rate_limit.py
-
curl -L -o test_auth_rate_limit.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/unit/test_auth_rate_limit.py
1.46 kB
| import pytest | |
| from app.core.errors import RateLimited | |
| from app.core.rate_limit import RateLimiter | |
| class Clock: | |
| def __init__(self): | |
| self.now = 1000.0 | |
| def __call__(self) -> float: | |
| return self.now | |
| def test_blocks_after_limit_and_recovers_after_window(): | |
| clock = Clock() | |
| limiter = RateLimiter(3, window_s=60, clock=clock) | |
| for _ in range(3): | |
| limiter.hit("a") | |
| with pytest.raises(RateLimited): | |
| limiter.hit("a") | |
| clock.now += 30 | |
| with pytest.raises(RateLimited): | |
| limiter.check("a") | |
| clock.now += 30.1 | |
| limiter.hit("a") | |
| def test_window_slides_per_hit(): | |
| clock = Clock() | |
| limiter = RateLimiter(2, window_s=60, clock=clock) | |
| limiter.hit("a") | |
| clock.now += 40 | |
| limiter.hit("a") | |
| clock.now += 21 # the first hit has left the window, the second has not | |
| limiter.hit("a") | |
| with pytest.raises(RateLimited): | |
| limiter.check("a") | |
| def test_any_exhausted_key_blocks_and_keys_are_independent(): | |
| limiter = RateLimiter(1, clock=Clock()) | |
| limiter.hit("email:a", "ip:1") | |
| limiter.check("email:b", "ip:2") | |
| with pytest.raises(RateLimited): | |
| limiter.check("email:b", "ip:1") | |
| def test_check_does_not_record_and_reset_clears(): | |
| limiter = RateLimiter(1, clock=Clock()) | |
| for _ in range(5): | |
| limiter.check("a") | |
| limiter.record("a") | |
| with pytest.raises(RateLimited): | |
| limiter.check("a") | |
| limiter.reset() | |
| limiter.check("a") | |