Skip to content

Commit

Permalink
Fix tests to reflect changes in subscribe endpoint
Browse files Browse the repository at this point in the history
The subscribe endpoint checks now if the user has already subscribed
  • Loading branch information
juuso-j committed Apr 11, 2024
1 parent c640947 commit 4d95cc9
Showing 1 changed file with 45 additions and 13 deletions.
58 changes: 45 additions & 13 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",
[
("192.168.1.40"),
("199.168.1.40"),
],
)
def test_mailing_list_unsubscribe_throttling(
Expand Down Expand Up @@ -227,18 +227,31 @@ def test_profile_patch_result_can_be_used(api_client_authenticated, users, profi


@pytest.mark.django_db
def test_mailing_list_subscribe(api_client, users, results, mailing_lists):
def test_mailing_list_subscribe(api_client, users, results):
url = reverse("account:profiles-subscribe")
response = api_client.post(
url, {"email": "[email protected]", "user": users.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.first().emails.first() == MailingListEmail.objects.first()
MailingList.objects.filter(result=user.result).first().emails.first()
== MailingListEmail.objects.first()
)
assert MailingList.objects.first().result == users.first().result
assert MailingList.objects.first().result == user.result


@pytest.mark.django_db
def test_mailing_user_has_subscribed(api_client, users, results, mailing_lists):
url = reverse("account:profiles-subscribe")
response = api_client.post(
url, {"email": "[email protected]", "user": users.first().id}
)
assert response.status_code == 201
response = api_client.post(
url, {"email": "[email protected]", "user": users.first().id}
)
assert response.status_code == 400


@pytest.mark.django_db
Expand All @@ -261,15 +274,15 @@ def test_mailing_list_subscribe_throttling(
url,
{
"email": f"throttlling_test_{count}@test.com",
"user": users.first().id,
"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"}
url, {"email": f"test_{count}@test.com", "user": users[count].id}
)
assert response.status_code == 429

Expand Down Expand Up @@ -319,24 +332,43 @@ def test_mailing_list_subscribe_with_invalid_post_data(


@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
Expand Down

0 comments on commit 4d95cc9

Please sign in to comment.