2018-03-02 09:16:49 +00:00
|
|
|
import mock
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from openapi_core.shortcuts import (
|
|
|
|
validate_parameters, validate_body, validate_data,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ResultMock(object):
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, body=None, parameters=None, data=None, error_to_raise=None):
|
|
|
|
self.body = body
|
|
|
|
self.parameters = parameters
|
|
|
|
self.data = data
|
|
|
|
self.error_to_raise = error_to_raise
|
|
|
|
|
|
|
|
def raise_for_errors(self):
|
|
|
|
if self.error_to_raise is not None:
|
|
|
|
raise self.error_to_raise
|
|
|
|
|
|
|
|
if self.parameters is not None:
|
|
|
|
return self.parameters
|
|
|
|
|
|
|
|
if self.data is not None:
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
|
2019-10-19 12:35:48 +00:00
|
|
|
class FactoryClassMock(object):
|
2018-03-02 09:16:49 +00:00
|
|
|
|
|
|
|
_instances = {}
|
|
|
|
|
|
|
|
def __new__(cls, obj):
|
|
|
|
if obj not in cls._instances:
|
|
|
|
cls._instances[obj] = object.__new__(cls)
|
|
|
|
return cls._instances[obj]
|
|
|
|
|
|
|
|
def __init__(self, obj):
|
|
|
|
self.obj = obj
|
|
|
|
|
|
|
|
|
|
|
|
class TestValidateParameters(object):
|
|
|
|
|
|
|
|
@mock.patch('openapi_core.shortcuts.RequestValidator.validate')
|
2019-10-19 12:35:48 +00:00
|
|
|
def test_no_request_factory(self, mock_validate):
|
2018-03-02 09:16:49 +00:00
|
|
|
spec = mock.sentinel.spec
|
|
|
|
request = mock.sentinel.request
|
|
|
|
parameters = mock.sentinel.parameters
|
|
|
|
mock_validate.return_value = ResultMock(parameters=parameters)
|
|
|
|
|
|
|
|
result = validate_parameters(spec, request)
|
|
|
|
|
|
|
|
assert result == parameters
|
|
|
|
mock_validate.aasert_called_once_with(request)
|
|
|
|
|
|
|
|
@mock.patch('openapi_core.shortcuts.RequestValidator.validate')
|
2019-10-19 12:35:48 +00:00
|
|
|
def test_no_request_factory_error(self, mock_validate):
|
2018-03-02 09:16:49 +00:00
|
|
|
spec = mock.sentinel.spec
|
|
|
|
request = mock.sentinel.request
|
|
|
|
mock_validate.return_value = ResultMock(error_to_raise=ValueError)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
validate_parameters(spec, request)
|
|
|
|
|
|
|
|
mock_validate.aasert_called_once_with(request)
|
|
|
|
|
|
|
|
@mock.patch('openapi_core.shortcuts.RequestValidator.validate')
|
2019-10-19 12:35:48 +00:00
|
|
|
def test_request_factory(self, mock_validate):
|
2018-03-02 09:16:49 +00:00
|
|
|
spec = mock.sentinel.spec
|
|
|
|
request = mock.sentinel.request
|
|
|
|
parameters = mock.sentinel.parameters
|
|
|
|
mock_validate.return_value = ResultMock(parameters=parameters)
|
2019-10-19 12:35:48 +00:00
|
|
|
request_factory = FactoryClassMock
|
2018-03-02 09:16:49 +00:00
|
|
|
|
2019-10-19 12:35:48 +00:00
|
|
|
result = validate_parameters(spec, request, request_factory)
|
2018-03-02 09:16:49 +00:00
|
|
|
|
|
|
|
assert result == parameters
|
|
|
|
mock_validate.assert_called_once_with(
|
2019-10-19 12:35:48 +00:00
|
|
|
FactoryClassMock(request),
|
2018-03-02 09:16:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@mock.patch('openapi_core.shortcuts.RequestValidator.validate')
|
2019-10-19 12:35:48 +00:00
|
|
|
def test_request_factory_error(self, mock_validate):
|
2018-03-02 09:16:49 +00:00
|
|
|
spec = mock.sentinel.spec
|
|
|
|
request = mock.sentinel.request
|
|
|
|
mock_validate.return_value = ResultMock(error_to_raise=ValueError)
|
2019-10-19 12:35:48 +00:00
|
|
|
request_factory = FactoryClassMock
|
2018-03-02 09:16:49 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2019-10-19 12:35:48 +00:00
|
|
|
validate_parameters(spec, request, request_factory)
|
2018-03-02 09:16:49 +00:00
|
|
|
|
|
|
|
mock_validate.assert_called_once_with(
|
2019-10-19 12:35:48 +00:00
|
|
|
FactoryClassMock(request),
|
2018-03-02 09:16:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TestValidateBody(object):
|
|
|
|
|
|
|
|
@mock.patch('openapi_core.shortcuts.RequestValidator.validate')
|
2019-10-19 12:35:48 +00:00
|
|
|
def test_no_request_factory(self, mock_validate):
|
2018-03-02 09:16:49 +00:00
|
|
|
spec = mock.sentinel.spec
|
|
|
|
request = mock.sentinel.request
|
|
|
|
body = mock.sentinel.body
|
|
|
|
mock_validate.return_value = ResultMock(body=body)
|
|
|
|
|
|
|
|
result = validate_body(spec, request)
|
|
|
|
|
|
|
|
assert result == body
|
|
|
|
mock_validate.aasert_called_once_with(request)
|
|
|
|
|
|
|
|
@mock.patch('openapi_core.shortcuts.RequestValidator.validate')
|
2019-10-19 12:35:48 +00:00
|
|
|
def test_no_request_factory_error(self, mock_validate):
|
2018-03-02 09:16:49 +00:00
|
|
|
spec = mock.sentinel.spec
|
|
|
|
request = mock.sentinel.request
|
|
|
|
mock_validate.return_value = ResultMock(error_to_raise=ValueError)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
validate_body(spec, request)
|
|
|
|
|
|
|
|
mock_validate.aasert_called_once_with(request)
|
|
|
|
|
|
|
|
@mock.patch('openapi_core.shortcuts.RequestValidator.validate')
|
2019-10-19 12:35:48 +00:00
|
|
|
def test_request_factory(self, mock_validate):
|
2018-03-02 09:16:49 +00:00
|
|
|
spec = mock.sentinel.spec
|
|
|
|
request = mock.sentinel.request
|
|
|
|
body = mock.sentinel.body
|
|
|
|
mock_validate.return_value = ResultMock(body=body)
|
2019-10-19 12:35:48 +00:00
|
|
|
request_factory = FactoryClassMock
|
2018-03-02 09:16:49 +00:00
|
|
|
|
2019-10-19 12:35:48 +00:00
|
|
|
result = validate_body(spec, request, request_factory)
|
2018-03-02 09:16:49 +00:00
|
|
|
|
|
|
|
assert result == body
|
|
|
|
mock_validate.assert_called_once_with(
|
2019-10-19 12:35:48 +00:00
|
|
|
FactoryClassMock(request),
|
2018-03-02 09:16:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@mock.patch('openapi_core.shortcuts.RequestValidator.validate')
|
2019-10-19 12:35:48 +00:00
|
|
|
def test_request_factory_error(self, mock_validate):
|
2018-03-02 09:16:49 +00:00
|
|
|
spec = mock.sentinel.spec
|
|
|
|
request = mock.sentinel.request
|
|
|
|
mock_validate.return_value = ResultMock(error_to_raise=ValueError)
|
2019-10-19 12:35:48 +00:00
|
|
|
request_factory = FactoryClassMock
|
2018-03-02 09:16:49 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2019-10-19 12:35:48 +00:00
|
|
|
validate_body(spec, request, request_factory)
|
2018-03-02 09:16:49 +00:00
|
|
|
|
|
|
|
mock_validate.assert_called_once_with(
|
2019-10-19 12:35:48 +00:00
|
|
|
FactoryClassMock(request),
|
2018-03-02 09:16:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TestvalidateData(object):
|
|
|
|
|
|
|
|
@mock.patch('openapi_core.shortcuts.ResponseValidator.validate')
|
2019-10-19 12:35:48 +00:00
|
|
|
def test_no_factories(self, mock_validate):
|
2018-03-02 09:16:49 +00:00
|
|
|
spec = mock.sentinel.spec
|
|
|
|
request = mock.sentinel.request
|
|
|
|
response = mock.sentinel.response
|
|
|
|
data = mock.sentinel.data
|
|
|
|
mock_validate.return_value = ResultMock(data=data)
|
|
|
|
|
|
|
|
result = validate_data(spec, request, response)
|
|
|
|
|
|
|
|
assert result == data
|
|
|
|
mock_validate.aasert_called_once_with(request, response)
|
|
|
|
|
|
|
|
@mock.patch('openapi_core.shortcuts.ResponseValidator.validate')
|
2019-10-19 12:35:48 +00:00
|
|
|
def test_no_factories_error(self, mock_validate):
|
2018-03-02 09:16:49 +00:00
|
|
|
spec = mock.sentinel.spec
|
|
|
|
request = mock.sentinel.request
|
|
|
|
response = mock.sentinel.response
|
|
|
|
mock_validate.return_value = ResultMock(error_to_raise=ValueError)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
validate_data(spec, request, response)
|
|
|
|
|
|
|
|
mock_validate.aasert_called_once_with(request, response)
|
|
|
|
|
|
|
|
@mock.patch('openapi_core.shortcuts.ResponseValidator.validate')
|
2019-10-19 12:35:48 +00:00
|
|
|
def test_factories(self, mock_validate):
|
2018-03-02 09:16:49 +00:00
|
|
|
spec = mock.sentinel.spec
|
|
|
|
request = mock.sentinel.request
|
|
|
|
response = mock.sentinel.response
|
|
|
|
data = mock.sentinel.data
|
|
|
|
mock_validate.return_value = ResultMock(data=data)
|
2019-10-19 12:35:48 +00:00
|
|
|
request_factory = FactoryClassMock
|
|
|
|
response_factory = FactoryClassMock
|
2018-03-02 09:16:49 +00:00
|
|
|
|
|
|
|
result = validate_data(
|
|
|
|
spec, request, response,
|
2019-10-19 12:35:48 +00:00
|
|
|
request_factory, response_factory,
|
2018-03-02 09:16:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result == data
|
|
|
|
mock_validate.assert_called_once_with(
|
2019-10-19 12:35:48 +00:00
|
|
|
FactoryClassMock(request),
|
|
|
|
FactoryClassMock(response),
|
2018-03-02 09:16:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@mock.patch('openapi_core.shortcuts.ResponseValidator.validate')
|
2019-10-19 12:35:48 +00:00
|
|
|
def test_factories_error(self, mock_validate):
|
2018-03-02 09:16:49 +00:00
|
|
|
spec = mock.sentinel.spec
|
|
|
|
request = mock.sentinel.request
|
|
|
|
response = mock.sentinel.response
|
|
|
|
mock_validate.return_value = ResultMock(error_to_raise=ValueError)
|
2019-10-19 12:35:48 +00:00
|
|
|
request_factory = FactoryClassMock
|
|
|
|
response_factory = FactoryClassMock
|
2018-03-02 09:16:49 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
validate_data(
|
|
|
|
spec, request, response,
|
2019-10-19 12:35:48 +00:00
|
|
|
request_factory, response_factory,
|
2018-03-02 09:16:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
mock_validate.assert_called_once_with(
|
2019-10-19 12:35:48 +00:00
|
|
|
FactoryClassMock(request),
|
|
|
|
FactoryClassMock(response),
|
2018-03-02 09:16:49 +00:00
|
|
|
)
|