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 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)