Merge "Tags: harden validations"

This commit is contained in:
Zuul 2017-12-13 04:30:55 +00:00 committed by Gerrit Code Review
commit 0ee1099335
2 changed files with 16 additions and 3 deletions

View File

@ -71,7 +71,7 @@ def validate_tag(tag):
def validate_tags(body):
if 'tags' not in body:
if not isinstance(body, dict) or 'tags' not in body:
raise exceptions.InvalidInput(error_message=_("Invalid tags body"))
msg = validators.validate_list_of_unique_strings(body['tags'], MAX_TAG_LEN)
if msg:

View File

@ -161,8 +161,13 @@ class TestTagApiBase(test_securitygroup.SecurityGroupsTestCase,
subresource='tags', sub_id=tag)
return req.get_response(self.ext_api)
def _put_tags(self, tags):
body = {'tags': tags}
def _put_tags(self, tags=None, body=None):
if tags:
body = {'tags': tags}
elif body:
body = body
else:
body = {}
req = self._req('PUT', self.collection, data=body, id=self.resource_id,
subresource='tags')
return req.get_response(self.ext_api)
@ -273,6 +278,14 @@ class TestResourceTagApi(TestTagApiBase):
self._assertEqualTags(['red', 'green'], tags)
self._test_notification_report(expect_notify)
def test_put_invalid_tags(self):
res = self._put_tags()
self.assertEqual(400, res.status_int)
res = self._put_tags(body=7)
self.assertEqual(400, res.status_int)
res = self._put_tags(body={'invalid': True})
self.assertEqual(400, res.status_int)
def test_put_tags_replace(self):
res = self._put_tags(['red', 'green'])
self.assertEqual(200, res.status_int)