Skip to content

Commit

Permalink
error checking moved to check_and_route_emails and test for error added
Browse files Browse the repository at this point in the history
  • Loading branch information
markj0hnst0n committed Dec 17, 2024
1 parent 0c941b9 commit 3f0641c
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 16 deletions.
14 changes: 0 additions & 14 deletions mail/libraries/data_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
from mail.models import LicenceData, Mail, UsageData
from mail.serializers import LicenceDataMailSerializer, UpdateResponseSerializer, UsageDataMailSerializer

from mail.chief.licence_reply import LicenceReplyProcessor


class EdifactFileError(Exception):
pass
Expand Down Expand Up @@ -59,18 +57,6 @@ def serialize_email_message(dto: EmailMessageDto) -> Mail or None:

logging.info("Successfully serialized email (subject: %s)", dto.subject)

if _mail.response_subject and "licenceReply" in _mail.response_subject:
processor = LicenceReplyProcessor.load_licence_reply_from_mail(_mail)

if processor._current_rejected:
rejected_transaction_errors = processor._current_rejected.errors
for error in rejected_transaction_errors:
if "Duplicate transaction reference" in error.text:
run_number = LicenceData.objects.get(mail=_mail).hmrc_run_number
raise EdifactFileError(
f"Unable to process file due to the following error: {error.text}. It was sent in run number {run_number}"
)

# if _mail.response_data and "Duplicate transaction reference" in _mail.response_data:
# raise EdifactFileError(
# f"Unable to process file due to error with mail {_mail.id} the edi file was {_mail.edi_filename}"
Expand Down
18 changes: 17 additions & 1 deletion mail/libraries/routing_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
sort_dtos_by_date,
)
from mail.libraries.mailbox_service import get_message_iterator
from mail.models import Mail
from mail.models import Mail, LicenceData
from mail.servers import MailServer, smtp_send

logger = logging.getLogger(__name__)


class EdifactFileError(Exception):
pass


def get_spire_to_dit_mailserver() -> MailServer:
"""
Mailbox that receives emails sent from SPIRE
Expand Down Expand Up @@ -221,9 +225,21 @@ def get_email_message_dtos(server: MailServer, number: Optional[int] = 3) -> Lis

def check_and_notify_rejected_licences(mail):
from mail.celery_tasks import notify_users_of_rejected_licences
from mail.chief.licence_reply import LicenceReplyProcessor

if not settings.SEND_REJECTED_EMAIL:
return

if mail.response_data and ReplyStatusEnum.REJECTED in mail.response_data:
notify_users_of_rejected_licences(str(mail.id), mail.response_subject)

if mail.response_subject and "licenceReply" in mail.response_subject:
processor = LicenceReplyProcessor.load_licence_reply_from_mail(mail)

if processor.file_errors:
for error in processor.file_errors:
if "Duplicate transaction reference" in error.text:
run_number = LicenceData.objects.get(mail=mail).hmrc_run_number
raise EdifactFileError(
f"Unable to process file due to the following error: {error.text}. It was sent in run number {run_number}"
)
1 change: 0 additions & 1 deletion mail/tests/libraries/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import logging

from django.test import testcases
from django.utils import timezone
Expand Down
98 changes: 98 additions & 0 deletions mail/tests/test_celery_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,101 @@ def test_processing_of_licence_reply_with_rejected_licences(
self.assertEqual(message["From"], emails_data[index]["sender"])
self.assertEqual(message["To"], emails_data[index]["recipients"])
self.assertEqual(message["Subject"], emails_data[index]["subject"])

@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",
},
],
)
]
)
@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_duplicate_transaction_error(
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\\202111091457\\{run_number}",
"2\\fileError\\3057\\Duplicate transaction reference = 20240005721P - Input Line Number = 0\\2",
"3\\fileTrailer\\0\\0\\1",
]
)

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):
with pytest.raises(Exception) as excinfo:
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"])

0 comments on commit 3f0641c

Please sign in to comment.