| 1 |
import pytest |
| 2 |
from django.core.cache import cache |
| 3 |
from django.core.exceptions import ImproperlyConfigured |
| 4 |
|
| 5 |
from ninja import NinjaAPI, Router |
| 6 |
from ninja.testing import TestAsyncClient, TestClient |
| 7 |
from ninja.throttling import ( |
| 8 |
AnonRateThrottle, |
| 9 |
AuthRateThrottle, |
| 10 |
BaseThrottle, |
| 11 |
SimpleRateThrottle, |
| 12 |
UserRateThrottle, |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
@pytest.fixture(autouse=True) |
| 17 |
def clear_cache_for_every_case(): |
| 18 |
cache.clear() |
| 19 |
|
| 20 |
|
| 21 |
def test_global_throttling(): |
| 22 |
th = AnonRateThrottle("1/s") |
| 23 |
set_throttle_timer(th, 0) |
| 24 |
|
| 25 |
api = NinjaAPI(throttle=[th]) |
| 26 |
|
| 27 |
@api.get("/check") |
| 28 |
def check(request): |
| 29 |
return "OK" |
| 30 |
|
| 31 |
client = TestClient(api) |
| 32 |
|
| 33 |
resp = client.get("/check") |
| 34 |
assert resp.status_code == 200 |
| 35 |
assert resp.content == b'"OK"' |
| 36 |
|
| 37 |
resp = client.get("/check") |
| 38 |
assert resp.status_code == 429 |
| 39 |
assert resp.json() == {"detail": "Too many requests."} |
| 40 |
|
| 41 |
set_throttle_timer(th, 2) |
| 42 |
resp = client.get("/check") |
| 43 |
assert resp.status_code == 200 |
| 44 |
assert resp.content == b'"OK"' |
| 45 |
|
| 46 |
|
| 47 |
def test_router_throttling(): |
| 48 |
th = AnonRateThrottle("1/s") |
| 49 |
set_throttle_timer(th, 0) |
| 50 |
|
| 51 |
api = NinjaAPI() |
| 52 |
router = Router() |
| 53 |
|
| 54 |
@router.get("/check") |
| 55 |
def check(request): |
| 56 |
return "OK" |
| 57 |
|
| 58 |
api.add_router("/router", router, throttle=th) |
| 59 |
|
| 60 |
client = TestClient(api) |
| 61 |
|
| 62 |
resp = client.get("/router/check") |
| 63 |
assert resp.status_code == 200 |
| 64 |
assert resp.content == b'"OK"' |
| 65 |
|
| 66 |
resp = client.get("/router/check") |
| 67 |
assert resp.status_code == 429 |
| 68 |
assert resp.json() == {"detail": "Too many requests."} |
| 69 |
|
| 70 |
|
| 71 |
def test_router2_throttling(): |
| 72 |
"Here we test that child router inherits the throttling from api instance" |
| 73 |
th = AnonRateThrottle("1/s") |
| 74 |
set_throttle_timer(th, 0) |
| 75 |
|
| 76 |
api = NinjaAPI(throttle=th) |
| 77 |
router = Router() |
| 78 |
|
| 79 |
@router.get("/check") |
| 80 |
def check(request): |
| 81 |
return "OK" |
| 82 |
|
| 83 |
api.add_router("/router", router) |
| 84 |
|
| 85 |
client = TestClient(api) |
| 86 |
|
| 87 |
resp = client.get("/router/check") |
| 88 |
assert resp.status_code == 200 |
| 89 |
assert resp.content == b'"OK"' |
| 90 |
|
| 91 |
resp = client.get("/router/check") |
| 92 |
assert resp.status_code == 429 |
| 93 |
assert resp.json() == {"detail": "Too many requests."} |
| 94 |
|
| 95 |
|
| 96 |
def test_operation_throttling(): |
| 97 |
th = AnonRateThrottle("1/s") |
| 98 |
set_throttle_timer(th, 0) |
| 99 |
|
| 100 |
api = NinjaAPI() |
| 101 |
|
| 102 |
@api.get("/check1", throttle=th) |
| 103 |
def check(request): |
| 104 |
return "OK" |
| 105 |
|
| 106 |
client = TestClient(api) |
| 107 |
|
| 108 |
resp = client.get("/check1") |
| 109 |
assert resp.status_code == 200 |
| 110 |
assert resp.content == b'"OK"' |
| 111 |
|
| 112 |
resp = client.get("/check1") |
| 113 |
assert resp.status_code == 429 |
| 114 |
assert resp.json() == {"detail": "Too many requests."} |
| 115 |
|
| 116 |
|
| 117 |
@pytest.mark.asyncio |
| 118 |
async def test_async_throttling(): |
| 119 |
th = AnonRateThrottle("1/s") |
| 120 |
set_throttle_timer(th, 0) |
| 121 |
|
| 122 |
api = NinjaAPI(throttle=th) |
| 123 |
|
| 124 |
@api.get("/check-async") |
| 125 |
async def check(request): |
| 126 |
return "OK" |
| 127 |
|
| 128 |
client = TestAsyncClient(api) |
| 129 |
|
| 130 |
resp = await client.get("/check-async") |
| 131 |
assert resp.status_code == 200 |
| 132 |
|
| 133 |
resp = await client.get("/check-async") |
| 134 |
assert resp.status_code == 429 |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
_client = TestClient(NinjaAPI()) |
| 140 |
|
| 141 |
|
| 142 |
def build_request(addr="8.8.8.8", x_forwarded_for=None): |
| 143 |
"Creates a mock request with the given address and optional X-Forwarded-For header." |
| 144 |
meta = {"REMOTE_ADDR": addr} |
| 145 |
if x_forwarded_for: |
| 146 |
meta["HTTP_X_FORWARDED_FOR"] = x_forwarded_for |
| 147 |
return _client._build_request("GET", "/", {}, {"META": meta}) |
| 148 |
|
| 149 |
|
| 150 |
def test_throttle_anon(): |
| 151 |
th = AnonRateThrottle("1/s") |
| 152 |
set_throttle_timer(th, 0) |
| 153 |
|
| 154 |
request = build_request() |
| 155 |
request.auth = None |
| 156 |
|
| 157 |
assert th.allow_request(request) is True |
| 158 |
assert th.wait() == 1.0 |
| 159 |
assert th.get_cache_key(request) == "throttle_anon_8.8.8.8" |
| 160 |
|
| 161 |
|
| 162 |
assert th.allow_request(request) is False |
| 163 |
|
| 164 |
|
| 165 |
request.auth = "some" |
| 166 |
assert th.allow_request(request) is True |
| 167 |
assert th.allow_request(request) is True |
| 168 |
assert th.allow_request(request) is True |
| 169 |
assert th.get_cache_key(request) is None |
| 170 |
|
| 171 |
|
| 172 |
def test_throttle_auth(): |
| 173 |
th = AuthRateThrottle("1/s") |
| 174 |
set_throttle_timer(th, 0) |
| 175 |
|
| 176 |
request = build_request() |
| 177 |
request.auth = None |
| 178 |
|
| 179 |
assert th.allow_request(request) is True |
| 180 |
assert th.allow_request(request) is False |
| 181 |
|
| 182 |
request.auth = "some" |
| 183 |
assert th.allow_request(request) is True |
| 184 |
assert th.allow_request(request) is False |
| 185 |
|
| 186 |
set_throttle_timer(th, 2) |
| 187 |
assert th.allow_request(request) is True |
| 188 |
|
| 189 |
assert ( |
| 190 |
th.get_cache_key(request) |
| 191 |
== "throttle_auth_a6b46dd0d1ae5e86cbc8f37e75ceeb6760230c1ca4ffbcb0c97b96dd7d9c464b" |
| 192 |
) |
| 193 |
|
| 194 |
|
| 195 |
def test_throttle_user(): |
| 196 |
th = UserRateThrottle("1/s") |
| 197 |
set_throttle_timer(th, 0) |
| 198 |
|
| 199 |
request = build_request() |
| 200 |
request.user.is_authenticated = True |
| 201 |
request.user.pk = 123 |
| 202 |
|
| 203 |
assert th.allow_request(request) is True |
| 204 |
assert th.allow_request(request) is False |
| 205 |
|
| 206 |
set_throttle_timer(th, 2) |
| 207 |
assert th.allow_request(request) is True |
| 208 |
|
| 209 |
assert th.get_cache_key(request) == "throttle_user_123" |
| 210 |
|
| 211 |
|
| 212 |
request.user.is_authenticated = False |
| 213 |
assert th.allow_request(request) is True |
| 214 |
assert th.allow_request(request) is False |
| 215 |
assert ( |
| 216 |
th.get_cache_key(request) == "throttle_user_8.8.8.8" |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
def test_wait(): |
| 221 |
th = AuthRateThrottle("5/m") |
| 222 |
set_throttle_timer(th, 0) |
| 223 |
|
| 224 |
request = build_request() |
| 225 |
request.auth = None |
| 226 |
|
| 227 |
for _i in range(5): |
| 228 |
assert th.allow_request(request) is True |
| 229 |
|
| 230 |
assert th.allow_request(request) is False |
| 231 |
assert th.wait() == 60 |
| 232 |
|
| 233 |
set_throttle_timer(th, 30) |
| 234 |
assert th.allow_request(request) is False |
| 235 |
assert th.wait() == 30 |
| 236 |
|
| 237 |
|
| 238 |
th.history = [] |
| 239 |
|
| 240 |
set_throttle_timer(th, 0) |
| 241 |
assert th.wait() == 10 |
| 242 |
|
| 243 |
|
| 244 |
th.history = [0] * 10 |
| 245 |
th.now = 0 |
| 246 |
assert th.wait() is None |
| 247 |
|
| 248 |
|
| 249 |
def test_rate_parser(): |
| 250 |
th = SimpleRateThrottle("1/s") |
| 251 |
assert th.parse_rate(None) == (None, None) |
| 252 |
assert th.parse_rate("1/s") == (1, 1) |
| 253 |
assert th.parse_rate("1/sec") == (1, 1) |
| 254 |
assert th.parse_rate("100/10s") == (100, 10) |
| 255 |
assert th.parse_rate("100/10sec") == (100, 10) |
| 256 |
assert th.parse_rate("100/10") == (100, 10) |
| 257 |
assert th.parse_rate("5/m") == (5, 60) |
| 258 |
assert th.parse_rate("5/min") == (5, 60) |
| 259 |
assert th.parse_rate("500/10m") == (500, 600) |
| 260 |
assert th.parse_rate("500/10min") == (500, 600) |
| 261 |
assert th.parse_rate("10/h") == (10, 3600) |
| 262 |
assert th.parse_rate("10/hour") == (10, 3600) |
| 263 |
assert th.parse_rate("1000/2h") == (1000, 7200) |
| 264 |
assert th.parse_rate("1000/2hour") == (1000, 7200) |
| 265 |
assert th.parse_rate("100/d") == (100, 86400) |
| 266 |
assert th.parse_rate("100/day") == (100, 86400) |
| 267 |
assert th.parse_rate("10_000/7d") == (10000, 86400 * 7) |
| 268 |
assert th.parse_rate("10_000/7day") == (10000, 86400 * 7) |
| 269 |
|
| 270 |
with pytest.raises(ValueError): |
| 271 |
th.parse_rate("42") |
| 272 |
|
| 273 |
|
| 274 |
def test_proxy_throttle(): |
| 275 |
from ninja.conf import settings |
| 276 |
|
| 277 |
settings.NUM_PROXIES = 0 |
| 278 |
|
| 279 |
th = SimpleRateThrottle("1/s") |
| 280 |
request = build_request(x_forwarded_for=None) |
| 281 |
assert th.get_ident(request) == "8.8.8.8" |
| 282 |
|
| 283 |
settings.NUM_PROXIES = 0 |
| 284 |
request = build_request(x_forwarded_for="8.8.8.8,127.0.0.1") |
| 285 |
assert th.get_ident(request) == "8.8.8.8" |
| 286 |
|
| 287 |
settings.NUM_PROXIES = 1 |
| 288 |
assert th.get_ident(request) == "127.0.0.1" |
| 289 |
|
| 290 |
settings.NUM_PROXIES = None |
| 291 |
|
| 292 |
|
| 293 |
def test_base_classes(): |
| 294 |
base = BaseThrottle() |
| 295 |
with pytest.raises(NotImplementedError): |
| 296 |
base.allow_request(build_request()) |
| 297 |
assert base.wait() is None |
| 298 |
|
| 299 |
sample = SimpleRateThrottle("1/s") |
| 300 |
with pytest.raises(NotImplementedError): |
| 301 |
sample.allow_request(build_request()) |
| 302 |
|
| 303 |
throttle = AnonRateThrottle() |
| 304 |
with pytest.raises(ImproperlyConfigured): |
| 305 |
throttle.scope = None |
| 306 |
throttle.get_rate() |
| 307 |
|
| 308 |
sample_scope2 = SimpleRateThrottle("1/s") |
| 309 |
sample_scope2.scope = "scope2" |
| 310 |
with pytest.raises(ImproperlyConfigured): |
| 311 |
sample_scope2.get_rate() |
| 312 |
|
| 313 |
|
| 314 |
def set_throttle_timer(throttle: BaseThrottle, value: int): |
| 315 |
""" |
| 316 |
Explicitly set the timer, overriding time.time() |
| 317 |
""" |
| 318 |
throttle.timer = lambda: value |