Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relativise record names to zone name #188

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion netbox_dns/models/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def validate_name(self):
zone.to_unicode()
name.to_unicode()

self.name = name.to_text()
self.name = name.relativize(zone).to_text()

except dns.exception.DNSException as exc:
raise ValidationError(
Expand Down
3 changes: 0 additions & 3 deletions netbox_dns/tests/record/test_name_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ def test_name_validation_ok(self):
name=record.get("name"), zone=record.get("zone"), **self.record_data
)

self.assertEqual(record_object.name, record.get("name"))

def test_srv_validation_ok(self):
record = Record.objects.create(
name="_ldaps._tcp",
Expand Down Expand Up @@ -156,7 +154,6 @@ def test_name_validation_tolerant_ok(self):
record_object = Record.objects.create(
name=record.get("name"), zone=record.get("zone"), **self.record_data
)
self.assertEqual(record_object.name, record.get("name"))

@override_settings(
PLUGINS_CONFIG={
Expand Down
97 changes: 97 additions & 0 deletions netbox_dns/tests/record/test_normalization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from django.test import TestCase, override_settings
from django.core.exceptions import ValidationError

from netbox_dns.models import Zone, Record, RecordTypeChoices, NameServer


class RecordValidationTest(TestCase):
zone_data = {
"default_ttl": 86400,
"soa_rname": "hostmaster.example.com",
"soa_refresh": 43200,
"soa_retry": 7200,
"soa_expire": 2419200,
"soa_ttl": 86400,
"soa_minimum": 3600,
"soa_serial": 1,
}

record_data = {
"ttl": 86400,
}

@classmethod
def setUpTestData(cls):
cls.nameserver = NameServer.objects.create(name="ns1.example.com")
cls.zones = [
Zone(name="zone1.example.com", **cls.zone_data, soa_mname=cls.nameserver),
]
Zone.objects.bulk_create(cls.zones)

def test_normalize_to_empty_name(self):
f_zone = self.zones[0]

record = Record.objects.create(
name="zone1.example.com.",
zone=f_zone,
type=RecordTypeChoices.A,
value="10.0.1.42",
)

self.assertEqual(record.name, "@")

def test_normalize_to_relative_name(self):
f_zone = self.zones[0]

record = Record.objects.create(
name="sub.zone1.example.com.",
zone=f_zone,
type=RecordTypeChoices.A,
value="10.0.1.42",
)

self.assertEqual(record.name, "sub")

def test_normalize_to_empty_name_nonmatching_zone(self):
f_zone = self.zones[0]

with self.assertRaises(ValidationError):
Record.objects.create(
name="zone2.example.com.",
zone=f_zone,
type=RecordTypeChoices.A,
value="10.0.1.42",
)

def test_normalize_to_relative_name_nonmatching_zone(self):
f_zone = self.zones[0]

with self.assertRaises(ValidationError):
Record.objects.create(
name="sub.zone2.example.com.",
zone=f_zone,
type=RecordTypeChoices.A,
value="10.0.1.42",
)

def test_normalize_to_empty_name_parent_zone(self):
f_zone = self.zones[0]

with self.assertRaises(ValidationError):
Record.objects.create(
name="example.com.",
zone=f_zone,
type=RecordTypeChoices.A,
value="10.0.1.42",
)

def test_normalize_to_relative_name_parent_zone(self):
f_zone = self.zones[0]

with self.assertRaises(ValidationError):
Record.objects.create(
name="example.com.",
zone=f_zone,
type=RecordTypeChoices.A,
value="10.0.1.42",
)