@pytest.mark.parametrize('params', [ {'message': 'hello'}, {'message': 'hello', 'send_to_sentry': True}, {'message': 'hello', 'send_to_sentry': False}]) @patch('app_name.response.create_error_response') def test_create_fatal_response(mock_create_error_response, params): response.create_fatal_response(**params) send_to_sentry = params.get('send_to_sentry', True) mock_create_error_response.assert_called_with( response.ERROR_CODE_INTERNAL_ERROR, 'hello', status=500, send_to_sentry=send_to_sentry) @pytest.mark.parametrize('params', [ {'message': 'hello'}, {'message': 'hello', 'send_to_sentry': True}, {'message': 'hello', 'send_to_sentry': False}]) @patch('app_name.response.create_error_response') def test_create_not_found_response_sentry(mock_create_response, params): """Test creating a not found response doesn't send to sentry by default. """ response.create_not_found_response(**params) send_to_sentry = params.get('send_to_sentry', False) mock_create_response.assert_called_with( response.ERROR_CODE_NOT_FOUND, 'hello', status=404, send_to_sentry=send_to_sentry) @pytest.mark.parametrize('sentry_client_exists, params', [ (True, {}), # send_to_sentry not set, defaults False (False, {}), # send_to_sentry not set, defaults False (True, {'send_to_sentry': True}), (False, {'send_to_sentry': True}), (False, {'send_to_sentry': False})]) @patch('app_name.response.sentry') def test_send_error_response_sentry( mock_sentry, sentry_client_exists, params): """Test sending an error message to Sentry. Checks sentry message is only sent if send_to_sentry is passed as True and sentry client exists. """ mock_sentry.sentry_client.__bool__.return_value = sentry_client_exists response.create_error_response('code', 'message', 400, **params) if sentry_client_exists and params.get('send_to_sentry', False): mock_sentry.sentry_client.captureMessage.assert_called_with( message="A 'code' error was created.", stack=True, extra={ 'errors': {'message': 'message', 'code': 'code'}, 'status': 400}) else: assert mock_sentry.sentry_client.captureMessage.call_count == 0