def test_notify_with_http_handles_400_500_responses(mock_logger: MagicMock) -> None: test_400_url = "http://test-400-url.com" test_500_url = "http://test-500-url.com" responses.add(method=responses.GET, url=test_400_url, status=400) responses.add(method=responses.GET, url=test_500_url, status=500) notify_with_http( channel={"type": "http", "url": test_400_url, "forward_event_payload": False}, payload={}, ) mock_logger.debug.assert_called_with( ( f"notification sent to {test_400_url} failed with: " "400 Client Error: Bad Request for url: http://test-400-url.com/" ) ) notify_with_http( channel={"type": "http", "url": test_500_url, "forward_event_payload": False}, payload={}, ) mock_logger.debug.assert_called_with( ( f"notification sent to {test_500_url} failed with: " "500 Server Error: Internal Server Error for url: http://test-500-url.com/" ) )
def test_notify_with_http_will_forward_event_payload(mock_logger: MagicMock) -> None: test_url = "http://test-post-url.com" test_payload = {"test-key": "test-val", "test-dict": {"test-key-2": "test-val-2"}} responses.add( method=responses.POST, url=test_url, match=[responses.matchers.json_params_matcher(test_payload)], ) notify_with_http(channel={"type": "http", "url": test_url}, payload=test_payload) mock_logger.debug.assert_not_called()
def test_notify_with_http_wont_forward_event_payload(mock_logger: MagicMock) -> None: test_url = "http://test-post-url.com" test_payload = {"test-key": "test-val", "test-dict": {"test-key-2": "test-val-2"}} responses.add( method=responses.GET, url=test_url, ) notify_with_http( channel={"type": "http", "url": test_url, "forward_event_payload": False}, payload=test_payload, ) mock_logger.debug.assert_not_called()
def test_notify_with_http_handles_datetimes_present_in_payload( mock_logger: MagicMock, ) -> None: test_url = "http://test-datetime-url.com" now = datetime.now() now_timestamp = now.isoformat() test_payload = {"test-key": "test-val", "test-datetime": now} test_json_payload = {"test-key": "test-val", "test-datetime": now_timestamp} responses.add( method=responses.POST, url=test_url, match=[responses.matchers.json_params_matcher(test_json_payload)], ) notify_with_http(channel={"type": "http", "url": test_url}, payload=test_payload) mock_logger.debug.assert_not_called()
def test_notify_with_http_gracefully_handles_exceptions( mock_requests: MagicMock, mock_logger: MagicMock ) -> None: exception = requests.exceptions.RequestException("An Exception") mock_requests.get.side_effect = exception notify_with_http( channel={ "type": "http", "url": "http://test-url.com", "forward_event_payload": False, }, payload={}, ) mock_logger.debug.assert_called_once_with( "failed calling notification endpoint", exc_info=exception )
def test_notify_with_http_handles_error_present_in_payload( mock_logger: MagicMock, ) -> None: test_url = "http://test-error-url.com" exception = ChaosException("Something went wrong here") test_payload = {"test-key": "test-val", "error": exception} test_json_payload = { "test-key": "test-val", "error": "An exception was raised: ChaosException('Something went wrong here')", } responses.add( method=responses.POST, url=test_url, match=[responses.matchers.json_params_matcher(test_json_payload)], ) notify_with_http(channel={"type": "http", "url": test_url}, payload=test_payload) mock_logger.debug.assert_not_called()
def test_notify_with_http_requires_a_url(mock_logger: MagicMock) -> None: notify_with_http(channel={"type": "http", "url": ""}, payload={}) mock_logger.debug.assert_called_with("missing url in notification channel")