shortstats/tests/unitaires/test_outils_site.py
ploush 81ef12a9cd
Some checks failed
build / test (push) Failing after 2m48s
build / images (collector) (push) Has been skipped
build / images (web) (push) Has been skipped
upgrade 1.4.0
2026-11-29 20:43:50 +01:00

27 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""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"]