async def test_send_message_http_error(self, wd_mock, log_mock): wdo = mock.MagicMock() wdo.publish.return_value = test_utilities.awaitable(None) wd_mock.return_value = wdo self.wf._lookup_endpoint_details = mock.MagicMock() self.wf._lookup_endpoint_details.return_value = test_utilities.awaitable( LOOKUP_RESPONSE) self.wf._prepare_outbound_message = mock.MagicMock() self.wf._prepare_outbound_message.return_value = test_utilities.awaitable( ("123", { "qwe": "qwe" }, "message")) wdo.set_outbound_status.return_value = test_utilities.awaitable(None) future = asyncio.Future() future.set_exception(httpclient.HTTPClientError(code=409)) self.transmission.make_request.return_value = future error, text, work_description_response = await self.wf.handle_outbound_message( from_asid="202020", message_id="123", correlation_id="qwe", interaction_details={'action': ''}, payload="nice message", work_description_object=None) wdo.set_outbound_status.assert_called_with( work_description.MessageStatus. OUTBOUND_SYNC_MESSAGE_RESPONSE_RECEIVED) self.assertEqual(error, 500) self.assertEqual(text, 'Error(s) received from Spine: HTTP 409: Conflict')
async def update_network(self, network, **kwargs): if 'trained' in kwargs: raise httpclient.HTTPClientError("Use `upload_network` method") url = f'{self.api_url}/networks/{network["_id"]}/' headers = {'Content-Type': 'application/json', 'If-Match': network['_etag']} body = json.dumps(kwargs) response = await self.client.fetch(url, raise_error=False, method='PATCH', body=body, headers=headers) print(response.body) return json.loads(response.body)
async def test_should_not_retry_request_if_non_retriable_exception_raised( self): test_cases = [("HTTPClientError 400", httpclient.HTTPClientError(code=400), httpclient.HTTPClientError), ("SSLError", ssl.SSLError, ssl.SSLError)] for description, error, expected_result in test_cases: with self.subTest(description): with patch.object(httpclient.AsyncHTTPClient(), "fetch") as mock_fetch: mock_fetch.side_effect = error with self.assertRaises(expected_result): await self.transmission.make_request( URL_VALUE, HEADERS, MESSAGE)
async def test_should_retry_request_if_retriable_exception_raised(self): test_cases = [("HTTPClientError 599", httpclient.HTTPClientError(code=599)), ("Generic Exception", Exception)] for description, error in test_cases: with self.subTest(description): with patch.object(httpclient.AsyncHTTPClient(), "fetch") as mock_fetch: sentinel.result.code = 200 mock_fetch.side_effect = [ error, awaitable(sentinel.result) ] actual_response = await self.transmission.make_request( URL_VALUE, HEADERS, MESSAGE) self.assertIs(actual_response, sentinel.result, "Expected content should be returned.")
async def test_initial_audit_log_should_be_called_handling_is_invoked( self, wd_mock, log_mock): self._setup_success_workflow() wdo = mock.MagicMock() wdo.publish.return_value = test_utilities.awaitable(None) wd_mock.return_value = wdo wdo.set_outbound_status.return_value = test_utilities.awaitable(None) future = asyncio.Future() future.set_exception(httpclient.HTTPClientError(code=451)) self.transmission.make_request.return_value = future await self.wf.handle_outbound_message( from_asid="202020", message_id="123", correlation_id="qwe", interaction_details={'action': 'test-interaction'}, payload="nice message", work_description_object=None) # audit log should be called at start log_mock.audit('0100', 'Outbound Synchronous workflow invoked.', {})
async def test_send_message_http_status_code_err(self, wd_mock): self._setup_success_workflow() wdo = mock.MagicMock() wdo.publish.return_value = test_utilities.awaitable(None) wd_mock.return_value = wdo wdo.set_outbound_status.return_value = test_utilities.awaitable(None) future = asyncio.Future() future.set_exception(httpclient.HTTPClientError(code=451)) self.transmission.make_request.return_value = future error, text, work_description_response = await self.wf.handle_outbound_message( from_asid="202020", message_id="123", correlation_id="qwe", interaction_details={'action': ''}, payload="nice message", work_description_object=None) wdo.set_outbound_status.assert_called_with( work_description.MessageStatus.OUTBOUND_MESSAGE_RESPONSE_RECEIVED) self.assertEqual(error, 500) self.assertEqual(text, 'Error(s) received from Spine: HTTP 451: Unknown')