def test_payload_id_matches_manifest_id(self): with self.subTest("Incorrect manifest occurrences returns 500 error"): dictionary = { 'action': "urn:nhs-itk:services:201005:SendNHS111Report", 'service': "urn:nhs-itk:services:201005:SendNHS111Report", 'manifestCount': "2", 'manifests': [{"id": 'one'}, {'id': 'one'}], 'payloadCount': "2", 'payloads': [{"id": 'one'}, {'id': "two"}] } expected = FileUtilities.get_file_string( str(self.expectedXmlFileDir / 'payloadID_does_not_match_manifestID.xml')) msg = self.builder.build_message(dictionary) message_handler = MessageHandler(msg) self.assertEqual(message_handler.error_flag, True) XmlUtilities.assert_xml_equal_utf_8(expected, message_handler.get_response()) with self.subTest("Incorrect manifest occurrences returns 500 error"): dictionary = { 'action': "urn:nhs-itk:services:201005:SendNHS111Report", 'service': "urn:nhs-itk:services:201005:SendNHS111Report", 'manifestCount': "2", 'manifests': [{"id": 'one'}, {'id': "two"}], 'payloadCount': "2", 'payloads': [{"id": 'one'}, {'id': "two"}] } msg = self.builder.build_message(dictionary) message_handler = MessageHandler(msg) self.assertEqual(message_handler.error_flag, False) XmlUtilities.assert_xml_equal_utf_8(self.success_response, message_handler.get_response())
def test_manifest_payload_count(self): with self.subTest("Mismatched counts: 500 response"): counts = { 'action': "urn:nhs-itk:services:201005:SendNHS111Report", 'service': "urn:nhs-itk:services:201005:SendNHS111Report", 'manifestCount': "1", 'manifests': [{"id": 'one'}], 'payloadCount': "2", 'payloads': [{"id": 'one'}, {'id': "two"}] } expected = FileUtilities.get_file_string( str(self.expectedXmlFileDir / 'manifest_not_equal_to_payload_count.xml')) msg = self.builder.build_message(counts) message_handler = MessageHandler(msg) self.assertEqual(message_handler.error_flag, True) XmlUtilities.assert_xml_equal_utf_8(expected, message_handler.get_response()) with self.subTest("Equal counts: 200 response"): counts = { 'action': "urn:nhs-itk:services:201005:SendNHS111Report", 'service': "urn:nhs-itk:services:201005:SendNHS111Report", 'manifestCount': "2", 'manifests': [{"id": 'one'}, {"id": "two"}], 'payloadCount': "2", 'payloads': [{"id": 'one'}, {'id': "two"}] } msg = self.builder.build_message(counts) message_handler = MessageHandler(msg) self.assertEqual(message_handler.error_flag, False) XmlUtilities.assert_xml_equal_utf_8(self.success_response, message_handler.get_response())
def test_action_not_matching_service(self): with self.subTest("Two differing services result in a 500 error"): service_dict = {'action': "urn:nhs-itk:services:201005:SendNHS111Report-v2-0-ThisDoesNotMatchBelow", 'service': "urn:nhs-itk:services:201005:SendNHS111Report-Bad_Service-ThisDoesNotMatchAbove", 'manifestCount': 0, 'payloadCount': 0 } expected = FileUtilities.get_file_string( str(self.expectedXmlFileDir / 'invalid_action_service_values_response.xml')) msg = self.builder.build_message(service_dict) message_handler = MessageHandler(msg) self.assertEqual(message_handler.error_flag, True) XmlUtilities.assert_xml_equal_utf_8(expected, message_handler.get_response()) with self.subTest("Two services which are the same should return 200 code"): service_dict = {'action': "urn:nhs-itk:services:201005:SendNHS111Report", 'service': "urn:nhs-itk:services:201005:SendNHS111Report", 'manifestCount': 0, 'payloadCount': 0 } msg = self.builder.build_message(service_dict) message_handler = MessageHandler(msg) self.assertEqual(message_handler.error_flag, False) XmlUtilities.assert_xml_equal_utf_8(self.success_response, message_handler.get_response())
def test_python_dictionary_example(self): """ Basic test to demonstrate passing a python dict to the interface instead of a json file """ expected_string = FileUtilities.get_file_string( str(Path(ROOT_DIR) / 'scr/tests/test_xmls/cleanSummaryUpdate.xml')) from scr.tests.hashes.basic_dict import input_hash render = self.summaryCareRecord.populate_template(input_hash) XmlUtilities.assert_xml_equal(expected_string, render)
def test_empty_hash(self): """ Tests the contents are empty when a completely blank hash is provided """ expected_xml_file_path = str(self.xmlFileDir / 'EmptyHash.xml') hash_file_path = str(self.hashFileDir / 'EmptyHash.json') expected_string = FileUtilities.get_file_string(expected_xml_file_path) render = self.summaryCareRecord.populate_template_with_file( hash_file_path) XmlUtilities.assert_xml_equal(expected_string, render)
def test_empty_html(self): """ A test for an empty human readable content value """ expected_xml_file_path = str(self.xmlFileDir / 'EmptyHtmlGpSummaryUpdate.xml') hash_file_path = str(self.hashFileDir / 'emptyHtmlHash.json') expected_string = FileUtilities.get_file_string(expected_xml_file_path) render = self.summaryCareRecord.populate_template_with_file( hash_file_path) XmlUtilities.assert_xml_equal(expected_string, render)
def test_extended_html(self): """ Uses a larger set of Html for the human readable contents """ expected_xml_file_path = str(self.xmlFileDir / 'SummaryUpdateExtendedContents.xml') hash_file_path = str(self.hashFileDir / 'extendedHTMLhash.json') expected_string = FileUtilities.get_file_string(expected_xml_file_path) render = self.summaryCareRecord.populate_template_with_file( hash_file_path) XmlUtilities.assert_xml_equal(expected_string, render)
def test_basic(self): """ A basic test using the clean summary update from the spine tests """ expected_xml_file_path = str(self.xmlFileDir / 'cleanSummaryUpdate.xml') hash_file_path = str(self.hashFileDir / 'hash16UK05.json') expected_string = FileUtilities.get_file_string(expected_xml_file_path) render = self.summaryCareRecord.populate_template_with_file( hash_file_path) XmlUtilities.assert_xml_equal(expected_string, render)
def test_json_string_example(self): """ Basic example showing how a json string can be passed to the interface """ expected_string = FileUtilities.get_file_string( str(self.xmlFileDir / 'cleanSummaryUpdate.xml')) json_file = str(self.hashFileDir / 'hash16UK05.json') with open(json_file) as file: data = file.read() # Reads file contents into a string render = self.summaryCareRecord.populate_template_with_json_string( data) XmlUtilities.assert_xml_equal(expected_string, render)
def test_multipleReplacementOf(self): """ Note: THIS IS NOT A VALID XML INSTANCE This test is build purely for demonstrating having a variable number of occurrences of a partial in mustache, it does not conform to the schema and will not be accepted as a valid message """ expected_xml_file_path = str(self.xmlFileDir / 'multipleReplacementOf.xml') hash_file_path = str(self.hashFileDir / 'multiReplacementOfhash.json') expected_string = FileUtilities.get_file_string(expected_xml_file_path) render = self.summaryCareRecord.populate_template_with_file( hash_file_path) XmlUtilities.assert_xml_equal(expected_string, render)
def test_replacementOf(self): """ Note: this is not a valid xml instance This is to demonstrate the condition aspect of the replacementOf partial, this partial doesnt appear in the previous tests but here a list with a single element is used to show how conditionals are used in mustache """ expected_xml_file_path = str(self.xmlFileDir / 'replacementOf.xml') hash_file_path = str(self.hashFileDir / 'replacementOfhash.json') expected_string = FileUtilities.get_file_string(expected_xml_file_path) render = self.summaryCareRecord.populate_template_with_file( hash_file_path) XmlUtilities.assert_xml_equal(expected_string, render)
def test_post(self, mock_get_uuid, mock_get_timestamp): mock_get_uuid.return_value = "5BB171D4-53B2-4986-90CF-428BE6D157F5" mock_get_timestamp.return_value = "2012-03-15T06:51:08Z" expected_ack_response = FileUtilities.get_file_string( str(self.message_dir / EXPECTED_RESPONSE_FILE)) request_body = FileUtilities.get_file_string( str(self.message_dir / REQUEST_FILE)) mock_callback = Mock() self.callbacks[REF_TO_MESSAGE_ID] = mock_callback ack_response = self.fetch("/", method="POST", body=request_body, headers=CONTENT_TYPE_HEADERS) self.assertEqual(ack_response.code, 200) self.assertEqual(ack_response.headers["Content-Type"], "text/xml") XmlUtilities.assert_xml_equal(expected_ack_response, ack_response.body) mock_callback.assert_called_with(EXPECTED_MESSAGE)
def test_build_message(self, mock_get_uuid, mock_get_timestamp): mock_get_uuid.return_value = MOCK_UUID mock_get_timestamp.return_value = "2012-03-15T06:51:08Z" expected_message = FileUtilities.get_file_string(str(self.expected_message_dir / EXPECTED_EBXML)) message_id, message = self.builder.build_message({ FROM_PARTY_ID: "TESTGEN-201324", TO_PARTY_ID: "YEA-0000806", CPA_ID: "S1001A1630", CONVERSATION_ID: "79F49A34-9798-404C-AEC4-FD38DD81C138", RECEIVED_MESSAGE_TIMESTAMP: "2013-04-16T07:52:09Z", RECEIVED_MESSAGE_ID: "0CDBA95F-74DA-47E9-8383-7B8E9167D146", }) expected_message_bytes = expected_message.encode() message_bytes = message.encode() self.assertEqual(MOCK_UUID, message_id) XmlUtilities.assert_xml_equal(expected_message_bytes, message_bytes)