"""Test response module.""" import json from unittest.mock import patch from deliveryhistory import response def test_response_creation(): """Test the default response creation.""" resp = response.Response() assert not resp.message assert not resp.errors assert resp assert resp.status == 200 def test_response_creation_with_message(): """Test the creation of a response with a message.""" message = 'something' resp = response.Response(message=message) assert resp assert resp.message == message assert resp.status == 200 def test_response_creation_with_errors(): """Test the response creation with errors.""" error = 'something' resp = response.Response(errors=error) assert not resp.message assert not resp assert resp.errors == error assert resp.status == 400 # default def test_response_creation_with_status(): """Test the response creation with a defined status. Within the 200's, the response is considered valid. Above it's invalid. """ for status in range(200, 300): resp = response.Response(status=status) assert resp assert resp.status == status for status in range(300, 500): resp = response.Response(status=status) assert not resp assert resp.status == status def test_create_error_response(): """Test the creation of a fatal response.""" error = 'something' calls = [ (response.create_fatal_response, 500, response.ERROR_CODE_INTERNAL_ERROR), (response.create_not_found_response, 404, response.ERROR_CODE_NOT_FOUND)] for call, status, code in calls: resp = call() assert not resp assert resp.status == status resp = call(error) assert resp.errors.get('code') == code assert resp.errors.get('message') == error resp = response.create_error_response('code', error) assert resp.errors.get('code') == 'code' assert resp.errors.get('message') == error def test_flaskify_dict_response(): """Test flaskifying a response.""" resp = response.flaskify(response.Response(message=dict(key='value'))) assert resp.data == b'{"key": "value"}' assert resp.mimetype == 'application/json' assert resp.status_code == 200 def test_flaskify_list_response(): """Test flaskifying a response.""" resp = response.flaskify(response.Response(message=['value1', 'value2'])) assert resp.data == b'["value1", "value2"]' assert resp.mimetype == 'application/json' assert resp.status_code == 200 def test_flaskify_string_response(): """Test flaskifying a response.""" resp = response.flaskify(response.Response(message='value1')) assert resp.data == b'value1' assert resp.mimetype == 'text/plain' assert resp.status_code == 200 def test_flaskify_error_response(): """Flaskify an error response.""" resp = response.flaskify(response.create_fatal_response('Fatal Error')) assert resp.status_code == 500 data = json.loads(resp.data.decode('utf8')) assert data == {'message': 'Fatal Error', 'code': 'internal_error'} @patch('deliveryhistory.response.sentry') def test_send_to_sentry(mock_sentry): """Test sending an error message to Sentry.""" error_response = response.Response( message='response message', errors={'error_code': 'error_message'}, status=204) sentry_message = 'sentry message' # when sentry client exists, sentry message is sent mock_sentry.sentry_client.__bool__.return_value = True response.send_to_sentry(error_response, sentry_message) mock_sentry.sentry_client.captureMessage.assert_called_with( message=sentry_message, stack=True, extra={ 'message': error_response.message, 'errors': error_response.errors, 'status': error_response.status})