-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): user-specific daily rate limit
- Loading branch information
1 parent
d6c4833
commit b4f922d
Showing
6 changed files
with
126 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 5.0.6 on 2024-06-17 21:19 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("desecapi", "0037_remove_tokendomainpolicy_perm_dyndns"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="user", | ||
name="throttle_daily_rate", | ||
field=models.PositiveIntegerField(null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from unittest import mock | ||
import time | ||
|
||
from django.core.cache import cache | ||
from django.test import TestCase, override_settings | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
from rest_framework.test import APIRequestFactory, force_authenticate | ||
|
||
from desecapi.models import User | ||
|
||
|
||
class MockView(APIView): | ||
@property | ||
def throttle_classes(self): | ||
# Need to import here so that the module is only loaded once the settings override is in effect | ||
from desecapi.throttling import UserRateThrottle | ||
|
||
return (UserRateThrottle,) | ||
|
||
def get(self, request): | ||
return Response("foo") | ||
|
||
|
||
class ThrottlingTestCase(TestCase): | ||
""" | ||
Based on DRF's test_throttling.py. | ||
""" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.factory = APIRequestFactory() | ||
|
||
def _test_requests_are_throttled(self, counts, user=None): | ||
cache.clear() | ||
request = self.factory.get("/") | ||
if user is not None: | ||
force_authenticate(request, user=user) | ||
with override_settings( | ||
REST_FRAMEWORK={"DEFAULT_THROTTLE_RATES": {"user": "10/d"}} | ||
): | ||
view = MockView.as_view() | ||
sum_delay = 0 | ||
for delay, count, max_wait in counts: | ||
sum_delay += delay | ||
with mock.patch( | ||
"desecapi.throttling.UserRateThrottle.timer", | ||
return_value=time.time() + sum_delay, | ||
): | ||
for _ in range(count): | ||
response = view(request) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
response = view(request) | ||
self.assertEqual( | ||
response.status_code, status.HTTP_429_TOO_MANY_REQUESTS | ||
) | ||
self.assertTrue( | ||
max_wait - 1 <= float(response["Retry-After"]) <= max_wait | ||
) | ||
|
||
def test_requests_are_throttled_unauthenticated(self): | ||
self._test_requests_are_throttled([(0, 10, 86400)]) | ||
|
||
def test_requests_are_throttled_user(self): | ||
for email, throttle_daily_rate in [ | ||
("[email protected]", None), | ||
("[email protected]", 3), | ||
("[email protected]", 30), | ||
]: | ||
user = User.objects.create_user( | ||
email=email, password="", throttle_daily_rate=throttle_daily_rate | ||
) | ||
self._test_requests_are_throttled( | ||
[(0, throttle_daily_rate or 10, 86400)], user=user | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters