Merge branch 'master' into flake8

This commit is contained in:
A 2018-08-13 20:37:29 +01:00 committed by GitHub
commit 2be6cd2776
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 236 additions and 22 deletions

View file

@ -6,7 +6,7 @@ from openapi_core.validation.models import BaseValidationResult
class RequestParameters(dict): class RequestParameters(dict):
valid_locations = ['path', 'query', 'headers', 'cookies'] valid_locations = ['path', 'query', 'header', 'cookie']
def __getitem__(self, location): def __getitem__(self, location):
self.validate_location(location) self.validate_location(location)

View file

@ -31,8 +31,8 @@ class FlaskOpenAPIRequest(BaseOpenAPIRequest):
return { return {
'path': self.request.view_args, 'path': self.request.view_args,
'query': self.request.args, 'query': self.request.args,
'headers': self.request.headers, 'header': self.request.headers,
'cookies': self.request.cookies, 'cookie': self.request.cookies,
} }
@property @property

View file

@ -1,5 +1,5 @@
mock mock==2.0.0
pytest pytest==3.5.0
pytest-flake8 pytest-flake8
pytest-cov pytest-cov==2.5.1
flask flask

View file

@ -74,6 +74,19 @@ paths:
operationId: createPets operationId: createPets
tags: tags:
- pets - 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: requestBody:
required: true required: true
content: content:

View file

@ -397,15 +397,29 @@ class TestPetstore(object):
} }
} }
data = json.dumps(data_json) data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest( request = MockRequest(
host_url, 'POST', '/pets', host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data, path_pattern=path_pattern, data=data,
headers=headers, cookies=cookies,
) )
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == {} assert parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
body = request.get_body(spec) body = request.get_body(spec)
@ -443,15 +457,29 @@ class TestPetstore(object):
} }
} }
data = json.dumps(data_json) data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest( request = MockRequest(
host_url, 'POST', '/pets', host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data, path_pattern=path_pattern, data=data,
headers=headers, cookies=cookies,
) )
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == {} assert parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
body = request.get_body(spec) body = request.get_body(spec)
@ -489,15 +517,29 @@ class TestPetstore(object):
} }
} }
data = json.dumps(data_json) data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest( request = MockRequest(
host_url, 'POST', '/pets', host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data, path_pattern=path_pattern, data=data,
headers=headers, cookies=cookies,
) )
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == {} assert parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
body = request.get_body(spec) body = request.get_body(spec)
@ -523,15 +565,29 @@ class TestPetstore(object):
'alias': alias, 'alias': alias,
} }
data = json.dumps(data_json) data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest( request = MockRequest(
host_url, 'POST', '/pets', host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data, path_pattern=path_pattern, data=data,
headers=headers, cookies=cookies,
) )
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == {} assert parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
with pytest.raises(NoOneOfSchema): with pytest.raises(NoOneOfSchema):
request.get_body(spec) request.get_body(spec)
@ -548,15 +604,29 @@ class TestPetstore(object):
} }
} }
data = json.dumps(data_json) data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest( request = MockRequest(
host_url, 'POST', '/pets', host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data, path_pattern=path_pattern, data=data,
headers=headers, cookies=cookies,
) )
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == {} assert parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
body = request.get_body(spec) body = request.get_body(spec)
@ -575,19 +645,101 @@ class TestPetstore(object):
'tag': 'cats', 'tag': 'cats',
} }
data = json.dumps(data_json) data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest( request = MockRequest(
host_url, 'POST', '/pets', host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data, mimetype='text/html', path_pattern=path_pattern, data=data, mimetype='text/html',
headers=headers, cookies=cookies,
) )
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == {} assert parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
with pytest.raises(InvalidContentType): with pytest.raises(InvalidContentType):
request.get_body(spec) 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): def test_post_pets_raises_invalid_server_error(self, spec):
host_url = 'http://flowerstore.swagger.io/v1' host_url = 'http://flowerstore.swagger.io/v1'
path_pattern = '/v1/pets' path_pattern = '/v1/pets'
@ -596,10 +748,17 @@ class TestPetstore(object):
'tag': 'cats', 'tag': 'cats',
} }
data = json.dumps(data_json) data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest( request = MockRequest(
host_url, 'POST', '/pets', host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data, mimetype='text/html', path_pattern=path_pattern, data=data, mimetype='text/html',
headers=headers, cookies=cookies,
) )
with pytest.raises(InvalidServer): with pytest.raises(InvalidServer):

View file

@ -86,9 +86,16 @@ class TestRequestValidator(object):
} }
def test_missing_body(self, validator): def test_missing_body(self, validator):
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest( request = MockRequest(
self.host_url, 'post', '/v1/pets', self.host_url, 'post', '/v1/pets',
path_pattern='/v1/pets', path_pattern='/v1/pets',
headers=headers, cookies=cookies,
) )
result = validator.validate(request) result = validator.validate(request)
@ -96,12 +103,26 @@ class TestRequestValidator(object):
assert len(result.errors) == 1 assert len(result.errors) == 1
assert type(result.errors[0]) == MissingRequestBody assert type(result.errors[0]) == MissingRequestBody
assert result.body is None 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): def test_invalid_content_type(self, validator):
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest( request = MockRequest(
self.host_url, 'post', '/v1/pets', self.host_url, 'post', '/v1/pets',
path_pattern='/v1/pets', mimetype='text/csv', path_pattern='/v1/pets', mimetype='text/csv',
headers=headers, cookies=cookies,
) )
result = validator.validate(request) result = validator.validate(request)
@ -109,7 +130,14 @@ class TestRequestValidator(object):
assert len(result.errors) == 1 assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidContentType assert type(result.errors[0]) == InvalidContentType
assert result.body is None 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): def test_post_pets(self, validator, spec_dict):
pet_name = 'Cat' pet_name = 'Cat'
@ -129,15 +157,29 @@ class TestRequestValidator(object):
} }
} }
data = json.dumps(data_json) data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest( request = MockRequest(
self.host_url, 'post', '/v1/pets', self.host_url, 'post', '/v1/pets',
path_pattern='/v1/pets', data=data, path_pattern='/v1/pets', data=data,
headers=headers, cookies=cookies,
) )
result = validator.validate(request) result = validator.validate(request)
assert result.errors == [] assert result.errors == []
assert result.parameters == {} assert result.parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
schemas = spec_dict['components']['schemas'] schemas = spec_dict['components']['schemas']
pet_model = schemas['PetCreate']['x-model'] pet_model = schemas['PetCreate']['x-model']

View file

@ -62,8 +62,8 @@ class TestFlaskOpenAPIRequest(object):
assert openapi_request.parameters == { assert openapi_request.parameters == {
'path': path, 'path': path,
'query': query, 'query': query,
'headers': headers, 'header': headers,
'cookies': cookies, 'cookie': cookies,
} }
assert openapi_request.host_url == request.host_url assert openapi_request.host_url == request.host_url
assert openapi_request.path == request.path assert openapi_request.path == request.path
@ -87,8 +87,8 @@ class TestFlaskOpenAPIRequest(object):
assert openapi_request.parameters == { assert openapi_request.parameters == {
'path': path, 'path': path,
'query': query, 'query': query,
'headers': headers, 'header': headers,
'cookies': cookies, 'cookie': cookies,
} }
assert openapi_request.host_url == request.host_url assert openapi_request.host_url == request.host_url
assert openapi_request.path == request.path assert openapi_request.path == request.path
@ -109,8 +109,8 @@ class TestFlaskOpenAPIRequest(object):
assert openapi_request.parameters == { assert openapi_request.parameters == {
'path': path, 'path': path,
'query': query, 'query': query,
'headers': headers, 'header': headers,
'cookies': cookies, 'cookie': cookies,
} }
assert openapi_request.host_url == request.host_url assert openapi_request.host_url == request.host_url
assert openapi_request.path == request.path assert openapi_request.path == request.path