paste-bin/tests/test_ratelimit.py
Jarian 985e070757 port: FastAPI rewrite built by the arch-llm spec pipeline
Spec-first port of the Flask app (still on main):
- schema.arch: signed arch-llm spec (single source of truth)
- paste_bin/: generated FastAPI backend, verified 51/51 frozen owner
  tests, 92.5% coverage (build r3, 2026-11-18)
- ui/: SPA + emitted OpenAPI contract (openapi.json + generated
  client.js); the app calls only window.PasteBinAPI
- 7 old bugs fixed (file raw 500, silent expiry, AVI-as-webp,
  stripped content, limiter leak, trusted mimetype, stale tmp)
- 62/62 live probe green; /health + JSON index added; JSON API

Full report on the wiki: 'FastAPI port (arch-llm pipeline)'.
2026-11-18 23:00:23 +00:00

43 lines
1.3 KiB
Python

# tests for module ratelimit (paste_bin/ratelimit.py)
import time
from paste_bin.ratelimit import UploadRateLimiter
def test_allows_up_to_max_then_blocks():
lim = UploadRateLimiter(max_attempts=3, window_seconds=60)
assert lim.allow("1.2.3.4") is True
assert lim.allow("1.2.3.4") is True
assert lim.allow("1.2.3.4") is True
assert lim.allow("1.2.3.4") is False
def test_other_ips_are_independent():
lim = UploadRateLimiter(max_attempts=2, window_seconds=60)
assert lim.allow("a") is True
assert lim.allow("a") is True
assert lim.allow("a") is False
assert lim.allow("b") is True
def test_window_slides():
lim = UploadRateLimiter(max_attempts=2, window_seconds=1)
assert lim.allow("x") is True
assert lim.allow("x") is True
assert lim.allow("x") is False
time.sleep(1.1)
assert lim.allow("x") is True
def test_stale_keys_pruned():
lim = UploadRateLimiter(max_attempts=10, window_seconds=1)
for _ in range(3):
lim.allow("ghost")
time.sleep(1.1)
assert "ghost" in lim.attempts # still present until a call prunes
lim.allow("live")
# old bug: one list per ip leaked forever — stale keys must be dropped
assert "ghost" not in lim.attempts
assert "live" in lim.attempts