Validation result datatypes and tests restructure

This commit is contained in:
p1c2u 2019-10-19 10:55:32 +01:00
parent f12b6d8445
commit 48ee8f9f87
14 changed files with 321 additions and 301 deletions

View file

@ -1,10 +1,10 @@
"""OpenAPI core validation models module""" """OpenAPI core validation datatypes module"""
import attr
@attr.s
class BaseValidationResult(object): class BaseValidationResult(object):
errors = attr.ib(factory=list)
def __init__(self, errors):
self.errors = errors
def raise_for_errors(self): def raise_for_errors(self):
for error in self.errors: for error in self.errors:

View file

@ -0,0 +1,21 @@
"""OpenAPI core validation request datatypes module"""
import attr
from openapi_core.validation.datatypes import BaseValidationResult
@attr.s
class RequestParameters(object):
path = attr.ib(factory=dict)
query = attr.ib(factory=dict)
header = attr.ib(factory=dict)
cookie = attr.ib(factory=dict)
def __getitem__(self, location):
return getattr(self, location)
@attr.s
class RequestValidationResult(BaseValidationResult):
body = attr.ib(default=None)
parameters = attr.ib(factory=RequestParameters)

View file

@ -1,41 +0,0 @@
"""OpenAPI core validation request models module"""
from openapi_core.schema.exceptions import OpenAPIMappingError
from openapi_core.validation.models import BaseValidationResult
class RequestParameters(dict):
valid_locations = ['path', 'query', 'header', 'cookie']
def __getitem__(self, location):
self.validate_location(location)
return self.setdefault(location, {})
def __setitem__(self, location, value):
raise NotImplementedError
def __add__(self, other):
if not isinstance(other, self.__class__):
raise ValueError("Invalid type")
for location in self.valid_locations:
if location in other:
self[location].update(other[location])
return self
@classmethod
def validate_location(cls, location):
if location not in cls.valid_locations:
raise OpenAPIMappingError(
"Unknown parameter location: {0}".format(str(location)))
class RequestValidationResult(BaseValidationResult):
def __init__(self, errors, body=None, parameters=None):
super(RequestValidationResult, self).__init__(errors)
self.body = body
self.parameters = parameters or RequestParameters()

View file

@ -4,7 +4,7 @@ from six import iteritems
from openapi_core.schema.exceptions import OpenAPIMappingError from openapi_core.schema.exceptions import OpenAPIMappingError
from openapi_core.schema.parameters.exceptions import MissingParameter from openapi_core.schema.parameters.exceptions import MissingParameter
from openapi_core.validation.request.models import ( from openapi_core.validation.request.datatypes import (
RequestParameters, RequestValidationResult, RequestParameters, RequestValidationResult,
) )
from openapi_core.validation.util import get_operation_pattern from openapi_core.validation.util import get_operation_pattern

View file

@ -0,0 +1,10 @@
"""OpenAPI core validation response datatypes module"""
import attr
from openapi_core.validation.datatypes import BaseValidationResult
@attr.s
class ResponseValidationResult(BaseValidationResult):
data = attr.ib(default=None)
headers = attr.ib(factory=dict)

View file

@ -1,10 +0,0 @@
"""OpenAPI core validation response models module"""
from openapi_core.validation.models import BaseValidationResult
class ResponseValidationResult(BaseValidationResult):
def __init__(self, errors, data=None, headers=None):
super(ResponseValidationResult, self).__init__(errors)
self.data = data
self.headers = headers

View file

@ -1,6 +1,6 @@
"""OpenAPI core validation response validators module""" """OpenAPI core validation response validators module"""
from openapi_core.schema.exceptions import OpenAPIMappingError from openapi_core.schema.exceptions import OpenAPIMappingError
from openapi_core.validation.response.models import ResponseValidationResult from openapi_core.validation.response.datatypes import ResponseValidationResult
from openapi_core.validation.util import get_operation_pattern from openapi_core.validation.util import get_operation_pattern

View file

@ -0,0 +1,191 @@
import pytest
from base64 import b64encode
from six import iteritems, text_type
from openapi_core.schema.media_types.models import MediaType
from openapi_core.schema.operations.models import Operation
from openapi_core.schema.parameters.models import Parameter
from openapi_core.schema.paths.models import Path
from openapi_core.schema.request_bodies.models import RequestBody
from openapi_core.schema.responses.models import Response
from openapi_core.schema.schemas.models import Schema
from openapi_core.schema.servers.models import Server, ServerVariable
from openapi_core.shortcuts import create_spec
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'
assert spec.info.title == spec_dict['info']['title']
assert spec.info.version == spec_dict['info']['version']
assert spec.get_server_url() == url
for idx, server in enumerate(spec.servers):
assert type(server) == Server
server_spec = spec_dict['servers'][idx]
assert server.url == server_spec['url']
assert server.default_url == url
for variable_name, variable in iteritems(server.variables):
assert type(variable) == ServerVariable
assert variable.name == variable_name
variable_spec = server_spec['variables'][variable_name]
assert variable.default == variable_spec['default']
assert variable.enum == variable_spec.get('enum')
for path_name, path in iteritems(spec.paths):
assert type(path) == Path
assert path.name == path_name
for http_method, operation in iteritems(path.operations):
operation_spec = spec_dict['paths'][path_name][http_method]
assert type(operation) == Operation
assert operation.path_name == path_name
assert operation.http_method == http_method
assert operation.operation_id is not None
assert operation.tags == operation_spec['tags']
responses_spec = operation_spec.get('responses')
for http_status, response in iteritems(operation.responses):
assert type(response) == Response
assert response.http_status == http_status
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']
assert response.description == description_spec
for mimetype, media_type in iteritems(response.content):
assert type(media_type) == MediaType
assert media_type.mimetype == mimetype
content_spec = response_spec['content'][mimetype]
example_spec = content_spec.get('example')
assert media_type.example == example_spec
schema_spec = content_spec.get('schema')
assert bool(schema_spec) == bool(media_type.schema)
if not schema_spec:
continue
# @todo: test with defererence
if '$ref' in schema_spec:
continue
assert type(media_type.schema) == Schema
assert media_type.schema.type.value ==\
schema_spec['type']
assert media_type.schema.required == schema_spec.get(
'required', [])
for parameter_name, parameter in iteritems(
response.headers):
assert type(parameter) == Parameter
assert parameter.name == parameter_name
headers_spec = response_spec['headers']
parameter_spec = headers_spec[parameter_name]
schema_spec = parameter_spec.get('schema')
assert bool(schema_spec) == bool(parameter.schema)
if not schema_spec:
continue
# @todo: test with defererence
if '$ref' in schema_spec:
continue
assert type(parameter.schema) == Schema
assert parameter.schema.type.value ==\
schema_spec['type']
assert parameter.schema.format ==\
schema_spec.get('format')
assert parameter.schema.required == schema_spec.get(
'required', [])
request_body_spec = operation_spec.get('requestBody')
assert bool(request_body_spec) == bool(operation.request_body)
if not request_body_spec:
continue
assert type(operation.request_body) == RequestBody
assert bool(operation.request_body.required) ==\
request_body_spec.get('required', False)
for mimetype, media_type in iteritems(
operation.request_body.content):
assert type(media_type) == MediaType
assert media_type.mimetype == mimetype
content_spec = request_body_spec['content'][mimetype]
schema_spec = content_spec.get('schema')
assert bool(schema_spec) == bool(media_type.schema)
if not schema_spec:
continue
# @todo: test with defererence
if '$ref' in schema_spec:
continue
assert type(media_type.schema) == Schema
assert media_type.schema.type.value ==\
schema_spec['type']
assert media_type.schema.format ==\
schema_spec.get('format')
assert media_type.schema.required == schema_spec.get(
'required', False)
if not spec.components:
return
for _, schema in iteritems(spec.components.schemas):
assert type(schema) == Schema

View file

@ -47,7 +47,7 @@ class TestMinimal(object):
assert len(result.errors) == 1 assert len(result.errors) == 1
assert isinstance(result.errors[0], InvalidOperation) assert isinstance(result.errors[0], InvalidOperation)
assert result.body is None assert result.body is None
assert result.parameters == {} assert result.parameters is None
@pytest.mark.parametrize("server", servers) @pytest.mark.parametrize("server", servers)
@pytest.mark.parametrize("spec_path", spec_paths) @pytest.mark.parametrize("spec_path", spec_paths)
@ -62,4 +62,4 @@ class TestMinimal(object):
assert len(result.errors) == 1 assert len(result.errors) == 1
assert isinstance(result.errors[0], InvalidPath) assert isinstance(result.errors[0], InvalidPath)
assert result.body is None assert result.body is None
assert result.parameters == {} assert result.parameters is None

View file

@ -3,27 +3,20 @@ import pytest
from datetime import datetime from datetime import datetime
from base64 import b64encode from base64 import b64encode
from uuid import UUID from uuid import UUID
from six import iteritems, text_type from six import text_type
from openapi_core.extensions.models.models import BaseModel from openapi_core.extensions.models.models import BaseModel
from openapi_core.schema.media_types.exceptions import ( from openapi_core.schema.media_types.exceptions import (
InvalidContentType, InvalidMediaTypeValue, InvalidContentType, InvalidMediaTypeValue,
) )
from openapi_core.schema.media_types.models import MediaType
from openapi_core.schema.operations.models import Operation
from openapi_core.schema.parameters.exceptions import ( from openapi_core.schema.parameters.exceptions import (
MissingRequiredParameter, InvalidParameterValue, EmptyParameterValue, MissingRequiredParameter, InvalidParameterValue, EmptyParameterValue,
) )
from openapi_core.schema.parameters.models import Parameter
from openapi_core.schema.paths.models import Path
from openapi_core.schema.request_bodies.models import RequestBody
from openapi_core.schema.responses.models import Response
from openapi_core.schema.schemas.enums import SchemaType from openapi_core.schema.schemas.enums import SchemaType
from openapi_core.schema.schemas.exceptions import InvalidSchemaValue from openapi_core.schema.schemas.exceptions import InvalidSchemaValue
from openapi_core.schema.schemas.models import Schema
from openapi_core.schema.servers.exceptions import InvalidServer from openapi_core.schema.servers.exceptions import InvalidServer
from openapi_core.schema.servers.models import Server, ServerVariable
from openapi_core.shortcuts import create_spec from openapi_core.shortcuts import create_spec
from openapi_core.validation.request.datatypes import RequestParameters
from openapi_core.validation.request.validators import RequestValidator from openapi_core.validation.request.validators import RequestValidator
from openapi_core.validation.response.validators import ResponseValidator from openapi_core.validation.response.validators import ResponseValidator
from openapi_core.wrappers.mock import MockRequest, MockResponse from openapi_core.wrappers.mock import MockRequest, MockResponse
@ -59,151 +52,6 @@ class TestPetstore(object):
def response_validator(self, spec): def response_validator(self, spec):
return ResponseValidator(spec) return ResponseValidator(spec)
def test_spec(self, spec, spec_dict):
url = 'http://petstore.swagger.io/v1'
assert spec.info.title == spec_dict['info']['title']
assert spec.info.version == spec_dict['info']['version']
assert spec.get_server_url() == url
for idx, server in enumerate(spec.servers):
assert type(server) == Server
server_spec = spec_dict['servers'][idx]
assert server.url == server_spec['url']
assert server.default_url == url
for variable_name, variable in iteritems(server.variables):
assert type(variable) == ServerVariable
assert variable.name == variable_name
variable_spec = server_spec['variables'][variable_name]
assert variable.default == variable_spec['default']
assert variable.enum == variable_spec.get('enum')
for path_name, path in iteritems(spec.paths):
assert type(path) == Path
assert path.name == path_name
for http_method, operation in iteritems(path.operations):
operation_spec = spec_dict['paths'][path_name][http_method]
assert type(operation) == Operation
assert operation.path_name == path_name
assert operation.http_method == http_method
assert operation.operation_id is not None
assert operation.tags == operation_spec['tags']
responses_spec = operation_spec.get('responses')
for http_status, response in iteritems(operation.responses):
assert type(response) == Response
assert response.http_status == http_status
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']
assert response.description == description_spec
for mimetype, media_type in iteritems(response.content):
assert type(media_type) == MediaType
assert media_type.mimetype == mimetype
content_spec = response_spec['content'][mimetype]
example_spec = content_spec.get('example')
assert media_type.example == example_spec
schema_spec = content_spec.get('schema')
assert bool(schema_spec) == bool(media_type.schema)
if not schema_spec:
continue
# @todo: test with defererence
if '$ref' in schema_spec:
continue
assert type(media_type.schema) == Schema
assert media_type.schema.type.value ==\
schema_spec['type']
assert media_type.schema.required == schema_spec.get(
'required', [])
for parameter_name, parameter in iteritems(
response.headers):
assert type(parameter) == Parameter
assert parameter.name == parameter_name
headers_spec = response_spec['headers']
parameter_spec = headers_spec[parameter_name]
schema_spec = parameter_spec.get('schema')
assert bool(schema_spec) == bool(parameter.schema)
if not schema_spec:
continue
# @todo: test with defererence
if '$ref' in schema_spec:
continue
assert type(parameter.schema) == Schema
assert parameter.schema.type.value ==\
schema_spec['type']
assert parameter.schema.format ==\
schema_spec.get('format')
assert parameter.schema.required == schema_spec.get(
'required', [])
request_body_spec = operation_spec.get('requestBody')
assert bool(request_body_spec) == bool(operation.request_body)
if not request_body_spec:
continue
assert type(operation.request_body) == RequestBody
assert bool(operation.request_body.required) ==\
request_body_spec.get('required', False)
for mimetype, media_type in iteritems(
operation.request_body.content):
assert type(media_type) == MediaType
assert media_type.mimetype == mimetype
content_spec = request_body_spec['content'][mimetype]
schema_spec = content_spec.get('schema')
assert bool(schema_spec) == bool(media_type.schema)
if not schema_spec:
continue
# @todo: test with defererence
if '$ref' in schema_spec:
continue
assert type(media_type.schema) == Schema
assert media_type.schema.type.value ==\
schema_spec['type']
assert media_type.schema.format ==\
schema_spec.get('format')
assert media_type.schema.required == schema_spec.get(
'required', False)
if not spec.components:
return
for schema_name, schema in iteritems(spec.components.schemas):
assert type(schema) == Schema
def test_get_pets(self, spec, response_validator): def test_get_pets(self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1' host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets' path_pattern = '/v1/pets'
@ -219,13 +67,13 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
body = request.get_body(spec) body = request.get_body(spec)
assert parameters == { assert parameters == RequestParameters(
'query': { query={
'limit': 20, 'limit': 20,
'page': 1, 'page': 1,
'search': '', 'search': '',
} }
} )
assert body is None assert body is None
data_json = { data_json = {
@ -255,13 +103,13 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
body = request.get_body(spec) body = request.get_body(spec)
assert parameters == { assert parameters == RequestParameters(
'query': { query={
'limit': 20, 'limit': 20,
'page': 1, 'page': 1,
'search': '', 'search': '',
} }
} )
assert body is None assert body is None
data_json = { data_json = {
@ -301,13 +149,13 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
body = request.get_body(spec) body = request.get_body(spec)
assert parameters == { assert parameters == RequestParameters(
'query': { query={
'limit': 20, 'limit': 20,
'page': 1, 'page': 1,
'search': '', 'search': '',
} }
} )
assert body is None assert body is None
data_json = { data_json = {
@ -352,14 +200,14 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
body = request.get_body(spec) body = request.get_body(spec)
assert parameters == { assert parameters == RequestParameters(
'query': { query={
'limit': 20, 'limit': 20,
'page': 1, 'page': 1,
'search': '', 'search': '',
'ids': [12, 13], 'ids': [12, 13],
} }
} )
assert body is None assert body is None
data_json = { data_json = {
@ -390,14 +238,14 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
body = request.get_body(spec) body = request.get_body(spec)
assert parameters == { assert parameters == RequestParameters(
'query': { query={
'limit': 20, 'limit': 20,
'page': 1, 'page': 1,
'search': '', 'search': '',
'tags': ['cats', 'dogs'], 'tags': ['cats', 'dogs'],
} }
} )
assert body is None assert body is None
data_json = { data_json = {
@ -498,13 +346,13 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == { assert parameters == RequestParameters(
'query': { query={
'limit': None, 'limit': None,
'page': 1, 'page': 1,
'search': '', 'search': '',
} }
} )
body = request.get_body(spec) body = request.get_body(spec)
@ -547,14 +395,14 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == { assert parameters == RequestParameters(
'header': { header={
'api_key': self.api_key, 'api_key': self.api_key,
}, },
'cookie': { cookie={
'user': 123, 'user': 123,
}, },
} )
body = request.get_body(spec) body = request.get_body(spec)
@ -607,14 +455,14 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == { assert parameters == RequestParameters(
'header': { header={
'api_key': self.api_key, 'api_key': self.api_key,
}, },
'cookie': { cookie={
'user': 123, 'user': 123,
}, },
} )
body = request.get_body(spec) body = request.get_body(spec)
@ -667,14 +515,14 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == { assert parameters == RequestParameters(
'header': { header={
'api_key': self.api_key, 'api_key': self.api_key,
}, },
'cookie': { cookie={
'user': 123, 'user': 123,
}, },
} )
body = request.get_body(spec) body = request.get_body(spec)
@ -715,14 +563,14 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == { assert parameters == RequestParameters(
'header': { header={
'api_key': self.api_key, 'api_key': self.api_key,
}, },
'cookie': { cookie={
'user': 123, 'user': 123,
}, },
} )
with pytest.raises(InvalidMediaTypeValue): with pytest.raises(InvalidMediaTypeValue):
request.get_body(spec) request.get_body(spec)
@ -754,14 +602,14 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == { assert parameters == RequestParameters(
'header': { header={
'api_key': self.api_key, 'api_key': self.api_key,
}, },
'cookie': { cookie={
'user': 123, 'user': 123,
}, },
} )
body = request.get_body(spec) body = request.get_body(spec)
@ -795,14 +643,14 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == { assert parameters == RequestParameters(
'header': { header={
'api_key': self.api_key, 'api_key': self.api_key,
}, },
'cookie': { cookie={
'user': 123, 'user': 123,
}, },
} )
with pytest.raises(InvalidContentType): with pytest.raises(InvalidContentType):
request.get_body(spec) request.get_body(spec)
@ -915,11 +763,11 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == { assert parameters == RequestParameters(
'path': { path={
'petId': 1, 'petId': 1,
} }
} )
body = request.get_body(spec) body = request.get_body(spec)
@ -960,11 +808,11 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == { assert parameters == RequestParameters(
'path': { path={
'petId': 1, 'petId': 1,
} }
} )
body = request.get_body(spec) body = request.get_body(spec)
@ -1002,11 +850,11 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == { assert parameters == RequestParameters(
'path': { path={
'petId': 1, 'petId': 1,
} }
} )
body = request.get_body(spec) body = request.get_body(spec)
@ -1032,7 +880,7 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
body = request.get_body(spec) body = request.get_body(spec)
assert parameters == {} assert parameters == RequestParameters()
assert body is None assert body is None
data_json = ['cats', 'birds'] data_json = ['cats', 'birds']
@ -1062,7 +910,7 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == {} assert parameters == RequestParameters()
with pytest.raises(InvalidMediaTypeValue): with pytest.raises(InvalidMediaTypeValue):
request.get_body(spec) request.get_body(spec)
@ -1080,7 +928,7 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == {} assert parameters == RequestParameters()
with pytest.raises(InvalidMediaTypeValue): with pytest.raises(InvalidMediaTypeValue):
request.get_body(spec) request.get_body(spec)
@ -1098,7 +946,7 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
assert parameters == {} assert parameters == RequestParameters()
with pytest.raises(InvalidMediaTypeValue): with pytest.raises(InvalidMediaTypeValue):
request.get_body(spec) request.get_body(spec)
@ -1121,7 +969,7 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
body = request.get_body(spec) body = request.get_body(spec)
assert parameters == {} assert parameters == RequestParameters()
assert isinstance(body, BaseModel) assert isinstance(body, BaseModel)
assert body.name == pet_name assert body.name == pet_name
@ -1167,7 +1015,7 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
body = request.get_body(spec) body = request.get_body(spec)
assert parameters == {} assert parameters == RequestParameters()
assert isinstance(body, BaseModel) assert isinstance(body, BaseModel)
assert body.created == created assert body.created == created
assert body.name == pet_name assert body.name == pet_name
@ -1214,7 +1062,7 @@ class TestPetstore(object):
parameters = request.get_parameters(spec) parameters = request.get_parameters(spec)
body = request.get_body(spec) body = request.get_body(spec)
assert parameters == {} assert parameters == RequestParameters()
assert isinstance(body, BaseModel) assert isinstance(body, BaseModel)
assert body.created == datetime(2016, 4, 16, 16, 6, 5) assert body.created == datetime(2016, 4, 16, 16, 6, 5)
assert body.name == pet_name assert body.name == pet_name
@ -1262,7 +1110,7 @@ class TestPetstore(object):
with pytest.raises(InvalidMediaTypeValue): with pytest.raises(InvalidMediaTypeValue):
request.get_body(spec) request.get_body(spec)
assert parameters == {} assert parameters == RequestParameters()
code = 400 code = 400
message = 'Bad request' message = 'Bad request'

View file

@ -17,6 +17,7 @@ from openapi_core.schema.responses.exceptions import (
) )
from openapi_core.schema.servers.exceptions import InvalidServer from openapi_core.schema.servers.exceptions import InvalidServer
from openapi_core.shortcuts import create_spec from openapi_core.shortcuts import create_spec
from openapi_core.validation.request.datatypes import RequestParameters
from openapi_core.validation.request.validators import RequestValidator from openapi_core.validation.request.validators import RequestValidator
from openapi_core.validation.response.validators import ResponseValidator from openapi_core.validation.response.validators import ResponseValidator
from openapi_core.wrappers.mock import MockRequest, MockResponse from openapi_core.wrappers.mock import MockRequest, MockResponse
@ -54,7 +55,7 @@ class TestRequestValidator(object):
assert len(result.errors) == 1 assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidServer assert type(result.errors[0]) == InvalidServer
assert result.body is None assert result.body is None
assert result.parameters == {} assert result.parameters is None
def test_invalid_path(self, validator): def test_invalid_path(self, validator):
request = MockRequest(self.host_url, 'get', '/v1') request = MockRequest(self.host_url, 'get', '/v1')
@ -64,7 +65,7 @@ class TestRequestValidator(object):
assert len(result.errors) == 1 assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidPath assert type(result.errors[0]) == InvalidPath
assert result.body is None assert result.body is None
assert result.parameters == {} assert result.parameters is None
def test_invalid_operation(self, validator): def test_invalid_operation(self, validator):
request = MockRequest(self.host_url, 'patch', '/v1/pets') request = MockRequest(self.host_url, 'patch', '/v1/pets')
@ -74,7 +75,7 @@ class TestRequestValidator(object):
assert len(result.errors) == 1 assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidOperation assert type(result.errors[0]) == InvalidOperation
assert result.body is None assert result.body is None
assert result.parameters == {} assert result.parameters is None
def test_missing_parameter(self, validator): def test_missing_parameter(self, validator):
request = MockRequest(self.host_url, 'get', '/v1/pets') request = MockRequest(self.host_url, 'get', '/v1/pets')
@ -83,12 +84,12 @@ class TestRequestValidator(object):
assert type(result.errors[0]) == MissingRequiredParameter assert type(result.errors[0]) == MissingRequiredParameter
assert result.body is None assert result.body is None
assert result.parameters == { assert result.parameters == RequestParameters(
'query': { query={
'page': 1, 'page': 1,
'search': '', 'search': '',
}, },
} )
def test_get_pets(self, validator): def test_get_pets(self, validator):
request = MockRequest( request = MockRequest(
@ -100,13 +101,13 @@ class TestRequestValidator(object):
assert result.errors == [] assert result.errors == []
assert result.body is None assert result.body is None
assert result.parameters == { assert result.parameters == RequestParameters(
'query': { query={
'limit': 10, 'limit': 10,
'page': 1, 'page': 1,
'search': '', 'search': '',
}, },
} )
def test_missing_body(self, validator): def test_missing_body(self, validator):
headers = { headers = {
@ -126,14 +127,14 @@ 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 == RequestParameters(
'header': { header={
'api_key': self.api_key, 'api_key': self.api_key,
}, },
'cookie': { cookie={
'user': 123, 'user': 123,
}, },
} )
def test_invalid_content_type(self, validator): def test_invalid_content_type(self, validator):
headers = { headers = {
@ -153,14 +154,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 == RequestParameters(
'header': { header={
'api_key': self.api_key, 'api_key': self.api_key,
}, },
'cookie': { cookie={
'user': 123, 'user': 123,
}, },
} )
def test_post_pets(self, validator, spec_dict): def test_post_pets(self, validator, spec_dict):
pet_name = 'Cat' pet_name = 'Cat'
@ -195,14 +196,14 @@ class TestRequestValidator(object):
result = validator.validate(request) result = validator.validate(request)
assert result.errors == [] assert result.errors == []
assert result.parameters == { assert result.parameters == RequestParameters(
'header': { header={
'api_key': self.api_key, 'api_key': self.api_key,
}, },
'cookie': { cookie={
'user': 123, 'user': 123,
}, },
} )
schemas = spec_dict['components']['schemas'] schemas = spec_dict['components']['schemas']
pet_model = schemas['PetCreate']['x-model'] pet_model = schemas['PetCreate']['x-model']
@ -225,11 +226,11 @@ class TestRequestValidator(object):
assert result.errors == [] assert result.errors == []
assert result.body is None assert result.body is None
assert result.parameters == { assert result.parameters == RequestParameters(
'path': { path={
'petId': 1, 'petId': 1,
}, },
} )
class TestPathItemParamsValidator(object): class TestPathItemParamsValidator(object):
@ -280,7 +281,7 @@ class TestPathItemParamsValidator(object):
assert len(result.errors) == 1 assert len(result.errors) == 1
assert type(result.errors[0]) == MissingRequiredParameter assert type(result.errors[0]) == MissingRequiredParameter
assert result.body is None assert result.body is None
assert result.parameters == {} assert result.parameters == RequestParameters()
def test_request_invalid_param(self, validator): def test_request_invalid_param(self, validator):
request = MockRequest( request = MockRequest(
@ -292,7 +293,7 @@ class TestPathItemParamsValidator(object):
assert len(result.errors) == 1 assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidParameterValue assert type(result.errors[0]) == InvalidParameterValue
assert result.body is None assert result.body is None
assert result.parameters == {} assert result.parameters == RequestParameters()
def test_request_valid_param(self, validator): def test_request_valid_param(self, validator):
request = MockRequest( request = MockRequest(
@ -303,7 +304,7 @@ class TestPathItemParamsValidator(object):
assert len(result.errors) == 0 assert len(result.errors) == 0
assert result.body is None assert result.body is None
assert result.parameters == {'query': {'resId': 10}} assert result.parameters == RequestParameters(query={'resId': 10})
def test_request_override_param(self, spec_dict): def test_request_override_param(self, spec_dict):
# override path parameter on operation # override path parameter on operation
@ -324,7 +325,7 @@ class TestPathItemParamsValidator(object):
assert len(result.errors) == 0 assert len(result.errors) == 0
assert result.body is None assert result.body is None
assert result.parameters == {} assert result.parameters == RequestParameters()
def test_request_override_param_uniqueness(self, spec_dict): def test_request_override_param_uniqueness(self, spec_dict):
# add parameter on operation with same name as on path but # add parameter on operation with same name as on path but
@ -347,7 +348,7 @@ class TestPathItemParamsValidator(object):
assert len(result.errors) == 1 assert len(result.errors) == 1
assert type(result.errors[0]) == MissingRequiredParameter assert type(result.errors[0]) == MissingRequiredParameter
assert result.body is None assert result.body is None
assert result.parameters == {} assert result.parameters == RequestParameters()
class TestResponseValidator(object): class TestResponseValidator(object):