always return json in responses

This commit is contained in:
2021-10-20 02:12:39 +03:00
parent e10e362dae
commit eb16ef12f3
8 changed files with 62 additions and 36 deletions

View File

@ -1,13 +1,24 @@
import json
import logging
import pytest
from aiohttp.web_exceptions import HTTPBadRequest, HTTPNoContent
from aiohttp.web_exceptions import HTTPBadRequest, HTTPInternalServerError, HTTPNoContent
from pytest_mock import MockerFixture
from typing import Any
from unittest.mock import AsyncMock
from ahriman.web.middlewares.exception_handler import exception_handler
def _extract_body(response: Any) -> Any:
"""
extract json body from given object
:param response: response (any actually) object
:return: body key from the object converted to json
"""
return json.loads(getattr(response, "body"))
async def test_exception_handler(mocker: MockerFixture) -> None:
"""
must pass success response
@ -23,7 +34,7 @@ async def test_exception_handler(mocker: MockerFixture) -> None:
async def test_exception_handler_success(mocker: MockerFixture) -> None:
"""
must pass client exception
must pass 2xx and 3xx codes
"""
request = pytest.helpers.request("", "", "")
request_handler = AsyncMock(side_effect=HTTPNoContent())
@ -37,27 +48,41 @@ async def test_exception_handler_success(mocker: MockerFixture) -> None:
async def test_exception_handler_client_error(mocker: MockerFixture) -> None:
"""
must pass client exception
must handle client exception
"""
request = pytest.helpers.request("", "", "")
request_handler = AsyncMock(side_effect=HTTPBadRequest())
logging_mock = mocker.patch("logging.Logger.exception")
handler = exception_handler(logging.getLogger())
with pytest.raises(HTTPBadRequest):
await handler(request, request_handler)
response = await handler(request, request_handler)
assert _extract_body(response) == {"error": HTTPBadRequest().reason}
logging_mock.assert_not_called()
async def test_exception_handler_server_error(mocker: MockerFixture) -> None:
"""
must log server exception and re-raise it
must handle server exception
"""
request = pytest.helpers.request("", "", "")
request_handler = AsyncMock(side_effect=Exception())
request_handler = AsyncMock(side_effect=HTTPInternalServerError())
logging_mock = mocker.patch("logging.Logger.exception")
handler = exception_handler(logging.getLogger())
with pytest.raises(Exception):
await handler(request, request_handler)
response = await handler(request, request_handler)
assert _extract_body(response) == {"error": HTTPInternalServerError().reason}
logging_mock.assert_called_once()
async def test_exception_handler_unknown_error(mocker: MockerFixture) -> None:
"""
must log server exception and re-raise it
"""
request = pytest.helpers.request("", "", "")
request_handler = AsyncMock(side_effect=Exception("An error"))
logging_mock = mocker.patch("logging.Logger.exception")
handler = exception_handler(logging.getLogger())
response = await handler(request, request_handler)
assert _extract_body(response) == {"error": "An error"}
logging_mock.assert_called_once()