Add string length validation to names in APIs

Add string length validation bays, bay models and containers.

Change-Id: I50f39e6416bda1a64f8bd997a0bb4c38ead63bfd
Partial-Bug: 1406457
This commit is contained in:
Tom Cammann 2015-05-01 15:56:46 +01:00
parent 16cf763e8c
commit 88fd313abd
6 changed files with 50 additions and 3 deletions

View File

@ -70,7 +70,7 @@ class Bay(base.APIBase):
uuid = types.uuid
"""Unique UUID for this bay"""
name = wtypes.text
name = wtypes.StringType(min_length=1, max_length=255)
"""Name of this bay"""
baymodel_id = wsme.wsproperty(wtypes.text, _get_baymodel_id,

View File

@ -56,7 +56,7 @@ class BayModel(base.APIBase):
uuid = types.uuid
"""Unique UUID for this baymodel"""
name = wtypes.text
name = wtypes.StringType(min_length=1, max_length=255)
"""The name of the bay model"""
coe = wsme.wsproperty(wtypes.text, _get_coe, _set_coe, mandatory=True)

View File

@ -69,7 +69,7 @@ class Container(base.APIBase):
uuid = types.uuid
"""Unique UUID for this container"""
name = wtypes.text
name = wtypes.StringType(min_length=1, max_length=255)
"""Name of this container"""
image_id = wtypes.text

View File

@ -464,6 +464,20 @@ class TestPost(api_base.FunctionalTest):
self.assertEqual(400, response.status_int)
self.assertTrue(response.json['error_message'])
def test_create_bay_with_invalid_long_name(self):
bdict = apiutils.bay_post_data(name='x' * 256)
response = self.post_json('/bays', bdict, expect_errors=True)
self.assertEqual('application/json', response.content_type)
self.assertEqual(400, response.status_int)
self.assertTrue(response.json['error_message'])
def test_create_bay_with_invalid_empty_name(self):
bdict = apiutils.bay_post_data(name='')
response = self.post_json('/bays', bdict, expect_errors=True)
self.assertEqual('application/json', response.content_type)
self.assertEqual(400, response.status_int)
self.assertTrue(response.json['error_message'])
def test_create_bay_with_timeout_none(self):
bdict = apiutils.bay_post_data()
bdict['bay_create_timeout'] = None

View File

@ -369,6 +369,20 @@ class TestPost(api_base.FunctionalTest):
# Check that 'id' is not in first arg of positional args
self.assertNotIn('id', cc_mock.call_args[0][0])
def test_create_baymodel_with_invalid_empty_str_name(self):
with mock.patch.object(self.dbapi, 'create_baymodel',
wraps=self.dbapi.create_baymodel) as cc_mock:
cdict = apiutils.baymodel_post_data(name='')
self.assertRaises(AppError, self.post_json, '/baymodels', cdict)
self.assertFalse(cc_mock.called)
def test_create_baymodel_with_invalid_long_name(self):
with mock.patch.object(self.dbapi, 'create_baymodel',
wraps=self.dbapi.create_baymodel) as cc_mock:
cdict = apiutils.baymodel_post_data(name='i' * 256)
self.assertRaises(AppError, self.post_json, '/baymodels', cdict)
self.assertFalse(cc_mock.called)
@mock.patch('magnum.common.clients.OpenStackClients')
def test_create_baymodel_with_invalid_docker_volume_size(self,
mock_openstack_client):

View File

@ -204,3 +204,22 @@ class TestContainerController(db_base.DbTestCase):
c = response.json['containers']
self.assertEqual(0, len(c))
self.assertTrue(mock_container_create.called)
@patch('magnum.conductor.api.API.container_create')
def test_create_container_without_name(self, mock_container_create):
# No name param
params = ('{"image_id": "ubuntu", "command": "env",'
'"bay_uuid": "fff114da-3bfa-4a0f-a123-c0dffad9718e"}')
self.assertRaises(AppError, self.app.post, '/v1/containers',
params=params, content_type='application/json')
self.assertTrue(mock_container_create.not_called)
@patch('magnum.conductor.api.API.container_create')
def test_create_container_invalid_long_name(self, mock_container_create,):
# Long name
params = ('{"name": "' + 'i' * 256 + '", "image_id": "ubuntu",'
'"command": "env",'
'"bay_uuid": "fff114da-3bfa-4a0f-a123-c0dffad9718e"}')
self.assertRaises(AppError, self.app.post, '/v1/containers',
params=params, content_type='application/json')
self.assertTrue(mock_container_create.not_called)