test_last_used_throttle.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """When a credential's last-used time is written again (#3360)."""
  2. from datetime import datetime, timedelta, timezone
  3. from backend.app.services.last_used import LastUsedThrottle
  4. NOW = datetime(2026, 10, 24, 12, 0, tzinfo=timezone.utc)
  5. def _throttle():
  6. return LastUsedThrottle(timedelta(minutes=1))
  7. def test_never_used_is_due():
  8. assert _throttle().due("k", None, NOW)
  9. def test_used_within_the_minute_is_not_due():
  10. assert not _throttle().due("k", NOW - timedelta(seconds=30), NOW)
  11. def test_used_over_a_minute_ago_is_due():
  12. assert _throttle().due("k", NOW - timedelta(minutes=2), NOW)
  13. def test_a_naive_stored_time_is_read_as_utc():
  14. assert not _throttle().due("k", (NOW - timedelta(seconds=30)).replace(tzinfo=None), NOW)
  15. def test_a_time_in_the_future_is_due():
  16. """A clock that ran ahead would otherwise freeze the time until it got there."""
  17. assert _throttle().due("k", NOW + timedelta(hours=3), NOW)
  18. def test_an_attempt_counts_when_the_stored_time_did_not_move():
  19. """A failed write leaves the stored time where it was."""
  20. throttle = _throttle()
  21. stale = NOW - timedelta(hours=1)
  22. assert throttle.due("k", stale, NOW)
  23. assert not throttle.due("k", stale, NOW + timedelta(seconds=10))
  24. assert throttle.due("k", stale, NOW + timedelta(minutes=2))
  25. def test_attempts_are_per_credential():
  26. throttle = _throttle()
  27. assert throttle.due("a", None, NOW)
  28. assert throttle.due("b", None, NOW)