From 742268fe09befad44030168595a61b7704e3802e Mon Sep 17 00:00:00 2001 From: HoverHell Date: Tue, 12 Jun 2018 19:44:30 +0300 Subject: [PATCH 1/3] Request headers and cookies parameter parsing fix --- openapi_core/validation/request/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi_core/validation/request/models.py b/openapi_core/validation/request/models.py index e7d57cf..55d4887 100644 --- a/openapi_core/validation/request/models.py +++ b/openapi_core/validation/request/models.py @@ -6,7 +6,7 @@ from openapi_core.validation.models import BaseValidationResult class RequestParameters(dict): - valid_locations = ['path', 'query', 'headers', 'cookies'] + valid_locations = ['path', 'query', 'header', 'cookie'] def __getitem__(self, location): self.validate_location(location) From f8e977cc17f7e031999f5e51fff4a90fc586374d Mon Sep 17 00:00:00 2001 From: Artur Maciag Date: Mon, 9 Jul 2018 12:10:05 +0100 Subject: [PATCH 2/3] Parameters tests --- openapi_core/wrappers/flask.py | 4 +- tests/integration/data/v3.0/petstore.yaml | 13 ++ tests/integration/test_petstore.py | 171 +++++++++++++++++- tests/integration/test_validators.py | 48 ++++- tests/integration/test_wrappers.py | 12 +- .../{test_paramters.py => test_parameters.py} | 0 6 files changed, 231 insertions(+), 17 deletions(-) rename tests/unit/schema/{test_paramters.py => test_parameters.py} (100%) diff --git a/openapi_core/wrappers/flask.py b/openapi_core/wrappers/flask.py index 84bf093..ab91665 100644 --- a/openapi_core/wrappers/flask.py +++ b/openapi_core/wrappers/flask.py @@ -31,8 +31,8 @@ class FlaskOpenAPIRequest(BaseOpenAPIRequest): return { 'path': self.request.view_args, 'query': self.request.args, - 'headers': self.request.headers, - 'cookies': self.request.cookies, + 'header': self.request.headers, + 'cookie': self.request.cookies, } @property diff --git a/tests/integration/data/v3.0/petstore.yaml b/tests/integration/data/v3.0/petstore.yaml index 4f6c807..572d5fd 100644 --- a/tests/integration/data/v3.0/petstore.yaml +++ b/tests/integration/data/v3.0/petstore.yaml @@ -74,6 +74,19 @@ paths: operationId: createPets tags: - pets + parameters: + - name: api_key + in: header + schema: + type: integer + format: int32 + required: true + - name: user + in: cookie + schema: + type: integer + format: int32 + required: true requestBody: required: true content: diff --git a/tests/integration/test_petstore.py b/tests/integration/test_petstore.py index 1b2aa77..8b1c54f 100644 --- a/tests/integration/test_petstore.py +++ b/tests/integration/test_petstore.py @@ -392,15 +392,29 @@ class TestPetstore(object): } } data = json.dumps(data_json) + headers = { + 'api_key': '12345', + } + cookies = { + 'user': '123', + } request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, + headers=headers, cookies=cookies, ) parameters = request.get_parameters(spec) - assert parameters == {} + assert parameters == { + 'header': { + 'api_key': 12345, + }, + 'cookie': { + 'user': 123, + }, + } body = request.get_body(spec) @@ -438,15 +452,29 @@ class TestPetstore(object): } } data = json.dumps(data_json) + headers = { + 'api_key': '12345', + } + cookies = { + 'user': '123', + } request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, + headers=headers, cookies=cookies, ) parameters = request.get_parameters(spec) - assert parameters == {} + assert parameters == { + 'header': { + 'api_key': 12345, + }, + 'cookie': { + 'user': 123, + }, + } body = request.get_body(spec) @@ -484,15 +512,29 @@ class TestPetstore(object): } } data = json.dumps(data_json) + headers = { + 'api_key': '12345', + } + cookies = { + 'user': '123', + } request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, + headers=headers, cookies=cookies, ) parameters = request.get_parameters(spec) - assert parameters == {} + assert parameters == { + 'header': { + 'api_key': 12345, + }, + 'cookie': { + 'user': 123, + }, + } body = request.get_body(spec) @@ -518,15 +560,29 @@ class TestPetstore(object): 'alias': alias, } data = json.dumps(data_json) + headers = { + 'api_key': '12345', + } + cookies = { + 'user': '123', + } request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, + headers=headers, cookies=cookies, ) parameters = request.get_parameters(spec) - assert parameters == {} + assert parameters == { + 'header': { + 'api_key': 12345, + }, + 'cookie': { + 'user': 123, + }, + } with pytest.raises(NoOneOfSchema): request.get_body(spec) @@ -543,15 +599,29 @@ class TestPetstore(object): } } data = json.dumps(data_json) + headers = { + 'api_key': '12345', + } + cookies = { + 'user': '123', + } request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, + headers=headers, cookies=cookies, ) parameters = request.get_parameters(spec) - assert parameters == {} + assert parameters == { + 'header': { + 'api_key': 12345, + }, + 'cookie': { + 'user': 123, + }, + } body = request.get_body(spec) @@ -570,19 +640,101 @@ class TestPetstore(object): 'tag': 'cats', } data = json.dumps(data_json) + headers = { + 'api_key': '12345', + } + cookies = { + 'user': '123', + } request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, mimetype='text/html', + headers=headers, cookies=cookies, ) parameters = request.get_parameters(spec) - assert parameters == {} + assert parameters == { + 'header': { + 'api_key': 12345, + }, + 'cookie': { + 'user': 123, + }, + } with pytest.raises(InvalidContentType): request.get_body(spec) + def test_post_pets_missing_cookie(self, spec, spec_dict): + host_url = 'http://petstore.swagger.io/v1' + path_pattern = '/v1/pets' + pet_name = 'Cat' + pet_healthy = True + data_json = { + 'name': pet_name, + 'ears': { + 'healthy': pet_healthy, + } + } + data = json.dumps(data_json) + headers = { + 'api_key': '12345', + } + + request = MockRequest( + host_url, 'POST', '/pets', + path_pattern=path_pattern, data=data, + headers=headers, + ) + + with pytest.raises(MissingRequiredParameter): + request.get_parameters(spec) + + body = request.get_body(spec) + + schemas = spec_dict['components']['schemas'] + pet_model = schemas['PetCreate']['x-model'] + assert body.__class__.__name__ == pet_model + assert body.name == pet_name + assert not hasattr(body, 'tag') + assert not hasattr(body, 'address') + + def test_post_pets_missing_header(self, spec, spec_dict): + host_url = 'http://petstore.swagger.io/v1' + path_pattern = '/v1/pets' + pet_name = 'Cat' + pet_healthy = True + data_json = { + 'name': pet_name, + 'ears': { + 'healthy': pet_healthy, + } + } + data = json.dumps(data_json) + cookies = { + 'user': '123', + } + + request = MockRequest( + host_url, 'POST', '/pets', + path_pattern=path_pattern, data=data, + cookies=cookies, + ) + + with pytest.raises(MissingRequiredParameter): + request.get_parameters(spec) + + body = request.get_body(spec) + + schemas = spec_dict['components']['schemas'] + pet_model = schemas['PetCreate']['x-model'] + assert body.__class__.__name__ == pet_model + assert body.name == pet_name + assert not hasattr(body, 'tag') + assert not hasattr(body, 'address') + def test_post_pets_raises_invalid_server_error(self, spec): host_url = 'http://flowerstore.swagger.io/v1' path_pattern = '/v1/pets' @@ -591,10 +743,17 @@ class TestPetstore(object): 'tag': 'cats', } data = json.dumps(data_json) + headers = { + 'api_key': '12345', + } + cookies = { + 'user': '123', + } request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, mimetype='text/html', + headers=headers, cookies=cookies, ) with pytest.raises(InvalidServer): diff --git a/tests/integration/test_validators.py b/tests/integration/test_validators.py index d5e6aae..43c185a 100644 --- a/tests/integration/test_validators.py +++ b/tests/integration/test_validators.py @@ -86,9 +86,16 @@ class TestRequestValidator(object): } def test_missing_body(self, validator): + headers = { + 'api_key': '12345', + } + cookies = { + 'user': '123', + } request = MockRequest( self.host_url, 'post', '/v1/pets', path_pattern='/v1/pets', + headers=headers, cookies=cookies, ) result = validator.validate(request) @@ -96,12 +103,26 @@ class TestRequestValidator(object): assert len(result.errors) == 1 assert type(result.errors[0]) == MissingRequestBody assert result.body is None - assert result.parameters == {} + assert result.parameters == { + 'header': { + 'api_key': 12345, + }, + 'cookie': { + 'user': 123, + }, + } def test_invalid_content_type(self, validator): + headers = { + 'api_key': '12345', + } + cookies = { + 'user': '123', + } request = MockRequest( self.host_url, 'post', '/v1/pets', path_pattern='/v1/pets', mimetype='text/csv', + headers=headers, cookies=cookies, ) result = validator.validate(request) @@ -109,7 +130,14 @@ class TestRequestValidator(object): assert len(result.errors) == 1 assert type(result.errors[0]) == InvalidContentType assert result.body is None - assert result.parameters == {} + assert result.parameters == { + 'header': { + 'api_key': 12345, + }, + 'cookie': { + 'user': 123, + }, + } def test_post_pets(self, validator, spec_dict): pet_name = 'Cat' @@ -129,15 +157,29 @@ class TestRequestValidator(object): } } data = json.dumps(data_json) + headers = { + 'api_key': '12345', + } + cookies = { + 'user': '123', + } request = MockRequest( self.host_url, 'post', '/v1/pets', path_pattern='/v1/pets', data=data, + headers=headers, cookies=cookies, ) result = validator.validate(request) assert result.errors == [] - assert result.parameters == {} + assert result.parameters == { + 'header': { + 'api_key': 12345, + }, + 'cookie': { + 'user': 123, + }, + } schemas = spec_dict['components']['schemas'] pet_model = schemas['PetCreate']['x-model'] diff --git a/tests/integration/test_wrappers.py b/tests/integration/test_wrappers.py index 29a357d..ff0c157 100644 --- a/tests/integration/test_wrappers.py +++ b/tests/integration/test_wrappers.py @@ -62,8 +62,8 @@ class TestFlaskOpenAPIRequest(object): assert openapi_request.parameters == { 'path': path, 'query': query, - 'headers': headers, - 'cookies': cookies, + 'header': headers, + 'cookie': cookies, } assert openapi_request.host_url == request.host_url assert openapi_request.path == request.path @@ -87,8 +87,8 @@ class TestFlaskOpenAPIRequest(object): assert openapi_request.parameters == { 'path': path, 'query': query, - 'headers': headers, - 'cookies': cookies, + 'header': headers, + 'cookie': cookies, } assert openapi_request.host_url == request.host_url assert openapi_request.path == request.path @@ -109,8 +109,8 @@ class TestFlaskOpenAPIRequest(object): assert openapi_request.parameters == { 'path': path, 'query': query, - 'headers': headers, - 'cookies': cookies, + 'header': headers, + 'cookie': cookies, } assert openapi_request.host_url == request.host_url assert openapi_request.path == request.path diff --git a/tests/unit/schema/test_paramters.py b/tests/unit/schema/test_parameters.py similarity index 100% rename from tests/unit/schema/test_paramters.py rename to tests/unit/schema/test_parameters.py From 2487d0e5c1c8d61d3519c7f2ed5e5f227e36ab27 Mon Sep 17 00:00:00 2001 From: p1c2u Date: Mon, 13 Aug 2018 20:36:21 +0100 Subject: [PATCH 3/3] Dev requirements freeze --- requirements_dev.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/requirements_dev.txt b/requirements_dev.txt index 2326086..697427b 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -1,6 +1,6 @@ -mock -pytest +mock==2.0.0 +pytest==3.5.0 pytest-pep8 pytest-flakes -pytest-cov +pytest-cov==2.5.1 flask \ No newline at end of file