From 4992c26fe0fd67307c1f6330f0ece30f88429f51 Mon Sep 17 00:00:00 2001 From: zhurong Date: Wed, 20 Jan 2016 17:26:21 +0000 Subject: [PATCH] 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 --- murano/api/v1/environments.py | 11 ++++++--- murano/tests/unit/api/v1/test_environments.py | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/murano/api/v1/environments.py b/murano/api/v1/environments.py index 511e61e2..84c985ac 100644 --- a/murano/api/v1/environments.py +++ b/murano/api/v1/environments.py @@ -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) diff --git a/murano/tests/unit/api/v1/test_environments.py b/murano/tests/unit/api/v1/test_environments.py index 96d62429..6eafb831 100644 --- a/murano/tests/unit/api/v1/test_environments.py +++ b/murano/tests/unit/api/v1/test_environments.py @@ -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)