From ac2b523302604abca83212233d5d68ea43651877 Mon Sep 17 00:00:00 2001 From: Nikolay Starodubtsev Date: Mon, 19 Oct 2015 17:20:25 +0300 Subject: [PATCH] Remove obsolete names checks Since we don't use environments and environment templates names during deployment process we don't need to check what is the name of the environment. Now we should only keep it human-readable and check that it exists. Partial-Bug: #1405788 Change-Id: I88b447e0dbe9fde07602d4ce1e746c1ccfa62637 --- murano/api/v1/environments.py | 32 +++++++------------ murano/api/v1/templates.py | 20 ++++++------ .../functional/api/v1/test_env_templates.py | 2 +- murano/tests/functional/api/v1/test_envs.py | 26 +++++++++++++++ .../tests/unit/api/v1/test_env_templates.py | 2 +- murano/tests/unit/api/v1/test_environments.py | 9 +++--- 6 files changed, 53 insertions(+), 38 deletions(-) diff --git a/murano/api/v1/environments.py b/murano/api/v1/environments.py index 722a0bc9..37ce3528 100644 --- a/murano/api/v1/environments.py +++ b/murano/api/v1/environments.py @@ -12,8 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import re - from oslo_db import exception as db_exc from oslo_log import log as logging from sqlalchemy import desc @@ -39,8 +37,6 @@ LOG = logging.getLogger(__name__) API_NAME = 'Environments' -VALID_NAME_REGEX = re.compile('^[a-zA-Z]+[\w.-]*$') - class Controller(object): @request_statistics.stats_count(API_NAME, 'Index') @@ -67,7 +63,7 @@ class Controller(object): LOG.debug('Environments:Create '.format(body=body)) policy.check('create_environment', request.context) - if not body.get('name'): + if not('name' in body and body['name'].strip()): msg = _('Please, specify a name of the environment to create') LOG.exception(msg) raise exc.HTTPBadRequest(explanation=msg) @@ -77,20 +73,14 @@ class Controller(object): msg = _('Environment name should be 255 characters maximum') LOG.exception(msg) raise exc.HTTPBadRequest(explanation=msg) - if VALID_NAME_REGEX.match(name): - try: - environment = envs.EnvironmentServices.create( - body.copy(), - request.context) - except db_exc.DBDuplicateEntry: - msg = _('Environment with specified name already exists') - LOG.exception(msg) - raise exc.HTTPConflict(explanation=msg) - else: - msg = _('Environment name must contain only alphanumeric or "_-." ' - 'characters, must start with alpha') + try: + environment = envs.EnvironmentServices.create( + body.copy(), + request.context) + except db_exc.DBDuplicateEntry: + msg = _('Environment with specified name already exists') LOG.exception(msg) - raise exc.HTTPClientError(explanation=msg) + raise exc.HTTPConflict(explanation=msg) return environment.to_dict() @@ -138,7 +128,7 @@ class Controller(object): session = db_session.get_session() environment = session.query(models.Environment).get(environment_id) - if VALID_NAME_REGEX.match(str(body['name'])): + if str(body['name']).strip(): try: environment.update(body) environment.save(session) @@ -147,8 +137,8 @@ class Controller(object): LOG.error(msg) raise exc.HTTPConflict(explanation=msg) else: - msg = _('Environment name must contain only alphanumeric ' - 'or "_-." characters, must start with alpha') + msg = _('Environment name must contain at least one ' + 'non-white space symbol') LOG.error(msg) raise exc.HTTPClientError(explanation=msg) diff --git a/murano/api/v1/templates.py b/murano/api/v1/templates.py index 385ca401..364de5cb 100644 --- a/murano/api/v1/templates.py +++ b/murano/api/v1/templates.py @@ -16,7 +16,6 @@ from oslo_db import exception as db_exc from oslo_log import log as logging from webob import exc -from murano.api.v1 import environments as envs_api from murano.api.v1 import request_statistics from murano.common.i18n import _, _LE from murano.common import policy @@ -63,9 +62,9 @@ class Controller(object): try: LOG.debug('ENV TEMP NAME: {templ_name}>'.format( templ_name=body['name'])) - if not envs_api.VALID_NAME_REGEX.match(str(body['name'])): - msg = _('Environment Template must contain only alphanumeric ' - 'or "_-." characters, must start with alpha') + if not str(body['name']).strip(): + msg = _('Environment Template must contain at least one ' + 'non-white space symbol') LOG.error(msg) raise exc.HTTPBadRequest(msg) except Exception: @@ -125,9 +124,9 @@ class Controller(object): try: LOG.debug('ENV TEMP NAME: {temp_name}>'.format( temp_name=body['name'])) - if not envs_api.VALID_NAME_REGEX.match(str(body['name'])): - msg = _('Env Template must contain only alphanumeric ' - 'or "_-." characters, must start with alpha') + if not str(body['name']).strip(): + msg = _('Environment Template must contain at least one ' + 'non-white space symbol') LOG.exception(msg) raise exc.HTTPBadRequest(msg) except Exception: @@ -182,10 +181,9 @@ class Controller(object): template = env_temps.EnvTemplateServices.\ get_env_template(env_template_id) - if ('name' not in body or - not envs_api.VALID_NAME_REGEX.match(str(body['name']))): - msg = _('Environment must contain only alphanumeric ' - 'or "_-." characters, must start with alpha') + if ('name' not in body or not str(body['name']).strip()): + msg = _('Environment Template must contain at least one ' + 'non-white space symbol') LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) LOG.debug('ENVIRONMENT NAME: {env_name}>'.format( diff --git a/murano/tests/functional/api/v1/test_env_templates.py b/murano/tests/functional/api/v1/test_env_templates.py index 8c29d9f0..48f50789 100644 --- a/murano/tests/functional/api/v1/test_env_templates.py +++ b/murano/tests/functional/api/v1/test_env_templates.py @@ -147,7 +147,7 @@ class TestEnvTemplate(base.TestCase): """Check the deletion of an wrong environment template request.""" self.assertRaises(exceptions.BadRequest, self.client.create_env_template, - '-+3') + ' ') @tag('all', 'coverage') @attr(type='negative') diff --git a/murano/tests/functional/api/v1/test_envs.py b/murano/tests/functional/api/v1/test_envs.py index 94298dfa..2d3a430b 100644 --- a/murano/tests/functional/api/v1/test_envs.py +++ b/murano/tests/functional/api/v1/test_envs.py @@ -54,6 +54,32 @@ class TestEnvironments(base.TestCase): self.environments.pop(self.environments.index(env)) + @tag('all', 'coverage') + @attr(type='smoke') + def test_create_and_delete_environment_with_unicode_name(self): + environments_list_start = self.client.get_environments_list()[1] + + unicode_name = u'$yaql \u2665 unicode' + resp, env = self.client.create_environment(unicode_name) + self.environments.append(env) + + self.assertEqual(resp.status, 200) + self.assertEqual(unicode_name, env['name']) + + environments_list = self.client.get_environments_list()[1] + + self.assertEqual(len(environments_list_start['environments']) + 1, + len(environments_list['environments'])) + + self.client.delete_environment(env['id']) + + environments_list = self.client.get_environments_list()[1] + + self.assertEqual(len(environments_list_start['environments']), + len(environments_list['environments'])) + + self.environments.pop(self.environments.index(env)) + @tag('all', 'coverage') @attr(type='smoke') def test_get_environment(self): diff --git a/murano/tests/unit/api/v1/test_env_templates.py b/murano/tests/unit/api/v1/test_env_templates.py index f2364c83..115fec7b 100644 --- a/murano/tests/unit/api/v1/test_env_templates.py +++ b/murano/tests/unit/api/v1/test_env_templates.py @@ -95,7 +95,7 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): ) self.expect_policy_check('create_env_template') - body = {'name': 'my+#temp'} + body = {'name': ' '} req = self._post('/templates', json.dumps(body)) result = req.get_response(self.api) self.assertEqual(400, result.status_code) diff --git a/murano/tests/unit/api/v1/test_environments.py b/murano/tests/unit/api/v1/test_environments.py index e77ef6b0..96d62429 100644 --- a/murano/tests/unit/api/v1/test_environments.py +++ b/murano/tests/unit/api/v1/test_environments.py @@ -140,13 +140,14 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): ) self.expect_policy_check('create_environment') - body = {'name': 'my+#env'} + body = {'name': ' '} req = self._post('/environments', json.dumps(body)) result = req.get_response(self.api) self.assertEqual(400, result.status_code) def test_unicode_environment_name_create(self): - """Check that an unicode env name results in an HTTPClientError.""" + """Check that an unicode env name doesn't raise an HTTPClientError.""" + self._configure_opts() self._set_policy_rules( {'list_environments': '@', 'create_environment': '@', @@ -154,10 +155,10 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): ) self.expect_policy_check('create_environment') - body = {'name': u'yaql ♥ unicode'.encode('utf-8')} + body = {'name': u'$yaql \u2665 unicode'} req = self._post('/environments', json.dumps(body)) result = req.get_response(self.api) - self.assertEqual(400, result.status_code) + self.assertEqual(200, result.status_code) def test_no_environment_name_create(self): """Check that no env name provided results in an HTTPBadResquest."""