"""Tests for Handlers.""" import json from unittest.mock import MagicMock from unittest.mock import patch import application from oto import response from oto import status as http_status from product_film import handlers from product_film.constants import error from product_film.logic import track_crop @patch('product_film.handlers.g') def test_exception_handler(mock_g): """Verify exception_Handler returns 500 status code and json payload.""" message = ( 'The server encountered an internal error ' 'and was unable to complete your request.') mock_error = MagicMock() server_response = handlers.exception_handler(mock_error) mock_g.log.exception.assert_called_with(mock_error) # assert status code is 500 assert server_response.status_code == http_status.INTERNAL_ERROR # assert json payload response_message = json.loads(server_response.data.decode()) assert response_message['message'] == message assert response_message['code'] == response.error.ERROR_CODE_INTERNAL_ERROR def test_get_track_crop_info(mocker): """Verify get_track_crop_info.""" expected_result = dict( track_id=1, crop_left=10, crop_right=10, crop_top=10, crop_bottom=10, orchard_user_id=1) mocker.patch.object( track_crop, 'get_track_crop_info', return_value=response.Response(message=expected_result)) server_response = handlers.get_track_crop_info(1) assert server_response.status_code == http_status.OK def test_get_track_crop_info_not_found(mocker): """Verify get_track_crop_info for not found response.""" mocker.patch.object( handlers, 'get_track_crop_info', return_value=response.create_not_found_response()) server_response = handlers.get_track_crop_info(1) assert server_response.status == http_status.NOT_FOUND def test_get_track_crop_info_bad_request(mocker): """Verify get_track_crop_info for bad request.""" mocker.patch.object( track_crop, 'get_track_crop_info', return_value=response.create_error_response( code=error.BAD_REQUEST_CODE, message='Bad Request')) server_response = handlers.get_track_crop_info(None) assert server_response.status_code == http_status.BAD_REQUEST def test_add_track_crop_info(mocker): """Verify add_track_crop_info.""" expected_result = dict( track_id=1, crop_left=11, crop_right=1, crop_top=20, crop_bottom=10, orchard_user_id=1) mock_track = mocker.patch.object( track_crop, 'add_track_crop_info', return_value=response.Response(message=expected_result)) input_data = dict( left=11, right=1, top=20, bottom=10, orchard_user_id=1) with application.app.test_request_context( data=json.dumps(input_data), content_type='application/json'): server_response = handlers.add_track_crop_info(1) assert server_response.status_code == http_status.OK mock_track.assert_called_with(input_data, 1) def test_add_track_crop_info_bad_request(mocker): """Verify add_track_crop_info for bad request.""" mocker.patch.object( track_crop, 'add_track_crop_info', return_value=response.create_error_response( code=error.BAD_REQUEST_CODE, message='Bad Request')) input_data = dict( left=11, right=12, top=13, bottom=14, orchard_user_id=1) with application.app.test_request_context( data=json.dumps(input_data), content_type='application/json'): server_response = handlers.add_track_crop_info(None) assert server_response.status_code == http_status.BAD_REQUEST