"""Tests for exception handler.""" import json from unittest.mock import MagicMock from unittest.mock import patch import application from oto import error as oto_error from oto import status as oto_status import pytest from availability import config from availability import handlers @pytest.fixture def app_context(): """Yield app context.""" with application.app.app_context(): yield @patch('availability.handlers.g') def test_exception_handler(mock_g, app_context): """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 == 500 # assert json payload response_message = json.loads(server_response.data.decode()) assert response_message['message'] == message assert response_message['code'] == oto_error.ERROR_CODE_INTERNAL_ERROR def test_health(client): """Assert health check endpoint works.""" response = client.get(config.HEALTH_CHECK) assert response.status_code == oto_status.OK assert json.loads(response.data.decode()) == {'status': 'ok'}