From e4164db9f5a94e3a319dfc70be0bbe33038e5559 Mon Sep 17 00:00:00 2001 From: Wassilios Lytras Date: Fri, 3 May 2024 14:46:24 +0200 Subject: [PATCH] Fix: Original Message was not found when MDN is received. * Fix github action build fail due to: https://stackoverflow.com/questions/71673404/importerror-cannot-import-name-unicodefun-from-click * Added partner setting to force canonicalize binary. * Formatted with black * Add specific error when MDN received, but Original Message was not found. Related to https://github.com/abhishek-ram/django-pyas2/issues/45 and will be implemented/used in django-pyas2. --- pyas2lib/as2.py | 5 +++++ pyas2lib/tests/test_advanced.py | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pyas2lib/as2.py b/pyas2lib/as2.py index cb320a3..9c74e54 100644 --- a/pyas2lib/as2.py +++ b/pyas2lib/as2.py @@ -943,6 +943,11 @@ def parse(self, raw_content, find_message_cb): # Call the find message callback which should return a Message instance orig_message = find_message_cb(self.orig_message_id, orig_recipient) + if not orig_message: + status = "failed/Failure" + details_status = "original-message-not-found" + return status, details_status + # Extract the headers and save it mdn_headers = {} for k, v in self.payload.items(): diff --git a/pyas2lib/tests/test_advanced.py b/pyas2lib/tests/test_advanced.py index fefda8d..a0aca23 100644 --- a/pyas2lib/tests/test_advanced.py +++ b/pyas2lib/tests/test_advanced.py @@ -396,6 +396,33 @@ def test_mdn_not_found(self): self.assertEqual(status, "failed/Failure") self.assertEqual(detailed_status, "mdn-not-found") + def test_mdn_original_message_not_found(self): + """Test that the MDN parser raises MDN not found when a non MDN message is passed.""" + self.partner.mdn_mode = as2.SYNCHRONOUS_MDN + self.out_message = as2.Message(self.org, self.partner) + self.out_message.build(self.test_data) + + # Parse the generated AS2 message as the partner + raw_out_message = ( + self.out_message.headers_str + b"\r\n" + self.out_message.content + ) + in_message = as2.Message() + _, _, mdn = in_message.parse( + raw_out_message, + find_org_cb=self.find_org, + find_partner_cb=self.find_partner, + find_message_cb=lambda x, y: False, + ) + + # Parse the MDN + out_mdn = as2.Mdn() + status, detailed_status = out_mdn.parse( + mdn.headers_str + b"\r\n" + mdn.content, find_message_cb=lambda x, y: False + ) + + self.assertEqual(status, "failed/Failure") + self.assertEqual(detailed_status, "original-message-not-found") + def test_unsigned_mdn_sent_error(self): """Test the case where a signed mdn was expected but unsigned mdn was returned.""" self.partner.mdn_mode = as2.SYNCHRONOUS_MDN