Browse Source

Validate that MX preference parses as int

pull/77/head
Ross McFarland 9 years ago
parent
commit
6fc82fd279
2 changed files with 21 additions and 3 deletions
  1. +7
    -1
      octodns/record.py
  2. +14
    -2
      tests/test_octodns_record.py

+ 7
- 1
octodns/record.py View File

@ -424,8 +424,14 @@ class MxValue(object):
@classmethod @classmethod
def _validate_value(cls, value): def _validate_value(cls, value):
reasons = [] 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') reasons.append('missing preference')
except ValueError:
reasons.append('invalid preference "{}"'.format(preference))
exchange = None exchange = None
try: try:
exchange = value.get('exchange', None) or value['value'] exchange = value.get('exchange', None) or value['value']


+ 14
- 2
tests/test_octodns_record.py View File

@ -898,7 +898,7 @@ class TestRecordValidation(TestCase):
} }
}) })
# missing priority
# missing preference
with self.assertRaises(ValidationError) as ctx: with self.assertRaises(ValidationError) as ctx:
Record.new(self.zone, '', { Record.new(self.zone, '', {
'type': 'MX', 'type': 'MX',
@ -909,7 +909,19 @@ class TestRecordValidation(TestCase):
}) })
self.assertEquals(['missing preference'], ctx.exception.reasons) 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: with self.assertRaises(ValidationError) as ctx:
Record.new(self.zone, '', { Record.new(self.zone, '', {
'type': 'MX', 'type': 'MX',


Loading…
Cancel
Save