Add group parameter feature

This commit is contained in:
Vincent Maillol
2021-09-26 19:08:39 +02:00
parent 4a49d3b53d
commit 799080bbd0
11 changed files with 472 additions and 34 deletions

View File

@@ -5,6 +5,7 @@ from enum import Enum
from aiohttp import web
from aiohttp_pydantic import PydanticView
from aiohttp_pydantic.injectors import Group
class JSONEncoder(json.JSONEncoder):
@@ -32,6 +33,31 @@ class ViewWithEnumType(PydanticView):
return web.json_response({"format": format}, dumps=JSONEncoder().encode)
class Signature(Group):
signature_expired: datetime
signature_scope: str = "read"
@property
def expired(self) -> datetime:
return self.signature_expired
@property
def scope(self) -> str:
return self.signature_scope
class ArticleViewWithSignatureGroup(PydanticView):
async def get(
self,
*,
signature: Signature,
):
return web.json_response(
{"expired": signature.expired, "scope": signature.scope},
dumps=JSONEncoder().encode,
)
async def test_get_article_without_required_header_should_return_an_error_message(
aiohttp_client, loop
):
@@ -134,3 +160,21 @@ async def test_correct_value_to_header_defined_with_str_enum(aiohttp_client, loo
assert await resp.json() == {"format": "UMT"}
assert resp.status == 200
assert resp.content_type == "application/json"
async def test_with_signature_group(aiohttp_client, loop):
app = web.Application()
app.router.add_view("/article", ArticleViewWithSignatureGroup)
client = await aiohttp_client(app)
resp = await client.get(
"/article",
headers={
"signature_expired": "2020-10-04T18:01:00",
"signature.scope": "write",
},
)
assert await resp.json() == {"expired": "2020-10-04T18:01:00", "scope": "read"}
assert resp.status == 200
assert resp.content_type == "application/json"