diff --git a/openapi_core/validation/models.py b/openapi_core/validation/datatypes.py similarity index 54% rename from openapi_core/validation/models.py rename to openapi_core/validation/datatypes.py index e6366bb..0bf8676 100644 --- a/openapi_core/validation/models.py +++ b/openapi_core/validation/datatypes.py @@ -1,10 +1,10 @@ -"""OpenAPI core validation models module""" +"""OpenAPI core validation datatypes module""" +import attr +@attr.s class BaseValidationResult(object): - - def __init__(self, errors): - self.errors = errors + errors = attr.ib(factory=list) def raise_for_errors(self): for error in self.errors: diff --git a/openapi_core/validation/request/datatypes.py b/openapi_core/validation/request/datatypes.py new file mode 100644 index 0000000..2e5b8ea --- /dev/null +++ b/openapi_core/validation/request/datatypes.py @@ -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) diff --git a/openapi_core/validation/request/models.py b/openapi_core/validation/request/models.py deleted file mode 100644 index 85ebc11..0000000 --- a/openapi_core/validation/request/models.py +++ /dev/null @@ -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() diff --git a/openapi_core/validation/request/validators.py b/openapi_core/validation/request/validators.py index ba4851e..a6d8e78 100644 --- a/openapi_core/validation/request/validators.py +++ b/openapi_core/validation/request/validators.py @@ -4,7 +4,7 @@ from six import iteritems from openapi_core.schema.exceptions import OpenAPIMappingError from openapi_core.schema.parameters.exceptions import MissingParameter -from openapi_core.validation.request.models import ( +from openapi_core.validation.request.datatypes import ( RequestParameters, RequestValidationResult, ) from openapi_core.validation.util import get_operation_pattern diff --git a/openapi_core/validation/response/datatypes.py b/openapi_core/validation/response/datatypes.py new file mode 100644 index 0000000..38cfb07 --- /dev/null +++ b/openapi_core/validation/response/datatypes.py @@ -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) diff --git a/openapi_core/validation/response/models.py b/openapi_core/validation/response/models.py deleted file mode 100644 index 1a735c6..0000000 --- a/openapi_core/validation/response/models.py +++ /dev/null @@ -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 diff --git a/openapi_core/validation/response/validators.py b/openapi_core/validation/response/validators.py index 2ed923a..341b294 100644 --- a/openapi_core/validation/response/validators.py +++ b/openapi_core/validation/response/validators.py @@ -1,6 +1,6 @@ """OpenAPI core validation response validators module""" 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 diff --git a/tests/integration/test_empty.py b/tests/integration/schema/test_empty.py similarity index 100% rename from tests/integration/test_empty.py rename to tests/integration/schema/test_empty.py diff --git a/tests/integration/test_link_spec.py b/tests/integration/schema/test_link_spec.py similarity index 100% rename from tests/integration/test_link_spec.py rename to tests/integration/schema/test_link_spec.py diff --git a/tests/integration/test_path_params.py b/tests/integration/schema/test_path_params.py similarity index 100% rename from tests/integration/test_path_params.py rename to tests/integration/schema/test_path_params.py diff --git a/tests/integration/schema/test_spec.py b/tests/integration/schema/test_spec.py new file mode 100644 index 0000000..9ad67c9 --- /dev/null +++ b/tests/integration/schema/test_spec.py @@ -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 diff --git a/tests/integration/test_minimal.py b/tests/integration/validation/test_minimal.py similarity index 96% rename from tests/integration/test_minimal.py rename to tests/integration/validation/test_minimal.py index a307985..936c27b 100644 --- a/tests/integration/test_minimal.py +++ b/tests/integration/validation/test_minimal.py @@ -47,7 +47,7 @@ class TestMinimal(object): assert len(result.errors) == 1 assert isinstance(result.errors[0], InvalidOperation) assert result.body is None - assert result.parameters == {} + assert result.parameters is None @pytest.mark.parametrize("server", servers) @pytest.mark.parametrize("spec_path", spec_paths) @@ -62,4 +62,4 @@ class TestMinimal(object): assert len(result.errors) == 1 assert isinstance(result.errors[0], InvalidPath) assert result.body is None - assert result.parameters == {} + assert result.parameters is None diff --git a/tests/integration/test_petstore.py b/tests/integration/validation/test_petstore.py similarity index 80% rename from tests/integration/test_petstore.py rename to tests/integration/validation/test_petstore.py index 5e57c5e..b07f7f0 100644 --- a/tests/integration/test_petstore.py +++ b/tests/integration/validation/test_petstore.py @@ -3,27 +3,20 @@ import pytest from datetime import datetime from base64 import b64encode 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.schema.media_types.exceptions import ( 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 ( 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.exceptions import InvalidSchemaValue -from openapi_core.schema.schemas.models import Schema 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.validation.request.datatypes import RequestParameters from openapi_core.validation.request.validators import RequestValidator from openapi_core.validation.response.validators import ResponseValidator from openapi_core.wrappers.mock import MockRequest, MockResponse @@ -59,151 +52,6 @@ class TestPetstore(object): 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_name, schema in iteritems(spec.components.schemas): - assert type(schema) == Schema - def test_get_pets(self, spec, response_validator): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets' @@ -219,13 +67,13 @@ class TestPetstore(object): parameters = request.get_parameters(spec) body = request.get_body(spec) - assert parameters == { - 'query': { + assert parameters == RequestParameters( + query={ 'limit': 20, 'page': 1, 'search': '', } - } + ) assert body is None data_json = { @@ -255,13 +103,13 @@ class TestPetstore(object): parameters = request.get_parameters(spec) body = request.get_body(spec) - assert parameters == { - 'query': { + assert parameters == RequestParameters( + query={ 'limit': 20, 'page': 1, 'search': '', } - } + ) assert body is None data_json = { @@ -301,13 +149,13 @@ class TestPetstore(object): parameters = request.get_parameters(spec) body = request.get_body(spec) - assert parameters == { - 'query': { + assert parameters == RequestParameters( + query={ 'limit': 20, 'page': 1, 'search': '', } - } + ) assert body is None data_json = { @@ -352,14 +200,14 @@ class TestPetstore(object): parameters = request.get_parameters(spec) body = request.get_body(spec) - assert parameters == { - 'query': { + assert parameters == RequestParameters( + query={ 'limit': 20, 'page': 1, 'search': '', 'ids': [12, 13], } - } + ) assert body is None data_json = { @@ -390,14 +238,14 @@ class TestPetstore(object): parameters = request.get_parameters(spec) body = request.get_body(spec) - assert parameters == { - 'query': { + assert parameters == RequestParameters( + query={ 'limit': 20, 'page': 1, 'search': '', 'tags': ['cats', 'dogs'], } - } + ) assert body is None data_json = { @@ -498,13 +346,13 @@ class TestPetstore(object): parameters = request.get_parameters(spec) - assert parameters == { - 'query': { + assert parameters == RequestParameters( + query={ 'limit': None, 'page': 1, 'search': '', } - } + ) body = request.get_body(spec) @@ -547,14 +395,14 @@ class TestPetstore(object): parameters = request.get_parameters(spec) - assert parameters == { - 'header': { + assert parameters == RequestParameters( + header={ 'api_key': self.api_key, }, - 'cookie': { + cookie={ 'user': 123, }, - } + ) body = request.get_body(spec) @@ -607,14 +455,14 @@ class TestPetstore(object): parameters = request.get_parameters(spec) - assert parameters == { - 'header': { + assert parameters == RequestParameters( + header={ 'api_key': self.api_key, }, - 'cookie': { + cookie={ 'user': 123, }, - } + ) body = request.get_body(spec) @@ -667,14 +515,14 @@ class TestPetstore(object): parameters = request.get_parameters(spec) - assert parameters == { - 'header': { + assert parameters == RequestParameters( + header={ 'api_key': self.api_key, }, - 'cookie': { + cookie={ 'user': 123, }, - } + ) body = request.get_body(spec) @@ -715,14 +563,14 @@ class TestPetstore(object): parameters = request.get_parameters(spec) - assert parameters == { - 'header': { + assert parameters == RequestParameters( + header={ 'api_key': self.api_key, }, - 'cookie': { + cookie={ 'user': 123, }, - } + ) with pytest.raises(InvalidMediaTypeValue): request.get_body(spec) @@ -754,14 +602,14 @@ class TestPetstore(object): parameters = request.get_parameters(spec) - assert parameters == { - 'header': { + assert parameters == RequestParameters( + header={ 'api_key': self.api_key, }, - 'cookie': { + cookie={ 'user': 123, }, - } + ) body = request.get_body(spec) @@ -795,14 +643,14 @@ class TestPetstore(object): parameters = request.get_parameters(spec) - assert parameters == { - 'header': { + assert parameters == RequestParameters( + header={ 'api_key': self.api_key, }, - 'cookie': { + cookie={ 'user': 123, }, - } + ) with pytest.raises(InvalidContentType): request.get_body(spec) @@ -915,11 +763,11 @@ class TestPetstore(object): parameters = request.get_parameters(spec) - assert parameters == { - 'path': { + assert parameters == RequestParameters( + path={ 'petId': 1, } - } + ) body = request.get_body(spec) @@ -960,11 +808,11 @@ class TestPetstore(object): parameters = request.get_parameters(spec) - assert parameters == { - 'path': { + assert parameters == RequestParameters( + path={ 'petId': 1, } - } + ) body = request.get_body(spec) @@ -1002,11 +850,11 @@ class TestPetstore(object): parameters = request.get_parameters(spec) - assert parameters == { - 'path': { + assert parameters == RequestParameters( + path={ 'petId': 1, } - } + ) body = request.get_body(spec) @@ -1032,7 +880,7 @@ class TestPetstore(object): parameters = request.get_parameters(spec) body = request.get_body(spec) - assert parameters == {} + assert parameters == RequestParameters() assert body is None data_json = ['cats', 'birds'] @@ -1062,7 +910,7 @@ class TestPetstore(object): parameters = request.get_parameters(spec) - assert parameters == {} + assert parameters == RequestParameters() with pytest.raises(InvalidMediaTypeValue): request.get_body(spec) @@ -1080,7 +928,7 @@ class TestPetstore(object): parameters = request.get_parameters(spec) - assert parameters == {} + assert parameters == RequestParameters() with pytest.raises(InvalidMediaTypeValue): request.get_body(spec) @@ -1098,7 +946,7 @@ class TestPetstore(object): parameters = request.get_parameters(spec) - assert parameters == {} + assert parameters == RequestParameters() with pytest.raises(InvalidMediaTypeValue): request.get_body(spec) @@ -1121,7 +969,7 @@ class TestPetstore(object): parameters = request.get_parameters(spec) body = request.get_body(spec) - assert parameters == {} + assert parameters == RequestParameters() assert isinstance(body, BaseModel) assert body.name == pet_name @@ -1167,7 +1015,7 @@ class TestPetstore(object): parameters = request.get_parameters(spec) body = request.get_body(spec) - assert parameters == {} + assert parameters == RequestParameters() assert isinstance(body, BaseModel) assert body.created == created assert body.name == pet_name @@ -1214,7 +1062,7 @@ class TestPetstore(object): parameters = request.get_parameters(spec) body = request.get_body(spec) - assert parameters == {} + assert parameters == RequestParameters() assert isinstance(body, BaseModel) assert body.created == datetime(2016, 4, 16, 16, 6, 5) assert body.name == pet_name @@ -1262,7 +1110,7 @@ class TestPetstore(object): with pytest.raises(InvalidMediaTypeValue): request.get_body(spec) - assert parameters == {} + assert parameters == RequestParameters() code = 400 message = 'Bad request' diff --git a/tests/integration/test_validators.py b/tests/integration/validation/test_validators.py similarity index 93% rename from tests/integration/test_validators.py rename to tests/integration/validation/test_validators.py index 2d3af8f..c696cf1 100644 --- a/tests/integration/test_validators.py +++ b/tests/integration/validation/test_validators.py @@ -17,6 +17,7 @@ from openapi_core.schema.responses.exceptions import ( ) from openapi_core.schema.servers.exceptions import InvalidServer 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.response.validators import ResponseValidator from openapi_core.wrappers.mock import MockRequest, MockResponse @@ -54,7 +55,7 @@ class TestRequestValidator(object): assert len(result.errors) == 1 assert type(result.errors[0]) == InvalidServer assert result.body is None - assert result.parameters == {} + assert result.parameters is None def test_invalid_path(self, validator): request = MockRequest(self.host_url, 'get', '/v1') @@ -64,7 +65,7 @@ class TestRequestValidator(object): assert len(result.errors) == 1 assert type(result.errors[0]) == InvalidPath assert result.body is None - assert result.parameters == {} + assert result.parameters is None def test_invalid_operation(self, validator): request = MockRequest(self.host_url, 'patch', '/v1/pets') @@ -74,7 +75,7 @@ class TestRequestValidator(object): assert len(result.errors) == 1 assert type(result.errors[0]) == InvalidOperation assert result.body is None - assert result.parameters == {} + assert result.parameters is None def test_missing_parameter(self, validator): request = MockRequest(self.host_url, 'get', '/v1/pets') @@ -83,12 +84,12 @@ class TestRequestValidator(object): assert type(result.errors[0]) == MissingRequiredParameter assert result.body is None - assert result.parameters == { - 'query': { + assert result.parameters == RequestParameters( + query={ 'page': 1, 'search': '', }, - } + ) def test_get_pets(self, validator): request = MockRequest( @@ -100,13 +101,13 @@ class TestRequestValidator(object): assert result.errors == [] assert result.body is None - assert result.parameters == { - 'query': { + assert result.parameters == RequestParameters( + query={ 'limit': 10, 'page': 1, 'search': '', }, - } + ) def test_missing_body(self, validator): headers = { @@ -126,14 +127,14 @@ class TestRequestValidator(object): assert len(result.errors) == 1 assert type(result.errors[0]) == MissingRequestBody assert result.body is None - assert result.parameters == { - 'header': { + assert result.parameters == RequestParameters( + header={ 'api_key': self.api_key, }, - 'cookie': { + cookie={ 'user': 123, }, - } + ) def test_invalid_content_type(self, validator): headers = { @@ -153,14 +154,14 @@ class TestRequestValidator(object): assert len(result.errors) == 1 assert type(result.errors[0]) == InvalidContentType assert result.body is None - assert result.parameters == { - 'header': { + assert result.parameters == RequestParameters( + header={ 'api_key': self.api_key, }, - 'cookie': { + cookie={ 'user': 123, }, - } + ) def test_post_pets(self, validator, spec_dict): pet_name = 'Cat' @@ -195,14 +196,14 @@ class TestRequestValidator(object): result = validator.validate(request) assert result.errors == [] - assert result.parameters == { - 'header': { + assert result.parameters == RequestParameters( + header={ 'api_key': self.api_key, }, - 'cookie': { + cookie={ 'user': 123, }, - } + ) schemas = spec_dict['components']['schemas'] pet_model = schemas['PetCreate']['x-model'] @@ -225,11 +226,11 @@ class TestRequestValidator(object): assert result.errors == [] assert result.body is None - assert result.parameters == { - 'path': { + assert result.parameters == RequestParameters( + path={ 'petId': 1, }, - } + ) class TestPathItemParamsValidator(object): @@ -280,7 +281,7 @@ class TestPathItemParamsValidator(object): assert len(result.errors) == 1 assert type(result.errors[0]) == MissingRequiredParameter assert result.body is None - assert result.parameters == {} + assert result.parameters == RequestParameters() def test_request_invalid_param(self, validator): request = MockRequest( @@ -292,7 +293,7 @@ class TestPathItemParamsValidator(object): assert len(result.errors) == 1 assert type(result.errors[0]) == InvalidParameterValue assert result.body is None - assert result.parameters == {} + assert result.parameters == RequestParameters() def test_request_valid_param(self, validator): request = MockRequest( @@ -303,7 +304,7 @@ class TestPathItemParamsValidator(object): assert len(result.errors) == 0 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): # override path parameter on operation @@ -324,7 +325,7 @@ class TestPathItemParamsValidator(object): assert len(result.errors) == 0 assert result.body is None - assert result.parameters == {} + assert result.parameters == RequestParameters() def test_request_override_param_uniqueness(self, spec_dict): # add parameter on operation with same name as on path but @@ -347,7 +348,7 @@ class TestPathItemParamsValidator(object): assert len(result.errors) == 1 assert type(result.errors[0]) == MissingRequiredParameter assert result.body is None - assert result.parameters == {} + assert result.parameters == RequestParameters() class TestResponseValidator(object):