-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from City-of-Turku/feature/subscribe-allow-any
Feature/subscribe allow any
- Loading branch information
Showing
6 changed files
with
141 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.1.13 on 2024-04-10 15:59 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("account", "0012_profile_is_interested_in_mobility"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="user", | ||
name="has_subscribed", | ||
field=models.BooleanField(default=False), | ||
), | ||
] |
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 |
---|---|---|
|
@@ -43,7 +43,7 @@ def test_unauthenticated_cannot_do_anything(api_client, users): | |
@pytest.mark.parametrize( | ||
"ip_address", | ||
[ | ||
("192.168.1.40"), | ||
("199.168.1.40"), | ||
], | ||
) | ||
def test_mailing_list_unsubscribe_throttling( | ||
|
@@ -227,46 +227,80 @@ def test_profile_patch_result_can_be_used(api_client_authenticated, users, profi | |
|
||
|
||
@pytest.mark.django_db | ||
def test_mailing_list_unauthenticated_subscribe(api_client, results): | ||
def test_mailing_list_subscribe(api_client, users, results): | ||
url = reverse("account:profiles-subscribe") | ||
response = api_client.post( | ||
url, {"email": "[email protected]", "result": results.first().id} | ||
user = users.get(username="test1") | ||
response = api_client.post(url, {"email": "[email protected]", "user": user.id}) | ||
assert response.status_code == 201 | ||
assert MailingListEmail.objects.count() == 1 | ||
assert MailingListEmail.objects.first().email == "[email protected]" | ||
assert ( | ||
MailingList.objects.filter(result=user.result).first().emails.first() | ||
== MailingListEmail.objects.first() | ||
) | ||
assert response.status_code == 401 | ||
assert MailingList.objects.first().result == user.result | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_mailing_list_subscribe( | ||
api_client_authenticated, users, results, mailing_lists | ||
): | ||
def test_mailing_user_has_subscribed(api_client, users, results, mailing_lists): | ||
url = reverse("account:profiles-subscribe") | ||
response = api_client_authenticated.post( | ||
url, {"email": "[email protected]", "result": results.first().id} | ||
response = api_client.post( | ||
url, {"email": "[email protected]", "user": users.first().id} | ||
) | ||
assert response.status_code == 201 | ||
assert MailingListEmail.objects.count() == 1 | ||
assert MailingListEmail.objects.first().email == "[email protected]" | ||
assert ( | ||
MailingList.objects.first().emails.first() == MailingListEmail.objects.first() | ||
response = api_client.post( | ||
url, {"email": "[email protected]", "user": users.first().id} | ||
) | ||
assert response.status_code == 400 | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.parametrize( | ||
"ip_address", | ||
[ | ||
("92.68.21.220"), | ||
], | ||
) | ||
def test_mailing_list_subscribe_throttling( | ||
api_client_with_custom_ip_address, mailing_list_emails, users | ||
): | ||
num_requests = int( | ||
ProfileViewSet.subscribe.kwargs["throttle_classes"][0].rate.split("/")[0] | ||
) | ||
url = reverse("account:profiles-subscribe") | ||
count = 0 | ||
while count < num_requests: | ||
response = api_client_with_custom_ip_address.post( | ||
url, | ||
{ | ||
"email": f"throttlling_test_{count}@test.com", | ||
"user": users[count].id, | ||
}, | ||
) | ||
assert response.status_code == 201 | ||
count += 1 | ||
|
||
time.sleep(2) | ||
response = api_client_with_custom_ip_address.post( | ||
url, {"email": f"test_{count}@test.com", "user": users[count].id} | ||
) | ||
assert response.status_code == 429 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_mailing_list_is_created_on_subscribe(api_client_authenticated, users, results): | ||
def test_mailing_list_is_created_on_subscribe(api_client, users, results): | ||
assert MailingList.objects.count() == 0 | ||
url = reverse("account:profiles-subscribe") | ||
response = api_client_authenticated.post( | ||
url, {"email": "[email protected]", "result": results.first().id} | ||
response = api_client.post( | ||
url, {"email": "[email protected]", "user": users.first().id} | ||
) | ||
assert response.status_code == 201 | ||
assert MailingList.objects.count() == 1 | ||
assert MailingListEmail.objects.count() == 1 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_mailing_list_subscribe_with_invalid_emails( | ||
api_client_authenticated, users, results | ||
): | ||
def test_mailing_list_subscribe_with_invalid_emails(api_client, users, results): | ||
assert MailingList.objects.count() == 0 | ||
url = reverse("account:profiles-subscribe") | ||
for email in [ | ||
|
@@ -276,9 +310,7 @@ def test_mailing_list_subscribe_with_invalid_emails( | |
"john.doe@example", | ||
"john.doe@example", | ||
]: | ||
response = api_client_authenticated.post( | ||
url, {"email": email, "result": results.first().id} | ||
) | ||
response = api_client.post(url, {"email": email, "user": users.first().id}) | ||
assert response.status_code == 400 | ||
assert MailingList.objects.count() == 0 | ||
assert MailingList.objects.count() == 0 | ||
|
@@ -290,43 +322,68 @@ def test_mailing_list_subscribe_with_invalid_post_data( | |
): | ||
url = reverse("account:profiles-subscribe") | ||
# Missing email | ||
response = api_client_authenticated.post(url, {"result": results.first().id}) | ||
response = api_client_authenticated.post(url, {"user": users.first().id}) | ||
assert response.status_code == 400 | ||
assert MailingList.objects.count() == 0 | ||
assert MailingList.objects.count() == 0 | ||
# Missing result | ||
response = api_client_authenticated.post(url, {"email": "[email protected]"}) | ||
assert response.status_code == 400 | ||
assert MailingList.objects.count() == 0 | ||
assert MailingList.objects.count() == 0 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_mailing_list_unsubscribe(api_client, mailing_list_emails): | ||
@pytest.mark.parametrize( | ||
"ip_address", | ||
[ | ||
("100.1.1.40"), | ||
], | ||
) | ||
def test_mailing_list_unsubscribe( | ||
api_client_with_custom_ip_address, mailing_list_emails | ||
): | ||
num_mailing_list_emails = mailing_list_emails.count() | ||
assert MailingListEmail.objects.count() == num_mailing_list_emails | ||
assert MailingList.objects.first().emails.count() == num_mailing_list_emails | ||
url = reverse("account:profiles-unsubscribe") | ||
response = api_client.post(url, {"email": "[email protected]"}) | ||
response = api_client_with_custom_ip_address.post(url, {"email": "[email protected]"}) | ||
assert response.status_code == 200 | ||
assert MailingListEmail.objects.count() == num_mailing_list_emails - 1 | ||
assert MailingList.objects.first().emails.count() == num_mailing_list_emails - 1 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_mailing_list_unsubscribe_non_existing_email(api_client, mailing_list_emails): | ||
@pytest.mark.django_db | ||
@pytest.mark.parametrize( | ||
"ip_address", | ||
[ | ||
("101.1.1.40"), | ||
], | ||
) | ||
def test_mailing_list_unsubscribe_non_existing_email( | ||
api_client_with_custom_ip_address, mailing_list_emails | ||
): | ||
num_mailing_list_emails = mailing_list_emails.count() | ||
assert MailingListEmail.objects.count() == num_mailing_list_emails | ||
assert MailingList.objects.first().emails.count() == num_mailing_list_emails | ||
url = reverse("account:profiles-unsubscribe") | ||
response = api_client.post(url, {"email": "[email protected]"}) | ||
response = api_client_with_custom_ip_address.post( | ||
url, {"email": "[email protected]"} | ||
) | ||
assert response.status_code == 400 | ||
assert MailingListEmail.objects.count() == num_mailing_list_emails | ||
assert MailingList.objects.first().emails.count() == num_mailing_list_emails | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_mailing_list_unsubscribe_email_not_provided(api_client, mailing_list_emails): | ||
@pytest.mark.parametrize( | ||
"ip_address", | ||
[ | ||
("12.6.121.22"), | ||
], | ||
) | ||
def test_mailing_list_unsubscribe_email_not_provided( | ||
api_client_with_custom_ip_address, mailing_list_emails | ||
): | ||
url = reverse("account:profiles-unsubscribe") | ||
response = api_client.post(url) | ||
response = api_client_with_custom_ip_address.post(url) | ||
assert response.status_code == 400 |
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