| 1 |
import pytest |
| 2 |
|
| 3 |
from allauth.core.internal import ratelimit |
| 4 |
|
| 5 |
|
| 6 |
def test_rollback_consume(rf, enable_cache): |
| 7 |
def consume(): |
| 8 |
request = rf.post("/") |
| 9 |
config = {"foo": "2/m/ip"} |
| 10 |
return ratelimit.consume(request, config=config, action="foo") |
| 11 |
|
| 12 |
usage1 = consume() |
| 13 |
assert len(usage1.usage) > 0 |
| 14 |
usage2 = consume() |
| 15 |
assert len(usage2.usage) > 0 |
| 16 |
no_usage = consume() |
| 17 |
assert no_usage is None |
| 18 |
usage1.rollback() |
| 19 |
assert consume() |
| 20 |
assert not consume() |
| 21 |
|
| 22 |
|
| 23 |
def test_apply_rate(): |
| 24 |
rate = ratelimit.Rate(amount=2, duration=60, per="ip") |
| 25 |
now = 1000.0 |
| 26 |
|
| 27 |
# First hit against an empty history: allowed and recorded. |
| 28 |
allowed, history = ratelimit.apply_rate([], now, rate) |
| 29 |
assert allowed |
| 30 |
assert history == [now] |
| 31 |
|
| 32 |
# Second hit within the window: allowed, recorded newest-first. |
| 33 |
allowed, history = ratelimit.apply_rate(history, now + 1, rate) |
| 34 |
assert allowed |
| 35 |
assert history == [now + 1, now] |
| 36 |
|
| 37 |
# Third hit within the window: over the limit, nothing recorded. |
| 38 |
allowed, history = ratelimit.apply_rate(history, now + 2, rate) |
| 39 |
assert not allowed |
| 40 |
assert history == [now + 1, now] |
| 41 |
|
| 42 |
# A dry run never records, even when allowed. |
| 43 |
allowed, history = ratelimit.apply_rate([], now, rate, dry_run=True) |
| 44 |
assert allowed |
| 45 |
assert history == [] |
| 46 |
|
| 47 |
# Timestamps older than the duration are dropped, freeing capacity. |
| 48 |
allowed, history = ratelimit.apply_rate([now - 61, now - 62], now, rate) |
| 49 |
assert allowed |
| 50 |
assert history == [now] |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.parametrize( |
| 54 |
"rate,values", |
| 55 |
[ |
| 56 |
("5/m", [(5, 60, "ip")]), |
| 57 |
("5/m/user", [(5, 60, "user")]), |
| 58 |
("2/3.5m/key", [(2, 210, "key")]), |
| 59 |
("3/5m/user,20/0.5m/ip", [(3, 300, "user"), (20, 30, "ip")]), |
| 60 |
("7/2h", [(7, 7200, "ip")]), |
| 61 |
("7/0.25d", [(7, 21600, "ip")]), |
| 62 |
], |
| 63 |
) |
| 64 |
def test_parse(rate, values): |
| 65 |
rates = ratelimit.parse_rates(rate) |
| 66 |
assert len(rates) == len(values) |
| 67 |
for i, rate in enumerate(rates): |
| 68 |
assert rate.amount == values[i][0] |
| 69 |
assert rate.duration == values[i][1] |
| 70 |
assert rate.per == values[i][2] |
| 71 |
|
| 72 |
|
| 73 |
@pytest.mark.parametrize( |
| 74 |
"ip,prefix,expected", |
| 75 |
[ |
| 76 |
("192.168.1.1", 64, "192.168.1.1"), |
| 77 |
("10.0.0.255", 48, "10.0.0.255"), |
| 78 |
("2001:db8:85a3::8a2e:370:7334", 64, "2001:db8:85a3::"), |
| 79 |
("2001:db8:85a3:1234:5678:abcd:ef01:2345", 64, "2001:db8:85a3:1234::"), |
| 80 |
("2001:db8:85a3:1234:5678:abcd:ef01:2345", 48, "2001:db8:85a3::"), |
| 81 |
( |
| 82 |
"2001:db8:85a3:1234:5678:abcd:ef01:2345", |
| 83 |
128, |
| 84 |
"2001:db8:85a3:1234:5678:abcd:ef01:2345", |
| 85 |
), |
| 86 |
("::1", 64, "::"), |
| 87 |
("invalid", 64, "invalid"), |
| 88 |
], |
| 89 |
) |
| 90 |
def test_truncate_ip(ip, prefix, expected, settings): |
| 91 |
settings.ALLAUTH_RATE_LIMIT_IPV6_PREFIX = prefix |
| 92 |
assert ratelimit.truncate_ip(ip) == expected |