Skip to content

Commit

Permalink
Fixed checking request body type (#139)
Browse files Browse the repository at this point in the history
* Fixed checking request body type

* Updated version
  • Loading branch information
andrey-berenda authored Sep 9, 2019
1 parent 4b11375 commit b906376
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
5 changes: 5 additions & 0 deletions tests/controllers/controller_with_request_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ class ControllerWithRequestData:
@winter.route_post('{?query}')
def method(self, query: typing.Optional[str], data: Data) -> Data:
return data

@winter.request_body('data')
@winter.route_post('many/')
def many_method(self, data: typing.List[Data]) -> Data:
return data
35 changes: 33 additions & 2 deletions tests/http/test_request_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,37 @@ def test_request_body():
assert response.json() == expected_data


def test_request_body_as_list():
client = APIClient()
user = AuthorizedUser()
client.force_authenticate(user)

data = [{
'id': 1,
'name': 'test name',
'is_god': True,
'status': 'active',
'items': [1, 2],
}]
expected_data = [{
'id': 1,
'with_default': 5,
'name': 'test name',
'is_god': True,
'status': 'active',
'optional_status': None,
'items': [1, 2],
'optional_items': None,
}]
data = json.dumps(data)

# Act
response = client.post('/with-request-data/many/', data=data, content_type='application/json')

assert response.status_code == HTTPStatus.OK
assert response.json() == expected_data


def test_request_body_with_errors():
client = APIClient()
user = AuthorizedUser()
Expand Down Expand Up @@ -67,15 +98,15 @@ def test_request_body_with_errors():
assert response.json() == expected_data


def test_with_argument_not_dataclass():
def test_with_argument_not_valid_annotation():
def method(argument: int):
return argument

annotation_decorator = request_body('argument')

with pytest.raises(AssertionError) as exception:
annotation_decorator(method)
assert exception.value.args == ('Argument should be dataclass in "method"',)
assert exception.value.args == ('Invalid request body type',)


def test_without_argument():
Expand Down
2 changes: 1 addition & 1 deletion winter/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.1.1'
__version__ = '2.2.0'
11 changes: 10 additions & 1 deletion winter/http/request_body_annotation.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import typing

import dataclasses

from ..core import annotate_method

ListType = type(typing.List)


@dataclasses.dataclass
class RequestBodyAnnotation:
argument_name: str


def check_request_body_type(argument_type):
if not dataclasses.is_dataclass(argument_type) and not isinstance(argument_type, ListType):
raise AssertionError('Invalid request body type')


def request_body(argument_name: str):

def wrapper(func_or_method):
Expand All @@ -17,6 +26,6 @@ def wrapper(func_or_method):
argument = method.get_argument(argument_name)
method_name = method.func.__name__
assert argument is not None, f'Not found argument "{argument_name}" in "{method_name}"'
assert dataclasses.is_dataclass(argument.type_), f'Argument should be dataclass in "{method_name}"'
check_request_body_type(argument.type_)
return method
return wrapper

0 comments on commit b906376

Please sign in to comment.