from unittest.mock import patch from ytownership.utils 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 @patch('ytownership.utils.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})