mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-23 18:59:56 +00:00
make apispec dependency optional
This commit is contained in:
160
tests/ahriman/web/apispec/test_decorators.py
Normal file
160
tests/ahriman/web/apispec/test_decorators.py
Normal file
@ -0,0 +1,160 @@
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from ahriman.models.user_access import UserAccess
|
||||
from ahriman.web.apispec import decorators
|
||||
from ahriman.web.apispec.decorators import _response_schema, apidocs
|
||||
from ahriman.web.schemas import LoginSchema
|
||||
|
||||
|
||||
def test_response_schema() -> None:
|
||||
"""
|
||||
must generate response schema
|
||||
"""
|
||||
schema = _response_schema(None)
|
||||
assert schema.pop(204)
|
||||
assert schema.pop(401)
|
||||
assert schema.pop(403)
|
||||
assert schema.pop(500)
|
||||
|
||||
|
||||
def test_response_schema_no_403() -> None:
|
||||
"""
|
||||
must generate response schema without 403 error
|
||||
"""
|
||||
schema = _response_schema(None, error_403_enabled=False)
|
||||
assert 403 not in schema
|
||||
|
||||
|
||||
def test_response_schema_400() -> None:
|
||||
"""
|
||||
must generate response schema with 400 error
|
||||
"""
|
||||
schema = _response_schema(None, error_400_enabled=True)
|
||||
assert schema.pop(400)
|
||||
|
||||
|
||||
def test_response_schema_404() -> None:
|
||||
"""
|
||||
must generate response schema with 404 error
|
||||
"""
|
||||
schema = _response_schema(None, error_404_description="description")
|
||||
assert schema.pop(404)
|
||||
|
||||
|
||||
def test_response_schema_200() -> None:
|
||||
"""
|
||||
must generate response schema with 200 response
|
||||
"""
|
||||
schema = _response_schema(LoginSchema)
|
||||
response = schema.pop(200)
|
||||
assert response["schema"] == LoginSchema
|
||||
assert 204 not in schema
|
||||
|
||||
|
||||
def test_response_schema_code() -> None:
|
||||
"""
|
||||
must override status code
|
||||
"""
|
||||
schema = _response_schema(None, code=302)
|
||||
assert schema.pop(302)
|
||||
assert 204 not in schema
|
||||
|
||||
|
||||
def test_apidocs() -> None:
|
||||
"""
|
||||
must return decorated function
|
||||
"""
|
||||
annotated = apidocs(
|
||||
tags=["tags"],
|
||||
summary="summary",
|
||||
description="description",
|
||||
permission=UserAccess.Unauthorized,
|
||||
)(MagicMock())
|
||||
assert annotated.__apispec__
|
||||
|
||||
|
||||
def test_apidocs_authorization() -> None:
|
||||
"""
|
||||
must return decorated function with authorization details
|
||||
"""
|
||||
annotated = apidocs(
|
||||
tags=["tags"],
|
||||
summary="summary",
|
||||
description="description",
|
||||
permission=UserAccess.Full,
|
||||
)(MagicMock())
|
||||
assert any(schema["put_into"] == "cookies" for schema in annotated.__schemas__)
|
||||
|
||||
|
||||
def test_apidocs_match() -> None:
|
||||
"""
|
||||
must return decorated function with match details
|
||||
"""
|
||||
annotated = apidocs(
|
||||
tags=["tags"],
|
||||
summary="summary",
|
||||
description="description",
|
||||
permission=UserAccess.Unauthorized,
|
||||
match_schema=LoginSchema,
|
||||
)(MagicMock())
|
||||
assert any(schema["put_into"] == "match_info" for schema in annotated.__schemas__)
|
||||
|
||||
|
||||
def test_apidocs_querystring() -> None:
|
||||
"""
|
||||
must return decorated function with query string details
|
||||
"""
|
||||
annotated = apidocs(
|
||||
tags=["tags"],
|
||||
summary="summary",
|
||||
description="description",
|
||||
permission=UserAccess.Unauthorized,
|
||||
query_schema=LoginSchema,
|
||||
)(MagicMock())
|
||||
assert any(schema["put_into"] == "querystring" for schema in annotated.__schemas__)
|
||||
|
||||
|
||||
def test_apidocs_json() -> None:
|
||||
"""
|
||||
must return decorated function with json details
|
||||
"""
|
||||
annotated = apidocs(
|
||||
tags=["tags"],
|
||||
summary="summary",
|
||||
description="description",
|
||||
permission=UserAccess.Unauthorized,
|
||||
body_schema=LoginSchema,
|
||||
)(MagicMock())
|
||||
assert any(schema["put_into"] == "json" for schema in annotated.__schemas__)
|
||||
|
||||
|
||||
def test_apidocs_form() -> None:
|
||||
"""
|
||||
must return decorated function with generic body details
|
||||
"""
|
||||
annotated = apidocs(
|
||||
tags=["tags"],
|
||||
summary="summary",
|
||||
description="description",
|
||||
permission=UserAccess.Unauthorized,
|
||||
body_schema=LoginSchema,
|
||||
body_location="form",
|
||||
)(MagicMock())
|
||||
assert any(schema["put_into"] == "form" for schema in annotated.__schemas__)
|
||||
|
||||
|
||||
def test_apidocs_import_error(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must return same function if no apispec module available
|
||||
"""
|
||||
mocker.patch.object(decorators, "aiohttp_apispec", None)
|
||||
mock = MagicMock()
|
||||
|
||||
annotated = apidocs(
|
||||
tags=["tags"],
|
||||
summary="summary",
|
||||
description="description",
|
||||
permission=UserAccess.Unauthorized,
|
||||
)(mock)
|
||||
assert annotated == mock
|
24
tests/ahriman/web/apispec/test_init.py
Normal file
24
tests/ahriman/web/apispec/test_init.py
Normal file
@ -0,0 +1,24 @@
|
||||
import importlib
|
||||
import sys
|
||||
|
||||
from pytest_mock import MockerFixture
|
||||
from ahriman.web import apispec
|
||||
|
||||
|
||||
def test_import_apispec() -> None:
|
||||
"""
|
||||
must correctly import apispec
|
||||
"""
|
||||
assert apispec.aiohttp_apispec
|
||||
|
||||
|
||||
def test_import_apispec_missing(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must correctly process missing module
|
||||
"""
|
||||
mocker.patch.dict(sys.modules, {"aiohttp_apispec": None})
|
||||
importlib.reload(apispec)
|
||||
|
||||
assert apispec.aiohttp_apispec is None
|
||||
assert apispec.Schema is object
|
||||
assert apispec.fields("arg", kwargs=42)
|
@ -4,7 +4,8 @@ from aiohttp.web import Application
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman import __version__
|
||||
from ahriman.web.apispec import _info, _security, _servers, setup_apispec
|
||||
from ahriman.web.apispec import spec
|
||||
from ahriman.web.apispec.spec import _info, _security, _servers, setup_apispec
|
||||
from ahriman.web.keys import ConfigurationKey
|
||||
|
||||
|
||||
@ -47,7 +48,7 @@ def test_setup_apispec(application: Application, mocker: MockerFixture) -> None:
|
||||
must set api specification
|
||||
"""
|
||||
apispec_mock = mocker.patch("aiohttp_apispec.setup_aiohttp_apispec")
|
||||
setup_apispec(application)
|
||||
assert setup_apispec(application)
|
||||
apispec_mock.assert_called_once_with(
|
||||
application,
|
||||
url="/api-docs/swagger.json",
|
||||
@ -56,3 +57,11 @@ def test_setup_apispec(application: Application, mocker: MockerFixture) -> None:
|
||||
servers=pytest.helpers.anyvar(int),
|
||||
security=pytest.helpers.anyvar(int),
|
||||
)
|
||||
|
||||
|
||||
def test_setup_apispec_import_error(application: Application, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must return none if apispec is not available
|
||||
"""
|
||||
mocker.patch.object(spec, "aiohttp_apispec", None)
|
||||
assert setup_apispec(application) is None
|
@ -90,6 +90,8 @@ def schema_request(handler: Callable[..., Awaitable[Any]], *, location: str = "j
|
||||
Schema: request schema as set by the decorators
|
||||
"""
|
||||
schemas: list[dict[str, Any]] = handler.__schemas__ # type: ignore[attr-defined]
|
||||
help(handler)
|
||||
print(schemas)
|
||||
return next(schema["schema"] for schema in schemas if schema["put_into"] == location)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user