diff --git a/src/apps/competitions/emails.py b/src/apps/competitions/emails.py index 33316daa0..3152a2b0d 100644 --- a/src/apps/competitions/emails.py +++ b/src/apps/competitions/emails.py @@ -14,13 +14,16 @@ def send_participation_requested_emails(participant): 'user': participant.user } # Notify Organizers - codalab_send_mail( - context_data=context, - subject=f'{participant.user.username} applied to your competition', - html_file="emails/participation/organizer/participation_requested.html", - text_file="emails/participation/organizer/participation_requested.txt", - to_email=get_organizer_emails(participant.competition) - ) + for organizer in participant.competition.all_organizers: + if organizer.is_deleted: + continue + codalab_send_mail( + context_data={**context, 'user': organizer}, + subject=f'{participant.user.username} applied to your competition', + html_file="emails/participation/organizer/participation_requested.html", + text_file="emails/participation/organizer/participation_requested.txt", + to_email=organizer.email + ) # Notify User codalab_send_mail( diff --git a/src/apps/competitions/tests/test_emails.py b/src/apps/competitions/tests/test_emails.py new file mode 100644 index 000000000..a032db200 --- /dev/null +++ b/src/apps/competitions/tests/test_emails.py @@ -0,0 +1,33 @@ +from types import SimpleNamespace +from unittest import mock + +from competitions.emails import send_participation_requested_emails + + +def test_participation_request_uses_each_recipient_as_email_user(): + participant_user = SimpleNamespace( + username='participant', email='participant@example.com', is_deleted=False + ) + organizers = [ + SimpleNamespace(username='owner', email='owner@example.com', is_deleted=False), + SimpleNamespace(username='collaborator', email='collaborator@example.com', is_deleted=False), + SimpleNamespace(username='deleted', email='deleted@example.com', is_deleted=True), + ] + participant = SimpleNamespace( + user=participant_user, + competition=SimpleNamespace(title='Competition', all_organizers=organizers), + ) + + with mock.patch('competitions.emails.codalab_send_mail') as send_mail: + send_participation_requested_emails(participant) + + assert [call.kwargs['to_email'] for call in send_mail.call_args_list] == [ + 'owner@example.com', + 'collaborator@example.com', + 'participant@example.com', + ] + assert [call.kwargs['context_data']['user'] for call in send_mail.call_args_list] == [ + organizers[0], + organizers[1], + participant_user, + ]