From 29869a648bcf56f6ec95001e169ca1aa327df8f1 Mon Sep 17 00:00:00 2001 From: Graham Hayes Date: Mon, 13 Apr 2015 12:17:42 +0200 Subject: [PATCH] Add validation of RRSet Type Change-Id: I91861dbbd9f74edd76cb2d2c8cf3ac9507278aba Closes-Bug: #1443348 --- designate/objects/recordset.py | 20 +++++++++++++--- .../tests/test_api/test_v2/test_recordsets.py | 23 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/designate/objects/recordset.py b/designate/objects/recordset.py index a18e7c8e3..1b6bbc7d6 100644 --- a/designate/objects/recordset.py +++ b/designate/objects/recordset.py @@ -129,9 +129,24 @@ class RecordSet(base.DictObjectMixin, base.PersistentObjectMixin, def validate(self): + errors = ValidationErrorList() + # Get the right classes (e.g. A for Recordsets with type: 'A') - record_list_cls = self.obj_cls_from_name('%sList' % self.type) - record_cls = self.obj_cls_from_name(self.type) + try: + record_list_cls = self.obj_cls_from_name('%sList' % self.type) + record_cls = self.obj_cls_from_name(self.type) + except KeyError as e: + e = ValidationError() + e.path = ['recordset', 'type'] + e.validator = 'value' + e.validator_value = [self.type] + e.message = ("'%(type)s' is not a supported Record type" + % {'type': self.type}) + # Add it to the list for later + errors.append(e) + raise exceptions.InvalidObject( + "Provided object does not match " + "schema", errors=errors, object=self) # Get any rules that the record type imposes on the record changes = record_cls.get_recordset_schema_changes() @@ -142,7 +157,6 @@ class RecordSet(base.DictObjectMixin, base.PersistentObjectMixin, old_fields = deepcopy(self.FIELDS) self.FIELDS = utils.deep_dict_merge(self.FIELDS, changes) - errors = ValidationErrorList() error_indexes = [] # Copy these for safekeeping old_records = deepcopy(self.records) diff --git a/designate/tests/test_api/test_v2/test_recordsets.py b/designate/tests/test_api/test_v2/test_recordsets.py index 561c76df8..84f2476e2 100644 --- a/designate/tests/test_api/test_v2/test_recordsets.py +++ b/designate/tests/test_api/test_v2/test_recordsets.py @@ -107,6 +107,29 @@ class ApiV2RecordSetsTest(ApiV2TestCase): self._assert_exception( 'invalid_object', 400, self.client.post_json, url, body) + def test_create_recordset_with_invalid_type(self): + # Prepare a RecordSet fixture + body = self.get_recordset_fixture( + self.domain['name'], + 'A', + fixture=0, + values={ + 'name': 'name.%s' % self.domain['name'], + 'records': [ + '192.0.2.1', + '192.0.2.2', + ] + } + ) + + del body['type'] + + url = '/zones/%s/recordsets' % self.domain['id'] + + # Ensure it fails with a 400 + self._assert_exception( + 'invalid_object', 400, self.client.post_json, url, body) + def test_create_recordset_invalid_id(self): self._assert_invalid_uuid(self.client.post, '/zones/%s/recordsets')