Count successful forward_attempts in the last hour and stop sync/retry when MAX_FORWARDS_PER_HOUR is reached to protect domain sending limits.
37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
"""Rate limit tests."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from forwarder.services.rate_limit import ForwardRateLimitError, check_forward_allowed
|
|
|
|
|
|
def test_check_forward_allowed_when_under_limit():
|
|
db = MagicMock()
|
|
with patch("forwarder.services.rate_limit.get_settings") as settings:
|
|
settings.return_value.max_forwards_per_hour = 200
|
|
with patch("forwarder.services.rate_limit.count_forwards_last_hour", return_value=199):
|
|
check_forward_allowed(db)
|
|
|
|
|
|
def test_check_forward_allowed_raises_at_limit():
|
|
db = MagicMock()
|
|
with patch("forwarder.services.rate_limit.get_settings") as settings:
|
|
settings.return_value.max_forwards_per_hour = 200
|
|
with patch("forwarder.services.rate_limit.count_forwards_last_hour", return_value=200):
|
|
with pytest.raises(ForwardRateLimitError) as exc:
|
|
check_forward_allowed(db)
|
|
assert exc.value.limit == 200
|
|
assert exc.value.used == 200
|
|
|
|
|
|
def test_unlimited_when_config_zero():
|
|
db = MagicMock()
|
|
with patch("forwarder.services.rate_limit.get_settings") as settings:
|
|
settings.return_value.max_forwards_per_hour = 0
|
|
with patch("forwarder.services.rate_limit.count_forwards_last_hour", return_value=9999):
|
|
check_forward_allowed(db)
|