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:
TommyLike 2017-02-20 20:56:13 -05:00
parent 2decc903c4
commit 52fb5585bc
3 changed files with 32 additions and 51 deletions

View File

@ -1263,20 +1263,14 @@ class Controller(object):
@staticmethod
def validate_name_and_description(body):
name = body.get('name')
if name is not None:
if isinstance(name, six.string_types):
body['name'] = name.strip()
for attribute in ['name', 'description',
'display_name', 'display_description']:
value = body.get(attribute)
if value is not None:
if isinstance(value, six.string_types):
body[attribute] = value.strip()
try:
utils.check_string_length(body['name'], 'Name',
min_length=0, max_length=255)
except exception.InvalidInput as error:
raise webob.exc.HTTPBadRequest(explanation=error.msg)
description = body.get('description')
if description is not None:
try:
utils.check_string_length(description, 'Description',
utils.check_string_length(body[attribute], attribute,
min_length=0, max_length=255)
except exception.InvalidInput as error:
raise webob.exc.HTTPBadRequest(explanation=error.msg)

View File

@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import ddt
import inspect
import mock
@ -820,6 +821,7 @@ class ResponseObjectTest(test.TestCase):
self.assertEqual({}, robj.serializers)
@ddt.data
class ValidBodyTest(test.TestCase):
def setUp(self):
@ -855,51 +857,32 @@ class ValidBodyTest(test.TestCase):
name, 'Name', min_length=1, max_length=255,
remove_whitespaces=False)
def test_validate_string_length_with_name_contains_white_spaces(
self):
body = {'name': 'a' * 255 + " "}
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}
@ddt.data('name', 'display_name', 'description', 'display_description')
def test_validate_name_and_description_with_name_too_long(self, attribute):
body = {attribute: 'a' * 256}
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.validate_name_and_description,
body)
def test_validate_name_and_description_with_desc_too_long(self):
body = {'description': 'a' * 256}
@ddt.data('name', 'display_name', 'description', 'display_description')
def test_validate_name_and_description_with_name_as_int(self, attribute):
body = {attribute: 1234}
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.validate_name_and_description,
body)
def test_validate_name_and_description_with_name_as_int(self):
body = {'name': 1234}
self.assertRaises(webob.exc.HTTPBadRequest,
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):
@ddt.data('name', 'display_name', 'description', 'display_description')
def test_validate_name_and_description_with_name_zero_length(self,
attribute):
# NOTE(jdg): We allow zero length names currently, particularly
# from Nova, changes to this require an API version bump
body = {'name': ""}
body = {attribute: ""}
self.controller.validate_name_and_description(body)
self.assertEqual('', body['name'])
def test_validate_name_and_description_with_desc_zero_length(self):
body = {'description': ""}
self.controller.validate_name_and_description(body)
self.assertEqual('', body['description'])
self.assertEqual('', body[attribute])
@ddt.data('name', 'display_name', 'description', 'display_description')
def test_validate_name_and_description_with_name_contains_white_spaces(
self):
body = {'name': 'a' * 255 + " "}
self, attribute):
body = {attribute: 'a' * 255 + " "}
self.controller.validate_name_and_description(body)
self.assertEqual('a' * 255, body['name'])
self.assertEqual('a' * 255, body[attribute])

View File

@ -0,0 +1,4 @@
---
fixes:
- Add 'display_name' and 'display_description' validation
for creating/updating snapshot and volume operations.