Merge "Remove obsolete names checks"

This commit is contained in:
Jenkins 2015-11-04 11:17:05 +00:00 committed by Gerrit Code Review
commit fb273e513f
6 changed files with 53 additions and 38 deletions

View File

@ -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 <Body {body}>'.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)

View File

@ -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 _
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(

View File

@ -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')

View File

@ -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):

View File

@ -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)

View File

@ -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."""