Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/increase custom throttling rates to 50 per day #60

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion account/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MailRateThrottle(AnonRateThrottle):
The IP address of the incoming request is used to generate a unique key to throttle against.
"""

rate = "10/day"
rate = "50/day"


class ProfileViewSet(UpdateModelMixin, viewsets.GenericViewSet):
Expand Down
17 changes: 17 additions & 0 deletions account/migrations/0016_order_users_by_date_joined.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.1.13 on 2024-04-30 11:01

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("account", "0015_email_and_mailing_list_jointly_unique"),
]

operations = [
migrations.AlterModelOptions(
name="user",
options={"ordering": ["-date_joined"]},
),
]
1 change: 1 addition & 0 deletions account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def save(self, *args, **kwargs):

class Meta:
db_table = "auth_user"
ordering = ["-date_joined"]


class Profile(models.Model):
Expand Down
18 changes: 16 additions & 2 deletions account/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient

from account.api.views import ProfileViewSet
from account.models import MailingList, MailingListEmail, Profile, User
from profiles.models import Result

Expand Down Expand Up @@ -29,7 +30,20 @@ def api_client_with_custom_ip_address(ip_address):
def users(results):
[
User.objects.create(username=f"test{i}", result=results[i % results.count()])
for i in range(20)
for i in range(4)
]
return User.objects.all()


@pytest.fixture
def throttling_users(results):
num_users = (
int(ProfileViewSet.unsubscribe.kwargs["throttle_classes"][0].rate.split("/")[0])
+ 2
)
[
User.objects.create(username=f"t_test{i}", result=results[i % results.count()])
for i in range(num_users)
]
return User.objects.all()

Expand Down Expand Up @@ -60,6 +74,6 @@ def mailing_list_emails(mailing_lists):
MailingListEmail.objects.create(
email=f"test_{c}@test.com", mailing_list=mailing_lists.first()
)
for c in range(20)
for c in range(52)
]
return MailingListEmail.objects.all()
51 changes: 10 additions & 41 deletions account/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_unauthenticated_cannot_do_anything(api_client, users):
@pytest.mark.parametrize(
"ip_address",
[
("199.168.1.40"),
("1.1.1.1"),
],
)
def test_mailing_list_unsubscribe_throttling(
Expand All @@ -59,7 +59,7 @@ def test_mailing_list_unsubscribe_throttling(
response = api_client_with_custom_ip_address.post(
url, {"email": f"test_{count}@test.com"}
)
assert response.status_code == 200
assert response.status_code == 200, response.content
count += 1
time.sleep(2)
response = api_client_with_custom_ip_address.post(
Expand Down Expand Up @@ -245,11 +245,11 @@ def test_mailing_user_has_subscribed(api_client, users, results, mailing_lists):
@pytest.mark.parametrize(
"ip_address",
[
("92.68.21.220"),
("1.1.1.2"),
],
)
def test_mailing_list_subscribe_throttling(
api_client_with_custom_ip_address, mailing_list_emails, users
api_client_with_custom_ip_address, throttling_users
):
num_requests = int(
ProfileViewSet.subscribe.kwargs["throttle_classes"][0].rate.split("/")[0]
Expand All @@ -261,15 +261,15 @@ def test_mailing_list_subscribe_throttling(
url,
{
"email": f"throttlling_test_{count}@test.com",
"user": users[count].id,
"user": throttling_users[count].id,
},
)
assert response.status_code == 201
assert response.status_code == 201, response.content
count += 1

time.sleep(2)
response = api_client_with_custom_ip_address.post(
url, {"email": f"test_{count}@test.com", "user": users[count].id}
url, {"email": f"test_{count}@test.com", "user": throttling_users[count].id}
)
assert response.status_code == 429

Expand Down Expand Up @@ -322,7 +322,7 @@ def test_mailing_list_subscribe_with_invalid_post_data(
@pytest.mark.parametrize(
"ip_address",
[
("100.1.1.40"),
("1.1.1.3"),
],
)
def test_mailing_list_unsubscribe(
Expand All @@ -343,7 +343,7 @@ def test_mailing_list_unsubscribe(
@pytest.mark.parametrize(
"ip_address",
[
("101.1.1.40"),
("1.1.1.4"),
],
)
def test_mailing_list_unsubscribe_non_existing_email(
Expand All @@ -365,7 +365,7 @@ def test_mailing_list_unsubscribe_non_existing_email(
@pytest.mark.parametrize(
"ip_address",
[
("12.6.121.22"),
("1.1.1.5"),
],
)
def test_mailing_list_unsubscribe_email_not_provided(
Expand All @@ -374,34 +374,3 @@ def test_mailing_list_unsubscribe_email_not_provided(
url = reverse("account:profiles-unsubscribe")
response = api_client_with_custom_ip_address.post(url)
assert response.status_code == 400


@pytest.mark.django_db
@pytest.mark.parametrize(
"ip_address",
[
("100.19.91.40"),
],
)
def test_mailing_list_unique_constraints(api_client_with_custom_ip_address, users):
url = reverse("account:profiles-subscribe")
user = users.get(username="test1")
response = api_client_with_custom_ip_address.post(
url, {"email": "[email protected]", "user": user.id}
)
assert response.status_code == 201
assert MailingListEmail.objects.count() == 1
# Subscribed as the result (MailingList) is different than for user 'test1'
user = users.get(username="test2")
response = api_client_with_custom_ip_address.post(
url, {"email": "[email protected]", "user": user.id}
)
assert response.status_code == 201
assert MailingListEmail.objects.count() == 2
# Fails, as the email and result are not jointly unique.
user = users.get(username="test3")
response = api_client_with_custom_ip_address.post(
url, {"email": "[email protected]", "user": user.id}
)
assert response.status_code == 400
assert MailingListEmail.objects.count() == 2
2 changes: 1 addition & 1 deletion profiles/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class StartPollRateThrottle(AnonRateThrottle):
The IP address of the incoming request is used to generate a unique key to throttle against.
"""

rate = "10/day"
rate = "50/day"


class CustomValidationError(ValidationError):
Expand Down
Loading