-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from uktrade/LTD-4793-debug
LTD-4793: Fix failure when sending licence rejected notifications email
- Loading branch information
Showing
6 changed files
with
143 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,17 @@ | |
|
||
from datetime import datetime, timezone | ||
from parameterized import parameterized | ||
from email.mime.multipart import MIMEMultipart | ||
from unittest import mock | ||
from django.test import TestCase, override_settings | ||
from unittest.mock import MagicMock | ||
|
||
from mail.celery_tasks import manage_inbox, notify_users_of_rejected_licences | ||
from mail.enums import ExtractTypeEnum, ReceptionStatusEnum, SourceEnum | ||
from mail.libraries.email_message_dto import EmailMessageDto | ||
from mail.libraries.routing_controller import check_and_route_emails | ||
from mail.models import LicenceData, Mail | ||
from mail.tests.factories import LicenceDataFactory, MailFactory | ||
from mail.tests.libraries.client import LiteHMRCTestClient | ||
|
||
|
||
|
@@ -28,7 +32,7 @@ def test_send_rejected_notification_email_success(self, lock_acquired, mock_cach | |
"""Test sending of licence rejected emails without and with retry scenario""" | ||
mock_cache.add.side_effect = lock_acquired | ||
|
||
notify_users_of_rejected_licences.delay("123", "CHIEF_SPIRE_licenceReply_202401180900_42557") | ||
notify_users_of_rejected_licences("123", "CHIEF_SPIRE_licenceReply_202401180900_42557") | ||
|
||
assert mock_cache.add.call_count == len(lock_acquired) | ||
mock_smtp_send.assert_called_once() | ||
|
@@ -114,3 +118,112 @@ def test_sending_of_new_message_from_spire_success( | |
|
||
assert mock_cache.add.call_count == len(lock_acquired) | ||
mock_smtp_send.assert_called_once() | ||
|
||
@parameterized.expand( | ||
[ | ||
# SEND_REJECTED_EMAIL state, mail sender and recipients | ||
( | ||
True, | ||
[ | ||
{ | ||
"sender": "[email protected]", | ||
"recipients": "[email protected]", | ||
"subject": "ILBDOTI_live_CHIEF_licenceReply_78120_202403060300", | ||
}, | ||
{ | ||
"sender": "[email protected]", | ||
"recipients": "[email protected]", | ||
"subject": "Licence rejected by HMRC", | ||
}, | ||
], | ||
), | ||
( | ||
False, | ||
[ | ||
{ | ||
"sender": "[email protected]", | ||
"recipients": "[email protected]", | ||
"subject": "ILBDOTI_live_CHIEF_licenceReply_78120_202403060300", | ||
} | ||
], | ||
), | ||
] | ||
) | ||
@override_settings( | ||
EMAIL_USER="[email protected]", | ||
NOTIFY_USERS=["[email protected]"], | ||
SPIRE_ADDRESS="[email protected]", | ||
) | ||
@mock.patch("mail.libraries.routing_controller.get_spire_to_dit_mailserver") | ||
@mock.patch("mail.libraries.routing_controller.get_hmrc_to_dit_mailserver") | ||
@mock.patch("mail.celery_tasks.smtp_send") | ||
@mock.patch("mail.celery_tasks.cache") | ||
@mock.patch("mail.libraries.routing_controller.get_email_message_dtos") | ||
def test_processing_of_licence_reply_with_rejected_licences( | ||
self, | ||
send_rejected_email_flag, | ||
emails_data, | ||
email_dtos, | ||
mock_cache, | ||
mock_smtp_send, | ||
mock_get_hmrc_to_dit_mailserver, | ||
mock_get_spire_to_dit_mailserver, | ||
): | ||
""" | ||
Test processing of licence reply from HMRC with rejected licences. | ||
If SEND_REJECTED_EMAIL=True then we send email notifications to users if any licences are rejected. | ||
""" | ||
obj = MagicMock() | ||
mock_get_hmrc_to_dit_mailserver.return_value = obj | ||
mock_get_spire_to_dit_mailserver.return_value = obj | ||
mock_cache.add.return_value = True | ||
|
||
run_number = 78120 | ||
mail = MailFactory( | ||
extract_type=ExtractTypeEnum.LICENCE_DATA, | ||
edi_filename=self.licence_data_file_name, | ||
edi_data=self.licence_data_file_body.decode("utf-8"), | ||
status=ReceptionStatusEnum.REPLY_PENDING, | ||
) | ||
LicenceDataFactory(mail=mail, source_run_number=run_number, hmrc_run_number=run_number) | ||
|
||
licence_reply_filename = f"ILBDOTI_live_CHIEF_licenceReply_{run_number}_202403060300" | ||
file_lines = "\n".join( | ||
[ | ||
f"1\\fileHeader\\CHIEF\SPIRE\\licenceReply\\202403061600\\{run_number}", | ||
"2\\accepted\\340631", | ||
"3\\rejected\\340632", | ||
"4\\end\\rejected\\3", | ||
"5\\fileTrailer\\1\\1\\0", | ||
] | ||
) | ||
|
||
email_message_dto = EmailMessageDto( | ||
run_number=f"{run_number}", | ||
sender="[email protected]", | ||
receiver="[email protected]", | ||
date="Mon, 17 May 2021 14:20:18 +0100", | ||
body="licence rejected", | ||
subject=licence_reply_filename, | ||
attachment=[licence_reply_filename, file_lines.encode("utf-8")], | ||
raw_data="qwerty", | ||
) | ||
email_dtos.return_value = [ | ||
(email_message_dto, lambda x: x), | ||
] | ||
|
||
with override_settings(SEND_REJECTED_EMAIL=send_rejected_email_flag): | ||
check_and_route_emails() | ||
|
||
mail.refresh_from_db() | ||
self.assertEqual(mail.status, ReceptionStatusEnum.REPLY_SENT) | ||
|
||
assert mock_cache.add.call_count == len(emails_data) | ||
mock_smtp_send.call_count == len(emails_data) | ||
|
||
for index, item in enumerate(mock_smtp_send.call_args_list): | ||
message = item.args[0] | ||
self.assertTrue(isinstance(message, MIMEMultipart)) | ||
self.assertEqual(message["From"], emails_data[index]["sender"]) | ||
self.assertEqual(message["To"], emails_data[index]["recipients"]) | ||
self.assertEqual(message["Subject"], emails_data[index]["subject"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters