Bay name must start with alphabets only

Bay name should start with only small or capital letter. Names
can contain small letter([a-z]), capital letter([A-Z]), hypen(-),
digits([0-1]), underscore (_) and periods (.). Names should follow
^[a-zA-Z][a-zA-Z0-9_.-]*$ regular expression.

Change-Id: I7581f0a99cee5708601b11c571a774acbeace3bd
Closes-Bug: #1605561
This commit is contained in:
Rajiv Kumar 2016-07-26 12:24:12 +05:30
parent 100cdedeaa
commit c89829622c
2 changed files with 93 additions and 1 deletions

View File

@ -83,7 +83,8 @@ class Bay(base.APIBase):
uuid = types.uuid
"""Unique UUID for this bay"""
name = wtypes.StringType(min_length=1, max_length=255)
name = wtypes.StringType(min_length=1, max_length=255,
pattern='^[a-zA-Z][a-zA-Z0-9_.-]*$')
"""Name of this bay"""
baymodel_id = wsme.wsproperty(wtypes.text, _get_baymodel_id,

View File

@ -538,6 +538,97 @@ class TestPost(api_base.FunctionalTest):
self.assertEqual(400, response.status_int)
self.assertTrue(response.json['errors'])
def test_create_bay_with_invalid_integer_name(self):
bdict = apiutils.bay_post_data(name='123456')
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['errors'])
def test_create_bay_with_invalid_integer_str_name(self):
bdict = apiutils.bay_post_data(name='123456test_bay')
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['errors'])
def test_create_bay_with_hyphen_invalid_at_start_name(self):
bdict = apiutils.bay_post_data(name='-test_bay')
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['errors'])
def test_create_bay_with_period_invalid_at_start_name(self):
bdict = apiutils.bay_post_data(name='.test_bay')
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['errors'])
def test_create_bay_with_underscore_invalid_at_start_name(self):
bdict = apiutils.bay_post_data(name='_test_bay')
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['errors'])
def test_create_bay_with_valid_str_int_name(self):
bdict = apiutils.bay_post_data(name='test_bay123456')
response = self.post_json('/bays', bdict, expect_errors=True)
self.assertEqual('application/json', response.content_type)
self.assertEqual(201, response.status_int)
self.assertEqual(response.json['name'], bdict['name'])
def test_create_bay_with_hyphen_valid_name(self):
bdict = apiutils.bay_post_data(name='test-bay')
response = self.post_json('/bays', bdict, expect_errors=True)
self.assertEqual('application/json', response.content_type)
self.assertEqual(201, response.status_int)
self.assertEqual(response.json['name'], bdict['name'])
def test_create_bay_with_period_valid_name(self):
bdict = apiutils.bay_post_data(name='test.bay')
response = self.post_json('/bays', bdict, expect_errors=True)
self.assertEqual('application/json', response.content_type)
self.assertEqual(201, response.status_int)
self.assertEqual(response.json['name'], bdict['name'])
def test_create_bay_with_period_at_end_valid_name(self):
bdict = apiutils.bay_post_data(name='testbay.')
response = self.post_json('/bays', bdict, expect_errors=True)
self.assertEqual('application/json', response.content_type)
self.assertEqual(201, response.status_int)
self.assertEqual(response.json['name'], bdict['name'])
def test_create_bay_with_hyphen_at_end_valid_name(self):
bdict = apiutils.bay_post_data(name='testbay-')
response = self.post_json('/bays', bdict, expect_errors=True)
self.assertEqual('application/json', response.content_type)
self.assertEqual(201, response.status_int)
self.assertEqual(response.json['name'], bdict['name'])
def test_create_bay_with_underscore_at_end_valid_name(self):
bdict = apiutils.bay_post_data(name='testbay_')
response = self.post_json('/bays', bdict, expect_errors=True)
self.assertEqual('application/json', response.content_type)
self.assertEqual(201, response.status_int)
self.assertEqual(response.json['name'], bdict['name'])
def test_create_bay_with_mix_special_char_valid_name(self):
bdict = apiutils.bay_post_data(name='test.-_bay')
response = self.post_json('/bays', bdict, expect_errors=True)
self.assertEqual('application/json', response.content_type)
self.assertEqual(201, response.status_int)
self.assertEqual(response.json['name'], bdict['name'])
def test_create_bay_with_capital_letter_start_valid_name(self):
bdict = apiutils.bay_post_data(name='Testbay')
response = self.post_json('/bays', bdict, expect_errors=True)
self.assertEqual('application/json', response.content_type)
self.assertEqual(201, response.status_int)
self.assertEqual(response.json['name'], bdict['name'])
def test_create_bay_with_invalid_empty_name(self):
bdict = apiutils.bay_post_data(name='')
response = self.post_json('/bays', bdict, expect_errors=True)