Skip to content

Commit aaf3633

Browse files
Improve email template tests (#4473)
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Marco Acierno <[email protected]>
1 parent 601884e commit aaf3633

File tree

9 files changed

+476
-309
lines changed

9 files changed

+476
-309
lines changed

backend/api/grants/tests/test_send_grant.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ def _send_grant(client, conference, conference_code=None, **kwargs):
9191
return response
9292

9393

94-
def test_send_grant(graphql_client, user, mocker, django_capture_on_commit_callbacks):
95-
mock_email_template = mocker.patch("api.grants.mutations.EmailTemplate")
94+
def test_send_grant(graphql_client, user, mocker, django_capture_on_commit_callbacks, sent_emails):
9695
graphql_client.force_login(user)
9796
conference = ConferenceFactory(active_grants=True)
9897
EmailTemplateFactory(
@@ -118,13 +117,18 @@ def test_send_grant(graphql_client, user, mocker, django_capture_on_commit_callb
118117
user=user, conference=conference, privacy_policy="grant"
119118
).exists()
120119

121-
# An email is sent to the user
122-
mock_email_template.objects.for_conference().get_by_identifier().send_email.assert_called_once_with(
123-
recipient=user,
124-
placeholders={
125-
"user_name": user.full_name,
126-
},
127-
)
120+
# Verify that the correct email template was used and email was sent
121+
emails_sent = sent_emails()
122+
assert emails_sent.count() == 1
123+
124+
sent_email = emails_sent.first()
125+
assert sent_email.email_template.identifier == EmailTemplateIdentifier.grant_application_confirmation
126+
assert sent_email.email_template.conference == conference
127+
assert sent_email.recipient == user
128+
assert sent_email.recipient_email == user.email
129+
130+
# Verify placeholders were processed correctly
131+
assert sent_email.placeholders["user_name"] == user.full_name
128132

129133

130134
def test_cannot_send_a_grant_if_grants_are_closed(graphql_client, user, mocker):

backend/api/submissions/tests/test_send_submission.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ def _submit_proposal(client, conference, submission, **kwargs):
141141

142142

143143
def test_submit_talk(
144-
graphql_client, user, django_capture_on_commit_callbacks, mocker, settings
144+
graphql_client, user, django_capture_on_commit_callbacks, mocker, settings, sent_emails
145145
):
146146
settings.FRONTEND_URL = "http://testserver"
147-
mock_email_template = mocker.patch("api.submissions.mutations.EmailTemplate")
148147
mock_notify = mocker.patch("api.submissions.mutations.notify_new_cfp_submission")
149148
graphql_client.force_login(user)
150149

@@ -219,14 +218,20 @@ def test_submit_talk(
219218

220219
mock_notify.delay.assert_called_once()
221220

222-
mock_email_template.objects.for_conference().get_by_identifier().send_email.assert_called_once_with(
223-
recipient=user,
224-
placeholders={
225-
"user_name": user.full_name,
226-
"proposal_title": "English",
227-
"proposal_url": f"http://testserver/submission/{talk.hashid}",
228-
},
229-
)
221+
# Verify that the correct email template was used and email was sent
222+
emails_sent = sent_emails()
223+
assert emails_sent.count() == 1
224+
225+
sent_email = emails_sent.first()
226+
assert sent_email.email_template.identifier == EmailTemplateIdentifier.proposal_received_confirmation
227+
assert sent_email.email_template.conference == conference
228+
assert sent_email.recipient == user
229+
assert sent_email.recipient_email == user.email
230+
231+
# Verify placeholders were processed correctly
232+
assert sent_email.placeholders["user_name"] == user.full_name
233+
assert sent_email.placeholders["proposal_title"] == "English"
234+
assert sent_email.placeholders["proposal_url"] == f"http://testserver/submission/{talk.hashid}"
230235

231236

232237
def test_submit_talk_with_photo_to_upload(graphql_client, user, mocker):

backend/conferences/tests/test_tasks.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from notifications.tests.factories import EmailTemplateFactory
33
from conferences.tests.factories import ConferenceVoucherFactory
44
from datetime import datetime, timezone
5-
from unittest.mock import patch
65

76
import time_machine
87
from conferences.models.conference_voucher import ConferenceVoucher
@@ -22,7 +21,7 @@
2221
ConferenceVoucher.VoucherType.GRANT,
2322
],
2423
)
25-
def test_send_conference_voucher_email(voucher_type):
24+
def test_send_conference_voucher_email(voucher_type, sent_emails):
2625
user = UserFactory(
2726
full_name="Marco Acierno",
2827
@@ -41,19 +40,21 @@ def test_send_conference_voucher_email(voucher_type):
4140
identifier=EmailTemplateIdentifier.voucher_code,
4241
)
4342

44-
with patch(
45-
"conferences.tasks.EmailTemplate"
46-
) as mock_email_template, time_machine.travel("2020-10-10 10:00:00Z", tick=False):
43+
with time_machine.travel("2020-10-10 10:00:00Z", tick=False):
4744
send_conference_voucher_email(conference_voucher_id=conference_voucher.id)
4845

49-
mock_email_template.objects.for_conference().get_by_identifier().send_email.assert_called_once_with(
50-
recipient=user,
51-
placeholders={
52-
"voucher_code": "ABC123",
53-
"voucher_type": voucher_type,
54-
"user_name": "Marco Acierno",
55-
},
56-
)
46+
emails_sent = sent_emails()
47+
assert emails_sent.count() == 1
48+
49+
sent_email = emails_sent.first()
50+
assert sent_email.email_template.identifier == EmailTemplateIdentifier.voucher_code
51+
assert sent_email.email_template.conference == conference_voucher.conference
52+
assert sent_email.recipient == user
53+
assert sent_email.placeholders == {
54+
"voucher_code": "ABC123",
55+
"voucher_type": voucher_type,
56+
"user_name": "Marco Acierno",
57+
}
5758

5859
conference_voucher.refresh_from_db()
5960
assert conference_voucher.voucher_email_sent_at == datetime(

backend/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,21 @@ def matcher(req):
116116
requests_mock.add_matcher(matcher)
117117

118118
return wrapper
119+
120+
121+
@pytest.fixture
122+
def sent_emails(db):
123+
"""
124+
Fixture to capture and provide access to SentEmail objects created during tests.
125+
This fixture allows tests to verify email template usage and email creation
126+
without mocking the EmailTemplate class.
127+
128+
Returns:
129+
A callable that returns a QuerySet of SentEmail objects created during the test.
130+
"""
131+
from notifications.models import SentEmail
132+
133+
def get_sent_emails():
134+
return SentEmail.objects.all()
135+
136+
return get_sent_emails

0 commit comments

Comments
 (0)