From 33ee729a08bbb77f5ce1e0cc78132eaf45c1f72b Mon Sep 17 00:00:00 2001 From: Galyna Zholtkevych Date: Mon, 25 Jul 2016 15:10:08 +0300 Subject: [PATCH] Deny chassis with too long description Catching error at earlier stage when creating chassis with too long description that does not fit to the database table field. Change-Id: Iccd002f959d262765eb50bf4391981bdbfeec907 Closes-Bug: #1588347 --- ironic/api/controllers/v1/chassis.py | 2 +- ironic/tests/unit/api/v1/test_chassis.py | 24 +++++++++++++++++++ ...-chassis-description-0690d6f67ed002d5.yaml | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/deny-too-long-chassis-description-0690d6f67ed002d5.yaml diff --git a/ironic/api/controllers/v1/chassis.py b/ironic/api/controllers/v1/chassis.py index e43841b5f1..494161c7f2 100644 --- a/ironic/api/controllers/v1/chassis.py +++ b/ironic/api/controllers/v1/chassis.py @@ -50,7 +50,7 @@ class Chassis(base.APIBase): uuid = types.uuid """The UUID of the chassis""" - description = wtypes.text + description = wtypes.StringType(max_length=255) """The description of the chassis""" extra = {wtypes.text: types.jsontype} diff --git a/ironic/tests/unit/api/v1/test_chassis.py b/ironic/tests/unit/api/v1/test_chassis.py index b3be071ccb..d0f55e4066 100644 --- a/ironic/tests/unit/api/v1/test_chassis.py +++ b/ironic/tests/unit/api/v1/test_chassis.py @@ -43,6 +43,11 @@ class TestChassisObject(base.TestCase): chassis = api_chassis.Chassis(**chassis_dict) self.assertEqual(wtypes.Unset, chassis.description) + def test_chassis_sample(self): + expected_description = 'Sample chassis' + sample = api_chassis.Chassis.sample(expand=False) + self.assertEqual(expected_description, sample.as_dict()['description']) + class TestListChassis(test_api_base.BaseApiTest): @@ -445,6 +450,25 @@ class TestPost(test_api_base.BaseApiTest): result = self.get_json('/chassis/%s' % cdict['uuid']) self.assertEqual(descr, result['description']) + def test_create_chassis_toolong_description(self): + descr = 'a' * 256 + valid_error_message = ('Value should have a maximum character ' + 'requirement of 255') + cdict = apiutils.chassis_post_data(description=descr) + response = self.post_json('/chassis', cdict, expect_errors=True) + self.assertEqual(http_client.BAD_REQUEST, response.status_int) + self.assertEqual('application/json', response.content_type) + self.assertIn(valid_error_message, response.json['error_message']) + + def test_create_chassis_invalid_description(self): + descr = 1334 + valid_error_message = 'Value should be string' + cdict = apiutils.chassis_post_data(description=descr) + response = self.post_json('/chassis', cdict, expect_errors=True) + self.assertEqual(http_client.BAD_REQUEST, response.status_int) + self.assertEqual('application/json', response.content_type) + self.assertIn(valid_error_message, response.json['error_message']) + class TestDelete(test_api_base.BaseApiTest): diff --git a/releasenotes/notes/deny-too-long-chassis-description-0690d6f67ed002d5.yaml b/releasenotes/notes/deny-too-long-chassis-description-0690d6f67ed002d5.yaml new file mode 100644 index 0000000000..fcd2dbf54b --- /dev/null +++ b/releasenotes/notes/deny-too-long-chassis-description-0690d6f67ed002d5.yaml @@ -0,0 +1,4 @@ +fixes: + - Deny too long description of chassis + (more than 255 chars) with appropriate + correct message.