Add the max length check for environment update

environment update don't check the name max length,
So will cause the db insert error.

Change-Id: I96c93615620435bcbc8d841ab3b34ab1d99389e9
Closes-Bug: #1536287
This commit is contained in:
zhurong 2016-01-20 17:26:21 +00:00
parent 86f5eefb5e
commit 4992c26fe0
2 changed files with 32 additions and 3 deletions

View File

@ -72,7 +72,7 @@ class Controller(object):
name = six.text_type(body['name'])
if len(name) > 255:
msg = _('Environment name should be 255 characters maximum')
LOG.exception(msg)
LOG.error(msg)
raise exc.HTTPBadRequest(explanation=msg)
try:
environment = envs.EnvironmentServices.create(
@ -80,7 +80,7 @@ class Controller(object):
request.context)
except db_exc.DBDuplicateEntry:
msg = _('Environment with specified name already exists')
LOG.exception(msg)
LOG.error(msg)
raise exc.HTTPConflict(explanation=msg)
return environment.to_dict()
@ -129,7 +129,12 @@ class Controller(object):
session = db_session.get_session()
environment = session.query(models.Environment).get(environment_id)
if str(body['name']).strip():
new_name = six.text_type(body['name'])
if new_name.strip():
if len(new_name) > 255:
msg = _('Environment name should be 255 characters maximum')
LOG.error(msg)
raise exc.HTTPBadRequest(explanation=msg)
try:
environment.update(body)
environment.save(session)

View File

@ -290,6 +290,30 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase):
result = req.get_response(self.api)
self.assertEqual(409, result.status_code)
def test_too_long_environment_name_update(self):
"""Check that update a too long env name results in
an HTTPBadResquest.
"""
self._set_policy_rules(
{'update_environment': '@'}
)
self._create_fake_environment('env1', '111')
self.expect_policy_check('update_environment',
{'environment_id': '111'})
new_name = 'env1' * 64
body = {
'name': new_name
}
req = self._put('/environments/111', json.dumps(body))
result = req.get_response(self.api)
self.assertEqual(400, result.status_code)
result_msg = result.text.replace('\n', '')
self.assertIn('Environment name should be 255 characters maximum',
result_msg)
def test_delete_environment(self):
"""Test that environment deletion results in the correct rpc call."""
result = self._test_delete_or_abandon(abandon=False)