Skip to content

Commit

Permalink
Added missing validation for CNAME values
Browse files Browse the repository at this point in the history
  • Loading branch information
shupp committed Nov 28, 2016
1 parent 0a84c9a commit f888156
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
26 changes: 25 additions & 1 deletion tests/models/test_record.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest

from vegadns.api.models.domain import Record
from vegadns.api.models.record import Record
from vegadns.api.models.recordtypes import RecordValueException


class TestRecord(unittest.TestCase):
Expand All @@ -14,3 +15,26 @@ def test_hostname_in_domain(self):
self.assertTrue(record.hostname_in_domain(domain, domain))
self.assertFalse(record.hostname_in_domain(bad, domain))
self.assertTrue(record.hostname_in_domain(good, domain))

def test_cname_validation_fail(self):
record = Record()
record.type = 'C'
record.domain_id = 1
record.host = 'foobar.com'
record.val = 'www.example.com '

with self.assertRaises(RecordValueException) as cm:
record.validate()
self.assertEquals(
'Invalid cname value: www.example.com ',
cm.exception.message
)

def test_cname_validation_success(self):
record = Record()
record.type = 'C'
record.domain_id = 1
record.host = 'foobar.com'
record.val = 'www.example.com'

self.assertIsNone(record.validate())
11 changes: 11 additions & 0 deletions vegadns/api/models/recordtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,21 @@ def validate(self, default_record=False):
class CNAMERecord(CommonRecord):
record_type = 'CNAME'

def validate_cname_record_value(self, default_record=False):
value = str(self.values.get("value"))
if default_record:
test_value = value.replace("DOMAIN", "example.com")
else:
test_value = value

if not ValidateDNS.record_hostname(test_value):
raise RecordValueException("Invalid cname value: " + value)

def validate(self, default_record=False):
if not default_record:
self.validate_domain_id()
self.validate_record_hostname(default_record)
self.validate_cname_record_value(default_record)

value = str(self.values.get("value"))
if ValidateIPAddress.ipv4(value):
Expand Down

0 comments on commit f888156

Please sign in to comment.