From 6fc82fd279e32c9e1f35d98a8d32575ba49831a3 Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Fri, 23 Jun 2017 13:17:32 -0700 Subject: [PATCH] Validate that MX preference parses as int --- octodns/record.py | 8 +++++++- tests/test_octodns_record.py | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/octodns/record.py b/octodns/record.py index 0388911..03ce675 100644 --- a/octodns/record.py +++ b/octodns/record.py @@ -424,8 +424,14 @@ class MxValue(object): @classmethod def _validate_value(cls, value): reasons = [] - if 'preference' not in value and 'priority' not in value: + try: + # seperate lines to have preference set in the ValueError case + preference = value.get('preference', None) or value['priority'] + int(preference) + except KeyError: reasons.append('missing preference') + except ValueError: + reasons.append('invalid preference "{}"'.format(preference)) exchange = None try: exchange = value.get('exchange', None) or value['value'] diff --git a/tests/test_octodns_record.py b/tests/test_octodns_record.py index cb87c70..0230e2c 100644 --- a/tests/test_octodns_record.py +++ b/tests/test_octodns_record.py @@ -898,7 +898,7 @@ class TestRecordValidation(TestCase): } }) - # missing priority + # missing preference with self.assertRaises(ValidationError) as ctx: Record.new(self.zone, '', { 'type': 'MX', @@ -909,7 +909,19 @@ class TestRecordValidation(TestCase): }) self.assertEquals(['missing preference'], ctx.exception.reasons) - # missing value + # invalid preference + with self.assertRaises(ValidationError) as ctx: + Record.new(self.zone, '', { + 'type': 'MX', + 'ttl': 600, + 'value': { + 'preference': 'nope', + 'exchange': 'foo.bar.com.' + } + }) + self.assertEquals(['invalid preference "nope"'], ctx.exception.reasons) + + # missing exchange with self.assertRaises(ValidationError) as ctx: Record.new(self.zone, '', { 'type': 'MX',