Improve error message in case no environment name

When created environment without name provided in request body, it
throw internal exception to user. So check whether name exist at
first, if not, return HTTPBadRequest with  more clear message to
user.

Change-Id: Ie0c0c9341b1084aae90ecfa7f7cf7b31e958902a
Closes-Bug: #1492703
This commit is contained in:
Lin Yang
2015-09-06 17:42:27 +08:00
parent d5c6bdf594
commit b6277155fe
2 changed files with 23 additions and 0 deletions

View File

@@ -63,6 +63,12 @@ class Controller(object):
def create(self, request, body):
LOG.debug(u'Environments:Create <Body {0}>'.format(body))
policy.check('create_environment', request.context)
if not body.get('name'):
msg = _('Please, specify a name of the environment to create')
LOG.exception(msg)
raise exc.HTTPBadRequest(explanation=msg)
name = unicode(body['name'])
if len(name) > 255:
msg = _('Environment name should be 255 characters maximum')

View File

@@ -158,6 +158,23 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase):
result = req.get_response(self.api)
self.assertEqual(400, result.status_code)
def test_no_environment_name_create(self):
"""Check that no env name provided results in an HTTPBadResquest."""
self._set_policy_rules(
{'list_environments': '@',
'create_environment': '@',
'show_environment': '@'}
)
self.expect_policy_check('create_environment')
body = {'no_name': 'fake'}
req = self._post('/environments', json.dumps(body))
result = req.get_response(self.api)
self.assertEqual(400, result.status_code)
result_msg = result.text.replace('\n', '')
self.assertIn('Please, specify a name of the environment to create',
result_msg)
def test_too_long_environment_name_create(self):
"""Check that an too long env name results in an HTTPBadResquest."""
self._set_policy_rules(