def test_serialize_with_one_attachment(self, mock_get_uuid, mock_get_timestamp): mock_get_uuid.side_effect = [ "8F1D7DE1-02AB-48D7-A797-A947B09F347F", test_ebxml_envelope.MOCK_UUID ] mock_get_timestamp.return_value = test_ebxml_envelope.MOCK_TIMESTAMP message_dictionary = get_test_message_dictionary() message_dictionary[ebxml_request_envelope.ATTACHMENTS] = [{ ebxml_request_envelope.ATTACHMENT_CONTENT_TYPE: 'text/plain', ebxml_request_envelope.ATTACHMENT_BASE64: False, ebxml_request_envelope.ATTACHMENT_DESCRIPTION: 'Some description', ebxml_request_envelope.ATTACHMENT_PAYLOAD: 'Some payload' }] envelope = ebxml_request_envelope.EbxmlRequestEnvelope( message_dictionary) message_id, http_headers, message = envelope.serialize() normalized_expected_message = self._get_expected_file_string( 'ebxml_request_one_attachment.xml') normalized_message = file_utilities.FileUtilities.normalize_line_endings( message) self.assertEqual(test_ebxml_envelope.MOCK_UUID, message_id) self.assertEqual(EXPECTED_HTTP_HEADERS, http_headers) self.assertEqual(normalized_expected_message, normalized_message)
def test_serialize_doesnt_include_xml_tag_when_corresponding_boolean_flag_set_to_false( self, mock_get_uuid, mock_get_timestamp): mock_get_uuid.return_value = test_ebxml_envelope.MOCK_UUID mock_get_timestamp.return_value = test_ebxml_envelope.MOCK_TIMESTAMP test_cases = [(ebxml_request_envelope.DUPLICATE_ELIMINATION, 'eb:DuplicateElimination'), (ebxml_request_envelope.ACK_REQUESTED, 'eb:AckRequested'), (ebxml_request_envelope.SYNC_REPLY, 'eb:SyncReply')] for boolean_tag, boolean_xml_tag in test_cases: with self.subTest(boolean_tag=boolean_tag): message_dictionary = get_test_message_dictionary() message_dictionary[boolean_tag] = False envelope = ebxml_request_envelope.EbxmlRequestEnvelope( message_dictionary) message_id, http_headers, message = envelope.serialize() normalized_message = file_utilities.FileUtilities.normalize_line_endings( message) self.assertEqual(test_ebxml_envelope.MOCK_UUID, message_id) self.assertEqual(EXPECTED_HTTP_HEADERS, http_headers) self.assertNotEqual( self.normalized_expected_serialized_message, normalized_message) self.assertNotIn(boolean_xml_tag, normalized_message)
def test_serialize_raises_error_when_required_tags_not_passed(self, mock_get_uuid, mock_get_timestamp): mock_get_uuid.return_value = test_ebxml_envelope.MOCK_UUID mock_get_timestamp.return_value = test_ebxml_envelope.MOCK_TIMESTAMP for required_tag in get_test_message_dictionary().keys(): with self.subTest(required_tag=required_tag): test_message_dict = get_test_message_dictionary() del test_message_dict[required_tag] envelope = ebxml_request_envelope.EbxmlRequestEnvelope(test_message_dict) with self.assertRaisesRegex(pystache_message_builder.MessageGenerationError, 'Failed to find key'): envelope.serialize()
def test_serialize_with_no_attachments(self, mock_get_uuid, mock_get_timestamp): mock_get_uuid.return_value = test_ebxml_envelope.MOCK_UUID mock_get_timestamp.return_value = test_ebxml_envelope.MOCK_TIMESTAMP envelope = ebxml_request_envelope.EbxmlRequestEnvelope(get_test_message_dictionary()) message_id, http_headers, message = envelope.serialize() normalized_message = file_utilities.normalize_line_endings(message) self.assertEqual(test_ebxml_envelope.MOCK_UUID, message_id) self.assertEqual(EXPECTED_HTTP_HEADERS, http_headers) self.assertEqual(self.normalized_expected_serialized_message, normalized_message)
def test_serialize_message_id_not_generated(self, mock_get_uuid, mock_get_timestamp): mock_get_timestamp.return_value = test_ebxml_envelope.MOCK_TIMESTAMP message_dictionary = get_test_message_dictionary() message_dictionary[ebxml_envelope.MESSAGE_ID] = test_ebxml_envelope.MOCK_UUID envelope = ebxml_request_envelope.EbxmlRequestEnvelope(message_dictionary) message_id, http_headers, message = envelope.serialize() normalized_message = file_utilities.normalize_line_endings(message) mock_get_uuid.assert_not_called() self.assertEqual(test_ebxml_envelope.MOCK_UUID, message_id) self.assertEqual(EXPECTED_HTTP_HEADERS, http_headers) self.assertEqual(self.normalized_expected_serialized_message, normalized_message)
async def _serialize_outbound_message(self, message_id, correlation_id, interaction_details, payload, wdo, to_party_key, cpa_id): try: interaction_details[ebxml_envelope.MESSAGE_ID] = message_id interaction_details[ebxml_request_envelope.MESSAGE] = payload interaction_details[ebxml_envelope.FROM_PARTY_ID] = self.party_key interaction_details[ ebxml_envelope.CONVERSATION_ID] = correlation_id interaction_details[ebxml_envelope.TO_PARTY_ID] = to_party_key interaction_details[ebxml_envelope.CPA_ID] = cpa_id _, http_headers, message = ebxml_request_envelope.EbxmlRequestEnvelope( interaction_details).serialize() except Exception as e: logger.error('0009', 'Failed to serialise outbound message. {Exception}', {'Exception': e}) await wdo.set_outbound_status( wd.MessageStatus.OUTBOUND_MESSAGE_PREPARATION_FAILED) return (500, 'Error serialising outbound message'), None, None if len(message) > self.max_request_size: logger.error( '0007', 'Request to send to Spine is too large after serialisation. ' '{RequestSize} {MaxRequestSize}', { 'RequestSize': len(message), 'MaxRequestSize': self.max_request_size }) await wdo.set_outbound_status( wd.MessageStatus.OUTBOUND_MESSAGE_PREPARATION_FAILED) return ( 400, f'Request to send to Spine is too large. MaxRequestSize={self.max_request_size} ' f'RequestSize={len(message)}'), None, None logger.info('0008', 'Message serialised successfully') await wdo.set_outbound_status( wd.MessageStatus.OUTBOUND_MESSAGE_PREPARED) return None, http_headers, message
def test_serialize_raises_error_when_required_attachment_tags_not_passed(self, mock_get_uuid, mock_get_timestamp): mock_get_timestamp.return_value = test_ebxml_envelope.MOCK_TIMESTAMP required_tags = [ ebxml_request_envelope.ATTACHMENT_CONTENT_TYPE, ebxml_request_envelope.ATTACHMENT_BASE64, ebxml_request_envelope.ATTACHMENT_DESCRIPTION, ebxml_request_envelope.ATTACHMENT_PAYLOAD ] for required_tag in required_tags: with self.subTest(required_tag=required_tag): mock_get_uuid.side_effect = ["8F1D7DE1-02AB-48D7-A797-A947B09F347F", test_ebxml_envelope.MOCK_UUID] message_dictionary = get_test_message_dictionary() message_dictionary[ebxml_request_envelope.ATTACHMENTS] = [{ ebxml_request_envelope.ATTACHMENT_CONTENT_TYPE: 'text/plain', ebxml_request_envelope.ATTACHMENT_BASE64: False, ebxml_request_envelope.ATTACHMENT_DESCRIPTION: 'Some description', ebxml_request_envelope.ATTACHMENT_PAYLOAD: 'Some payload' }] del message_dictionary[ebxml_request_envelope.ATTACHMENTS][0][required_tag] envelope = ebxml_request_envelope.EbxmlRequestEnvelope(message_dictionary) with self.assertRaisesRegex(pystache_message_builder.MessageGenerationError, 'Failed to find key'): envelope.serialize()