Pulled the per-IP sliding-window check out of auth_routes so the upcoming /profile/password endpoint can share it. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
31 lines
1 KiB
Python
31 lines
1 KiB
Python
import time
|
|
|
|
from l4d2web.services.rate_limit import check_rate_limit
|
|
|
|
|
|
def test_under_threshold_allows():
|
|
bucket: dict[str, list[float]] = {}
|
|
for _ in range(3):
|
|
assert check_rate_limit(bucket, "1.2.3.4", window=60.0, max_attempts=5) is False
|
|
|
|
|
|
def test_at_threshold_blocks():
|
|
bucket: dict[str, list[float]] = {}
|
|
for _ in range(5):
|
|
assert check_rate_limit(bucket, "1.2.3.4", window=60.0, max_attempts=5) is False
|
|
assert check_rate_limit(bucket, "1.2.3.4", window=60.0, max_attempts=5) is True
|
|
|
|
|
|
def test_other_ips_independent():
|
|
bucket: dict[str, list[float]] = {}
|
|
for _ in range(5):
|
|
check_rate_limit(bucket, "1.2.3.4", window=60.0, max_attempts=5)
|
|
assert check_rate_limit(bucket, "5.6.7.8", window=60.0, max_attempts=5) is False
|
|
|
|
|
|
def test_old_attempts_expire():
|
|
bucket: dict[str, list[float]] = {}
|
|
for _ in range(5):
|
|
check_rate_limit(bucket, "1.2.3.4", window=0.05, max_attempts=5)
|
|
time.sleep(0.1)
|
|
assert check_rate_limit(bucket, "1.2.3.4", window=0.05, max_attempts=5) is False
|