openapi-core/tests/integration/validation/test_petstore.py

1140 lines
33 KiB
Python
Raw Normal View History

2017-09-21 11:51:37 +00:00
import json
import pytest
2019-03-02 20:44:01 +00:00
from datetime import datetime
2019-02-26 16:49:25 +00:00
from base64 import b64encode
2019-02-26 10:37:49 +00:00
from uuid import UUID
from six import text_type
2017-09-21 11:51:37 +00:00
2018-08-21 17:33:24 +00:00
from openapi_core.extensions.models.models import BaseModel
2018-04-18 10:39:03 +00:00
from openapi_core.schema.media_types.exceptions import (
InvalidContentType, InvalidMediaTypeValue,
2017-09-21 11:51:37 +00:00
)
2018-04-18 10:39:03 +00:00
from openapi_core.schema.parameters.exceptions import (
MissingRequiredParameter, InvalidParameterValue, EmptyParameterValue,
)
2019-03-22 01:51:47 +00:00
from openapi_core.schema.schemas.enums import SchemaType
2019-09-03 00:38:19 +00:00
from openapi_core.schema.schemas.exceptions import InvalidSchemaValue
2018-04-18 10:39:03 +00:00
from openapi_core.schema.servers.exceptions import InvalidServer
2019-10-19 12:01:56 +00:00
from openapi_core.shortcuts import (
create_spec, validate_parameters, validate_body,
)
from openapi_core.testing import MockRequest, MockResponse
from openapi_core.validation.request.datatypes import RequestParameters
2018-04-17 12:38:23 +00:00
from openapi_core.validation.request.validators import RequestValidator
from openapi_core.validation.response.validators import ResponseValidator
2017-09-21 11:51:37 +00:00
class TestPetstore(object):
2019-03-07 21:55:27 +00:00
api_key = '12345'
2019-02-26 16:49:25 +00:00
@property
def api_key_encoded(self):
2019-03-07 21:55:27 +00:00
api_key_bytes = self.api_key.encode('utf8')
api_key_bytes_enc = b64encode(api_key_bytes)
return text_type(api_key_bytes_enc, 'utf8')
2019-02-26 16:49:25 +00:00
2019-09-03 00:38:19 +00:00
@pytest.fixture
def spec_uri(self):
return "file://tests/integration/data/v3.0/petstore.yaml"
2017-09-21 11:51:37 +00:00
@pytest.fixture
def spec_dict(self, factory):
return factory.spec_from_file("data/v3.0/petstore.yaml")
@pytest.fixture
2019-09-03 00:38:19 +00:00
def spec(self, spec_dict, spec_uri):
return create_spec(spec_dict, spec_uri)
2017-09-21 11:51:37 +00:00
2017-11-06 16:50:00 +00:00
@pytest.fixture
def request_validator(self, spec):
return RequestValidator(spec)
@pytest.fixture
def response_validator(self, spec):
return ResponseValidator(spec)
def test_get_pets(self, spec, response_validator):
2017-09-25 11:22:55 +00:00
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
2017-09-21 11:51:37 +00:00
query_params = {
'limit': '20',
}
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-09-25 13:20:23 +00:00
host_url, 'GET', '/pets',
2017-09-25 11:22:55 +00:00
path_pattern=path_pattern, args=query_params,
)
2017-09-21 11:51:37 +00:00
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
body = validate_body(spec, request)
2017-09-21 11:51:37 +00:00
assert parameters == RequestParameters(
query={
2017-09-21 11:51:37 +00:00
'limit': 20,
'page': 1,
2017-10-19 11:28:28 +00:00
'search': '',
2017-09-21 11:51:37 +00:00
}
)
2017-09-25 13:20:23 +00:00
assert body is None
2017-09-21 11:51:37 +00:00
2017-11-06 16:50:00 +00:00
data_json = {
'data': [],
}
data = json.dumps(data_json)
response = MockResponse(data)
response_result = response_validator.validate(request, response)
2017-11-14 13:36:05 +00:00
assert response_result.errors == []
2018-08-21 17:33:24 +00:00
assert isinstance(response_result.data, BaseModel)
assert response_result.data.data == []
2017-11-14 13:36:05 +00:00
2019-03-22 01:51:47 +00:00
def test_get_pets_response(self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
query_params = {
'limit': '20',
}
request = MockRequest(
host_url, 'GET', '/pets',
path_pattern=path_pattern, args=query_params,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
body = validate_body(spec, request)
2019-03-22 01:51:47 +00:00
assert parameters == RequestParameters(
query={
2019-03-22 01:51:47 +00:00
'limit': 20,
'page': 1,
'search': '',
}
)
2019-03-22 01:51:47 +00:00
assert body is None
data_json = {
'data': [
{
'id': 1,
'name': 'Cat',
2019-09-03 00:38:19 +00:00
'ears': {
'healthy': True,
},
2019-03-22 01:51:47 +00:00
}
],
}
data = json.dumps(data_json)
response = MockResponse(data)
response_result = response_validator.validate(request, response)
assert response_result.errors == []
assert isinstance(response_result.data, BaseModel)
assert len(response_result.data.data) == 1
assert response_result.data.data[0].id == 1
assert response_result.data.data[0].name == 'Cat'
def test_get_pets_invalid_response(self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
query_params = {
'limit': '20',
}
request = MockRequest(
host_url, 'GET', '/pets',
path_pattern=path_pattern, args=query_params,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
body = validate_body(spec, request)
2019-03-22 01:51:47 +00:00
assert parameters == RequestParameters(
query={
2019-03-22 01:51:47 +00:00
'limit': 20,
'page': 1,
'search': '',
}
)
2019-03-22 01:51:47 +00:00
assert body is None
data_json = {
'data': [
{
'id': 1,
'name': {
'first_name': 'Cat',
},
}
],
}
data = json.dumps(data_json)
response = MockResponse(data)
response_result = response_validator.validate(request, response)
assert response_result.errors == [
InvalidMediaTypeValue(
2019-09-03 00:38:19 +00:00
original_exception=InvalidSchemaValue(
type=SchemaType.OBJECT,
value=data_json,
2019-03-22 01:51:47 +00:00
),
),
]
assert response_result.data is None
2017-11-14 16:05:03 +00:00
def test_get_pets_ids_param(self, spec, response_validator):
2017-11-14 13:36:05 +00:00
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
query_params = {
'limit': '20',
'ids': ['12', '13'],
}
request = MockRequest(
host_url, 'GET', '/pets',
path_pattern=path_pattern, args=query_params,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
body = validate_body(spec, request)
2017-11-14 13:36:05 +00:00
assert parameters == RequestParameters(
query={
2017-11-14 13:36:05 +00:00
'limit': 20,
'page': 1,
'search': '',
'ids': [12, 13],
}
)
2017-11-14 13:36:05 +00:00
assert body is None
data_json = {
'data': [],
}
data = json.dumps(data_json)
response = MockResponse(data)
response_result = response_validator.validate(request, response)
2017-11-14 16:05:03 +00:00
assert response_result.errors == []
2018-08-21 17:33:24 +00:00
assert isinstance(response_result.data, BaseModel)
assert response_result.data.data == []
2017-11-14 16:05:03 +00:00
def test_get_pets_tags_param(self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
query_params = [
('limit', '20'),
('tags', 'cats,dogs'),
]
request = MockRequest(
host_url, 'GET', '/pets',
path_pattern=path_pattern, args=query_params,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
body = validate_body(spec, request)
2017-11-14 16:05:03 +00:00
assert parameters == RequestParameters(
query={
2017-11-14 16:05:03 +00:00
'limit': 20,
'page': 1,
'search': '',
'tags': ['cats', 'dogs'],
}
)
2017-11-14 16:05:03 +00:00
assert body is None
data_json = {
'data': [],
}
data = json.dumps(data_json)
response = MockResponse(data)
response_result = response_validator.validate(request, response)
2017-11-06 16:50:00 +00:00
assert response_result.errors == []
2018-08-21 17:33:24 +00:00
assert isinstance(response_result.data, BaseModel)
assert response_result.data.data == []
2017-11-06 16:50:00 +00:00
def test_get_pets_parameter_deserialization_error(self, spec):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
query_params = {
'limit': 1,
'tags': 12,
}
request = MockRequest(
host_url, 'GET', '/pets',
path_pattern=path_pattern, args=query_params,
)
with pytest.raises(InvalidParameterValue):
2019-10-19 12:01:56 +00:00
validate_parameters(spec, request)
2019-10-19 12:01:56 +00:00
body = validate_body(spec, request)
assert body is None
2017-09-25 14:15:00 +00:00
def test_get_pets_wrong_parameter_type(self, spec):
2017-09-25 11:22:55 +00:00
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
2017-09-25 14:15:00 +00:00
query_params = {
'limit': 'twenty',
}
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-09-25 13:20:23 +00:00
host_url, 'GET', '/pets',
2017-09-25 14:15:00 +00:00
path_pattern=path_pattern, args=query_params,
2017-09-25 11:22:55 +00:00
)
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
with pytest.raises(InvalidParameterValue):
2019-10-19 12:01:56 +00:00
validate_parameters(spec, request)
2017-09-21 11:51:37 +00:00
2019-10-19 12:01:56 +00:00
body = validate_body(spec, request)
2017-09-25 13:20:23 +00:00
assert body is None
2017-09-25 14:15:00 +00:00
def test_get_pets_raises_missing_required_param(self, spec):
2017-09-25 11:22:55 +00:00
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-09-25 13:20:23 +00:00
host_url, 'GET', '/pets',
2017-09-25 14:15:00 +00:00
path_pattern=path_pattern,
2017-09-25 11:22:55 +00:00
)
2017-09-21 11:51:37 +00:00
2018-04-18 10:39:03 +00:00
with pytest.raises(MissingRequiredParameter):
2019-10-19 12:01:56 +00:00
validate_parameters(spec, request)
2017-09-25 14:15:00 +00:00
2019-10-19 12:01:56 +00:00
body = validate_body(spec, request)
2017-09-21 11:51:37 +00:00
2017-09-25 13:20:23 +00:00
assert body is None
2017-09-21 11:51:37 +00:00
def test_get_pets_empty_value(self, spec):
2017-09-25 11:22:55 +00:00
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
2017-09-21 11:51:37 +00:00
query_params = {
'limit': '',
}
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-09-25 13:20:23 +00:00
host_url, 'GET', '/pets',
2017-09-25 11:22:55 +00:00
path_pattern=path_pattern, args=query_params,
)
2017-09-21 11:51:37 +00:00
2018-04-18 10:39:03 +00:00
with pytest.raises(EmptyParameterValue):
2019-10-19 12:01:56 +00:00
validate_parameters(spec, request)
body = validate_body(spec, request)
2017-09-21 11:51:37 +00:00
2017-09-25 13:20:23 +00:00
assert body is None
2017-09-21 11:51:37 +00:00
def test_get_pets_none_value(self, spec):
2017-09-25 11:22:55 +00:00
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
2017-09-21 11:51:37 +00:00
query_params = {
'limit': None,
}
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-09-25 13:20:23 +00:00
host_url, 'GET', '/pets',
2017-09-25 11:22:55 +00:00
path_pattern=path_pattern, args=query_params,
)
2017-09-21 11:51:37 +00:00
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2017-09-21 11:51:37 +00:00
assert parameters == RequestParameters(
query={
2017-09-21 11:51:37 +00:00
'limit': None,
'page': 1,
2017-10-19 11:28:28 +00:00
'search': '',
2017-09-21 11:51:37 +00:00
}
)
2017-09-21 11:51:37 +00:00
2019-10-19 12:01:56 +00:00
body = validate_body(spec, request)
2017-09-25 13:20:23 +00:00
assert body is None
2018-05-25 15:32:09 +00:00
def test_post_birds(self, spec, spec_dict):
2017-09-25 11:22:55 +00:00
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
pet_name = 'Cat'
pet_tag = 'cats'
pet_street = 'Piekna'
pet_city = 'Warsaw'
2018-02-28 12:01:05 +00:00
pet_healthy = False
2017-09-21 11:51:37 +00:00
data_json = {
'name': pet_name,
'tag': pet_tag,
2019-03-22 01:51:47 +00:00
'position': 2,
'address': {
'street': pet_street,
'city': pet_city,
2018-02-28 12:01:05 +00:00
},
'healthy': pet_healthy,
2018-05-25 15:32:09 +00:00
'wings': {
'healthy': pet_healthy,
}
2018-02-28 12:01:05 +00:00
}
data = json.dumps(data_json)
2018-07-09 11:10:05 +00:00
headers = {
2019-02-26 16:49:25 +00:00
'api_key': self.api_key_encoded,
2018-07-09 11:10:05 +00:00
}
cookies = {
'user': '123',
}
2018-02-28 12:01:05 +00:00
request = MockRequest(
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data,
2018-07-09 11:10:05 +00:00
headers=headers, cookies=cookies,
2018-02-28 12:01:05 +00:00
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2018-02-28 12:01:05 +00:00
assert parameters == RequestParameters(
header={
2019-02-26 16:49:25 +00:00
'api_key': self.api_key,
2018-07-09 11:10:05 +00:00
},
cookie={
2018-07-09 11:10:05 +00:00
'user': 123,
},
)
2018-02-28 12:01:05 +00:00
2019-10-19 12:01:56 +00:00
body = validate_body(spec, request)
2018-02-28 12:01:05 +00:00
schemas = spec_dict['components']['schemas']
pet_model = schemas['PetCreate']['x-model']
address_model = schemas['Address']['x-model']
assert body.__class__.__name__ == pet_model
assert body.name == pet_name
assert body.tag == pet_tag
assert body.position == 2
assert body.address.__class__.__name__ == address_model
assert body.address.street == pet_street
assert body.address.city == pet_city
assert body.healthy == pet_healthy
2018-05-25 15:32:09 +00:00
def test_post_cats(self, spec, spec_dict):
2018-02-28 12:01:05 +00:00
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
pet_name = 'Cat'
pet_tag = 'cats'
pet_street = 'Piekna'
pet_city = 'Warsaw'
2018-05-25 15:32:09 +00:00
pet_healthy = False
2018-02-28 12:01:05 +00:00
data_json = {
'name': pet_name,
'tag': pet_tag,
2019-03-22 01:51:47 +00:00
'position': 2,
2018-02-28 12:01:05 +00:00
'address': {
'street': pet_street,
'city': pet_city,
},
'healthy': pet_healthy,
2018-05-25 15:32:09 +00:00
'ears': {
'healthy': pet_healthy,
}
2017-09-21 11:51:37 +00:00
}
data = json.dumps(data_json)
2018-07-09 11:10:05 +00:00
headers = {
2019-02-26 16:49:25 +00:00
'api_key': self.api_key_encoded,
2018-07-09 11:10:05 +00:00
}
cookies = {
'user': '123',
}
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-09-25 13:20:23 +00:00
host_url, 'POST', '/pets',
2017-09-25 11:22:55 +00:00
path_pattern=path_pattern, data=data,
2018-07-09 11:10:05 +00:00
headers=headers, cookies=cookies,
2017-09-25 11:22:55 +00:00
)
2017-09-21 11:51:37 +00:00
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2017-09-25 13:20:23 +00:00
assert parameters == RequestParameters(
header={
2019-02-26 16:49:25 +00:00
'api_key': self.api_key,
2018-07-09 11:10:05 +00:00
},
cookie={
2018-07-09 11:10:05 +00:00
'user': 123,
},
)
2017-09-25 13:20:23 +00:00
2019-10-19 12:01:56 +00:00
body = validate_body(spec, request)
schemas = spec_dict['components']['schemas']
pet_model = schemas['PetCreate']['x-model']
address_model = schemas['Address']['x-model']
2017-09-25 13:20:23 +00:00
assert body.__class__.__name__ == pet_model
assert body.name == pet_name
assert body.tag == pet_tag
2017-10-17 13:46:09 +00:00
assert body.position == 2
2017-09-25 13:20:23 +00:00
assert body.address.__class__.__name__ == address_model
assert body.address.street == pet_street
assert body.address.city == pet_city
2018-05-25 15:32:09 +00:00
assert body.healthy == pet_healthy
2017-09-21 11:51:37 +00:00
2018-05-25 15:32:09 +00:00
def test_post_cats_boolean_string(self, spec, spec_dict):
2017-09-25 14:15:00 +00:00
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
2018-05-25 15:32:09 +00:00
pet_name = 'Cat'
pet_tag = 'cats'
pet_street = 'Piekna'
pet_city = 'Warsaw'
2019-03-22 01:51:47 +00:00
pet_healthy = False
2018-05-25 15:32:09 +00:00
data_json = {
'name': pet_name,
'tag': pet_tag,
2019-03-22 01:51:47 +00:00
'position': 2,
2018-05-25 15:32:09 +00:00
'address': {
'street': pet_street,
'city': pet_city,
},
'healthy': pet_healthy,
'ears': {
'healthy': pet_healthy,
}
}
2017-09-25 14:15:00 +00:00
data = json.dumps(data_json)
2018-07-09 11:10:05 +00:00
headers = {
2019-02-26 16:49:25 +00:00
'api_key': self.api_key_encoded,
2018-07-09 11:10:05 +00:00
}
cookies = {
'user': '123',
}
2017-09-25 14:15:00 +00:00
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-09-25 14:15:00 +00:00
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data,
2018-07-09 11:10:05 +00:00
headers=headers, cookies=cookies,
2017-09-25 14:15:00 +00:00
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2017-09-25 14:15:00 +00:00
assert parameters == RequestParameters(
header={
2019-02-26 16:49:25 +00:00
'api_key': self.api_key,
2018-07-09 11:10:05 +00:00
},
cookie={
2018-07-09 11:10:05 +00:00
'user': 123,
},
)
2017-09-25 14:15:00 +00:00
2019-10-19 12:01:56 +00:00
body = validate_body(spec, request)
2018-05-25 15:32:09 +00:00
schemas = spec_dict['components']['schemas']
pet_model = schemas['PetCreate']['x-model']
address_model = schemas['Address']['x-model']
assert body.__class__.__name__ == pet_model
assert body.name == pet_name
assert body.tag == pet_tag
assert body.position == 2
assert body.address.__class__.__name__ == address_model
assert body.address.street == pet_street
assert body.address.city == pet_city
assert body.healthy is False
2017-09-25 14:15:00 +00:00
2018-05-25 15:32:09 +00:00
def test_post_no_one_of_schema(self, spec, spec_dict):
2017-09-25 14:15:00 +00:00
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
pet_name = 'Cat'
alias = 'kitty'
data_json = {
'name': pet_name,
'alias': alias,
}
data = json.dumps(data_json)
2018-07-09 11:10:05 +00:00
headers = {
2019-02-26 16:49:25 +00:00
'api_key': self.api_key_encoded,
2018-07-09 11:10:05 +00:00
}
cookies = {
'user': '123',
}
2017-09-25 14:15:00 +00:00
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-09-25 14:15:00 +00:00
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data,
2018-07-09 11:10:05 +00:00
headers=headers, cookies=cookies,
2017-09-25 14:15:00 +00:00
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2017-09-25 14:15:00 +00:00
assert parameters == RequestParameters(
header={
2019-02-26 16:49:25 +00:00
'api_key': self.api_key,
2018-07-09 11:10:05 +00:00
},
cookie={
2018-07-09 11:10:05 +00:00
'user': 123,
},
)
2017-09-25 14:15:00 +00:00
with pytest.raises(InvalidMediaTypeValue):
2019-10-19 12:01:56 +00:00
validate_body(spec, request)
2017-09-25 14:15:00 +00:00
2018-05-25 15:32:09 +00:00
def test_post_cats_only_required_body(self, spec, spec_dict):
2017-09-25 14:15:00 +00:00
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
pet_name = 'Cat'
2018-05-25 15:32:09 +00:00
pet_healthy = True
2017-09-25 14:15:00 +00:00
data_json = {
'name': pet_name,
2018-05-25 15:32:09 +00:00
'ears': {
'healthy': pet_healthy,
}
2017-09-25 14:15:00 +00:00
}
data = json.dumps(data_json)
2018-07-09 11:10:05 +00:00
headers = {
2019-02-26 16:49:25 +00:00
'api_key': self.api_key_encoded,
2018-07-09 11:10:05 +00:00
}
cookies = {
'user': '123',
}
2017-09-25 14:15:00 +00:00
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-09-25 14:15:00 +00:00
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data,
2018-07-09 11:10:05 +00:00
headers=headers, cookies=cookies,
2017-09-25 14:15:00 +00:00
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2017-09-25 14:15:00 +00:00
assert parameters == RequestParameters(
header={
2019-02-26 16:49:25 +00:00
'api_key': self.api_key,
2018-07-09 11:10:05 +00:00
},
cookie={
2018-07-09 11:10:05 +00:00
'user': 123,
},
)
2017-09-25 14:15:00 +00:00
2019-10-19 12:01:56 +00:00
body = validate_body(spec, request)
2017-09-25 14:15:00 +00:00
schemas = spec_dict['components']['schemas']
pet_model = schemas['PetCreate']['x-model']
assert body.__class__.__name__ == pet_model
assert body.name == pet_name
2017-10-17 13:02:21 +00:00
assert not hasattr(body, 'tag')
assert not hasattr(body, 'address')
2017-09-25 14:15:00 +00:00
2017-10-09 14:57:07 +00:00
def test_post_pets_raises_invalid_mimetype(self, spec):
2017-09-25 11:22:55 +00:00
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
2017-09-21 11:51:37 +00:00
data_json = {
'name': 'Cat',
'tag': 'cats',
}
data = json.dumps(data_json)
2018-07-09 11:10:05 +00:00
headers = {
2019-02-26 16:49:25 +00:00
'api_key': self.api_key_encoded,
2018-07-09 11:10:05 +00:00
}
cookies = {
'user': '123',
}
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-09-25 13:20:23 +00:00
host_url, 'POST', '/pets',
2017-10-09 14:57:07 +00:00
path_pattern=path_pattern, data=data, mimetype='text/html',
2018-07-09 11:10:05 +00:00
headers=headers, cookies=cookies,
2017-09-25 11:22:55 +00:00
)
2017-09-21 11:51:37 +00:00
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2017-09-25 13:20:23 +00:00
assert parameters == RequestParameters(
header={
2019-02-26 16:49:25 +00:00
'api_key': self.api_key,
2018-07-09 11:10:05 +00:00
},
cookie={
2018-07-09 11:10:05 +00:00
'user': 123,
},
)
2017-09-25 13:20:23 +00:00
2017-11-03 11:18:48 +00:00
with pytest.raises(InvalidContentType):
2019-10-19 12:01:56 +00:00
validate_body(spec, request)
2017-09-21 11:51:37 +00:00
2018-07-09 11:10:05 +00:00
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 = {
2019-02-26 16:49:25 +00:00
'api_key': self.api_key_encoded,
2018-07-09 11:10:05 +00:00
}
request = MockRequest(
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data,
headers=headers,
)
with pytest.raises(MissingRequiredParameter):
2019-10-19 12:01:56 +00:00
validate_parameters(spec, request)
2018-07-09 11:10:05 +00:00
2019-10-19 12:01:56 +00:00
body = validate_body(spec, request)
2018-07-09 11:10:05 +00:00
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):
2019-10-19 12:01:56 +00:00
validate_parameters(spec, request)
2018-07-09 11:10:05 +00:00
2019-10-19 12:01:56 +00:00
body = validate_body(spec, request)
2018-07-09 11:10:05 +00:00
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')
2017-09-25 11:22:55 +00:00
def test_post_pets_raises_invalid_server_error(self, spec):
host_url = 'http://flowerstore.swagger.io/v1'
path_pattern = '/v1/pets'
data_json = {
'name': 'Cat',
'tag': 'cats',
}
data = json.dumps(data_json)
2018-07-09 11:10:05 +00:00
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
2017-09-25 11:22:55 +00:00
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-09-25 13:20:23 +00:00
host_url, 'POST', '/pets',
2017-10-09 14:57:07 +00:00
path_pattern=path_pattern, data=data, mimetype='text/html',
2018-07-09 11:10:05 +00:00
headers=headers, cookies=cookies,
2017-09-25 11:22:55 +00:00
)
2017-11-03 11:18:48 +00:00
with pytest.raises(InvalidServer):
2019-10-19 12:01:56 +00:00
validate_parameters(spec, request)
2017-09-25 13:20:23 +00:00
2017-11-03 11:18:48 +00:00
with pytest.raises(InvalidServer):
2019-10-19 12:01:56 +00:00
validate_body(spec, request)
2017-09-25 11:22:55 +00:00
2017-11-06 16:50:00 +00:00
def test_get_pet(self, spec, response_validator):
2017-09-25 11:22:55 +00:00
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets/{petId}'
2017-09-21 11:51:37 +00:00
view_args = {
'petId': '1',
}
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-09-25 13:20:23 +00:00
host_url, 'GET', '/pets/1',
2017-09-25 11:22:55 +00:00
path_pattern=path_pattern, view_args=view_args,
2017-09-21 11:51:37 +00:00
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2017-09-21 11:51:37 +00:00
assert parameters == RequestParameters(
path={
2017-09-21 11:51:37 +00:00
'petId': 1,
}
)
2017-09-25 13:20:23 +00:00
2019-10-19 12:01:56 +00:00
body = validate_body(spec, request)
2017-09-25 13:20:23 +00:00
assert body is None
2017-11-06 16:50:00 +00:00
2018-08-21 17:33:24 +00:00
data_id = 1
data_name = 'test'
2017-11-06 16:50:00 +00:00
data_json = {
'data': {
2018-08-21 17:33:24 +00:00
'id': data_id,
'name': data_name,
2019-09-03 00:38:19 +00:00
'ears': {
'healthy': True,
},
2017-11-06 16:50:00 +00:00
},
}
data = json.dumps(data_json)
response = MockResponse(data)
response_result = response_validator.validate(request, response)
assert response_result.errors == []
2018-08-21 17:33:24 +00:00
assert isinstance(response_result.data, BaseModel)
assert isinstance(response_result.data.data, BaseModel)
assert response_result.data.data.id == data_id
assert response_result.data.data.name == data_name
2018-04-04 10:26:21 +00:00
def test_get_pet_not_found(self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets/{petId}'
view_args = {
'petId': '1',
}
request = MockRequest(
host_url, 'GET', '/pets/1',
path_pattern=path_pattern, view_args=view_args,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2018-04-04 10:26:21 +00:00
assert parameters == RequestParameters(
path={
2018-04-04 10:26:21 +00:00
'petId': 1,
}
)
2018-04-04 10:26:21 +00:00
2019-10-19 12:01:56 +00:00
body = validate_body(spec, request)
2018-04-04 10:26:21 +00:00
assert body is None
2018-08-21 17:33:24 +00:00
code = 404
message = 'Not found'
rootCause = 'Pet not found'
2018-04-04 10:26:21 +00:00
data_json = {
'code': 404,
2018-08-21 17:33:24 +00:00
'message': message,
'rootCause': rootCause,
2018-04-04 10:26:21 +00:00
}
data = json.dumps(data_json)
response = MockResponse(data, status_code=404)
response_result = response_validator.validate(request, response)
assert response_result.errors == []
2018-08-21 17:33:24 +00:00
assert isinstance(response_result.data, BaseModel)
assert response_result.data.code == code
assert response_result.data.message == message
assert response_result.data.rootCause == rootCause
2018-04-23 18:50:29 +00:00
2018-05-30 13:46:54 +00:00
def test_get_pet_wildcard(self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets/{petId}'
view_args = {
'petId': '1',
}
request = MockRequest(
host_url, 'GET', '/pets/1',
path_pattern=path_pattern, view_args=view_args,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2018-05-30 13:46:54 +00:00
assert parameters == RequestParameters(
path={
2018-05-30 13:46:54 +00:00
'petId': 1,
}
)
2018-05-30 13:46:54 +00:00
2019-10-19 12:01:56 +00:00
body = validate_body(spec, request)
2018-05-30 13:46:54 +00:00
assert body is None
data = b'imagedata'
response = MockResponse(data, mimetype='image/png')
response_result = response_validator.validate(request, response)
assert response_result.errors == []
assert response_result.data == data
2018-04-23 18:50:29 +00:00
def test_get_tags(self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/tags'
request = MockRequest(
host_url, 'GET', '/tags',
path_pattern=path_pattern,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
body = validate_body(spec, request)
2018-04-23 18:50:29 +00:00
assert parameters == RequestParameters()
2018-04-23 18:50:29 +00:00
assert body is None
2018-05-30 08:41:34 +00:00
data_json = ['cats', 'birds']
2018-04-23 18:50:29 +00:00
data = json.dumps(data_json)
response = MockResponse(data)
response_result = response_validator.validate(request, response)
assert response_result.errors == []
assert response_result.data == data_json
2018-05-25 15:32:09 +00:00
def test_post_tags_extra_body_properties(self, spec, spec_dict):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/tags'
pet_name = 'Dog'
alias = 'kitty'
data_json = {
'name': pet_name,
'alias': alias,
}
data = json.dumps(data_json)
request = MockRequest(
host_url, 'POST', '/tags',
path_pattern=path_pattern, data=data,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2018-05-25 15:32:09 +00:00
assert parameters == RequestParameters()
2018-05-25 15:32:09 +00:00
with pytest.raises(InvalidMediaTypeValue):
2019-10-19 12:01:56 +00:00
validate_body(spec, request)
2018-05-25 15:32:09 +00:00
def test_post_tags_empty_body(self, spec, spec_dict):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/tags'
data_json = {}
data = json.dumps(data_json)
request = MockRequest(
host_url, 'POST', '/tags',
path_pattern=path_pattern, data=data,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2018-05-25 15:32:09 +00:00
assert parameters == RequestParameters()
2018-05-25 15:32:09 +00:00
with pytest.raises(InvalidMediaTypeValue):
2019-10-19 12:01:56 +00:00
validate_body(spec, request)
2018-05-25 15:32:09 +00:00
def test_post_tags_wrong_property_type(self, spec):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/tags'
tag_name = 123
data = json.dumps(tag_name)
request = MockRequest(
host_url, 'POST', '/tags',
path_pattern=path_pattern, data=data,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2018-05-25 15:32:09 +00:00
assert parameters == RequestParameters()
2018-05-25 15:32:09 +00:00
with pytest.raises(InvalidMediaTypeValue):
2019-10-19 12:01:56 +00:00
validate_body(spec, request)
2018-05-30 10:15:17 +00:00
def test_post_tags_additional_properties(
self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/tags'
pet_name = 'Dog'
data_json = {
'name': pet_name,
}
data = json.dumps(data_json)
request = MockRequest(
host_url, 'POST', '/tags',
path_pattern=path_pattern, data=data,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
body = validate_body(spec, request)
2018-05-30 10:15:17 +00:00
assert parameters == RequestParameters()
2018-08-21 17:33:24 +00:00
assert isinstance(body, BaseModel)
assert body.name == pet_name
2018-05-30 10:15:17 +00:00
2018-08-21 17:33:24 +00:00
code = 400
message = 'Bad request'
rootCause = 'Tag already exist'
additionalinfo = 'Tag Dog already exist'
2018-05-30 10:15:17 +00:00
data_json = {
2018-08-21 17:33:24 +00:00
'code': code,
'message': message,
'rootCause': rootCause,
'additionalinfo': additionalinfo,
2018-05-30 10:15:17 +00:00
}
data = json.dumps(data_json)
response = MockResponse(data, status_code=404)
response_result = response_validator.validate(request, response)
assert response_result.errors == []
2018-08-21 17:33:24 +00:00
assert isinstance(response_result.data, BaseModel)
assert response_result.data.code == code
assert response_result.data.message == message
assert response_result.data.rootCause == rootCause
assert response_result.data.additionalinfo == additionalinfo
2018-08-02 18:30:51 +00:00
def test_post_tags_created_now(
self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/tags'
2018-08-05 12:40:34 +00:00
created = 'now'
2018-08-02 18:30:51 +00:00
pet_name = 'Dog'
data_json = {
2018-08-05 12:40:34 +00:00
'created': created,
2018-08-02 18:30:51 +00:00
'name': pet_name,
}
data = json.dumps(data_json)
request = MockRequest(
host_url, 'POST', '/tags',
path_pattern=path_pattern, data=data,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
body = validate_body(spec, request)
2018-08-02 18:30:51 +00:00
assert parameters == RequestParameters()
2018-08-05 12:40:34 +00:00
assert isinstance(body, BaseModel)
assert body.created == created
assert body.name == pet_name
2018-08-02 18:30:51 +00:00
2018-08-05 12:40:34 +00:00
code = 400
message = 'Bad request'
rootCause = 'Tag already exist'
additionalinfo = 'Tag Dog already exist'
2018-08-02 18:30:51 +00:00
data_json = {
'code': 400,
'message': 'Bad request',
'rootCause': 'Tag already exist',
'additionalinfo': 'Tag Dog already exist',
}
data = json.dumps(data_json)
response = MockResponse(data, status_code=404)
response_result = response_validator.validate(request, response)
assert response_result.errors == []
2018-08-05 12:40:34 +00:00
assert isinstance(response_result.data, BaseModel)
assert response_result.data.code == code
assert response_result.data.message == message
assert response_result.data.rootCause == rootCause
assert response_result.data.additionalinfo == additionalinfo
2018-08-02 18:30:51 +00:00
def test_post_tags_created_datetime(
self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/tags'
2018-08-05 12:40:34 +00:00
created = '2016-04-16T16:06:05Z'
2018-08-02 18:30:51 +00:00
pet_name = 'Dog'
data_json = {
2018-08-05 12:40:34 +00:00
'created': created,
2018-08-02 18:30:51 +00:00
'name': pet_name,
}
data = json.dumps(data_json)
request = MockRequest(
host_url, 'POST', '/tags',
path_pattern=path_pattern, data=data,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
body = validate_body(spec, request)
2018-08-02 18:30:51 +00:00
assert parameters == RequestParameters()
2018-08-05 12:40:34 +00:00
assert isinstance(body, BaseModel)
2019-03-02 20:44:01 +00:00
assert body.created == datetime(2016, 4, 16, 16, 6, 5)
2018-08-05 12:40:34 +00:00
assert body.name == pet_name
2018-08-02 18:30:51 +00:00
2018-08-05 12:40:34 +00:00
code = 400
message = 'Bad request'
rootCause = 'Tag already exist'
additionalinfo = 'Tag Dog already exist'
2018-08-02 18:30:51 +00:00
data_json = {
2018-08-05 12:40:34 +00:00
'code': code,
'message': message,
'rootCause': rootCause,
'additionalinfo': additionalinfo,
}
data = json.dumps(data_json)
response = MockResponse(data, status_code=404)
response_result = response_validator.validate(request, response)
assert response_result.errors == []
assert isinstance(response_result.data, BaseModel)
assert response_result.data.code == code
assert response_result.data.message == message
assert response_result.data.rootCause == rootCause
assert response_result.data.additionalinfo == additionalinfo
def test_post_tags_created_invalid_type(
self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/tags'
created = 'long time ago'
pet_name = 'Dog'
data_json = {
'created': created,
'name': pet_name,
}
data = json.dumps(data_json)
request = MockRequest(
host_url, 'POST', '/tags',
path_pattern=path_pattern, data=data,
)
2019-10-19 12:01:56 +00:00
parameters = validate_parameters(spec, request)
2019-03-02 20:44:01 +00:00
with pytest.raises(InvalidMediaTypeValue):
2019-10-19 12:01:56 +00:00
validate_body(spec, request)
2018-08-05 12:40:34 +00:00
assert parameters == RequestParameters()
2018-08-05 12:40:34 +00:00
code = 400
message = 'Bad request'
2019-02-26 10:37:49 +00:00
correlationId = UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
2018-08-05 12:40:34 +00:00
rootCause = 'Tag already exist'
additionalinfo = 'Tag Dog already exist'
data_json = {
'code': code,
'message': message,
2019-02-26 10:37:49 +00:00
'correlationId': str(correlationId),
2018-08-05 12:40:34 +00:00
'rootCause': rootCause,
'additionalinfo': additionalinfo,
2018-08-02 18:30:51 +00:00
}
data = json.dumps(data_json)
response = MockResponse(data, status_code=404)
response_result = response_validator.validate(request, response)
assert response_result.errors == []
assert isinstance(response_result.data, BaseModel)
assert response_result.data.code == code
assert response_result.data.message == message
2019-02-26 10:37:49 +00:00
assert response_result.data.correlationId == correlationId
2018-08-02 18:30:51 +00:00
assert response_result.data.rootCause == rootCause
assert response_result.data.additionalinfo == additionalinfo