Validate display_name/description attributes in API layer
We have string length checks for 'name' and 'description' in API layer, Add validations for 'display_name' and 'display_description'. Change-Id: I8c11d155d50800f7099466896abaa0a04f8c49cd
This commit is contained in:
@@ -1263,23 +1263,17 @@ class Controller(object):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def validate_name_and_description(body):
|
def validate_name_and_description(body):
|
||||||
name = body.get('name')
|
for attribute in ['name', 'description',
|
||||||
if name is not None:
|
'display_name', 'display_description']:
|
||||||
if isinstance(name, six.string_types):
|
value = body.get(attribute)
|
||||||
body['name'] = name.strip()
|
if value is not None:
|
||||||
try:
|
if isinstance(value, six.string_types):
|
||||||
utils.check_string_length(body['name'], 'Name',
|
body[attribute] = value.strip()
|
||||||
min_length=0, max_length=255)
|
try:
|
||||||
except exception.InvalidInput as error:
|
utils.check_string_length(body[attribute], attribute,
|
||||||
raise webob.exc.HTTPBadRequest(explanation=error.msg)
|
min_length=0, max_length=255)
|
||||||
|
except exception.InvalidInput as error:
|
||||||
description = body.get('description')
|
raise webob.exc.HTTPBadRequest(explanation=error.msg)
|
||||||
if description is not None:
|
|
||||||
try:
|
|
||||||
utils.check_string_length(description, 'Description',
|
|
||||||
min_length=0, max_length=255)
|
|
||||||
except exception.InvalidInput as error:
|
|
||||||
raise webob.exc.HTTPBadRequest(explanation=error.msg)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def validate_string_length(value, entity_name, min_length=0,
|
def validate_string_length(value, entity_name, min_length=0,
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import ddt
|
||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
@@ -820,6 +821,7 @@ class ResponseObjectTest(test.TestCase):
|
|||||||
self.assertEqual({}, robj.serializers)
|
self.assertEqual({}, robj.serializers)
|
||||||
|
|
||||||
|
|
||||||
|
@ddt.data
|
||||||
class ValidBodyTest(test.TestCase):
|
class ValidBodyTest(test.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@@ -855,51 +857,32 @@ class ValidBodyTest(test.TestCase):
|
|||||||
name, 'Name', min_length=1, max_length=255,
|
name, 'Name', min_length=1, max_length=255,
|
||||||
remove_whitespaces=False)
|
remove_whitespaces=False)
|
||||||
|
|
||||||
def test_validate_string_length_with_name_contains_white_spaces(
|
@ddt.data('name', 'display_name', 'description', 'display_description')
|
||||||
self):
|
def test_validate_name_and_description_with_name_too_long(self, attribute):
|
||||||
body = {'name': 'a' * 255 + " "}
|
body = {attribute: 'a' * 256}
|
||||||
self.controller.validate_string_length(
|
|
||||||
body['name'], 'name', min_length=1, max_length=255,
|
|
||||||
remove_whitespaces=True)
|
|
||||||
|
|
||||||
def test_validate_name_and_description_with_name_too_long(self):
|
|
||||||
body = {'name': 'a' * 256}
|
|
||||||
self.assertRaises(webob.exc.HTTPBadRequest,
|
self.assertRaises(webob.exc.HTTPBadRequest,
|
||||||
self.controller.validate_name_and_description,
|
self.controller.validate_name_and_description,
|
||||||
body)
|
body)
|
||||||
|
|
||||||
def test_validate_name_and_description_with_desc_too_long(self):
|
@ddt.data('name', 'display_name', 'description', 'display_description')
|
||||||
body = {'description': 'a' * 256}
|
def test_validate_name_and_description_with_name_as_int(self, attribute):
|
||||||
|
body = {attribute: 1234}
|
||||||
self.assertRaises(webob.exc.HTTPBadRequest,
|
self.assertRaises(webob.exc.HTTPBadRequest,
|
||||||
self.controller.validate_name_and_description,
|
self.controller.validate_name_and_description,
|
||||||
body)
|
body)
|
||||||
|
|
||||||
def test_validate_name_and_description_with_name_as_int(self):
|
@ddt.data('name', 'display_name', 'description', 'display_description')
|
||||||
body = {'name': 1234}
|
def test_validate_name_and_description_with_name_zero_length(self,
|
||||||
self.assertRaises(webob.exc.HTTPBadRequest,
|
attribute):
|
||||||
self.controller.validate_name_and_description,
|
|
||||||
body)
|
|
||||||
|
|
||||||
def test_validate_name_and_description_with_desc_as_int(self):
|
|
||||||
body = {'description': 1234}
|
|
||||||
self.assertRaises(webob.exc.HTTPBadRequest,
|
|
||||||
self.controller.validate_name_and_description,
|
|
||||||
body)
|
|
||||||
|
|
||||||
def test_validate_name_and_description_with_name_zero_length(self):
|
|
||||||
# NOTE(jdg): We allow zero length names currently, particularly
|
# NOTE(jdg): We allow zero length names currently, particularly
|
||||||
# from Nova, changes to this require an API version bump
|
# from Nova, changes to this require an API version bump
|
||||||
body = {'name': ""}
|
body = {attribute: ""}
|
||||||
self.controller.validate_name_and_description(body)
|
self.controller.validate_name_and_description(body)
|
||||||
self.assertEqual('', body['name'])
|
self.assertEqual('', body[attribute])
|
||||||
|
|
||||||
def test_validate_name_and_description_with_desc_zero_length(self):
|
|
||||||
body = {'description': ""}
|
|
||||||
self.controller.validate_name_and_description(body)
|
|
||||||
self.assertEqual('', body['description'])
|
|
||||||
|
|
||||||
|
@ddt.data('name', 'display_name', 'description', 'display_description')
|
||||||
def test_validate_name_and_description_with_name_contains_white_spaces(
|
def test_validate_name_and_description_with_name_contains_white_spaces(
|
||||||
self):
|
self, attribute):
|
||||||
body = {'name': 'a' * 255 + " "}
|
body = {attribute: 'a' * 255 + " "}
|
||||||
self.controller.validate_name_and_description(body)
|
self.controller.validate_name_and_description(body)
|
||||||
self.assertEqual('a' * 255, body['name'])
|
self.assertEqual('a' * 255, body[attribute])
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
---
|
||||||
|
fixes:
|
||||||
|
- Add 'display_name' and 'display_description' validation
|
||||||
|
for creating/updating snapshot and volume operations.
|
||||||
Reference in New Issue
Block a user