def test_allow_request_throttling_disabled(api_request, settings): settings.THROTTLING_RATES = {"test": {"rate": "1/s"}} settings.THROTTLING_ENABLED = False ip = "92.92.92.92" request = api_request.get("/", HTTP_X_FORWARDED_FOR=ip) action = "test" throttling.check_request(request, action) # even exceeding request doesn't raise any exception throttling.check_request(request, action)
def login(request): throttling.check_request(request, "login") if request.method != "POST": return http.HttpResponse(status=405) serializer = serializers.LoginSerializer(data=request.POST, context={"request": request}) if not serializer.is_valid(): return http.HttpResponse(json.dumps(serializer.errors), status=400, content_type="application/json") serializer.save(request) csrf.rotate_token(request) token = csrf.get_token(request) response = http.HttpResponse(status=200) response.set_cookie("csrftoken", token, max_age=None) return response
def test_allow_request(api_request, settings, mocker): settings.THROTTLING_RATES = {"test": {"rate": "2/s"}} ip = "92.92.92.92" request = api_request.get("/", HTTP_X_FORWARDED_FOR=ip) allow_request = mocker.spy(throttling.FunkwhaleThrottle, "allow_request") action = "test" throttling_scopes = { "test": { "anonymous": "test", "authenticated": "test" } } throttling.check_request(request, action) throttling.check_request(request, action) with pytest.raises(throttling.TooManyRequests): throttling.check_request(request, action) assert allow_request.call_count == 3 assert allow_request.call_args[0][1] == request assert allow_request.call_args[0][2] == throttling.DummyView( action=action, throttling_scopes=throttling_scopes)
def post(self, request, *args, **kwargs): throttling.check_request(request, "oauth-revoke-token") return super().post(request, *args, **kwargs)
def post(self, request, *args, **kwargs): throttling.check_request(request, "oauth-authorize") return super().post(request, *args, **kwargs)