Check whether the resource type attribute min is less than or equal to max
When use resource type api to create a new resource type, check whether min and min_length is less than max and max_length Closes-Bug: #1617561 Change-Id: Iccfe751f274066b4c11d15901b375dc1177e1887
This commit is contained in:
parent
bd3bfebff9
commit
6a8054cfef
|
@ -40,6 +40,16 @@ class InvalidResourceAttributeName(Exception):
|
|||
self.name = name
|
||||
|
||||
|
||||
class InvalidResourceAttributeValue(ValueError):
|
||||
"""Error raised when the resource attribute min is greater than max"""
|
||||
def __init__(self, min, max):
|
||||
super(InvalidResourceAttributeValue, self).__init__(
|
||||
"Resource attribute value min (or min_length) %s must be less "
|
||||
"than or equal to max (or max_length) %s!" % (str(min), str(max)))
|
||||
self.min = min
|
||||
self.max = max
|
||||
|
||||
|
||||
class CommonAttributeSchema(object):
|
||||
meta_schema_ext = {}
|
||||
schema_ext = None
|
||||
|
@ -80,10 +90,12 @@ class StringSchema(CommonAttributeSchema):
|
|||
|
||||
def __init__(self, min_length, max_length, *args, **kwargs):
|
||||
super(StringSchema, self).__init__(*args, **kwargs)
|
||||
if min_length > max_length:
|
||||
raise InvalidResourceAttributeValue(min_length, max_length)
|
||||
|
||||
self.min_length = min_length
|
||||
self.max_length = max_length
|
||||
|
||||
# TODO(sileht): ensure min_length <= max_length
|
||||
meta_schema_ext = {
|
||||
voluptuous.Required('min_length', default=0):
|
||||
voluptuous.All(int, voluptuous.Range(min=0, max=255)),
|
||||
|
@ -115,10 +127,12 @@ class NumberSchema(CommonAttributeSchema):
|
|||
|
||||
def __init__(self, min, max, *args, **kwargs):
|
||||
super(NumberSchema, self).__init__(*args, **kwargs)
|
||||
if max is not None and min > max:
|
||||
raise InvalidResourceAttributeValue(min, max)
|
||||
|
||||
self.min = min
|
||||
self.max = max
|
||||
|
||||
# TODO(sileht): ensure min_length <= max_length
|
||||
meta_schema_ext = {
|
||||
voluptuous.Required('min', default=None): voluptuous.Any(
|
||||
None, numbers.Real),
|
||||
|
|
|
@ -790,6 +790,8 @@ class ResourceTypesController(rest.RestController):
|
|||
rt = schema.resource_type_from_dict(**body)
|
||||
except resource_type.InvalidResourceAttributeName as e:
|
||||
abort(400, e)
|
||||
except resource_type.InvalidResourceAttributeValue as e:
|
||||
abort(400, e)
|
||||
|
||||
enforce("create resource type", body)
|
||||
try:
|
||||
|
|
|
@ -58,6 +58,36 @@ tests:
|
|||
# - "Invalid input: not a valid value for dictionary value @ data[u'attributes'][u'foo'][u'type']"
|
||||
- "Invalid input:"
|
||||
|
||||
- name: post resource type bad min_length value
|
||||
POST: $LAST_URL
|
||||
request_headers:
|
||||
x-roles: admin
|
||||
content-type: application/json
|
||||
data:
|
||||
name: my_custom_resource
|
||||
attributes:
|
||||
name:
|
||||
type: string
|
||||
required: true
|
||||
max_length: 2
|
||||
min_length: 5
|
||||
status: 400
|
||||
|
||||
- name: post resource type bad min value
|
||||
POST: $LAST_URL
|
||||
request_headers:
|
||||
x-roles: admin
|
||||
content-type: application/json
|
||||
data:
|
||||
name: my_custom_resource
|
||||
attributes:
|
||||
int:
|
||||
type: number
|
||||
required: false
|
||||
max: 3
|
||||
min: 8
|
||||
status: 400
|
||||
|
||||
# Create a type
|
||||
|
||||
- name: post resource type
|
||||
|
|
Loading…
Reference in New Issue