From b084d8ae7744dea03b310ec64f4e4d95d09188a2 Mon Sep 17 00:00:00 2001 From: MartinWheelerMT Date: Wed, 11 Dec 2024 15:04:19 +0000 Subject: [PATCH 1/8] add unit test for equals sign in file name when parsing ebxml --- .../messages/tests/test_ebxml_envelope.py | 10 +++++ ...manifest_contains_filename_with_equals.xml | 44 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml diff --git a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py index 2421c4e35..a702a077a 100644 --- a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py +++ b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py @@ -44,3 +44,13 @@ def test_cant_find_optional_text_value_during_parsing(self): ) self.assertEqual({}, values_dict) + + def test_filename_contains_equals_sign(self): + message = file_utilities.get_file_string( + str(self.message_dir / "ebxml_request_manifest_contains_filename_with_equals.xml") + ) + xml_tree = ElementTree.fromstring(message) + + result = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree) + values_dict = {} + self.assertEqual(result, values_dict) \ No newline at end of file diff --git a/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml b/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml new file mode 100644 index 000000000..69139e6ae --- /dev/null +++ b/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml @@ -0,0 +1,44 @@ + + + + + + YES-0000806 + + + A91424-9199121 + + S1001A1630 + 10F5A436-1913-43F0-9F18-95EA0E43E61A + urn:nhs:names:services:psis + MCCI_IN010000UK13 + + C614484E-4B10-499A-9ACD-5D645CFACF61 + 2019-05-04T20:55:16Z + F106022D-758B-49A9-A80A-8FF211C32A43 + + + + + + + + + + + HL7 payload + + + + Some description + + + Filename="filename_with=equals.BMP" + ContentType=application/octet-stream Compressed=Yes LargeAttachment=No + OriginalBase64=No Length=3345444 + + + + From 87b87bfa4e4f0bddfbc3de471d91e939147d2f10 Mon Sep 17 00:00:00 2001 From: MartinWheelerMT Date: Wed, 11 Dec 2024 15:25:05 +0000 Subject: [PATCH 2/8] add unit test for equals sign in file name when parsing ebxml --- .../mhs_common/messages/ebxml_envelope.py | 1 + .../messages/tests/test_ebxml_envelope.py | 7 +- ...manifest_contains_filename_with_equals.xml | 70 +++++++++++-------- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/mhs/common/mhs_common/messages/ebxml_envelope.py b/mhs/common/mhs_common/messages/ebxml_envelope.py index f4ed266a7..773c202a1 100644 --- a/mhs/common/mhs_common/messages/ebxml_envelope.py +++ b/mhs/common/mhs_common/messages/ebxml_envelope.py @@ -227,6 +227,7 @@ def parse_external_attachments(xml_tree: Element) -> Dict[str, str]: variables = descriptionParams = re.findall("(?:\".*?\"|\S)+", description.strip()) filename = None + print(variables) description_variables = dict(pair.split("=") for pair in variables) if "Filename" in description_variables: filename = description_variables["Filename"].replace('\\', '') diff --git a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py index a702a077a..54cebc538 100644 --- a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py +++ b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py @@ -51,6 +51,7 @@ def test_filename_contains_equals_sign(self): ) xml_tree = ElementTree.fromstring(message) - result = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree) - values_dict = {} - self.assertEqual(result, values_dict) \ No newline at end of file + external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree) + print("MartinTest") + print(external_attachments.keys()) + self.assertEqual(external_attachments, {}) diff --git a/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml b/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml index 69139e6ae..5e655fdc4 100644 --- a/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml +++ b/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml @@ -1,44 +1,56 @@ - - - - + + + - YES-0000806 + C88046-807354 - A91424-9199121 + P83007-822482 - S1001A1630 - 10F5A436-1913-43F0-9F18-95EA0E43E61A - urn:nhs:names:services:psis - MCCI_IN010000UK13 + d434c95b662b941a59f9 + A0000131-351C-452F-BEDB-3ED4759A0800 + urn:nhs:names:services:gp2gp + RCMR_IN030000UK07 - C614484E-4B10-499A-9ACD-5D645CFACF61 - 2019-05-04T20:55:16Z - F106022D-758B-49A9-A80A-8FF211C32A43 + F01D21CB-31A3-49C6-B0FC-A64C858149AA + 2024-12-10T08:02:33.405Z + 2024-12-10T14:17:33.405Z - + - - - - - - - - HL7 payload - - - - Some description + + + + + + RCMR_IN030000UK07 + - Filename="filename_with=equals.BMP" + Filename="EB653254-7854-450E-A3D3-B1711D99D665_adrian=marbles.BMP" + ContentType=application/octet-stream Compressed=Yes LargeAttachment=No + OriginalBase64=No Length=3345444 + + + Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" + ContentType=application/octet-stream Compressed=Yes LargeAttachment=No + OriginalBase64=No Length=3345444 + + + Filename="71EB2161-848F-4352-B20F-E385F94FFED2_adrian=marbles3.BMP" ContentType=application/octet-stream Compressed=Yes LargeAttachment=No OriginalBase64=No Length=3345444 - - + + \ No newline at end of file From 58331ee894697dd518475c35f15fd66c796a7649 Mon Sep 17 00:00:00 2001 From: MartinWheelerMT Date: Wed, 11 Dec 2024 16:15:27 +0000 Subject: [PATCH 3/8] add fix to split to handle an equal sign in the filename. --- .../mhs_common/messages/ebxml_envelope.py | 3 +-- .../messages/tests/test_ebxml_envelope.py | 20 +++++++++++++++---- ...manifest_contains_filename_with_equals.xml | 7 ------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/mhs/common/mhs_common/messages/ebxml_envelope.py b/mhs/common/mhs_common/messages/ebxml_envelope.py index 773c202a1..bdbe46af7 100644 --- a/mhs/common/mhs_common/messages/ebxml_envelope.py +++ b/mhs/common/mhs_common/messages/ebxml_envelope.py @@ -227,8 +227,7 @@ def parse_external_attachments(xml_tree: Element) -> Dict[str, str]: variables = descriptionParams = re.findall("(?:\".*?\"|\S)+", description.strip()) filename = None - print(variables) - description_variables = dict(pair.split("=") for pair in variables) + description_variables = dict(pair.split("=", 1) for pair in variables) if "Filename" in description_variables: filename = description_variables["Filename"].replace('\\', '') diff --git a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py index 54cebc538..ed3ae3788 100644 --- a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py +++ b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py @@ -46,12 +46,24 @@ def test_cant_find_optional_text_value_during_parsing(self): self.assertEqual({}, values_dict) def test_filename_contains_equals_sign(self): + expected_external_attachment1 = { + 'document_id': 'EB653254-7854-450E-A3D3-B1711D99D665_adrian=marbles.BMP', + 'message_id': 'MESSAGE GOES HERE', + 'description': 'DESCRIPTION GOES HERE', + 'title': 'EB653254-7854-450E-A3D3-B1711D99D665_adrian=marbles.BMP' + }, + expected_external_attachment2 = { + 'document_id': '_735BB673-D9C0-4B85-951E-98DD045C4713', + 'message_id': 'E54DEC57-6BA5-40AB-ACD0-1E383209C034', + 'description': 'DESCRIPTION GOES HERE', + 'title': 'EB653254-7854-450E-A3D3-B1711D99D665_adrian=marbles.BMP' + } + message = file_utilities.get_file_string( str(self.message_dir / "ebxml_request_manifest_contains_filename_with_equals.xml") ) xml_tree = ElementTree.fromstring(message) - external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree) - print("MartinTest") - print(external_attachments.keys()) - self.assertEqual(external_attachments, {}) + external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] + self.assertEqual(external_attachments[0], expected_external_attachment1) + diff --git a/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml b/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml index 5e655fdc4..a06052ae9 100644 --- a/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml +++ b/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml @@ -44,13 +44,6 @@ ContentType=application/octet-stream Compressed=Yes LargeAttachment=No OriginalBase64=No Length=3345444 - - Filename="71EB2161-848F-4352-B20F-E385F94FFED2_adrian=marbles3.BMP" - ContentType=application/octet-stream Compressed=Yes LargeAttachment=No - OriginalBase64=No Length=3345444 - \ No newline at end of file From e1e5e37c09731308d400a9bdaa49a0efac160b1c Mon Sep 17 00:00:00 2001 From: MartinWheelerMT Date: Wed, 11 Dec 2024 16:56:22 +0000 Subject: [PATCH 4/8] add fix to split to handle an equal sign in the filename. --- .../mhs_common/messages/ebxml_envelope.py | 6 +++--- .../messages/tests/test_ebxml_envelope.py | 17 +++++++---------- ...t_manifest_contains_filename_with_equals.xml | 13 +++---------- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/mhs/common/mhs_common/messages/ebxml_envelope.py b/mhs/common/mhs_common/messages/ebxml_envelope.py index bdbe46af7..c1c3e8d66 100644 --- a/mhs/common/mhs_common/messages/ebxml_envelope.py +++ b/mhs/common/mhs_common/messages/ebxml_envelope.py @@ -230,12 +230,12 @@ def parse_external_attachments(xml_tree: Element) -> Dict[str, str]: description_variables = dict(pair.split("=", 1) for pair in variables) if "Filename" in description_variables: filename = description_variables["Filename"].replace('\\', '') - + mid = mid_attribute.split(":")[1] - external_attachment = { + external_attachment = { EXTERNAL_ATTACHMENT_DOCUMENT_ID :document_id_attribute, EXTERNAL_ATTACHMENT_MESSAGE_ID :mid_attribute.split(":")[1], - EXTERNAL_ATTACHMENT_DESCRIPTION: description, + EXTERNAL_ATTACHMENT_DESCRIPTION: description, EXTERNAL_ATTACHMENT_TITLE: filename } diff --git a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py index ed3ae3788..9aef8f4b1 100644 --- a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py +++ b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py @@ -46,17 +46,13 @@ def test_cant_find_optional_text_value_during_parsing(self): self.assertEqual({}, values_dict) def test_filename_contains_equals_sign(self): - expected_external_attachment1 = { - 'document_id': 'EB653254-7854-450E-A3D3-B1711D99D665_adrian=marbles.BMP', - 'message_id': 'MESSAGE GOES HERE', - 'description': 'DESCRIPTION GOES HERE', - 'title': 'EB653254-7854-450E-A3D3-B1711D99D665_adrian=marbles.BMP' - }, - expected_external_attachment2 = { + expected_external_attachment = { 'document_id': '_735BB673-D9C0-4B85-951E-98DD045C4713', 'message_id': 'E54DEC57-6BA5-40AB-ACD0-1E383209C034', - 'description': 'DESCRIPTION GOES HERE', - 'title': 'EB653254-7854-450E-A3D3-B1711D99D665_adrian=marbles.BMP' + 'description': 'Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" ' + 'ContentType=application/octet-stream Compressed=Yes LargeAttachment=No ' + 'OriginalBase64=No Length=3345444', + 'title': '"735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP"' } message = file_utilities.get_file_string( @@ -65,5 +61,6 @@ def test_filename_contains_equals_sign(self): xml_tree = ElementTree.fromstring(message) external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] - self.assertEqual(external_attachments[0], expected_external_attachment1) + + self.assertEqual(external_attachments[0], expected_external_attachment) diff --git a/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml b/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml index a06052ae9..f19906d8f 100644 --- a/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml +++ b/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml @@ -30,19 +30,12 @@ RCMR_IN030000UK07 - - Filename="EB653254-7854-450E-A3D3-B1711D99D665_adrian=marbles.BMP" - ContentType=application/octet-stream Compressed=Yes LargeAttachment=No - OriginalBase64=No Length=3345444 - - Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" - ContentType=application/octet-stream Compressed=Yes LargeAttachment=No - OriginalBase64=No Length=3345444 + + Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" ContentType=application/octet-stream Compressed=Yes LargeAttachment=No OriginalBase64=No Length=3345444 + From bf2b3272d16fdbd1daec29f9197b8405772effcf Mon Sep 17 00:00:00 2001 From: MartinWheelerMT Date: Thu, 12 Dec 2024 10:17:52 +0000 Subject: [PATCH 5/8] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2db16a29c..d15064e52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Fix issue where an error occurred if the attachment filenames contained and equal sign. + ## [1.3.2] - 2024-09-09 ### Fixed From cdc91a882914c0b107c57652eab77dfbd696a137 Mon Sep 17 00:00:00 2001 From: MartinWheelerMT Date: Thu, 12 Dec 2024 14:26:18 +0000 Subject: [PATCH 6/8] Add additional test for when description does not contain filename --- .../messages/tests/test_ebxml_envelope.py | 54 +++++++++++++------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py index 9aef8f4b1..fe730fea7 100644 --- a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py +++ b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py @@ -45,22 +45,46 @@ def test_cant_find_optional_text_value_during_parsing(self): self.assertEqual({}, values_dict) - def test_filename_contains_equals_sign(self): - expected_external_attachment = { - 'document_id': '_735BB673-D9C0-4B85-951E-98DD045C4713', - 'message_id': 'E54DEC57-6BA5-40AB-ACD0-1E383209C034', - 'description': 'Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" ' - 'ContentType=application/octet-stream Compressed=Yes LargeAttachment=No ' - 'OriginalBase64=No Length=3345444', - 'title': '"735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP"' - } - - message = file_utilities.get_file_string( - str(self.message_dir / "ebxml_request_manifest_contains_filename_with_equals.xml") - ) - xml_tree = ElementTree.fromstring(message) + +def test_filename_contains_equals_sign(self): + expected_external_attachment = { + 'document_id': '_735BB673-D9C0-4B85-951E-98DD045C4713', + 'message_id': 'E54DEC57-6BA5-40AB-ACD0-1E383209C034', + 'description': 'Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" ' + 'ContentType=application/octet-stream Compressed=Yes LargeAttachment=No ' + 'OriginalBase64=No Length=3345444', + 'title': '"735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP"' + } + + manifest = ' \ + \ + \ + Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" \ + ContentType=application/octet-stream Compressed=Yes LargeAttachment=No \ + OriginalBase64=No Length=3345444 \ + \ + \ + ' + + xml_tree = ElementTree.fromstring(manifest) + + external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] + + self.assertEqual(external_attachments[0], expected_external_attachment) + + +def test_description_does_not_contain_filename(self): + manifest = ' \ + \ + This is not a GP2GP Comment \ + \ + ' + + xml_tree = ElementTree.fromstring(manifest) external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] - self.assertEqual(external_attachments[0], expected_external_attachment) + self.assertEqual(len(external_attachments), 0) From 6a82af3d05d62954da1975632102160ab8c561e7 Mon Sep 17 00:00:00 2001 From: MartinWheelerMT Date: Thu, 12 Dec 2024 14:46:22 +0000 Subject: [PATCH 7/8] Add additional test to test casing of 'Filename' in manifest description --- .../messages/tests/test_ebxml_envelope.py | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py index fe730fea7..1cadbe61f 100644 --- a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py +++ b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py @@ -25,6 +25,7 @@ ebxml_envelope.RECEIVED_MESSAGE_ID: "F106022D-758B-49A9-A80A-8FF211C32A43" } + class BaseTestEbxmlEnvelope(TestCase): current_dir = os.path.dirname(os.path.abspath(__file__)) message_dir = Path(current_dir) / MESSAGE_DIR @@ -75,16 +76,43 @@ def test_filename_contains_equals_sign(self): def test_description_does_not_contain_filename(self): - manifest = ' \ + manifest = ' \ + \ + This is not a GP2GP Comment \ + \ + ' + xml_tree = ElementTree.fromstring(manifest) + external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] + self.assertEqual(len(external_attachments), 0) + +def test_description_contains_filename_in_uppercase(self): + expected_external_attachment = { + 'document_id': '_735BB673-D9C0-4B85-951E-98DD045C4713', + 'message_id': 'E54DEC57-6BA5-40AB-ACD0-1E383209C034', + 'description': 'Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" ' + 'ContentType=application/octet-stream Compressed=Yes LargeAttachment=No ' + 'OriginalBase64=No Length=3345444', + 'title': '"735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP"' + } + + manifest = ' \ \ - This is not a GP2GP Comment \ + \ + FILENAME="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" \ + ContentType=application/octet-stream Compressed=Yes LargeAttachment=No \ + OriginalBase64=No Length=3345444 \ + \ \ ' - xml_tree = ElementTree.fromstring(manifest) + xml_tree = ElementTree.fromstring(manifest) - external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] + external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] - self.assertEqual(len(external_attachments), 0) + print("DEBUG2") + print(external_attachments[0]) + + self.assertEqual(external_attachments[0], expected_external_attachment) From 284d50c57915b2cbc1610ac4a3d33a56f7ed58c4 Mon Sep 17 00:00:00 2001 From: MartinWheelerMT Date: Thu, 12 Dec 2024 17:10:11 +0000 Subject: [PATCH 8/8] refactor test to generate the soap envelope in a seperate method to reduce duplication. Inline xml into the above method, and remove previously added xml file. Add test to ensure that `title` is none when the description does not contain a filename. Add test to ensure that title is set to none if case-sensitive 'Filename' is not provided as a key. Add test to ensure that title is not set, but attachment is still handled when a description does not contain key value pairs. Add test to ensure that the adaptor hands duplicated `Filename` keys by using the value from the last key. Add functionality to ebxml_envelope so that a check is completed on each description variable to ensure that it is a valid key value pair (`key=value`). --- .../mhs_common/messages/ebxml_envelope.py | 8 +- .../messages/tests/test_ebxml_envelope.py | 210 ++++++++++++------ ...manifest_contains_filename_with_equals.xml | 42 ---- 3 files changed, 149 insertions(+), 111 deletions(-) delete mode 100644 mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml diff --git a/mhs/common/mhs_common/messages/ebxml_envelope.py b/mhs/common/mhs_common/messages/ebxml_envelope.py index c1c3e8d66..0cf79d401 100644 --- a/mhs/common/mhs_common/messages/ebxml_envelope.py +++ b/mhs/common/mhs_common/messages/ebxml_envelope.py @@ -227,7 +227,13 @@ def parse_external_attachments(xml_tree: Element) -> Dict[str, str]: variables = descriptionParams = re.findall("(?:\".*?\"|\S)+", description.strip()) filename = None - description_variables = dict(pair.split("=", 1) for pair in variables) + + description_variables = {} + for pair in variables: + if "=" in pair: + key, value = pair.split("=", 1) + description_variables[key] = value + if "Filename" in description_variables: filename = description_variables["Filename"].replace('\\', '') diff --git a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py index 1cadbe61f..3b060cf23 100644 --- a/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py +++ b/mhs/common/mhs_common/messages/tests/test_ebxml_envelope.py @@ -46,73 +46,147 @@ def test_cant_find_optional_text_value_during_parsing(self): self.assertEqual({}, values_dict) + def test_filename_contains_equals_sign(self): + expected_external_attachment = { + 'document_id': '_735BB673-D9C0-4B85-951E-98DD045C4713', + 'message_id': 'E54DEC57-6BA5-40AB-ACD0-1E383209C034', + 'description': 'Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" ' + 'ContentType=application/octet-stream Compressed=Yes LargeAttachment=No ' + 'OriginalBase64=No Length=3345444', + 'title': '"735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP"' + } + + description = ('Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" ' + 'ContentType=application/octet-stream Compressed=Yes LargeAttachment=No OriginalBase64=No ' + 'Length=3345444') + + xml_tree = self.generate_soap_envelope_from_description(description) + + external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] + + self.assertEqual(external_attachments[0], expected_external_attachment) + + def test_description_does_not_contain_filename(self): + expected_external_attachment = { + 'document_id': '_735BB673-D9C0-4B85-951E-98DD045C4713', + 'message_id': 'E54DEC57-6BA5-40AB-ACD0-1E383209C034', + 'description': 'ContentType=application/octet-stream Compressed=Yes LargeAttachment=No ' + 'OriginalBase64=No Length=3345444', + 'title': None + } + + description = ( + 'ContentType=application/octet-stream Compressed=Yes LargeAttachment=No OriginalBase64=No ' + 'Length=3345444' + ) + + xml_tree = self.generate_soap_envelope_from_description(description) + external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] + + self.assertEqual(external_attachments[0], expected_external_attachment) + + def test_description_contains_filename_in_uppercase(self): + expected_external_attachment = { + 'document_id': '_735BB673-D9C0-4B85-951E-98DD045C4713', + 'message_id': 'E54DEC57-6BA5-40AB-ACD0-1E383209C034', + 'description': 'FILENAME="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" ' + 'ContentType=application/octet-stream Compressed=Yes LargeAttachment=No ' + 'OriginalBase64=No Length=3345444', + 'title': None + } + + description = ( + 'FILENAME="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" ' + 'ContentType=application/octet-stream Compressed=Yes LargeAttachment=No OriginalBase64=No ' + 'Length=3345444' + ) + + xml_tree = self.generate_soap_envelope_from_description(description) + + external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] + + self.assertEqual(external_attachments[0], expected_external_attachment) + + def test_description_is_not_a_valid_gp2gp_message(self): + expected_external_attachment = { + 'document_id': '_735BB673-D9C0-4B85-951E-98DD045C4713', + 'message_id': 'E54DEC57-6BA5-40AB-ACD0-1E383209C034', + 'description': 'This is not a valid GP2GP Message', + 'title': None + } + + description = 'This is not a valid GP2GP Message' -def test_filename_contains_equals_sign(self): - expected_external_attachment = { - 'document_id': '_735BB673-D9C0-4B85-951E-98DD045C4713', - 'message_id': 'E54DEC57-6BA5-40AB-ACD0-1E383209C034', - 'description': 'Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" ' - 'ContentType=application/octet-stream Compressed=Yes LargeAttachment=No ' - 'OriginalBase64=No Length=3345444', - 'title': '"735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP"' - } - - manifest = ' \ - \ - \ - Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" \ - ContentType=application/octet-stream Compressed=Yes LargeAttachment=No \ - OriginalBase64=No Length=3345444 \ - \ - \ - ' - - xml_tree = ElementTree.fromstring(manifest) - - external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] - - self.assertEqual(external_attachments[0], expected_external_attachment) - - -def test_description_does_not_contain_filename(self): - manifest = ' \ - \ - This is not a GP2GP Comment \ - \ - ' - xml_tree = ElementTree.fromstring(manifest) - external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] - self.assertEqual(len(external_attachments), 0) - -def test_description_contains_filename_in_uppercase(self): - expected_external_attachment = { - 'document_id': '_735BB673-D9C0-4B85-951E-98DD045C4713', - 'message_id': 'E54DEC57-6BA5-40AB-ACD0-1E383209C034', - 'description': 'Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" ' - 'ContentType=application/octet-stream Compressed=Yes LargeAttachment=No ' - 'OriginalBase64=No Length=3345444', - 'title': '"735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP"' - } - - manifest = ' \ - \ - \ - FILENAME="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" \ - ContentType=application/octet-stream Compressed=Yes LargeAttachment=No \ - OriginalBase64=No Length=3345444 \ - \ - \ - ' - - xml_tree = ElementTree.fromstring(manifest) - - external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] - - print("DEBUG2") - print(external_attachments[0]) - - self.assertEqual(external_attachments[0], expected_external_attachment) + xml_tree = self.generate_soap_envelope_from_description(description) + external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] + + self.assertEqual(external_attachments[0], expected_external_attachment) + + def test_description_contains_duplicated_filename_key(self): + expected_external_attachment = { + 'document_id': '_735BB673-D9C0-4B85-951E-98DD045C4713', + 'message_id': 'E54DEC57-6BA5-40AB-ACD0-1E383209C034', + 'description': 'Filename="marbles.BMP" Filename="marbles2.BMP" ' + 'ContentType=application/octet-stream Compressed=Yes LargeAttachment=No ' + 'OriginalBase64=No Length=3345444', + 'title': '"marbles2.BMP"' + } + + description = ( + 'Filename="marbles.BMP" Filename="marbles2.BMP" ContentType=application/octet-stream ' + 'Compressed=Yes LargeAttachment=No OriginalBase64=No Length=3345444' + ) + + xml_tree = self.generate_soap_envelope_from_description(description) + + external_attachments = ebxml_envelope.EbxmlEnvelope.parse_external_attachments(xml_tree)['external_attachments'] + + self.assertEqual(external_attachments[0], expected_external_attachment) + + @staticmethod + def generate_soap_envelope_from_description(description): + message = ''' + + + + + C88046-807354 + + + P83007-822482 + + d434c95b662b941a59f9 + A0000131-351C-452F-BEDB-3ED4759A0800 + urn:nhs:names:services:gp2gp + RCMR_IN030000UK07 + + F01D21CB-31A3-49C6-B0FC-A64C858149AA + 2024-12-10T08:02:33.405Z + 2024-12-10T14:17:33.405Z + + + + + + + + + RCMR_IN030000UK07 + + + + %s + + + + ''' % description + + xml_tree = ElementTree.fromstring(message) + return xml_tree diff --git a/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml b/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml deleted file mode 100644 index f19906d8f..000000000 --- a/mhs/common/mhs_common/messages/tests/test_messages/ebxml_request_manifest_contains_filename_with_equals.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - C88046-807354 - - - P83007-822482 - - d434c95b662b941a59f9 - A0000131-351C-452F-BEDB-3ED4759A0800 - urn:nhs:names:services:gp2gp - RCMR_IN030000UK07 - - F01D21CB-31A3-49C6-B0FC-A64C858149AA - 2024-12-10T08:02:33.405Z - 2024-12-10T14:17:33.405Z - - - - - - - - - RCMR_IN030000UK07 - - - - - Filename="735BB673-D9C0-4B85-951E-98DD045C4713_adrian=marbles2.BMP" ContentType=application/octet-stream Compressed=Yes LargeAttachment=No OriginalBase64=No Length=3345444 - - - - - \ No newline at end of file