diff --git a/openapi_core/validation/request/__init__.py b/openapi_core/validation/request/__init__.py index 71a6127f..5df11a56 100644 --- a/openapi_core/validation/request/__init__.py +++ b/openapi_core/validation/request/__init__.py @@ -11,12 +11,16 @@ DetectSpecRequestValidatorProxy, ) from openapi_core.validation.request.proxies import SpecRequestValidatorProxy -from openapi_core.validation.request.validators import RequestBodyValidator from openapi_core.validation.request.validators import ( - RequestParametersValidator, + APICallRequestBodyValidator, ) -from openapi_core.validation.request.validators import RequestSecurityValidator -from openapi_core.validation.request.validators import RequestValidator +from openapi_core.validation.request.validators import ( + APICallRequestParametersValidator, +) +from openapi_core.validation.request.validators import ( + APICallRequestSecurityValidator, +) +from openapi_core.validation.request.validators import APICallRequestValidator from openapi_core.validation.request.validators import V30RequestBodyValidator from openapi_core.validation.request.validators import ( V30RequestParametersValidator, @@ -85,37 +89,41 @@ # spec validators openapi_v30_request_body_validator = SpecRequestValidatorProxy( - RequestBodyValidator, + APICallRequestBodyValidator, schema_unmarshallers_factory=oas30_request_schema_unmarshallers_factory, ) openapi_v30_request_parameters_validator = SpecRequestValidatorProxy( - RequestParametersValidator, + APICallRequestParametersValidator, schema_unmarshallers_factory=oas30_request_schema_unmarshallers_factory, ) openapi_v30_request_security_validator = SpecRequestValidatorProxy( - RequestSecurityValidator, + APICallRequestSecurityValidator, schema_unmarshallers_factory=oas30_request_schema_unmarshallers_factory, ) openapi_v30_request_validator = SpecRequestValidatorProxy( - RequestValidator, + APICallRequestValidator, schema_unmarshallers_factory=oas30_request_schema_unmarshallers_factory, + deprecated="openapi_v30_request_validator", + use="V30RequestValidator", ) openapi_v31_request_body_validator = SpecRequestValidatorProxy( - RequestBodyValidator, + APICallRequestBodyValidator, schema_unmarshallers_factory=oas31_schema_unmarshallers_factory, ) openapi_v31_request_parameters_validator = SpecRequestValidatorProxy( - RequestParametersValidator, + APICallRequestParametersValidator, schema_unmarshallers_factory=oas31_schema_unmarshallers_factory, ) openapi_v31_request_security_validator = SpecRequestValidatorProxy( - RequestSecurityValidator, + APICallRequestSecurityValidator, schema_unmarshallers_factory=oas31_schema_unmarshallers_factory, ) openapi_v31_request_validator = SpecRequestValidatorProxy( - RequestValidator, + APICallRequestValidator, schema_unmarshallers_factory=oas31_schema_unmarshallers_factory, + deprecated="openapi_v31_request_validator", + use="V31RequestValidator", ) # spec validators alias to the latest v3 version diff --git a/openapi_core/validation/request/proxies.py b/openapi_core/validation/request/proxies.py index 1dd3feba..e4d97604 100644 --- a/openapi_core/validation/request/proxies.py +++ b/openapi_core/validation/request/proxies.py @@ -1,5 +1,6 @@ """OpenAPI spec validator validation proxies module.""" import warnings +from typing import TYPE_CHECKING from typing import Any from typing import Iterator from typing import Mapping @@ -11,20 +12,27 @@ from openapi_core.validation.exceptions import ValidatorDetectError from openapi_core.validation.request.datatypes import RequestValidationResult from openapi_core.validation.request.protocols import Request -from openapi_core.validation.request.validators import ( - BaseAPICallRequestValidator, -) + +if TYPE_CHECKING: + from openapi_core.validation.request.validators import ( + BaseAPICallRequestValidator, + ) class SpecRequestValidatorProxy: def __init__( self, - validator_cls: Type[BaseAPICallRequestValidator], + validator_cls: Type["BaseAPICallRequestValidator"], + deprecated: str = "RequestValidator", + use: Optional[str] = None, **validator_kwargs: Any, ): self.validator_cls = validator_cls self.validator_kwargs = validator_kwargs + self.deprecated = deprecated + self.use = use or self.validator_cls.__name__ + def validate( self, spec: Spec, @@ -32,8 +40,7 @@ def validate( base_url: Optional[str] = None, ) -> RequestValidationResult: warnings.warn( - "openapi_request_validator is deprecated. " - f"Use {self.validator_cls.__name__} class instead.", + f"{self.deprecated} is deprecated. Use {self.use} instead.", DeprecationWarning, ) validator = self.validator_cls( diff --git a/openapi_core/validation/request/validators.py b/openapi_core/validation/request/validators.py index 809e3f19..9547cbf3 100644 --- a/openapi_core/validation/request/validators.py +++ b/openapi_core/validation/request/validators.py @@ -65,6 +65,7 @@ from openapi_core.validation.request.protocols import BaseRequest from openapi_core.validation.request.protocols import Request from openapi_core.validation.request.protocols import WebhookRequest +from openapi_core.validation.request.proxies import SpecRequestValidatorProxy from openapi_core.validation.validators import BaseAPICallValidator from openapi_core.validation.validators import BaseValidator from openapi_core.validation.validators import BaseWebhookValidator @@ -105,7 +106,7 @@ def _validate( params = self._get_parameters(request.parameters, operation, path) except ParametersError as exc: params = exc.parameters - params_errors = exc.context + params_errors = exc.errors else: params_errors = [] @@ -154,7 +155,7 @@ def _validate_parameters( params = self._get_parameters(request.parameters, path, operation) except ParametersError as exc: params = exc.parameters - params_errors = exc.context + params_errors = exc.errors else: params_errors = [] @@ -328,7 +329,7 @@ def validate(self, request: WebhookRequest) -> RequestValidationResult: raise NotImplementedError -class RequestBodyValidator(BaseAPICallRequestValidator): +class APICallRequestBodyValidator(BaseAPICallRequestValidator): def validate(self, request: Request) -> RequestValidationResult: try: _, operation, _, _, _ = self._find_path(request) @@ -338,7 +339,7 @@ def validate(self, request: Request) -> RequestValidationResult: return self._validate_body(request, operation) -class RequestParametersValidator(BaseAPICallRequestValidator): +class APICallRequestParametersValidator(BaseAPICallRequestValidator): def validate(self, request: Request) -> RequestValidationResult: try: path, operation, _, path_result, _ = self._find_path(request) @@ -352,7 +353,7 @@ def validate(self, request: Request) -> RequestValidationResult: return self._validate_parameters(request, operation, path) -class RequestSecurityValidator(BaseAPICallRequestValidator): +class APICallRequestSecurityValidator(BaseAPICallRequestValidator): def validate(self, request: Request) -> RequestValidationResult: try: _, operation, _, _, _ = self._find_path(request) @@ -362,7 +363,7 @@ def validate(self, request: Request) -> RequestValidationResult: return self._validate_security(request, operation) -class RequestValidator(BaseAPICallRequestValidator): +class APICallRequestValidator(BaseAPICallRequestValidator): def validate(self, request: Request) -> RequestValidationResult: try: path, operation, _, path_result, _ = self._find_path(request) @@ -426,35 +427,35 @@ def validate(self, request: WebhookRequest) -> RequestValidationResult: return self._validate_security(request, operation) -class V30RequestBodyValidator(RequestBodyValidator): +class V30RequestBodyValidator(APICallRequestBodyValidator): schema_unmarshallers_factory = oas30_request_schema_unmarshallers_factory -class V30RequestParametersValidator(RequestParametersValidator): +class V30RequestParametersValidator(APICallRequestParametersValidator): schema_unmarshallers_factory = oas30_request_schema_unmarshallers_factory -class V30RequestSecurityValidator(RequestSecurityValidator): +class V30RequestSecurityValidator(APICallRequestSecurityValidator): schema_unmarshallers_factory = oas30_request_schema_unmarshallers_factory -class V30RequestValidator(RequestValidator): +class V30RequestValidator(APICallRequestValidator): schema_unmarshallers_factory = oas30_request_schema_unmarshallers_factory -class V31RequestBodyValidator(RequestBodyValidator): +class V31RequestBodyValidator(APICallRequestBodyValidator): schema_unmarshallers_factory = oas31_schema_unmarshallers_factory -class V31RequestParametersValidator(RequestParametersValidator): +class V31RequestParametersValidator(APICallRequestParametersValidator): schema_unmarshallers_factory = oas31_schema_unmarshallers_factory -class V31RequestSecurityValidator(RequestSecurityValidator): +class V31RequestSecurityValidator(APICallRequestSecurityValidator): schema_unmarshallers_factory = oas31_schema_unmarshallers_factory -class V31RequestValidator(RequestValidator): +class V31RequestValidator(APICallRequestValidator): schema_unmarshallers_factory = oas31_schema_unmarshallers_factory path_finder_cls = WebhookPathFinder @@ -477,3 +478,17 @@ class V31WebhookRequestSecurityValidator(WebhookRequestSecurityValidator): class V31WebhookRequestValidator(WebhookRequestValidator): schema_unmarshallers_factory = oas31_schema_unmarshallers_factory path_finder_cls = WebhookPathFinder + + +# backward compatibility +class RequestValidator(SpecRequestValidatorProxy): + def __init__( + self, + schema_unmarshallers_factory: SchemaUnmarshallersFactory, + **kwargs: Any, + ): + super().__init__( + APICallRequestValidator, + schema_unmarshallers_factory=schema_unmarshallers_factory, + **kwargs, + ) diff --git a/openapi_core/validation/response/__init__.py b/openapi_core/validation/response/__init__.py index 08a2de89..fcf265b0 100644 --- a/openapi_core/validation/response/__init__.py +++ b/openapi_core/validation/response/__init__.py @@ -11,11 +11,15 @@ DetectResponseValidatorProxy, ) from openapi_core.validation.response.proxies import SpecResponseValidatorProxy -from openapi_core.validation.response.validators import ResponseDataValidator from openapi_core.validation.response.validators import ( - ResponseHeadersValidator, + APICallResponseDataValidator, +) +from openapi_core.validation.response.validators import ( + APICallResponseHeadersValidator, +) +from openapi_core.validation.response.validators import ( + APICallResponseValidator, ) -from openapi_core.validation.response.validators import ResponseValidator from openapi_core.validation.response.validators import ( V30ResponseDataValidator, ) @@ -72,29 +76,33 @@ # spec validators openapi_v30_response_data_validator = SpecResponseValidatorProxy( - ResponseDataValidator, + APICallResponseDataValidator, schema_unmarshallers_factory=oas30_response_schema_unmarshallers_factory, ) openapi_v30_response_headers_validator = SpecResponseValidatorProxy( - ResponseHeadersValidator, + APICallResponseHeadersValidator, schema_unmarshallers_factory=oas30_response_schema_unmarshallers_factory, ) openapi_v30_response_validator = SpecResponseValidatorProxy( - ResponseValidator, + APICallResponseValidator, schema_unmarshallers_factory=oas30_response_schema_unmarshallers_factory, + deprecated="openapi_v30_response_validator", + use="V30ResponseValidator", ) openapi_v31_response_data_validator = SpecResponseValidatorProxy( - ResponseDataValidator, + APICallResponseDataValidator, schema_unmarshallers_factory=oas31_schema_unmarshallers_factory, ) openapi_v31_response_headers_validator = SpecResponseValidatorProxy( - ResponseHeadersValidator, + APICallResponseHeadersValidator, schema_unmarshallers_factory=oas31_schema_unmarshallers_factory, ) openapi_v31_response_validator = SpecResponseValidatorProxy( - ResponseValidator, + APICallResponseValidator, schema_unmarshallers_factory=oas31_schema_unmarshallers_factory, + deprecated="openapi_v31_response_validator", + use="V31ResponseValidator", ) # spec validators alias to the latest v3 version diff --git a/openapi_core/validation/response/proxies.py b/openapi_core/validation/response/proxies.py index fe399cc6..b4e99469 100644 --- a/openapi_core/validation/response/proxies.py +++ b/openapi_core/validation/response/proxies.py @@ -1,5 +1,6 @@ """OpenAPI spec validator validation proxies module.""" import warnings +from typing import TYPE_CHECKING from typing import Any from typing import Iterator from typing import Mapping @@ -12,20 +13,27 @@ from openapi_core.validation.request.protocols import Request from openapi_core.validation.response.datatypes import ResponseValidationResult from openapi_core.validation.response.protocols import Response -from openapi_core.validation.response.validators import ( - BaseAPICallResponseValidator, -) + +if TYPE_CHECKING: + from openapi_core.validation.response.validators import ( + BaseAPICallResponseValidator, + ) class SpecResponseValidatorProxy: def __init__( self, - validator_cls: Type[BaseAPICallResponseValidator], + validator_cls: Type["BaseAPICallResponseValidator"], + deprecated: str = "ResponseValidator", + use: Optional[str] = None, **validator_kwargs: Any, ): self.validator_cls = validator_cls self.validator_kwargs = validator_kwargs + self.deprecated = deprecated + self.use = use or self.validator_cls.__name__ + def validate( self, spec: Spec, @@ -34,8 +42,7 @@ def validate( base_url: Optional[str] = None, ) -> ResponseValidationResult: warnings.warn( - "openapi_response_validator is deprecated. " - f"Use {self.validator_cls.__name__} class instead.", + f"{self.deprecated} is deprecated. Use {self.use} instead.", DeprecationWarning, ) validator = self.validator_cls( diff --git a/openapi_core/validation/response/validators.py b/openapi_core/validation/response/validators.py index 06422311..d04e9daa 100644 --- a/openapi_core/validation/response/validators.py +++ b/openapi_core/validation/response/validators.py @@ -26,6 +26,9 @@ ) from openapi_core.unmarshalling.schemas.exceptions import UnmarshalError from openapi_core.unmarshalling.schemas.exceptions import ValidateError +from openapi_core.unmarshalling.schemas.factories import ( + SchemaUnmarshallersFactory, +) from openapi_core.util import chainiters from openapi_core.validation.decorators import ValidationErrorWrapper from openapi_core.validation.exceptions import ValidationError @@ -41,6 +44,7 @@ from openapi_core.validation.response.exceptions import MissingHeader from openapi_core.validation.response.exceptions import MissingRequiredHeader from openapi_core.validation.response.protocols import Response +from openapi_core.validation.response.proxies import SpecResponseValidatorProxy from openapi_core.validation.validators import BaseAPICallValidator from openapi_core.validation.validators import BaseValidator from openapi_core.validation.validators import BaseWebhookValidator @@ -259,7 +263,7 @@ def validate( raise NotImplementedError -class ResponseDataValidator(BaseAPICallResponseValidator): +class APICallResponseDataValidator(BaseAPICallResponseValidator): def validate( self, request: Request, @@ -276,7 +280,7 @@ def validate( ) -class ResponseHeadersValidator(BaseAPICallResponseValidator): +class APICallResponseHeadersValidator(BaseAPICallResponseValidator): def validate( self, request: Request, @@ -293,7 +297,7 @@ def validate( ) -class ResponseValidator(BaseAPICallResponseValidator): +class APICallResponseValidator(BaseAPICallResponseValidator): def validate( self, request: Request, @@ -369,27 +373,27 @@ def validate( ) -class V30ResponseDataValidator(ResponseDataValidator): +class V30ResponseDataValidator(APICallResponseDataValidator): schema_unmarshallers_factory = oas30_response_schema_unmarshallers_factory -class V30ResponseHeadersValidator(ResponseHeadersValidator): +class V30ResponseHeadersValidator(APICallResponseHeadersValidator): schema_unmarshallers_factory = oas30_response_schema_unmarshallers_factory -class V30ResponseValidator(ResponseValidator): +class V30ResponseValidator(APICallResponseValidator): schema_unmarshallers_factory = oas30_response_schema_unmarshallers_factory -class V31ResponseDataValidator(ResponseDataValidator): +class V31ResponseDataValidator(APICallResponseDataValidator): schema_unmarshallers_factory = oas31_schema_unmarshallers_factory -class V31ResponseHeadersValidator(ResponseHeadersValidator): +class V31ResponseHeadersValidator(APICallResponseHeadersValidator): schema_unmarshallers_factory = oas31_schema_unmarshallers_factory -class V31ResponseValidator(ResponseValidator): +class V31ResponseValidator(APICallResponseValidator): schema_unmarshallers_factory = oas31_schema_unmarshallers_factory @@ -403,3 +407,17 @@ class V31WebhookResponseHeadersValidator(WebhookResponseHeadersValidator): class V31WebhookResponseValidator(WebhookResponseValidator): schema_unmarshallers_factory = oas31_schema_unmarshallers_factory + + +# backward compatibility +class ResponseValidator(SpecResponseValidatorProxy): + def __init__( + self, + schema_unmarshallers_factory: SchemaUnmarshallersFactory, + **kwargs: Any, + ): + super().__init__( + APICallResponseValidator, + schema_unmarshallers_factory=schema_unmarshallers_factory, + **kwargs, + ) diff --git a/tests/integration/contrib/flask/test_flask_validator.py b/tests/integration/contrib/flask/test_flask_validator.py index 6e929c23..6ccdb3c0 100644 --- a/tests/integration/contrib/flask/test_flask_validator.py +++ b/tests/integration/contrib/flask/test_flask_validator.py @@ -5,7 +5,7 @@ from flask.testing import FlaskClient from flask.wrappers import Response -from openapi_core import openapi_request_validator +from openapi_core import V30RequestValidator from openapi_core.contrib.flask import FlaskOpenAPIRequest @@ -28,7 +28,8 @@ def datails_browse(id): from flask import request openapi_request = FlaskOpenAPIRequest(request) - result = openapi_request_validator.validate(spec, openapi_request) + validator = V30RequestValidator(spec) + result = validator.validate(openapi_request) assert not result.errors if request.args.get("q") == "string": diff --git a/tests/integration/contrib/werkzeug/test_werkzeug_validation.py b/tests/integration/contrib/werkzeug/test_werkzeug_validation.py index 278b7e70..0e8fa5b6 100644 --- a/tests/integration/contrib/werkzeug/test_werkzeug_validation.py +++ b/tests/integration/contrib/werkzeug/test_werkzeug_validation.py @@ -6,8 +6,8 @@ from werkzeug.wrappers import Request from werkzeug.wrappers import Response -from openapi_core import openapi_request_validator -from openapi_core import openapi_response_validator +from openapi_core import V30RequestValidator +from openapi_core import V30ResponseValidator from openapi_core.contrib.werkzeug import WerkzeugOpenAPIRequest from openapi_core.contrib.werkzeug import WerkzeugOpenAPIResponse @@ -53,7 +53,8 @@ def test_request_validator_root_path(self, client, spec): headers=headers, ) openapi_request = WerkzeugOpenAPIRequest(response.request) - result = openapi_request_validator.validate(spec, openapi_request) + validator = V30RequestValidator(spec) + result = validator.validate(openapi_request) assert not result.errors def test_request_validator_path_pattern(self, client, spec): @@ -70,7 +71,8 @@ def test_request_validator_path_pattern(self, client, spec): headers=headers, ) openapi_request = WerkzeugOpenAPIRequest(response.request) - result = openapi_request_validator.validate(spec, openapi_request) + validator = V30RequestValidator(spec) + result = validator.validate(openapi_request) assert not result.errors @responses.activate @@ -89,7 +91,6 @@ def test_response_validator_path_pattern(self, client, spec): ) openapi_request = WerkzeugOpenAPIRequest(response.request) openapi_response = WerkzeugOpenAPIResponse(response) - result = openapi_response_validator.validate( - spec, openapi_request, openapi_response - ) + validator = V30ResponseValidator(spec) + result = validator.validate(openapi_request, openapi_response) assert not result.errors diff --git a/tests/integration/validation/test_minimal.py b/tests/integration/validation/test_minimal.py index 198a8430..03ea2add 100644 --- a/tests/integration/validation/test_minimal.py +++ b/tests/integration/validation/test_minimal.py @@ -1,10 +1,9 @@ import pytest -from openapi_core import openapi_request_validator +from openapi_core import validate_request from openapi_core.templating.paths.exceptions import OperationNotFound from openapi_core.templating.paths.exceptions import PathNotFound from openapi_core.testing import MockRequest -from openapi_core.validation.request.datatypes import Parameters class TestMinimal: @@ -30,7 +29,7 @@ def test_hosts(self, factory, server, spec_path): spec = factory.spec_from_file(spec_path) request = MockRequest(server, "get", "/status") - result = openapi_request_validator.validate(spec, request) + result = validate_request(request, spec=spec) assert not result.errors @@ -40,12 +39,8 @@ def test_invalid_operation(self, factory, server, spec_path): spec = factory.spec_from_file(spec_path) request = MockRequest(server, "post", "/status") - result = openapi_request_validator.validate(spec, request) - - assert len(result.errors) == 1 - assert isinstance(result.errors[0], OperationNotFound) - assert result.body is None - assert result.parameters == Parameters() + with pytest.raises(OperationNotFound): + validate_request(request, spec) @pytest.mark.parametrize("server", servers) @pytest.mark.parametrize("spec_path", spec_paths) @@ -53,9 +48,5 @@ def test_invalid_path(self, factory, server, spec_path): spec = factory.spec_from_file(spec_path) request = MockRequest(server, "get", "/nonexistent") - result = openapi_request_validator.validate(spec, request) - - assert len(result.errors) == 1 - assert isinstance(result.errors[0], PathNotFound) - assert result.body is None - assert result.parameters == Parameters() + with pytest.raises(PathNotFound): + validate_request(request, spec=spec) diff --git a/tests/integration/validation/test_petstore.py b/tests/integration/validation/test_petstore.py index 906c2160..b5a3a8fe 100644 --- a/tests/integration/validation/test_petstore.py +++ b/tests/integration/validation/test_petstore.py @@ -8,7 +8,7 @@ import pytest from isodate.tzinfo import UTC -from openapi_core import openapi_v30_response_validator +from openapi_core import V30ResponseValidator from openapi_core import validate_request from openapi_core import validate_response from openapi_core.casting.schemas.exceptions import CastError @@ -22,25 +22,25 @@ from openapi_core.testing import MockRequest from openapi_core.testing import MockResponse from openapi_core.unmarshalling.schemas.exceptions import InvalidSchemaValue -from openapi_core.validation.request import openapi_v30_request_body_validator -from openapi_core.validation.request import ( - openapi_v30_request_parameters_validator, -) -from openapi_core.validation.request import ( - openapi_v30_request_security_validator, -) from openapi_core.validation.request.datatypes import Parameters from openapi_core.validation.request.exceptions import MissingRequiredParameter from openapi_core.validation.request.exceptions import ParameterError from openapi_core.validation.request.exceptions import RequestBodyError -from openapi_core.validation.response import ( - openapi_v30_response_data_validator, +from openapi_core.validation.request.validators import V30RequestBodyValidator +from openapi_core.validation.request.validators import ( + V30RequestParametersValidator, ) -from openapi_core.validation.response import ( - openapi_v30_response_headers_validator, +from openapi_core.validation.request.validators import ( + V30RequestSecurityValidator, ) from openapi_core.validation.response.exceptions import InvalidData from openapi_core.validation.response.exceptions import MissingRequiredHeader +from openapi_core.validation.response.validators import ( + V30ResponseDataValidator, +) +from openapi_core.validation.response.validators import ( + V30ResponseHeadersValidator, +) class TestPetstore: @@ -66,6 +66,14 @@ def spec_dict(self, factory): def spec(self, spec_dict, spec_uri): return Spec.from_dict(spec_dict, spec_url=spec_uri) + @pytest.fixture(scope="module") + def request_parameters_validator(self, spec): + return V30RequestParametersValidator(spec) + + @pytest.fixture(scope="module") + def response_validator(self, spec): + return V30ResponseValidator(spec) + def test_get_pets(self, spec): host_url = "http://petstore.swagger.io/v1" path_pattern = "/v1/pets" @@ -85,7 +93,7 @@ def test_get_pets(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -99,7 +107,7 @@ def test_get_pets(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_body_validator, + cls=V30RequestBodyValidator, ) assert result.body is None @@ -142,7 +150,7 @@ def test_get_pets_response(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -154,7 +162,7 @@ def test_get_pets_response(self, spec): ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -200,7 +208,7 @@ def test_get_pets_response_no_schema(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -212,7 +220,7 @@ def test_get_pets_response_no_schema(self, spec): ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -226,7 +234,7 @@ def test_get_pets_response_no_schema(self, spec): assert response_result.errors == [] assert response_result.data == data - def test_get_pets_invalid_response(self, spec): + def test_get_pets_invalid_response(self, spec, response_validator): host_url = "http://petstore.swagger.io/v1" path_pattern = "/v1/pets" query_params = { @@ -245,7 +253,7 @@ def test_get_pets_invalid_response(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -257,7 +265,7 @@ def test_get_pets_invalid_response(self, spec): ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -280,13 +288,11 @@ def test_get_pets_invalid_response(self, spec): request, response, spec=spec, - validator=openapi_v30_response_data_validator, + cls=V30ResponseDataValidator, ) assert type(exc_info.value.__cause__) is InvalidSchemaValue - response_result = openapi_v30_response_validator.validate( - spec, request, response - ) + response_result = response_validator.validate(request, response) assert response_result.errors == [InvalidData()] schema_errors = response_result.errors[0].__cause__.schema_errors @@ -317,7 +323,7 @@ def test_get_pets_ids_param(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -330,7 +336,7 @@ def test_get_pets_ids_param(self, spec): ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -367,7 +373,7 @@ def test_get_pets_tags_param(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -380,7 +386,7 @@ def test_get_pets_tags_param(self, spec): ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -418,12 +424,12 @@ def test_get_pets_parameter_deserialization_error(self, spec): validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert type(exc_info.value.__cause__) is DeserializeError result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -448,12 +454,12 @@ def test_get_pets_wrong_parameter_type(self, spec): validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert type(exc_info.value.__cause__) is CastError result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -473,11 +479,11 @@ def test_get_pets_raises_missing_required_param(self, spec): validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -502,12 +508,12 @@ def test_get_pets_empty_value(self, spec): validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert type(exc_info.value.__cause__) is EmptyQueryParameterValue result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -532,7 +538,7 @@ def test_get_pets_allow_empty_value(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -544,7 +550,7 @@ def test_get_pets_allow_empty_value(self, spec): ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -568,7 +574,7 @@ def test_get_pets_none_value(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -580,7 +586,7 @@ def test_get_pets_none_value(self, spec): ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -605,7 +611,7 @@ def test_get_pets_param_order(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -618,7 +624,7 @@ def test_get_pets_param_order(self, spec): ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -647,7 +653,7 @@ def test_get_pets_param_coordinates(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert is_dataclass(result.parameters.query["coordinates"]) @@ -659,7 +665,7 @@ def test_get_pets_param_coordinates(self, spec): assert result.parameters.query["coordinates"].lon == coordinates["lon"] result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -711,7 +717,7 @@ def test_post_birds(self, spec, spec_dict): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert is_dataclass(result.parameters.cookie["userdata"]) @@ -722,7 +728,7 @@ def test_post_birds(self, spec, spec_dict): assert result.parameters.cookie["userdata"].name == "user1" result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) schemas = spec_dict["components"]["schemas"] @@ -740,7 +746,7 @@ def test_post_birds(self, spec, spec_dict): result = validate_request( request, spec=spec, - validator=openapi_v30_request_security_validator, + cls=V30RequestSecurityValidator, ) assert result.security == {} @@ -788,7 +794,7 @@ def test_post_cats(self, spec, spec_dict): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -801,7 +807,7 @@ def test_post_cats(self, spec, spec_dict): ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) schemas = spec_dict["components"]["schemas"] @@ -859,7 +865,7 @@ def test_post_cats_boolean_string(self, spec, spec_dict): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -872,7 +878,7 @@ def test_post_cats_boolean_string(self, spec, spec_dict): ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) schemas = spec_dict["components"]["schemas"] @@ -917,7 +923,7 @@ def test_post_no_one_of_schema(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -933,7 +939,7 @@ def test_post_no_one_of_schema(self, spec): validate_request( request, spec=spec, - validator=openapi_v30_request_body_validator, + cls=V30RequestBodyValidator, ) assert type(exc_info.value.__cause__) is InvalidSchemaValue @@ -969,7 +975,7 @@ def test_post_cats_only_required_body(self, spec, spec_dict): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -982,7 +988,7 @@ def test_post_cats_only_required_body(self, spec, spec_dict): ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) schemas = spec_dict["components"]["schemas"] @@ -1021,7 +1027,7 @@ def test_post_pets_raises_invalid_mimetype(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -1037,7 +1043,7 @@ def test_post_pets_raises_invalid_mimetype(self, spec): validate_request( request, spec=spec, - validator=openapi_v30_request_body_validator, + cls=V30RequestBodyValidator, ) assert type(exc_info.value.__cause__) is MediaTypeNotFound @@ -1070,11 +1076,11 @@ def test_post_pets_missing_cookie(self, spec, spec_dict): validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) schemas = spec_dict["components"]["schemas"] @@ -1113,11 +1119,11 @@ def test_post_pets_missing_header(self, spec, spec_dict): validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) schemas = spec_dict["components"]["schemas"] @@ -1157,14 +1163,14 @@ def test_post_pets_raises_invalid_server_error(self, spec): validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) with pytest.raises(ServerNotFound): validate_request( request, spec=spec, - validator=openapi_v30_request_body_validator, + cls=V30RequestBodyValidator, ) data_id = 1 @@ -1186,7 +1192,7 @@ def test_post_pets_raises_invalid_server_error(self, spec): request, response, spec=spec, - validator=openapi_v30_response_data_validator, + cls=V30ResponseDataValidator, ) def test_get_pet(self, spec): @@ -1211,7 +1217,7 @@ def test_get_pet(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -1221,7 +1227,7 @@ def test_get_pet(self, spec): ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -1229,7 +1235,7 @@ def test_get_pet(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_security_validator, + cls=V30RequestSecurityValidator, ) assert result.security == { @@ -1275,7 +1281,7 @@ def test_get_pet_not_found(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -1285,7 +1291,7 @@ def test_get_pet_not_found(self, spec): ) result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -1326,7 +1332,7 @@ def test_get_pet_wildcard(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters( @@ -1338,7 +1344,7 @@ def test_get_pet_wildcard(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_body_validator, + cls=V30RequestBodyValidator, ) assert result.body is None @@ -1366,13 +1372,13 @@ def test_get_tags(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters() result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -1408,7 +1414,7 @@ def test_post_tags_extra_body_properties(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters() @@ -1417,7 +1423,7 @@ def test_post_tags_extra_body_properties(self, spec): validate_request( request, spec=spec, - validator=openapi_v30_request_body_validator, + cls=V30RequestBodyValidator, ) assert type(exc_info.value.__cause__) is InvalidSchemaValue @@ -1438,7 +1444,7 @@ def test_post_tags_empty_body(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters() @@ -1447,7 +1453,7 @@ def test_post_tags_empty_body(self, spec): validate_request( request, spec=spec, - validator=openapi_v30_request_body_validator, + cls=V30RequestBodyValidator, ) assert type(exc_info.value.__cause__) is InvalidSchemaValue @@ -1468,7 +1474,7 @@ def test_post_tags_wrong_property_type(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters() @@ -1477,7 +1483,7 @@ def test_post_tags_wrong_property_type(self, spec): validate_request( request, spec=spec, - validator=openapi_v30_request_body_validator, + cls=V30RequestBodyValidator, ) assert type(exc_info.value.__cause__) is InvalidSchemaValue @@ -1501,13 +1507,13 @@ def test_post_tags_additional_properties(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters() result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert is_dataclass(result.body) @@ -1557,13 +1563,13 @@ def test_post_tags_created_now(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters() result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert is_dataclass(result.body) @@ -1614,13 +1620,13 @@ def test_post_tags_created_datetime(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters() result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert is_dataclass(result.body) @@ -1646,7 +1652,7 @@ def test_post_tags_created_datetime(self, spec): request, response, spec=spec, - validator=openapi_v30_response_data_validator, + cls=V30ResponseDataValidator, ) assert is_dataclass(result.data) @@ -1686,7 +1692,7 @@ def test_post_tags_created_invalid_type(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters() @@ -1695,7 +1701,7 @@ def test_post_tags_created_invalid_type(self, spec): validate_request( request, spec=spec, - validator=openapi_v30_request_body_validator, + cls=V30RequestBodyValidator, ) assert type(exc_info.value.__cause__) is InvalidSchemaValue @@ -1742,13 +1748,13 @@ def test_delete_tags_with_requestbody(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters() result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert is_dataclass(result.body) @@ -1770,7 +1776,7 @@ def test_delete_tags_with_requestbody(self, spec): request, response, spec=spec, - validator=openapi_v30_response_headers_validator, + cls=V30ResponseHeadersValidator, ) assert result.headers == { @@ -1790,18 +1796,20 @@ def test_delete_tags_no_requestbody(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters() result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None - def test_delete_tags_raises_missing_required_response_header(self, spec): + def test_delete_tags_raises_missing_required_response_header( + self, spec, response_validator + ): host_url = "http://petstore.swagger.io/v1" path_pattern = "/v1/tags" request = MockRequest( @@ -1814,13 +1822,13 @@ def test_delete_tags_raises_missing_required_response_header(self, spec): result = validate_request( request, spec=spec, - validator=openapi_v30_request_parameters_validator, + cls=V30RequestParametersValidator, ) assert result.parameters == Parameters() result = validate_request( - request, spec=spec, validator=openapi_v30_request_body_validator + request, spec=spec, cls=V30RequestBodyValidator ) assert result.body is None @@ -1829,9 +1837,7 @@ def test_delete_tags_raises_missing_required_response_header(self, spec): response = MockResponse(data, status_code=200) with pytest.warns(DeprecationWarning): - response_result = openapi_v30_response_validator.validate( - spec, request, response - ) + response_result = response_validator.validate(request, response) assert response_result.errors == [ MissingRequiredHeader(name="x-delete-confirm"), diff --git a/tests/integration/validation/test_read_only_write_only.py b/tests/integration/validation/test_read_only_write_only.py index 7b0a3a24..c7fd7ad1 100644 --- a/tests/integration/validation/test_read_only_write_only.py +++ b/tests/integration/validation/test_read_only_write_only.py @@ -3,8 +3,8 @@ import pytest -from openapi_core import openapi_v30_request_validator -from openapi_core import openapi_v30_response_validator +from openapi_core import V30RequestValidator +from openapi_core import V30ResponseValidator from openapi_core.testing import MockRequest from openapi_core.testing import MockResponse from openapi_core.validation.request.exceptions import InvalidRequestBody @@ -16,8 +16,18 @@ def spec(factory): return factory.spec_from_file("data/v3.0/read_only_write_only.yaml") +@pytest.fixture(scope="class") +def request_validator(spec): + return V30RequestValidator(spec) + + +@pytest.fixture(scope="class") +def response_validator(spec): + return V30ResponseValidator(spec) + + class TestReadOnly: - def test_write_a_read_only_property(self, spec): + def test_write_a_read_only_property(self, request_validator): data = json.dumps( { "id": 10, @@ -29,13 +39,13 @@ def test_write_a_read_only_property(self, spec): host_url="", method="POST", path="/users", data=data ) - result = openapi_v30_request_validator.validate(spec, request) + result = request_validator.validate(request) assert len(result.errors) == 1 assert type(result.errors[0]) == InvalidRequestBody assert result.body is None - def test_read_only_property_response(self, spec): + def test_read_only_property_response(self, response_validator): data = json.dumps( { "id": 10, @@ -47,9 +57,7 @@ def test_read_only_property_response(self, spec): response = MockResponse(data) - result = openapi_v30_response_validator.validate( - spec, request, response - ) + result = response_validator.validate(request, response) assert not result.errors assert is_dataclass(result.data) @@ -59,7 +67,7 @@ def test_read_only_property_response(self, spec): class TestWriteOnly: - def test_write_only_property(self, spec): + def test_write_only_property(self, request_validator): data = json.dumps( { "name": "Pedro", @@ -71,7 +79,7 @@ def test_write_only_property(self, spec): host_url="", method="POST", path="/users", data=data ) - result = openapi_v30_request_validator.validate(spec, request) + result = request_validator.validate(request) assert not result.errors assert is_dataclass(result.body) @@ -79,7 +87,7 @@ def test_write_only_property(self, spec): assert result.body.name == "Pedro" assert result.body.hidden == False - def test_read_a_write_only_property(self, spec): + def test_read_a_write_only_property(self, response_validator): data = json.dumps( { "id": 10, @@ -91,9 +99,7 @@ def test_read_a_write_only_property(self, spec): request = MockRequest(host_url="", method="POST", path="/users") response = MockResponse(data) - result = openapi_v30_response_validator.validate( - spec, request, response - ) + result = response_validator.validate(request, response) assert result.errors == [InvalidData()] assert result.data is None diff --git a/tests/integration/validation/test_security_override.py b/tests/integration/validation/test_security_override.py index 318fe011..b041f09e 100644 --- a/tests/integration/validation/test_security_override.py +++ b/tests/integration/validation/test_security_override.py @@ -2,7 +2,7 @@ import pytest -from openapi_core import openapi_request_validator +from openapi_core import V30RequestValidator from openapi_core.templating.security.exceptions import SecurityNotFound from openapi_core.testing import MockRequest from openapi_core.validation.request.exceptions import SecurityError @@ -13,6 +13,11 @@ def spec(factory): return factory.spec_from_file("data/v3.0/security_override.yaml") +@pytest.fixture(scope="class") +def request_validator(spec): + return V30RequestValidator(spec) + + class TestSecurityOverride: host_url = "http://petstore.swagger.io" @@ -25,28 +30,28 @@ def api_key_encoded(self): api_key_bytes_enc = b64encode(api_key_bytes) return str(api_key_bytes_enc, "utf8") - def test_default(self, spec): + def test_default(self, request_validator): args = {"api_key": self.api_key} request = MockRequest(self.host_url, "get", "/resource/one", args=args) - result = openapi_request_validator.validate(spec, request) + result = request_validator.validate(request) assert not result.errors assert result.security == { "api_key": self.api_key, } - def test_default_invalid(self, spec): + def test_default_invalid(self, request_validator): request = MockRequest(self.host_url, "get", "/resource/one") - result = openapi_request_validator.validate(spec, request) + result = request_validator.validate(request) assert len(result.errors) == 1 assert type(result.errors[0]) is SecurityError assert type(result.errors[0].__cause__) is SecurityNotFound assert result.security is None - def test_override(self, spec): + def test_override(self, request_validator): authorization = "Basic " + self.api_key_encoded headers = { "Authorization": authorization, @@ -55,27 +60,27 @@ def test_override(self, spec): self.host_url, "post", "/resource/one", headers=headers ) - result = openapi_request_validator.validate(spec, request) + result = request_validator.validate(request) assert not result.errors assert result.security == { "petstore_auth": self.api_key_encoded, } - def test_override_invalid(self, spec): + def test_override_invalid(self, request_validator): request = MockRequest(self.host_url, "post", "/resource/one") - result = openapi_request_validator.validate(spec, request) + result = request_validator.validate(request) assert len(result.errors) == 1 assert type(result.errors[0]) is SecurityError assert type(result.errors[0].__cause__) is SecurityNotFound assert result.security is None - def test_remove(self, spec): + def test_remove(self, request_validator): request = MockRequest(self.host_url, "put", "/resource/one") - result = openapi_request_validator.validate(spec, request) + result = request_validator.validate(request) assert not result.errors assert result.security == {} diff --git a/tests/integration/validation/test_validators.py b/tests/integration/validation/test_validators.py index 835e35a3..45d32719 100644 --- a/tests/integration/validation/test_validators.py +++ b/tests/integration/validation/test_validators.py @@ -57,7 +57,8 @@ def spec(self, spec_dict): def test_request_server_error(self, spec): request = MockRequest("http://petstore.invalid.net/v1", "get", "/") - result = openapi_request_validator.validate(spec, request) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate(spec, request) assert len(result.errors) == 1 assert type(result.errors[0]) == PathNotFound @@ -67,7 +68,8 @@ def test_request_server_error(self, spec): def test_invalid_path(self, spec): request = MockRequest(self.host_url, "get", "/v1") - result = openapi_request_validator.validate(spec, request) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate(spec, request) assert len(result.errors) == 1 assert type(result.errors[0]) == PathNotFound @@ -77,7 +79,8 @@ def test_invalid_path(self, spec): def test_invalid_operation(self, spec): request = MockRequest(self.host_url, "patch", "/v1/pets") - result = openapi_request_validator.validate(spec, request) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate(spec, request) assert len(result.errors) == 1 assert type(result.errors[0]) == OperationNotFound @@ -169,7 +172,8 @@ def test_missing_body(self, spec): cookies=cookies, ) - result = openapi_request_validator.validate(spec, request) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate(spec, request) assert len(result.errors) == 1 assert type(result.errors[0]) == MissingRequiredRequestBody @@ -202,7 +206,8 @@ def test_invalid_content_type(self, spec): cookies=cookies, ) - result = openapi_request_validator.validate(spec, request) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate(spec, request) assert len(result.errors) == 1 assert type(result.errors[0]) == RequestBodyError @@ -259,7 +264,8 @@ def test_invalid_complex_parameter(self, spec, spec_dict): cookies=cookies, ) - result = openapi_request_validator.validate(spec, request) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate(spec, request) assert result.errors == [ InvalidParameter(name="userdata", location="cookie") @@ -319,7 +325,8 @@ def test_post_pets(self, spec, spec_dict): cookies=cookies, ) - result = openapi_request_validator.validate(spec, request) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate(spec, request) assert result.errors == [] assert result.parameters == Parameters( @@ -386,7 +393,8 @@ def test_get_pet_unauthorized(self, spec): view_args={"petId": "1"}, ) - result = openapi_request_validator.validate(spec, request) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate(spec, request) assert len(result.errors) == 1 assert type(result.errors[0]) is SecurityError @@ -411,7 +419,8 @@ def test_get_pet(self, spec): headers=headers, ) - result = openapi_request_validator.validate(spec, request) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate(spec, request) assert result.errors == [] assert result.body is None @@ -461,7 +470,8 @@ def spec(self, spec_dict): def test_request_missing_param(self, spec): request = MockRequest("http://example.com", "get", "/resource") - result = openapi_request_validator.validate(spec, request) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate(spec, request) assert len(result.errors) == 1 assert type(result.errors[0]) == MissingRequiredParameter @@ -475,7 +485,8 @@ def test_request_invalid_param(self, spec): "/resource", args={"resId": "invalid"}, ) - result = openapi_request_validator.validate(spec, request) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate(spec, request) assert result.errors == [ ParameterError(name="resId", location="query") @@ -491,7 +502,8 @@ def test_request_valid_param(self, spec): "/resource", args={"resId": "10"}, ) - result = openapi_request_validator.validate(spec, request) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate(spec, request) assert len(result.errors) == 0 assert result.body is None @@ -511,9 +523,10 @@ def test_request_override_param(self, spec, spec_dict): } ] request = MockRequest("http://example.com", "get", "/resource") - result = openapi_request_validator.validate( - spec, request, base_url="http://example.com" - ) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate( + spec, request, base_url="http://example.com" + ) assert len(result.errors) == 0 assert result.body is None @@ -534,9 +547,10 @@ def test_request_override_param_uniqueness(self, spec, spec_dict): } ] request = MockRequest("http://example.com", "get", "/resource") - result = openapi_request_validator.validate( - spec, request, base_url="http://example.com" - ) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate( + spec, request, base_url="http://example.com" + ) assert len(result.errors) == 1 assert type(result.errors[0]) == MissingRequiredParameter @@ -570,9 +584,10 @@ def test_request_object_deep_object_params(self, spec, spec_dict): "/resource", args={"paramObj[count]": 2, "paramObj[name]": "John"}, ) - result = openapi_request_validator.validate( - spec, request, base_url="http://example.com" - ) + with pytest.warns(DeprecationWarning): + result = openapi_request_validator.validate( + spec, request, base_url="http://example.com" + ) assert len(result.errors) == 0 assert result.body is None @@ -598,7 +613,10 @@ def test_invalid_server(self, spec): request = MockRequest("http://petstore.invalid.net/v1", "get", "/") response = MockResponse("Not Found", status_code=404) - result = openapi_response_validator.validate(spec, request, response) + with pytest.warns(DeprecationWarning): + result = openapi_response_validator.validate( + spec, request, response + ) assert len(result.errors) == 1 assert type(result.errors[0]) == PathNotFound @@ -609,7 +627,10 @@ def test_invalid_operation(self, spec): request = MockRequest(self.host_url, "patch", "/v1/pets") response = MockResponse("Not Found", status_code=404) - result = openapi_response_validator.validate(spec, request, response) + with pytest.warns(DeprecationWarning): + result = openapi_response_validator.validate( + spec, request, response + ) assert len(result.errors) == 1 assert type(result.errors[0]) == OperationNotFound @@ -620,7 +641,10 @@ def test_invalid_response(self, spec): request = MockRequest(self.host_url, "get", "/v1/pets") response = MockResponse("Not Found", status_code=409) - result = openapi_response_validator.validate(spec, request, response) + with pytest.warns(DeprecationWarning): + result = openapi_response_validator.validate( + spec, request, response + ) assert len(result.errors) == 1 assert type(result.errors[0]) == ResponseNotFound @@ -631,7 +655,10 @@ def test_invalid_content_type(self, spec): request = MockRequest(self.host_url, "get", "/v1/pets") response = MockResponse("Not Found", mimetype="text/csv") - result = openapi_response_validator.validate(spec, request, response) + with pytest.warns(DeprecationWarning): + result = openapi_response_validator.validate( + spec, request, response + ) assert result.errors == [DataError()] assert type(result.errors[0].__cause__) == MediaTypeNotFound @@ -642,7 +669,10 @@ def test_missing_body(self, spec): request = MockRequest(self.host_url, "get", "/v1/pets") response = MockResponse(None) - result = openapi_response_validator.validate(spec, request, response) + with pytest.warns(DeprecationWarning): + result = openapi_response_validator.validate( + spec, request, response + ) assert result.errors == [MissingData()] assert result.data is None @@ -652,7 +682,10 @@ def test_invalid_media_type(self, spec): request = MockRequest(self.host_url, "get", "/v1/pets") response = MockResponse("abcde") - result = openapi_response_validator.validate(spec, request, response) + with pytest.warns(DeprecationWarning): + result = openapi_response_validator.validate( + spec, request, response + ) assert result.errors == [DataError()] assert result.errors[0].__cause__ == MediaTypeDeserializeError( @@ -665,7 +698,10 @@ def test_invalid_media_type_value(self, spec): request = MockRequest(self.host_url, "get", "/v1/pets") response = MockResponse("{}") - result = openapi_response_validator.validate(spec, request, response) + with pytest.warns(DeprecationWarning): + result = openapi_response_validator.validate( + spec, request, response + ) assert result.errors == [InvalidData()] assert type(result.errors[0].__cause__) == InvalidSchemaValue @@ -682,7 +718,10 @@ def test_invalid_value(self, spec): response_data = json.dumps(response_json) response = MockResponse(response_data) - result = openapi_response_validator.validate(spec, request, response) + with pytest.warns(DeprecationWarning): + result = openapi_response_validator.validate( + spec, request, response + ) assert result.errors == [InvalidData()] assert type(result.errors[0].__cause__) == InvalidSchemaValue @@ -718,7 +757,10 @@ def test_invalid_header(self, spec): } response = MockResponse(response_data, headers=headers) - result = openapi_response_validator.validate(spec, request, response) + with pytest.warns(DeprecationWarning): + result = openapi_response_validator.validate( + spec, request, response + ) assert result.errors == [InvalidHeader(name="x-delete-date")] assert result.data is None @@ -740,7 +782,10 @@ def test_get_pets(self, spec): response_data = json.dumps(response_json) response = MockResponse(response_data) - result = openapi_response_validator.validate(spec, request, response) + with pytest.warns(DeprecationWarning): + result = openapi_response_validator.validate( + spec, request, response + ) assert result.errors == [] assert is_dataclass(result.data) diff --git a/tests/unit/unmarshalling/test_unmarshal.py b/tests/unit/unmarshalling/test_unmarshal.py index 4991e98e..1151d167 100644 --- a/tests/unit/unmarshalling/test_unmarshal.py +++ b/tests/unit/unmarshalling/test_unmarshal.py @@ -63,7 +63,7 @@ def test_schema_type_invalid(self, unmarshaller_factory): def test_schema_custom_format_invalid(self, unmarshaller_factory): class CustomFormatter(Formatter): - def unmarshal(self, value): + def format(self, value): raise ValueError formatter = CustomFormatter() @@ -233,7 +233,7 @@ def test_string_format_custom(self, unmarshaller_factory): formatted = "x-custom" class CustomFormatter(Formatter): - def unmarshal(self, value): + def format(self, value): return formatted custom_format = "custom" @@ -254,9 +254,35 @@ def unmarshal(self, value): assert result == formatted - def test_string_format_custom_value_error(self, unmarshaller_factory): + def test_string_format_custom_formatter(self, unmarshaller_factory): + formatted = "x-custom" + class CustomFormatter(Formatter): def unmarshal(self, value): + return formatted + + custom_format = "custom" + schema = { + "type": "string", + "format": custom_format, + } + spec = Spec.from_dict(schema, validator=None) + value = "x" + formatter = CustomFormatter() + custom_formatters = { + custom_format: formatter, + } + + with pytest.warns(DeprecationWarning): + result = unmarshaller_factory( + spec, custom_formatters=custom_formatters + )(value) + + assert result == formatted + + def test_string_format_custom_value_error(self, unmarshaller_factory): + class CustomFormatter(Formatter): + def format(self, value): raise ValueError custom_format = "custom" @@ -852,6 +878,54 @@ def test_schema_free_form_object( assert result == value + def test_additional_properties_list(self, unmarshaller_factory): + schema = {"type": "object"} + spec = Spec.from_dict(schema, validator=None) + + result = unmarshaller_factory(spec)({"user_ids": [1, 2, 3, 4]}) + + assert result == { + "user_ids": [1, 2, 3, 4], + } + + @pytest.mark.xfail(message="None and NOTSET should be distinguished") + def test_null_not_supported(self, unmarshaller_factory): + schema = {"type": "null"} + spec = Spec.from_dict(schema, validator=None) + + with pytest.raises(InvalidSchemaValue): + unmarshaller_factory(spec)(None) + + @pytest.mark.parametrize( + "types,value", + [ + (["string", "null"], "string"), + (["number", "null"], 2), + (["number", "null"], 3.14), + (["boolean", "null"], True), + (["array", "null"], [1, 2]), + (["object", "null"], {}), + ], + ) + def test_nultiple_types_not_supported( + self, unmarshaller_factory, types, value + ): + schema = {"type": types} + spec = Spec.from_dict(schema, validator=None) + + with pytest.raises(TypeError): + unmarshaller_factory(spec)(value) + + +class TestOAS30ReadSchemaUnmarshallerCall: + @pytest.fixture + def unmarshaller_factory(self, schema_unmarshaller_factory): + return partial( + schema_unmarshaller_factory, + OAS30Validator, + context=ValidationContext.RESPONSE, + ) + def test_read_only_properties(self, unmarshaller_factory): schema = { "type": "object", @@ -866,22 +940,20 @@ def test_read_only_properties(self, unmarshaller_factory): spec = Spec.from_dict(schema, validator=None) # readOnly properties may be admitted in a Response context - result = unmarshaller_factory( - spec, context=ValidationContext.RESPONSE - )({"id": 10}) + result = unmarshaller_factory(spec)({"id": 10}) assert result == { "id": 10, } - def test_read_only_properties_invalid(self, unmarshaller_factory): + def test_write_only_properties_invalid(self, unmarshaller_factory): schema = { "type": "object", "required": ["id"], "properties": { "id": { "type": "integer", - "readOnly": True, + "writeOnly": True, } }, } @@ -889,9 +961,17 @@ def test_read_only_properties_invalid(self, unmarshaller_factory): # readOnly properties are not admitted on a Request context with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(spec, context=ValidationContext.REQUEST)( - {"id": 10} - ) + unmarshaller_factory(spec)({"id": 10}) + + +class TestOAS30WriteSchemaUnmarshallerCall: + @pytest.fixture + def unmarshaller_factory(self, schema_unmarshaller_factory): + return partial( + schema_unmarshaller_factory, + OAS30Validator, + context=ValidationContext.REQUEST, + ) def test_write_only_properties(self, unmarshaller_factory): schema = { @@ -907,22 +987,20 @@ def test_write_only_properties(self, unmarshaller_factory): spec = Spec.from_dict(schema, validator=None) # readOnly properties may be admitted in a Response context - result = unmarshaller_factory(spec, context=ValidationContext.REQUEST)( - {"id": 10} - ) + result = unmarshaller_factory(spec)({"id": 10}) assert result == { "id": 10, } - def test_write_only_properties_invalid(self, unmarshaller_factory): + def test_read_only_properties_invalid(self, unmarshaller_factory): schema = { "type": "object", "required": ["id"], "properties": { "id": { "type": "integer", - "writeOnly": True, + "readOnly": True, } }, } @@ -930,49 +1008,7 @@ def test_write_only_properties_invalid(self, unmarshaller_factory): # readOnly properties are not admitted on a Request context with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(spec, context=ValidationContext.RESPONSE)( - {"id": 10} - ) - - def test_additional_properties_list(self, unmarshaller_factory): - schema = {"type": "object"} - spec = Spec.from_dict(schema, validator=None) - - result = unmarshaller_factory( - spec, context=ValidationContext.RESPONSE - )({"user_ids": [1, 2, 3, 4]}) - - assert result == { - "user_ids": [1, 2, 3, 4], - } - - @pytest.mark.xfail(message="None and NOTSET should be distinguished") - def test_null_not_supported(self, unmarshaller_factory): - schema = {"type": "null"} - spec = Spec.from_dict(schema, validator=None) - - with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(spec)(None) - - @pytest.mark.parametrize( - "types,value", - [ - (["string", "null"], "string"), - (["number", "null"], 2), - (["number", "null"], 3.14), - (["boolean", "null"], True), - (["array", "null"], [1, 2]), - (["object", "null"], {}), - ], - ) - def test_nultiple_types_not_supported( - self, unmarshaller_factory, types, value - ): - schema = {"type": types} - spec = Spec.from_dict(schema, validator=None) - - with pytest.raises(TypeError): - unmarshaller_factory(spec)(value) + unmarshaller_factory(spec)({"id": 10}) class TestOAS31SchemaUnmarshallerCall: diff --git a/tests/unit/validation/test_request_response_validators.py b/tests/unit/validation/test_request_response_validators.py new file mode 100644 index 00000000..fc5a0b15 --- /dev/null +++ b/tests/unit/validation/test_request_response_validators.py @@ -0,0 +1,103 @@ +from unittest import mock + +import pytest +from openapi_schema_validator import OAS31Validator + +from openapi_core.unmarshalling.schemas.factories import ( + SchemaUnmarshallersFactory, +) +from openapi_core.unmarshalling.schemas.formatters import Formatter +from openapi_core.validation import openapi_request_validator +from openapi_core.validation import openapi_response_validator +from openapi_core.validation.request.validators import RequestValidator +from openapi_core.validation.response.validators import ResponseValidator + + +class BaseTestValidate: + @pytest.fixture + def schema_unmarshallers_factory(self): + CUSTOM_FORMATTERS = {"custom": Formatter.from_callables()} + return SchemaUnmarshallersFactory( + OAS31Validator, + custom_formatters=CUSTOM_FORMATTERS, + ) + + +class TestRequestValidatorValidate(BaseTestValidate): + @pytest.fixture + def validator(self, schema_unmarshallers_factory): + return RequestValidator(schema_unmarshallers_factory) + + @mock.patch( + "openapi_core.validation.request.validators.APICallRequestValidator." + "validate", + ) + def test_valid(self, mock_validate, validator): + spec = mock.sentinel.spec + request = mock.sentinel.request + + with pytest.warns(DeprecationWarning): + result = validator.validate(spec, request) + + assert result == mock_validate.return_value + mock_validate.assert_called_once_with(request) + + +class TestResponseValidatorValidate(BaseTestValidate): + @pytest.fixture + def validator(self, schema_unmarshallers_factory): + return ResponseValidator(schema_unmarshallers_factory) + + @mock.patch( + "openapi_core.validation.response.validators.APICallResponseValidator." + "validate", + ) + def test_valid(self, mock_validate, validator): + spec = mock.sentinel.spec + request = mock.sentinel.request + response = mock.sentinel.response + + with pytest.warns(DeprecationWarning): + result = validator.validate(spec, request, response) + + assert result == mock_validate.return_value + mock_validate.assert_called_once_with(request, response) + + +class TestDetectProxyOpenAPIRequestValidator: + @pytest.fixture + def validator(self): + return openapi_request_validator + + @mock.patch( + "openapi_core.validation.request.validators.APICallRequestValidator." + "validate", + ) + def test_valid(self, mock_validate, validator, spec_v31): + request = mock.sentinel.request + + with pytest.warns(DeprecationWarning): + result = validator.validate(spec_v31, request) + + assert result == mock_validate.return_value + mock_validate.assert_called_once_with(request) + + +class TestDetectProxyOpenAPIResponsealidator: + @pytest.fixture + def validator(self): + return openapi_response_validator + + @mock.patch( + "openapi_core.validation.response.validators.APICallResponseValidator." + "validate", + ) + def test_valid(self, mock_validate, validator, spec_v31): + request = mock.sentinel.request + response = mock.sentinel.response + + with pytest.warns(DeprecationWarning): + result = validator.validate(spec_v31, request, response) + + assert result == mock_validate.return_value + mock_validate.assert_called_once_with(request, response) diff --git a/tests/unit/validation/test_shortcuts.py b/tests/unit/validation/test_shortcuts.py index 8d9c5189..0c2846c9 100644 --- a/tests/unit/validation/test_shortcuts.py +++ b/tests/unit/validation/test_shortcuts.py @@ -8,9 +8,13 @@ from openapi_core.validation.exceptions import ValidatorDetectError from openapi_core.validation.request.protocols import Request from openapi_core.validation.request.protocols import WebhookRequest +from openapi_core.validation.request.validators import APICallRequestValidator from openapi_core.validation.request.validators import RequestValidator from openapi_core.validation.request.validators import WebhookRequestValidator from openapi_core.validation.response.protocols import Response +from openapi_core.validation.response.validators import ( + APICallResponseValidator, +) from openapi_core.validation.response.validators import ResponseValidator from openapi_core.validation.response.validators import ( WebhookResponseValidator, @@ -38,7 +42,8 @@ def test_spec_type_error(self): validate_request(request, spec=spec) @mock.patch( - "openapi_core.validation.request.validators.RequestValidator.validate", + "openapi_core.validation.request.validators.APICallRequestValidator." + "validate", ) def test_request(self, mock_validate, spec_v31): request = mock.Mock(spec=Request) @@ -46,10 +51,11 @@ def test_request(self, mock_validate, spec_v31): result = validate_request(request, spec=spec_v31) assert result == mock_validate.return_value - mock_validate.validate.aasert_called_once_with(request) + mock_validate.assert_called_once_with(request) @mock.patch( - "openapi_core.validation.request.validators.RequestValidator.validate", + "openapi_core.validation.request.validators.APICallRequestValidator." + "validate", ) def test_spec_as_first_arg_deprecated(self, mock_validate, spec_v31): request = mock.Mock(spec=Request) @@ -58,10 +64,11 @@ def test_spec_as_first_arg_deprecated(self, mock_validate, spec_v31): result = validate_request(spec_v31, request) assert result == mock_validate.return_value - mock_validate.validate.aasert_called_once_with(request) + mock_validate.assert_called_once_with(request) @mock.patch( - "openapi_core.validation.request.validators.RequestValidator.validate", + "openapi_core.validation.request.validators.APICallRequestValidator." + "validate", ) def test_request_error(self, mock_validate, spec_v31): request = mock.Mock(spec=Request) @@ -70,7 +77,7 @@ def test_request_error(self, mock_validate, spec_v31): with pytest.raises(ValueError): validate_request(request, spec=spec_v31) - mock_validate.aasert_called_once_with(request) + mock_validate.assert_called_once_with(request) def test_validator(self, spec_v31): request = mock.Mock(spec=Request) @@ -82,16 +89,18 @@ def test_validator(self, spec_v31): ) assert result == validator.validate.return_value - validator.validate.aasert_called_once_with(request) + validator.validate.assert_called_once_with( + spec_v31, request, base_url=None + ) def test_validator_cls(self, spec_v31): request = mock.Mock(spec=Request) - validator_cls = mock.Mock(spec=RequestValidator) + validator_cls = mock.Mock(spec=APICallRequestValidator) result = validate_request(request, spec=spec_v31, cls=validator_cls) assert result == validator_cls().validate.return_value - validator_cls().validate.aasert_called_once_with(request) + validator_cls().validate.assert_called_once_with(request) @mock.patch( "openapi_core.validation.request.validators.WebhookRequestValidator." @@ -103,7 +112,7 @@ def test_webhook_request(self, mock_validate, spec_v31): result = validate_request(request, spec=spec_v31) assert result == mock_validate.return_value - mock_validate.validate.aasert_called_once_with(request) + mock_validate.assert_called_once_with(request) def test_webhook_request_validator_not_found(self, spec_v30): request = mock.Mock(spec=WebhookRequest) @@ -122,7 +131,7 @@ def test_webhook_request_error(self, mock_validate, spec_v31): with pytest.raises(ValueError): validate_request(request, spec=spec_v31) - mock_validate.aasert_called_once_with(request) + mock_validate.assert_called_once_with(request) def test_webhook_validator_cls(self, spec_v31): request = mock.Mock(spec=WebhookRequest) @@ -131,7 +140,7 @@ def test_webhook_validator_cls(self, spec_v31): result = validate_request(request, spec=spec_v31, cls=validator_cls) assert result == validator_cls().validate.return_value - validator_cls().validate.aasert_called_once_with(request) + validator_cls().validate.assert_called_once_with(request) class TestValidateResponse: @@ -165,7 +174,7 @@ def test_spec_type_error(self): validate_response(request, response, spec=spec) @mock.patch( - "openapi_core.validation.response.validators.ResponseValidator." + "openapi_core.validation.response.validators.APICallResponseValidator." "validate", ) def test_request_response(self, mock_validate, spec_v31): @@ -175,23 +184,24 @@ def test_request_response(self, mock_validate, spec_v31): result = validate_response(request, response, spec=spec_v31) assert result == mock_validate.return_value - mock_validate.aasert_called_once_with(request, response) + mock_validate.assert_called_once_with(request, response) @mock.patch( - "openapi_core.validation.response.validators.ResponseValidator." + "openapi_core.validation.response.validators.APICallResponseValidator." "validate", ) def test_spec_as_first_arg_deprecated(self, mock_validate, spec_v31): request = mock.Mock(spec=Request) response = mock.Mock(spec=Response) - result = validate_response(spec_v31, request, response) + with pytest.warns(DeprecationWarning): + result = validate_response(spec_v31, request, response) assert result == mock_validate.return_value - mock_validate.aasert_called_once_with(request, response) + mock_validate.assert_called_once_with(request, response) @mock.patch( - "openapi_core.validation.response.validators.ResponseValidator." + "openapi_core.validation.response.validators.APICallResponseValidator." "validate", ) def test_request_response_error(self, mock_validate, spec_v31): @@ -202,7 +212,7 @@ def test_request_response_error(self, mock_validate, spec_v31): with pytest.raises(ValueError): validate_response(request, response, spec=spec_v31) - mock_validate.aasert_called_once_with(request, response) + mock_validate.assert_called_once_with(request, response) def test_validator(self, spec_v31): request = mock.Mock(spec=Request) @@ -215,19 +225,21 @@ def test_validator(self, spec_v31): ) assert result == validator.validate.return_value - validator.validate.aasert_called_once_with(request) + validator.validate.assert_called_once_with( + spec_v31, request, response, base_url=None + ) def test_validator_cls(self, spec_v31): request = mock.Mock(spec=Request) response = mock.Mock(spec=Response) - validator_cls = mock.Mock(spec=ResponseValidator) + validator_cls = mock.Mock(spec=APICallResponseValidator) result = validate_response( request, response, spec=spec_v31, cls=validator_cls ) assert result == validator_cls().validate.return_value - validator_cls().validate.aasert_called_once_with(request) + validator_cls().validate.assert_called_once_with(request, response) def test_webhook_response_validator_not_found(self, spec_v30): request = mock.Mock(spec=WebhookRequest) @@ -247,7 +259,7 @@ def test_webhook_request(self, mock_validate, spec_v31): result = validate_response(request, response, spec=spec_v31) assert result == mock_validate.return_value - mock_validate.aasert_called_once_with(request, response) + mock_validate.assert_called_once_with(request, response) @mock.patch( "openapi_core.validation.response.validators.WebhookResponseValidator." @@ -261,7 +273,7 @@ def test_webhook_request_error(self, mock_validate, spec_v31): with pytest.raises(ValueError): validate_response(request, response, spec=spec_v31) - mock_validate.aasert_called_once_with(request, response) + mock_validate.assert_called_once_with(request, response) def test_webhook_response_cls(self, spec_v31): request = mock.Mock(spec=WebhookRequest) @@ -273,4 +285,4 @@ def test_webhook_response_cls(self, spec_v31): ) assert result == validator_cls().validate.return_value - validator_cls().validate.aasert_called_once_with(request) + validator_cls().validate.assert_called_once_with(request, response) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy