diff --git a/cinder/api/openstack/wsgi.py b/cinder/api/openstack/wsgi.py index 0e6d04b2d76..f37f2b9f239 100644 --- a/cinder/api/openstack/wsgi.py +++ b/cinder/api/openstack/wsgi.py @@ -1263,23 +1263,17 @@ 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() - 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', - min_length=0, max_length=255) - except exception.InvalidInput as error: - raise webob.exc.HTTPBadRequest(explanation=error.msg) + 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[attribute], attribute, + min_length=0, max_length=255) + except exception.InvalidInput as error: + raise webob.exc.HTTPBadRequest(explanation=error.msg) @staticmethod def validate_string_length(value, entity_name, min_length=0, diff --git a/cinder/tests/unit/api/openstack/test_wsgi.py b/cinder/tests/unit/api/openstack/test_wsgi.py index ed5d2409961..add4624b42a 100644 --- a/cinder/tests/unit/api/openstack/test_wsgi.py +++ b/cinder/tests/unit/api/openstack/test_wsgi.py @@ -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]) diff --git a/releasenotes/notes/check-displayname-displaydescription-123sd5gef91acb12.yaml b/releasenotes/notes/check-displayname-displaydescription-123sd5gef91acb12.yaml new file mode 100644 index 00000000000..fe24b8d0d47 --- /dev/null +++ b/releasenotes/notes/check-displayname-displaydescription-123sd5gef91acb12.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - Add 'display_name' and 'display_description' validation + for creating/updating snapshot and volume operations. \ No newline at end of file