"""Tests for the response module.""" import json from unittest.mock import Mock from api 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_flaskify_dict_response(): """Test flaskifying a dict 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 list 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_tuple_response(): """Test flaskifying a tuple 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_create_status_ok_response(monkeypatch): """Test the creation of a status ok response.""" expected_message = {'status': 'ok'} # Partial mocking of the response.Response class response_class_mock = Mock(wraps=response.Response) monkeypatch.setattr(response, 'Response', response_class_mock) resp = response.create_status_ok_response() # Asserts response_class_mock.assert_called_once_with(expected_message) assert resp.message == expected_message 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), (response.create_forbidden_response, 403, response.ERROR_CODE_FORBIDDEN_ERROR)] 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_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'}