Skip to content

Commit

Permalink
fix: Remove legacy weekly stats email (#2307)
Browse files Browse the repository at this point in the history
* fix: Remove legacy weekly stats email

* Remove test
  • Loading branch information
faucomte97 authored May 22, 2024
1 parent 6b1772d commit f23610c
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 140 deletions.
124 changes: 90 additions & 34 deletions portal/tests/test_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
send_dotmailer_consent_confirmation_email_to_user,
DotmailerUserType,
)
from django.core import mail
from django.test import Client
from django.urls import reverse

Expand Down Expand Up @@ -41,19 +40,15 @@ def now(cls):
monkeypatch.setattr(datetime, "datetime", mydatetime)


@pytest.mark.django_db
def test_send_new_users_numbers_email():
client = Client()
response = client.get(reverse("send_new_users_report"))
assert response.status_code == 200
assert len(mail.outbox) == 1


def test_newsletter_calls_correct_requests(mocker, monkeypatch):
mocked_create_contact = mocker.patch("common.helpers.emails.create_contact")
mocked_add_to_address_book = mocker.patch("common.helpers.emails.add_contact_to_address_book")
mocked_add_to_address_book = mocker.patch(
"common.helpers.emails.add_contact_to_address_book"
)

add_to_dotmailer("Ray", "Charles", "[email protected]", DotmailerUserType.TEACHER)
add_to_dotmailer(
"Ray", "Charles", "[email protected]", DotmailerUserType.TEACHER
)

mocked_create_contact.assert_called_once()
mocked_add_to_address_book.assert_called_once()
Expand All @@ -74,10 +69,15 @@ def test_delete_account(mocker):

delete_contact("[email protected]")

mocked_delete.assert_called_once_with(DOTMAILER_DELETE_USER_BY_ID_URL, auth=(DOTMAILER_USER, DOTMAILER_PASSWORD))
mocked_delete.assert_called_once_with(
DOTMAILER_DELETE_USER_BY_ID_URL,
auth=(DOTMAILER_USER, DOTMAILER_PASSWORD),
)


def test_newsletter_sends_correct_request_data(mocker, monkeypatch, patch_datetime_now):
def test_newsletter_sends_correct_request_data(
mocker, monkeypatch, patch_datetime_now
):
mocked_post = mocker.patch("common.helpers.emails.post")

expected_body1 = {
Expand All @@ -91,7 +91,13 @@ def test_newsletter_sends_correct_request_data(mocker, monkeypatch, patch_dateti
{"key": "FULLNAME", "value": "Ray Charles"},
],
},
"consentFields": [{"fields": [{"key": "DATETIMECONSENTED", "value": FAKE_TIME.__str__()}]}],
"consentFields": [
{
"fields": [
{"key": "DATETIMECONSENTED", "value": FAKE_TIME.__str__()}
]
}
],
"preferences": [{"trout": True}],
}

Expand All @@ -109,51 +115,76 @@ def test_newsletter_sends_correct_request_data(mocker, monkeypatch, patch_dateti
create_contact("Ray", "Charles", "[email protected]")

mocked_post.assert_called_once_with(
DOTMAILER_CREATE_CONTACT_URL, auth=(DOTMAILER_USER, DOTMAILER_PASSWORD), json=expected_body1
DOTMAILER_CREATE_CONTACT_URL,
auth=(DOTMAILER_USER, DOTMAILER_PASSWORD),
json=expected_body1,
)

add_contact_to_address_book("Ray", "Charles", "[email protected]", DotmailerUserType.TEACHER)
add_contact_to_address_book(
"Ray", "Charles", "[email protected]", DotmailerUserType.TEACHER
)

assert mocked_post.call_count == 3

mocked_post.assert_any_call(
DOTMAILER_MAIN_ADDRESS_BOOK_URL, auth=(DOTMAILER_USER, DOTMAILER_PASSWORD), json=expected_body2
DOTMAILER_MAIN_ADDRESS_BOOK_URL,
auth=(DOTMAILER_USER, DOTMAILER_PASSWORD),
json=expected_body2,
)

mocked_post.assert_any_call(
DOTMAILER_TEACHER_ADDRESS_BOOK_URL, auth=(DOTMAILER_USER, DOTMAILER_PASSWORD), json=expected_body2
DOTMAILER_TEACHER_ADDRESS_BOOK_URL,
auth=(DOTMAILER_USER, DOTMAILER_PASSWORD),
json=expected_body2,
)

mocked_post.reset_mock()

add_contact_to_address_book("Ray", "Charles", "[email protected]", DotmailerUserType.STUDENT)
add_contact_to_address_book(
"Ray", "Charles", "[email protected]", DotmailerUserType.STUDENT
)

assert mocked_post.call_count == 2

mocked_post.assert_any_call(
DOTMAILER_MAIN_ADDRESS_BOOK_URL, auth=(DOTMAILER_USER, DOTMAILER_PASSWORD), json=expected_body2
DOTMAILER_MAIN_ADDRESS_BOOK_URL,
auth=(DOTMAILER_USER, DOTMAILER_PASSWORD),
json=expected_body2,
)

mocked_post.assert_any_call(
DOTMAILER_STUDENT_ADDRESS_BOOK_URL, auth=(DOTMAILER_USER, DOTMAILER_PASSWORD), json=expected_body2
DOTMAILER_STUDENT_ADDRESS_BOOK_URL,
auth=(DOTMAILER_USER, DOTMAILER_PASSWORD),
json=expected_body2,
)

mocked_post.reset_mock()

add_contact_to_address_book("Ray", "Charles", "[email protected]", DotmailerUserType.NO_ACCOUNT)
add_contact_to_address_book(
"Ray",
"Charles",
"[email protected]",
DotmailerUserType.NO_ACCOUNT,
)

assert mocked_post.call_count == 2

mocked_post.assert_any_call(
DOTMAILER_MAIN_ADDRESS_BOOK_URL, auth=(DOTMAILER_USER, DOTMAILER_PASSWORD), json=expected_body2
DOTMAILER_MAIN_ADDRESS_BOOK_URL,
auth=(DOTMAILER_USER, DOTMAILER_PASSWORD),
json=expected_body2,
)

mocked_post.assert_any_call(
DOTMAILER_NO_ACCOUNT_ADDRESS_BOOK_URL, auth=(DOTMAILER_USER, DOTMAILER_PASSWORD), json=expected_body2
DOTMAILER_NO_ACCOUNT_ADDRESS_BOOK_URL,
auth=(DOTMAILER_USER, DOTMAILER_PASSWORD),
json=expected_body2,
)


def test_consent_calls_send_correct_request_data(mocker, monkeypatch, patch_datetime_now):
def test_consent_calls_send_correct_request_data(
mocker, monkeypatch, patch_datetime_now
):
mocked_post = mocker.patch("common.helpers.emails.post")
mocked_put = mocker.patch("common.helpers.emails.put")

Expand Down Expand Up @@ -181,21 +212,34 @@ def test_consent_calls_send_correct_request_data(mocker, monkeypatch, patch_date
{"key": "FULLNAME", "value": "Ray Charles"},
],
},
"consentFields": [{"fields": [{"key": "DATETIMECONSENTED", "value": FAKE_TIME.__str__()}]}],
"consentFields": [
{
"fields": [
{"key": "DATETIMECONSENTED", "value": FAKE_TIME.__str__()}
]
}
],
}

expected_body2 = {"campaignID": DOTMAILER_THANKS_FOR_STAYING_CAMPAIGN_ID, "contactIds": ["1"]}
expected_body2 = {
"campaignID": DOTMAILER_THANKS_FOR_STAYING_CAMPAIGN_ID,
"contactIds": ["1"],
}

add_consent_record_to_dotmailer_user(user)

mocked_put.assert_called_once_with(
DOTMAILER_PUT_CONSENT_DATA_URL, auth=(DOTMAILER_USER, DOTMAILER_PASSWORD), json=expected_body1
DOTMAILER_PUT_CONSENT_DATA_URL,
auth=(DOTMAILER_USER, DOTMAILER_PASSWORD),
json=expected_body1,
)

send_dotmailer_consent_confirmation_email_to_user(user)

mocked_post.assert_called_with(
DOTMAILER_SEND_CAMPAIGN_URL, auth=(DOTMAILER_USER, DOTMAILER_PASSWORD), json=expected_body2
DOTMAILER_SEND_CAMPAIGN_URL,
auth=(DOTMAILER_USER, DOTMAILER_PASSWORD),
json=expected_body2,
)


Expand All @@ -212,9 +256,15 @@ def test_dotmailer_consent_form(mocker, monkeypatch):
c = Client()
consent_form_url = reverse("consent_form")

mocked_get_user_success = mocker.patch("portal.views.dotmailer.get_dotmailer_user_by_email")
mocked_add_consent = mocker.patch("portal.views.dotmailer.add_consent_record_to_dotmailer_user")
mocked_send_campaign = mocker.patch("portal.views.dotmailer.send_dotmailer_consent_confirmation_email_to_user")
mocked_get_user_success = mocker.patch(
"portal.views.dotmailer.get_dotmailer_user_by_email"
)
mocked_add_consent = mocker.patch(
"portal.views.dotmailer.add_consent_record_to_dotmailer_user"
)
mocked_send_campaign = mocker.patch(
"portal.views.dotmailer.send_dotmailer_consent_confirmation_email_to_user"
)

get_consent_form_response = c.get(consent_form_url)

Expand Down Expand Up @@ -244,7 +294,10 @@ def test_dotmailer_consent_form(mocker, monkeypatch):
mocked_add_consent.assert_called_once()
mocked_send_campaign.assert_called_once()

mocker.patch("portal.views.dotmailer.add_consent_record_to_dotmailer_user", side_effect=KeyError)
mocker.patch(
"portal.views.dotmailer.add_consent_record_to_dotmailer_user",
side_effect=KeyError,
)

wrong_email_response = c.post(consent_form_url, data=good_request_data)

Expand All @@ -255,4 +308,7 @@ def test_dotmailer_consent_form(mocker, monkeypatch):

def _is_warning_message_showing(response):
messages = list(response.wsgi_request._messages)
assert messages[0].message == "Valid email address and consent required. Please try again."
assert (
messages[0].message
== "Valid email address and consent required. Please try again."
)
Loading

0 comments on commit f23610c

Please sign in to comment.