Merge "Fix versioned schema validation"
This commit is contained in:
commit
1896a39d15
|
@ -21,7 +21,7 @@ Request Body validating middleware.
|
||||||
import functools
|
import functools
|
||||||
|
|
||||||
from masakari.api import api_version_request as api_version
|
from masakari.api import api_version_request as api_version
|
||||||
from masakari.api.validation import validators
|
from masakari.api.validation.validators import _SchemaValidator
|
||||||
|
|
||||||
|
|
||||||
def schema(request_body_schema, min_version=None, max_version=None):
|
def schema(request_body_schema, min_version=None, max_version=None):
|
||||||
|
@ -45,13 +45,13 @@ def schema(request_body_schema, min_version=None, max_version=None):
|
||||||
else:
|
else:
|
||||||
ver = args[1].api_version_request
|
ver = args[1].api_version_request
|
||||||
|
|
||||||
ver.matches(min_ver, max_ver)
|
|
||||||
# Only validate against the schema if it lies within
|
# Only validate against the schema if it lies within
|
||||||
# the version range specified. Note that if both min
|
# the version range specified. Note that, if both min
|
||||||
# and max are not specified the validator will always
|
# and max are not specified, the validator will always
|
||||||
# be run.
|
# be run.
|
||||||
schema_validator = validators._SchemaValidator(request_body_schema)
|
if ver.matches(min_ver, max_ver):
|
||||||
schema_validator.validate(kwargs['body'])
|
schema_validator = _SchemaValidator(request_body_schema)
|
||||||
|
schema_validator.validate(kwargs['body'])
|
||||||
|
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
|
@ -28,8 +28,10 @@ from masakari import test
|
||||||
|
|
||||||
|
|
||||||
class FakeRequest(object):
|
class FakeRequest(object):
|
||||||
api_version_request = api_version.APIVersionRequest("1.0")
|
def __init__(self, version=None):
|
||||||
environ = {}
|
if version is None:
|
||||||
|
version = '1.0'
|
||||||
|
self.api_version_request = api_version.APIVersionRequest(version)
|
||||||
|
|
||||||
|
|
||||||
class ValidationRegex(test.NoDBTestCase):
|
class ValidationRegex(test.NoDBTestCase):
|
||||||
|
@ -552,3 +554,83 @@ class DatetimeTestCase(APIValidationTestCase):
|
||||||
self.post(body={
|
self.post(body={
|
||||||
'foo': '2016-01-14T01:00:00Z'}, req=FakeRequest()
|
'foo': '2016-01-14T01:00:00Z'}, req=FakeRequest()
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
|
class VersionedApiValidationTestCase(APIValidationTestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(__class__, self).setUp()
|
||||||
|
|
||||||
|
schema_pre13 = {
|
||||||
|
'type': 'object',
|
||||||
|
'properties': {
|
||||||
|
'foo': {
|
||||||
|
'type': 'string',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'additionalProperties': False,
|
||||||
|
}
|
||||||
|
|
||||||
|
schema_post13 = {
|
||||||
|
'type': 'object',
|
||||||
|
'properties': {
|
||||||
|
'bar': {
|
||||||
|
'type': 'boolean',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'additionalProperties': False,
|
||||||
|
}
|
||||||
|
|
||||||
|
@validation.schema(request_body_schema=schema_pre13,
|
||||||
|
min_version='1.1',
|
||||||
|
max_version='1.2')
|
||||||
|
@validation.schema(request_body_schema=schema_post13,
|
||||||
|
min_version='1.3')
|
||||||
|
def post(req, body):
|
||||||
|
return 'Validation succeeded.'
|
||||||
|
|
||||||
|
self.post = post
|
||||||
|
|
||||||
|
def check_validation_error(self, body, req):
|
||||||
|
try:
|
||||||
|
self.post(body=body, req=req)
|
||||||
|
except exception.ValidationError as ex:
|
||||||
|
self.assertEqual(http.BAD_REQUEST, ex.kwargs['code'])
|
||||||
|
except Exception as ex:
|
||||||
|
self.fail('An unexpected exception happens: %s' % ex)
|
||||||
|
else:
|
||||||
|
self.fail('Any exception does not happen.')
|
||||||
|
|
||||||
|
def test_validate_with_proper_microversions(self):
|
||||||
|
self.assertEqual('Validation succeeded.',
|
||||||
|
self.post(body={
|
||||||
|
'foo': 'ahappystring'}, req=FakeRequest('1.1')
|
||||||
|
))
|
||||||
|
self.assertEqual('Validation succeeded.',
|
||||||
|
self.post(body={
|
||||||
|
'foo': 'ahappystring'}, req=FakeRequest('1.2')
|
||||||
|
))
|
||||||
|
self.assertEqual('Validation succeeded.',
|
||||||
|
self.post(body={
|
||||||
|
'bar': True}, req=FakeRequest('1.3')
|
||||||
|
))
|
||||||
|
self.assertEqual('Validation succeeded.',
|
||||||
|
self.post(body={
|
||||||
|
'bar': True}, req=FakeRequest('1.10')
|
||||||
|
))
|
||||||
|
self.assertEqual('Validation succeeded.',
|
||||||
|
self.post(body={
|
||||||
|
'whatever': None}, req=FakeRequest('1.0')
|
||||||
|
))
|
||||||
|
|
||||||
|
def test_validate_with_improper_microversions(self):
|
||||||
|
self.check_validation_error(body={'bar': False},
|
||||||
|
req=FakeRequest('1.1'))
|
||||||
|
self.check_validation_error(body={'bar': False},
|
||||||
|
req=FakeRequest('1.2'))
|
||||||
|
self.check_validation_error(body={'foo': 'asadstring'},
|
||||||
|
req=FakeRequest('1.3'))
|
||||||
|
self.check_validation_error(body={'foo': 'asadstring'},
|
||||||
|
req=FakeRequest('1.10'))
|
||||||
|
self.check_validation_error(body={'foo': 'asadstring'},
|
||||||
|
req=FakeRequest('2.0'))
|
||||||
|
|
Loading…
Reference in New Issue