def setUp(self): """Define the test client and other test variables.""" self.zone_sample = ForwardZone(name='example.org', primary_ns='ns.example.org', email='*****@*****.**') clean_and_save(self.zone_sample) self.zone_1010 = ReverseZone(name='10.10.in-addr.arpa', primary_ns='ns.example.org', email='*****@*****.**') clean_and_save(self.zone_1010) self.network_sample = Network(range='10.0.0.0/24', description='some description') clean_and_save(self.network_sample) self.ns_hostsample = Host(name='ns.example.org', contact='*****@*****.**') clean_and_save(self.ns_hostsample) self.ns_hostip = Ipaddress(host=self.ns_hostsample, ipaddress='10.0.0.111') clean_and_save(self.ns_hostip) self.ns_sample = NameServer(name='ns.example.org', ttl=300) clean_and_save(self.ns_sample) self.zone_sample.nameservers.add(self.ns_sample) self.zone_sample.save()
def setUp(self): """Define the test client and other test variables.""" self.zone_sample = ForwardZone(name='example.org', primary_ns='ns.example.org', email='*****@*****.**', serialno=1234567890, refresh=400, retry=300, expire=800, ttl=300)
class NameServerDeletionTestCase(TestCase): """This class defines the test suite for the NameServer model.""" def setUp(self): """Define the test client and other test variables.""" self.zone_sample = ForwardZone(name='example.org', primary_ns='ns.example.org', email='*****@*****.**') clean_and_save(self.zone_sample) self.zone_1010 = ReverseZone(name='10.10.in-addr.arpa', primary_ns='ns.example.org', email='*****@*****.**') clean_and_save(self.zone_1010) self.network_sample = Network(range='10.0.0.0/24', description='some description') clean_and_save(self.network_sample) self.ns_hostsample = Host(name='ns.example.org', contact='*****@*****.**') clean_and_save(self.ns_hostsample) self.ns_hostip = Ipaddress(host=self.ns_hostsample, ipaddress='10.0.0.111') clean_and_save(self.ns_hostip) self.ns_sample = NameServer(name='ns.example.org', ttl=300) clean_and_save(self.ns_sample) self.zone_sample.nameservers.add(self.ns_sample) self.zone_sample.save() def test_model_cant_delete_ns_host(self): """Test that it won't delete nameserver host-object if in use in a zone""" with self.assertRaises(PermissionDenied): self.ns_hostsample.delete() def test_model_cant_delete_ns_hostip(self): """Test that it won't delete nameserver with only 1 IP if in use in a zone""" with self.assertRaises(PermissionDenied): self.ns_hostip.delete() def test_model_can_delete_ns_hostip(self): """Test that the model is able to delete an IP from a nameserver, if nameserver has multiple IPs.""" self.ns_hostip2 = Ipaddress(host=self.ns_hostsample, ipaddress='10.0.0.112') clean_and_save(self.ns_hostip2) old_count = Ipaddress.objects.count() self.ns_hostip2.delete() new_count = Ipaddress.objects.count() self.assertNotEqual(old_count, new_count)
def setUp(self): """Define the test client and other variables.""" super().setUp() self.zone_one = ForwardZone(name="example.org", primary_ns="ns1.example.org", email="*****@*****.**") self.host_one = Host(name='ns1.example.org', contact="*****@*****.**") self.host_two = Host(name='ns2.example.org', contact="*****@*****.**") self.host_three = Host(name='ns3.example.org', contact="*****@*****.**") self.ns_one = NameServer(name='ns1.example.org', ttl=400) self.ns_two = NameServer(name='ns2.example.org', ttl=400) self.post_data_one = { 'name': 'example.com', 'primary_ns': ['ns1.example.org', 'ns2.example.org'], 'email': "*****@*****.**", 'refresh': 400, 'retry': 300, 'expire': 800, 'soa_ttl': 350 } self.post_data_two = { 'name': 'example.net', 'primary_ns': ['ns1.example.org', 'ns2.example.org'], 'email': "*****@*****.**" } self.patch_data = {'refresh': '500', 'expire': '1000'} clean_and_save(self.host_one) clean_and_save(self.host_two) clean_and_save(self.ns_one) clean_and_save(self.ns_two) clean_and_save(self.zone_one)
def setUp(self): """Define the test client and other test variables.""" self.zone_sample = ForwardZone(name='example.org', primary_ns='some-ns-server.example.org', email='*****@*****.**') clean_and_save(self.zone_sample) self.ns_sample = NameServer(name='some-ns-server.example.org', ttl=300)
class ModelForwardZoneTestCase(TestCase): """This class defines the test suite for the ForwardZone model.""" # TODO: test this for sub-zones (sub.example.org) def setUp(self): """Define the test client and other test variables.""" self.zone_sample = ForwardZone(name='example.org', primary_ns='ns.example.org', email='*****@*****.**', serialno=1234567890, refresh=400, retry=300, expire=800, ttl=300) def test_model_can_create_a_zone(self): """Test that the model is able to create a zone.""" old_count = ForwardZone.objects.count() clean_and_save(self.zone_sample) new_count = ForwardZone.objects.count() self.assertNotEqual(old_count, new_count) def test_model_can_change_a_zone(self): """Test that the model is able to change a zone.""" clean_and_save(self.zone_sample) old_name = self.zone_sample.name new_name = 'example.com' zone_sample_id = ForwardZone.objects.get(name=old_name).id self.zone_sample.name = new_name clean_and_save(self.zone_sample) updated_name = ForwardZone.objects.get(pk=zone_sample_id).name self.assertEqual(new_name, updated_name) def test_model_can_delete_a_zone(self): """Test that the model is able to delete a zone.""" clean_and_save(self.zone_sample) old_count = ForwardZone.objects.count() self.zone_sample.delete() new_count = ForwardZone.objects.count() self.assertNotEqual(old_count, new_count)
def forward_zone_by_hostname(request, *args, **kwargs): """ Get which zone would match a hostname. Note the hostname does not need to exist as a Host. """ hostname = kwargs['hostname'].lower() zone = ForwardZone.get_zone_by_hostname(hostname) if zone is None: raise Http404 if zone.name != hostname and zone.delegations.exists(): for delegation in zone.delegations.all(): if hostname == delegation.name or hostname.endswith( f".{delegation.name}"): serializer = ForwardZoneDelegationSerializer(delegation) ret = {"delegation": serializer.data} return Response(ret, status=status.HTTP_200_OK) serializer = ForwardZoneSerializer(zone) ret = {"zone": serializer.data} return Response(ret, status=status.HTTP_200_OK)
def validate(self, data): data = super().validate(data) if data.get('name'): data['zone'] = ForwardZone.get_zone_by_hostname(data['name']) return data
def test_update_serialno(self): # Force update by setting serialno_updated_at in the past zone = ForwardZone(name='example.org', primary_ns='ns.example.org', email='*****@*****.**') zone.save() zone.serialno_updated_at = timezone.now() - timedelta(minutes=10) old_serial = zone.serialno zone.save() zone.update_serialno() self.assertLess(old_serial, zone.serialno) # Will not update serialno just becase updated = True, requires a timedelta old_serial = zone.serialno self.updated = True zone.update_serialno() zone.save() zone.refresh_from_db() self.assertEqual(old_serial, zone.serialno) self.assertFalse(zone.updated) # Make sure the serialno does not wrap, but instead keeps stays the same zone.serialno += 98 self.assertEqual(zone.serialno % 100, 99) self.updated = True zone.serialno_updated_at = timezone.now() - timedelta(minutes=10) old_serial = zone.serialno zone.update_serialno() self.assertEqual(old_serial, zone.serialno)
class ModelForwardZoneTestCase(TestCase): """This class defines the test suite for the ForwardZone model.""" # TODO: test this for sub-zones (sub.example.org) def setUp(self): """Define the test client and other test variables.""" self.zone_sample = ForwardZone(name='example.org', primary_ns='ns.example.org', email='*****@*****.**', serialno=1234567890, refresh=400, retry=300, expire=800, ttl=300) def test_model_can_create_a_zone(self): """Test that the model is able to create a zone.""" old_count = ForwardZone.objects.count() clean_and_save(self.zone_sample) new_count = ForwardZone.objects.count() self.assertNotEqual(old_count, new_count) str(self.zone_sample) def test_model_can_change_a_zone(self): """Test that the model is able to change a zone.""" clean_and_save(self.zone_sample) old_name = self.zone_sample.name new_name = 'example.com' zone_sample_id = ForwardZone.objects.get(name=old_name).id self.zone_sample.name = new_name clean_and_save(self.zone_sample) updated_name = ForwardZone.objects.get(pk=zone_sample_id).name self.assertEqual(new_name, updated_name) def test_model_can_delete_a_zone(self): """Test that the model is able to delete a zone.""" clean_and_save(self.zone_sample) old_count = ForwardZone.objects.count() self.zone_sample.delete() new_count = ForwardZone.objects.count() self.assertNotEqual(old_count, new_count) def test_update_serialno(self): # Force update by setting serialno_updated_at in the past zone = ForwardZone(name='example.org', primary_ns='ns.example.org', email='*****@*****.**') zone.save() zone.serialno_updated_at = timezone.now() - timedelta(minutes=10) old_serial = zone.serialno zone.save() zone.update_serialno() self.assertLess(old_serial, zone.serialno) # Will not update serialno just becase updated = True, requires a timedelta old_serial = zone.serialno self.updated = True zone.update_serialno() zone.save() zone.refresh_from_db() self.assertEqual(old_serial, zone.serialno) self.assertFalse(zone.updated) # Make sure the serialno does not wrap, but instead keeps stays the same zone.serialno += 98 self.assertEqual(zone.serialno % 100, 99) self.updated = True zone.serialno_updated_at = timezone.now() - timedelta(minutes=10) old_serial = zone.serialno zone.update_serialno() self.assertEqual(old_serial, zone.serialno)