Skip to content
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
22 changes: 13 additions & 9 deletions backend/api/grants/tests/test_send_grant.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ def _send_grant(client, conference, conference_code=None, **kwargs):
return response


def test_send_grant(graphql_client, user, mocker, django_capture_on_commit_callbacks):
mock_email_template = mocker.patch("api.grants.mutations.EmailTemplate")
def test_send_grant(graphql_client, user, mocker, django_capture_on_commit_callbacks, sent_emails):
graphql_client.force_login(user)
conference = ConferenceFactory(active_grants=True)
EmailTemplateFactory(
Expand All @@ -118,13 +117,18 @@ def test_send_grant(graphql_client, user, mocker, django_capture_on_commit_callb
user=user, conference=conference, privacy_policy="grant"
).exists()

# An email is sent to the user
mock_email_template.objects.for_conference().get_by_identifier().send_email.assert_called_once_with(
recipient=user,
placeholders={
"user_name": user.full_name,
},
)
# Verify that the correct email template was used and email was sent
emails_sent = sent_emails()
assert emails_sent.count() == 1

sent_email = emails_sent.first()
assert sent_email.email_template.identifier == EmailTemplateIdentifier.grant_application_confirmation
assert sent_email.email_template.conference == conference
assert sent_email.recipient == user
assert sent_email.recipient_email == user.email

# Verify placeholders were processed correctly
assert sent_email.placeholders["user_name"] == user.full_name


def test_cannot_send_a_grant_if_grants_are_closed(graphql_client, user, mocker):
Expand Down
25 changes: 15 additions & 10 deletions backend/api/submissions/tests/test_send_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,9 @@ def _submit_proposal(client, conference, submission, **kwargs):


def test_submit_talk(
graphql_client, user, django_capture_on_commit_callbacks, mocker, settings
graphql_client, user, django_capture_on_commit_callbacks, mocker, settings, sent_emails
):
settings.FRONTEND_URL = "http://testserver"
mock_email_template = mocker.patch("api.submissions.mutations.EmailTemplate")
mock_notify = mocker.patch("api.submissions.mutations.notify_new_cfp_submission")
graphql_client.force_login(user)

Expand Down Expand Up @@ -219,14 +218,20 @@ def test_submit_talk(

mock_notify.delay.assert_called_once()

mock_email_template.objects.for_conference().get_by_identifier().send_email.assert_called_once_with(
recipient=user,
placeholders={
"user_name": user.full_name,
"proposal_title": "English",
"proposal_url": f"http://testserver/submission/{talk.hashid}",
},
)
# Verify that the correct email template was used and email was sent
emails_sent = sent_emails()
assert emails_sent.count() == 1

sent_email = emails_sent.first()
assert sent_email.email_template.identifier == EmailTemplateIdentifier.proposal_received_confirmation
assert sent_email.email_template.conference == conference
assert sent_email.recipient == user
assert sent_email.recipient_email == user.email

# Verify placeholders were processed correctly
assert sent_email.placeholders["user_name"] == user.full_name
assert sent_email.placeholders["proposal_title"] == "English"
assert sent_email.placeholders["proposal_url"] == f"http://testserver/submission/{talk.hashid}"


def test_submit_talk_with_photo_to_upload(graphql_client, user, mocker):
Expand Down
27 changes: 14 additions & 13 deletions backend/conferences/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from notifications.tests.factories import EmailTemplateFactory
from conferences.tests.factories import ConferenceVoucherFactory
from datetime import datetime, timezone
from unittest.mock import patch

import time_machine
from conferences.models.conference_voucher import ConferenceVoucher
Expand All @@ -22,7 +21,7 @@
ConferenceVoucher.VoucherType.GRANT,
],
)
def test_send_conference_voucher_email(voucher_type):
def test_send_conference_voucher_email(voucher_type, sent_emails):
user = UserFactory(
full_name="Marco Acierno",
email="[email protected]",
Expand All @@ -41,19 +40,21 @@ def test_send_conference_voucher_email(voucher_type):
identifier=EmailTemplateIdentifier.voucher_code,
)

with patch(
"conferences.tasks.EmailTemplate"
) as mock_email_template, time_machine.travel("2020-10-10 10:00:00Z", tick=False):
with time_machine.travel("2020-10-10 10:00:00Z", tick=False):
send_conference_voucher_email(conference_voucher_id=conference_voucher.id)

mock_email_template.objects.for_conference().get_by_identifier().send_email.assert_called_once_with(
recipient=user,
placeholders={
"voucher_code": "ABC123",
"voucher_type": voucher_type,
"user_name": "Marco Acierno",
},
)
emails_sent = sent_emails()
assert emails_sent.count() == 1

sent_email = emails_sent.first()
assert sent_email.email_template.identifier == EmailTemplateIdentifier.voucher_code
assert sent_email.email_template.conference == conference_voucher.conference
assert sent_email.recipient == user
assert sent_email.placeholders == {
"voucher_code": "ABC123",
"voucher_type": voucher_type,
"user_name": "Marco Acierno",
}

conference_voucher.refresh_from_db()
assert conference_voucher.voucher_email_sent_at == datetime(
Expand Down
18 changes: 18 additions & 0 deletions backend/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,21 @@ def matcher(req):
requests_mock.add_matcher(matcher)

return wrapper


@pytest.fixture
def sent_emails(db):
"""
Fixture to capture and provide access to SentEmail objects created during tests.
This fixture allows tests to verify email template usage and email creation
without mocking the EmailTemplate class.

Returns:
A callable that returns a QuerySet of SentEmail objects created during the test.
"""
from notifications.models import SentEmail

def get_sent_emails():
return SentEmail.objects.all()

return get_sent_emails
Loading
Loading