2019-10-19 09:55:32 +00:00
|
|
|
import pytest
|
|
|
|
from base64 import b64encode
|
|
|
|
from six import iteritems, text_type
|
|
|
|
|
|
|
|
from openapi_core.shortcuts import create_spec
|
2021-04-23 11:36:27 +00:00
|
|
|
from openapi_core.spec.servers import get_server_url
|
|
|
|
from openapi_core.spec.specs import get_spec_url
|
2019-10-19 09:55:32 +00:00
|
|
|
from openapi_core.validation.request.validators import RequestValidator
|
|
|
|
from openapi_core.validation.response.validators import ResponseValidator
|
|
|
|
|
|
|
|
|
|
|
|
class TestPetstore(object):
|
|
|
|
|
|
|
|
api_key = '12345'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def api_key_encoded(self):
|
|
|
|
api_key_bytes = self.api_key.encode('utf8')
|
|
|
|
api_key_bytes_enc = b64encode(api_key_bytes)
|
|
|
|
return text_type(api_key_bytes_enc, 'utf8')
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spec_uri(self):
|
|
|
|
return "file://tests/integration/data/v3.0/petstore.yaml"
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spec_dict(self, factory):
|
|
|
|
return factory.spec_from_file("data/v3.0/petstore.yaml")
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spec(self, spec_dict, spec_uri):
|
|
|
|
return create_spec(spec_dict, spec_uri)
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def request_validator(self, spec):
|
|
|
|
return RequestValidator(spec)
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def response_validator(self, spec):
|
|
|
|
return ResponseValidator(spec)
|
|
|
|
|
|
|
|
def test_spec(self, spec, spec_dict):
|
|
|
|
url = 'http://petstore.swagger.io/v1'
|
2020-02-03 19:23:09 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
info = spec / 'info'
|
2020-02-03 19:23:09 +00:00
|
|
|
info_spec = spec_dict['info']
|
2021-04-23 11:36:27 +00:00
|
|
|
assert info['title'] == info_spec['title']
|
|
|
|
assert info['description'] == info_spec['description']
|
|
|
|
assert info['termsOfService'] == info_spec['termsOfService']
|
|
|
|
assert info['version'] == info_spec['version']
|
2020-02-03 19:23:09 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
contact = info / 'contact'
|
2020-02-03 19:23:09 +00:00
|
|
|
contact_spec = info_spec['contact']
|
2021-04-23 11:36:27 +00:00
|
|
|
assert contact['name'] == contact_spec['name']
|
|
|
|
assert contact['url'] == contact_spec['url']
|
|
|
|
assert contact['email'] == contact_spec['email']
|
2020-02-03 19:23:09 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
license = info / 'license'
|
2020-02-03 19:23:09 +00:00
|
|
|
license_spec = info_spec['license']
|
2021-04-23 11:36:27 +00:00
|
|
|
assert license['name'] == license_spec['name']
|
|
|
|
assert license['url'] == license_spec['url']
|
2019-10-19 09:55:32 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
security = spec / 'security'
|
2020-02-04 01:21:46 +00:00
|
|
|
security_spec = spec_dict.get('security', [])
|
2021-04-23 11:36:27 +00:00
|
|
|
for idx, security_reqs in enumerate(security):
|
|
|
|
security_reqs_spec = security_spec[idx]
|
|
|
|
for scheme_name, security_req in iteritems(security_reqs):
|
|
|
|
security_req == security_reqs_spec[scheme_name]
|
2020-02-04 01:21:46 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
assert get_spec_url(spec) == url
|
2019-10-19 09:55:32 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
servers = spec / 'servers'
|
|
|
|
for idx, server in enumerate(servers):
|
2019-10-19 09:55:32 +00:00
|
|
|
server_spec = spec_dict['servers'][idx]
|
2021-04-23 11:36:27 +00:00
|
|
|
assert server['url'] == server_spec['url']
|
|
|
|
assert get_server_url(server) == url
|
2019-10-19 09:55:32 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
variables = server / 'variables'
|
|
|
|
for variable_name, variable in iteritems(variables):
|
2019-10-19 09:55:32 +00:00
|
|
|
variable_spec = server_spec['variables'][variable_name]
|
2021-04-23 11:36:27 +00:00
|
|
|
assert variable['default'] == variable_spec['default']
|
|
|
|
assert variable['enum'] == variable_spec.get('enum')
|
2020-02-07 11:21:11 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
paths = spec / 'paths'
|
|
|
|
for path_name, path in iteritems(paths):
|
2020-02-07 11:21:11 +00:00
|
|
|
path_spec = spec_dict['paths'][path_name]
|
2021-04-23 11:36:27 +00:00
|
|
|
assert path.getkey('summary') == path_spec.get('summary')
|
|
|
|
assert path.getkey('description') == path_spec.get('description')
|
2020-02-07 11:21:11 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
servers = path.get('servers', [])
|
2020-02-07 11:21:11 +00:00
|
|
|
servers_spec = path_spec.get('servers', [])
|
2021-04-23 11:36:27 +00:00
|
|
|
for idx, server in enumerate(servers):
|
2020-02-07 11:21:11 +00:00
|
|
|
server_spec = servers_spec[idx]
|
|
|
|
assert server.url == server_spec['url']
|
|
|
|
assert server.default_url == server_spec['url']
|
|
|
|
assert server.description == server_spec.get('description')
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
variables = server.get('variables', {})
|
|
|
|
for variable_name, variable in iteritems(variables):
|
2020-02-07 11:21:11 +00:00
|
|
|
variable_spec = server_spec['variables'][variable_name]
|
2021-04-23 11:36:27 +00:00
|
|
|
assert variable['default'] == variable_spec['default']
|
|
|
|
assert variable.getkey('enum') == variable_spec.get('enum')
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
'get', 'put', 'post', 'delete', 'options',
|
|
|
|
'head', 'patch', 'trace',
|
|
|
|
]
|
|
|
|
for http_method in operations:
|
|
|
|
if http_method not in path:
|
|
|
|
continue
|
|
|
|
operation = path / http_method
|
2020-02-07 11:21:11 +00:00
|
|
|
operation_spec = path_spec[http_method]
|
2019-10-19 09:55:32 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
assert operation['operationId'] is not None
|
|
|
|
assert operation['tags'] == operation_spec['tags']
|
|
|
|
assert operation['summary'] == operation_spec.get('summary')
|
|
|
|
assert operation.getkey('description') == operation_spec.get(
|
2019-10-21 22:16:24 +00:00
|
|
|
'description')
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
ext_docs = operation.get('externalDocs')
|
2019-10-21 22:16:24 +00:00
|
|
|
ext_docs_spec = operation_spec.get('externalDocs')
|
2021-04-23 11:36:27 +00:00
|
|
|
assert bool(ext_docs_spec) == bool(ext_docs)
|
2019-10-21 22:16:24 +00:00
|
|
|
if ext_docs_spec:
|
2021-04-23 11:36:27 +00:00
|
|
|
assert ext_docs['url'] == ext_docs_spec['url']
|
|
|
|
assert ext_docs.getkey('description') == ext_docs_spec.get(
|
2019-10-21 22:16:24 +00:00
|
|
|
'description')
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
servers = operation.get('servers', [])
|
2019-10-22 17:21:40 +00:00
|
|
|
servers_spec = operation_spec.get('servers', [])
|
2021-04-23 11:36:27 +00:00
|
|
|
for idx, server in enumerate(servers):
|
2019-10-22 17:21:40 +00:00
|
|
|
server_spec = servers_spec[idx]
|
2021-04-23 11:36:27 +00:00
|
|
|
assert server['url'] == server_spec['url']
|
|
|
|
assert get_server_url(server) == server_spec['url']
|
|
|
|
assert server['description'] == server_spec.get(
|
|
|
|
'description')
|
2019-10-22 17:21:40 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
variables = server.get('variables', {})
|
|
|
|
for variable_name, variable in iteritems(variables):
|
2019-10-22 17:21:40 +00:00
|
|
|
variable_spec = server_spec['variables'][variable_name]
|
2021-04-23 11:36:27 +00:00
|
|
|
assert variable['default'] == variable_spec['default']
|
|
|
|
assert variable.getkey('enum') == variable_spec.get(
|
|
|
|
'enum')
|
2019-10-22 17:21:40 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
security = operation.get('security', [])
|
2021-02-02 12:39:53 +00:00
|
|
|
security_spec = operation_spec.get('security')
|
|
|
|
if security_spec is not None:
|
2021-04-23 11:36:27 +00:00
|
|
|
for idx, security_reqs in enumerate(security):
|
|
|
|
security_reqs_spec = security_spec[idx]
|
|
|
|
for scheme_name, security_req in iteritems(
|
|
|
|
security_reqs):
|
|
|
|
security_req == security_reqs_spec[scheme_name]
|
2020-02-04 01:21:46 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
responses = operation / 'responses'
|
2019-10-19 09:55:32 +00:00
|
|
|
responses_spec = operation_spec.get('responses')
|
2021-04-23 11:36:27 +00:00
|
|
|
for http_status, response in iteritems(responses):
|
2019-10-19 09:55:32 +00:00
|
|
|
response_spec = responses_spec[http_status]
|
|
|
|
|
|
|
|
if not response_spec:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# @todo: test with defererence
|
|
|
|
if '$ref' in response_spec:
|
|
|
|
continue
|
|
|
|
|
|
|
|
description_spec = response_spec['description']
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
assert response.getkey('description') == description_spec
|
2019-10-19 09:55:32 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
headers = response.get('headers', {})
|
|
|
|
for parameter_name, parameter in iteritems(headers):
|
2019-10-19 09:55:32 +00:00
|
|
|
headers_spec = response_spec['headers']
|
|
|
|
parameter_spec = headers_spec[parameter_name]
|
2021-04-23 11:36:27 +00:00
|
|
|
|
|
|
|
schema = parameter.get('schema')
|
2019-10-19 09:55:32 +00:00
|
|
|
schema_spec = parameter_spec.get('schema')
|
2021-04-23 11:36:27 +00:00
|
|
|
assert bool(schema_spec) == bool(schema)
|
2019-10-19 09:55:32 +00:00
|
|
|
|
|
|
|
if not schema_spec:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# @todo: test with defererence
|
|
|
|
if '$ref' in schema_spec:
|
|
|
|
continue
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
assert schema['type'] ==\
|
2019-10-19 09:55:32 +00:00
|
|
|
schema_spec['type']
|
2021-04-23 11:36:27 +00:00
|
|
|
assert schema.getkey('format') ==\
|
2019-10-19 09:55:32 +00:00
|
|
|
schema_spec.get('format')
|
2021-04-23 11:36:27 +00:00
|
|
|
assert schema.getkey('required') == schema_spec.get(
|
|
|
|
'required')
|
2019-10-19 09:55:32 +00:00
|
|
|
|
2021-03-30 10:12:36 +00:00
|
|
|
content_spec = parameter_spec.get('content')
|
|
|
|
assert bool(content_spec) == bool(parameter.content)
|
|
|
|
|
|
|
|
if not content_spec:
|
|
|
|
continue
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
content = parameter.get('content', {})
|
|
|
|
for mimetype, media_type in iteritems(content):
|
2021-03-30 10:12:36 +00:00
|
|
|
media_spec = parameter_spec['content'][mimetype]
|
2021-04-23 11:36:27 +00:00
|
|
|
schema = media_type.get('schema')
|
2021-03-30 10:12:36 +00:00
|
|
|
schema_spec = media_spec.get('schema')
|
2021-04-23 11:36:27 +00:00
|
|
|
assert bool(schema_spec) == bool(schema)
|
2021-03-30 10:12:36 +00:00
|
|
|
|
|
|
|
if not schema_spec:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# @todo: test with defererence
|
|
|
|
if '$ref' in schema_spec:
|
|
|
|
continue
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
assert schema['type'] ==\
|
2021-03-30 10:12:36 +00:00
|
|
|
schema_spec['type']
|
2021-04-23 11:36:27 +00:00
|
|
|
assert schema.getkey('format') ==\
|
2021-03-30 10:12:36 +00:00
|
|
|
schema_spec.get('format')
|
2021-04-23 11:36:27 +00:00
|
|
|
assert schema.getkey('required') == \
|
|
|
|
schema_spec.get('required')
|
2021-03-30 10:12:36 +00:00
|
|
|
|
2021-03-30 11:29:37 +00:00
|
|
|
content_spec = response_spec.get('content')
|
|
|
|
|
|
|
|
if not content_spec:
|
|
|
|
continue
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
content = response.get('content', {})
|
|
|
|
for mimetype, media_type in iteritems(content):
|
2021-03-30 11:29:37 +00:00
|
|
|
content_spec = response_spec['content'][mimetype]
|
|
|
|
|
|
|
|
example_spec = content_spec.get('example')
|
2021-04-23 11:36:27 +00:00
|
|
|
assert media_type.getkey('example') == example_spec
|
2021-03-30 11:29:37 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
schema = media_type.get('schema')
|
2021-03-30 11:29:37 +00:00
|
|
|
schema_spec = content_spec.get('schema')
|
2021-04-23 11:36:27 +00:00
|
|
|
assert bool(schema_spec) == bool(schema)
|
2021-03-30 11:29:37 +00:00
|
|
|
|
|
|
|
if not schema_spec:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# @todo: test with defererence
|
|
|
|
if '$ref' in schema_spec:
|
|
|
|
continue
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
assert schema['type'] == schema_spec['type']
|
|
|
|
assert schema.getkey('required') == schema_spec.get(
|
|
|
|
'required')
|
2021-03-30 11:29:37 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
request_body = operation.get('requestBody')
|
2019-10-19 09:55:32 +00:00
|
|
|
request_body_spec = operation_spec.get('requestBody')
|
2021-04-23 11:36:27 +00:00
|
|
|
assert bool(request_body_spec) == bool(request_body)
|
2019-10-19 09:55:32 +00:00
|
|
|
|
|
|
|
if not request_body_spec:
|
|
|
|
continue
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
assert bool(request_body.getkey('required')) ==\
|
|
|
|
request_body_spec.get('required')
|
2019-10-19 09:55:32 +00:00
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
content = request_body / 'content'
|
|
|
|
for mimetype, media_type in iteritems(content):
|
2019-10-19 09:55:32 +00:00
|
|
|
content_spec = request_body_spec['content'][mimetype]
|
|
|
|
schema_spec = content_spec.get('schema')
|
|
|
|
|
|
|
|
if not schema_spec:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# @todo: test with defererence
|
|
|
|
if '$ref' in schema_spec:
|
|
|
|
continue
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
schema = content.get('schema')
|
|
|
|
assert bool(schema_spec) == bool(schema)
|
|
|
|
|
|
|
|
assert schema.type.value ==\
|
2019-10-19 09:55:32 +00:00
|
|
|
schema_spec['type']
|
2021-04-23 11:36:27 +00:00
|
|
|
assert schema.format ==\
|
2019-10-19 09:55:32 +00:00
|
|
|
schema_spec.get('format')
|
2021-04-23 11:36:27 +00:00
|
|
|
assert schema.required == schema_spec.get(
|
2019-10-19 09:55:32 +00:00
|
|
|
'required', False)
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
components = spec.get('components')
|
|
|
|
if not components:
|
2019-10-19 09:55:32 +00:00
|
|
|
return
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
schemas = components.get('schemas', {})
|
|
|
|
for schema_name, schema in iteritems(schemas):
|
2020-02-17 12:40:32 +00:00
|
|
|
schema_spec = spec_dict['components']['schemas'][schema_name]
|
2021-04-23 11:36:27 +00:00
|
|
|
assert schema.getkey('readOnly') == schema_spec.get('readOnly')
|
|
|
|
assert schema.getkey('writeOnly') == schema_spec.get('writeOnly')
|