"""Heartbeat Test.""" import json from unittest.mock import MagicMock from project_manager.constant import error_const from project_manager.models import persister def _raise_exception_mock(): """Raise an exception for use in mocking.""" raise def test_heartbeat_success(client): """Assert that heartbeat returns expected json and status code.""" res = client.get('/hello') res_json = json.loads(res.data.decode('utf-8')) assert res_json == {'status': 'ok'} assert res.status_code == 200 def test_db_heartbeat_success(client): """Assert that database heartbeat returns expected json and status code.""" res = client.get('/hello_db') res_json = json.loads(res.data.decode('utf-8')) assert res_json == {'status': 'ok'} assert res.status_code == 200 def test_db_heartbeat_failure(client, monkeypatch): """Assert that db heartbeat returns status of 500 if service is down.""" monkeypatch.setattr( persister, 'check_db_connectivity', MagicMock(side_effect=_raise_exception_mock)) server_response = client.get('/hello_db') response_json = json.loads(server_response.data.decode('utf-8')) expected_response = { 'code': error_const.ERROR_CODE_INTERNAL_ERROR, 'message': error_const.ERROR_MSG_INTERNAL_SERVER } assert response_json == expected_response assert server_response.status_code == 500 def test_resource_not_found(client): """Assert that 404's return JSON when visiting routes that don't exist.""" server_response = client.get('/some_route_that_doesnt_exist') response_json = json.loads(server_response.data.decode('utf-8')) expected_response = { 'code': error_const.ERROR_CODE_NOT_FOUND, 'message': error_const.ERROR_MSG_NOT_FOUND } assert response_json == expected_response assert server_response.status_code == 404