27 lines
1.1 KiB
Python
27 lines
1.1 KiB
Python
"""Petits outils du site : limiteur de requêtes, événements en direct, formats."""
|
||
|
||
from __future__ import annotations
|
||
|
||
from shortstats.web import live
|
||
from shortstats.web.security import RateLimit
|
||
from shortstats.web.templating import fmt_age, fmt_num
|
||
|
||
|
||
def test_rate_limit():
|
||
rl = RateLimit(3, 60)
|
||
assert [rl.allow("ip", now=t) for t in (0, 1, 2, 3)] == [True, True, True, False]
|
||
assert rl.allow("autre", now=3) is True
|
||
assert rl.allow("ip", now=61) is True # fenêtre glissante
|
||
|
||
|
||
def test_parse_event_rejects_unknown():
|
||
assert live.parse_event('{"type": "run"}') == {"type": "run"}
|
||
assert live.parse_event('{"type": "charts", "user": "alice"}')["user"] == "alice"
|
||
for bad in ("x", "[]", '{"type": "autre"}', '{"type": "charts", "user": 3}'):
|
||
assert live.parse_event(bad) is None
|
||
|
||
|
||
def test_formatting():
|
||
assert fmt_num(1234567) == "1 234 567" and fmt_num(None) == "" and fmt_num(2.5) == "2,50"
|
||
assert [fmt_age(h) for h in (0.2, 5, 47, 50, 400, 1000, 3000, 9000, 20000)] == \
|
||
["12 min", "5 h", "47 h", "2 j", "2 sem.", "5 sem.", "4 mois", "1 an", "2 ans"]
|