예제 #1
0
    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()
예제 #2
0
 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)
예제 #3
0
    def setUp(self):
        """Define the test client and other test variables."""
        # Needs sample host and sample network to test properly
        self.host = Host.objects.create(name='host.example.org')

        self.ipaddress_sample = Ipaddress(host=self.host,
                                          ipaddress='192.168.202.123',
                                          macaddress='a4:34:d9:0e:88:b9')

        self.ipv6address_sample = Ipaddress(host=self.host,
                                            ipaddress='2001:db8::beef',
                                            macaddress='a4:34:d9:0e:88:b9')
예제 #4
0
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)
예제 #5
0
 def test_model_updated_by_added_ipv6(self):
     """Test to check that an PtrOverride is added when two hosts share the same ipv6.
        Also makes sure that the PtrOverride points to the first host which held the ipv6."""
     initial_count = PtrOverride.objects.count()
     ipv6_one = Ipaddress(host=self.host_ipv6_one, ipaddress='2001:db8::4')
     clean_and_save(ipv6_one)
     one_count = PtrOverride.objects.count()
     ipv6_two = Ipaddress(host=self.host_ipv6_two, ipaddress='2001:db8::4')
     clean_and_save(ipv6_two)
     ptr =  PtrOverride.objects.first()
     self.assertEqual(ptr.host, self.host_ipv6_one)
     self.assertEqual(ptr.ipaddress, '2001:db8::4')
     self.assertEqual(initial_count, 0)
     self.assertEqual(initial_count, one_count)
     self.assertEqual(PtrOverride.objects.count(), 1)
예제 #6
0
파일: views.py 프로젝트: JarleB/mreg
    def post(self, request, *args, **kwargs):
        if "name" in request.data:
            if self.queryset.filter(name=request.data["name"]).exists():
                content = {'ERROR': 'name already in use'}
                return Response(content, status=status.HTTP_409_CONFLICT)

        hostdata = request.data.copy()

        if 'ipaddress' in hostdata:
            ipkey = hostdata['ipaddress']
            del hostdata['ipaddress']
            host = Host()
            hostserializer = HostSerializer(host, data=hostdata)

            if hostserializer.is_valid(raise_exception=True):
                with transaction.atomic():
                    hostserializer.save()
                    ipdata = {'host': host.pk, 'ipaddress': ipkey}
                    ip = Ipaddress()
                    ipserializer = IpaddressSerializer(ip, data=ipdata)
                    if ipserializer.is_valid(raise_exception=True):
                        self.perform_create(ipserializer)
                        location = request.path + host.name
                        return Response(status=status.HTTP_201_CREATED, headers={'Location': location})
        else:
            host = Host()
            hostserializer = HostSerializer(host, data=hostdata)
            if hostserializer.is_valid(raise_exception=True):
                self.perform_create(hostserializer)
                location = request.path + host.name
                return Response(status=status.HTTP_201_CREATED, headers={'Location': location})
예제 #7
0
class ModelIpaddressTestCase(TestCase):
    """This class defines the test suite for the Ipaddress model."""

    def setUp(self):
        """Define the test client and other test variables."""
        # Needs sample host and sample network to test properly
        self.host_one = Host(name='some-host.example.org',
                             contact='*****@*****.**',
                             ttl=300,
                             loc='23 58 23 N 10 43 50 E 80m',
                             comment='some comment')

        self.network_sample = Network(range='129.240.202.0/20',
                                    description='some description',
                                    vlan=123,
                                    dns_delegated=False)

        clean_and_save(self.host_one)
        # clean_and_save(self.network_sample) # Needed when network ForeignKey is implemented.

        self.ipaddress_sample = Ipaddress(host=Host.objects.get(name='some-host.example.org'),
                                          ipaddress='129.240.202.123',
                                          macaddress='a4:34:d9:0e:88:b9')

    def test_model_can_create_ipaddress(self):
        """Test that the model is able to create an IP Address."""
        old_count = Ipaddress.objects.count()
        clean_and_save(self.ipaddress_sample)
        new_count = Ipaddress.objects.count()
        self.assertNotEqual(old_count, new_count)

    def test_model_can_change_ipaddress(self):
        """Test that the model is able to change an IP Address."""
        clean_and_save(self.ipaddress_sample)
        new_ipaddress = '129.240.202.124'
        self.ipaddress_sample.ipaddress = new_ipaddress
        clean_and_save(self.ipaddress_sample)
        updated_ipaddress = Ipaddress.objects.filter(host__name='some-host.example.org')[0].ipaddress
        self.assertEqual(new_ipaddress, updated_ipaddress)

    def test_model_can_delete_ipaddress(self):
        """Test that the model is able to delete an IP Address."""
        clean_and_save(self.ipaddress_sample)
        old_count = Ipaddress.objects.count()
        self.ipaddress_sample.delete()
        new_count = Ipaddress.objects.count()
        self.assertNotEqual(old_count, new_count)
예제 #8
0
 def test_model_add_and_remove_ip(self):
     """Test to check that an PtrOverride is added when two hosts share the same ip.
        Also makes sure that the PtrOverride points to the first host which held the ip.
        Also makes sure that the PtrOverride is deleted when the host is deleted."""
     initial_count = PtrOverride.objects.count()
     ip_one = Ipaddress(host=self.host_one, ipaddress='10.0.0.1')
     clean_and_save(ip_one)
     one_count = PtrOverride.objects.count()
     ip_two = Ipaddress(host=self.host_two, ipaddress='10.0.0.1')
     clean_and_save(ip_two)
     two_count = PtrOverride.objects.count()
     ptr = PtrOverride.objects.first()
     self.assertEqual(ptr.host, self.host_one)
     self.assertEqual(ptr.ipaddress, '10.0.0.1')
     self.assertEqual(initial_count, 0)
     self.assertEqual(initial_count, one_count)
     self.assertEqual(two_count, 1)
     self.host_one.delete()
     self.assertEqual(PtrOverride.objects.count(), 0)
예제 #9
0
    def setUp(self):
        """Define the test client and other test variables."""
        # Needs sample host and sample network to test properly
        self.host_one = Host(name='some-host.example.org',
                             contact='*****@*****.**',
                             ttl=300,
                             loc='23 58 23 N 10 43 50 E 80m',
                             comment='some comment')

        self.network_sample = Network(range='129.240.202.0/20',
                                    description='some description',
                                    vlan=123,
                                    dns_delegated=False)

        clean_and_save(self.host_one)
        # clean_and_save(self.network_sample) # Needed when network ForeignKey is implemented.

        self.ipaddress_sample = Ipaddress(host=Host.objects.get(name='some-host.example.org'),
                                          ipaddress='129.240.202.123',
                                          macaddress='a4:34:d9:0e:88:b9')
예제 #10
0
    def post(self, request, *args, **kwargs):
        if "name" in request.data:
            if self.queryset.filter(name=request.data["name"]).exists():
                content = {'ERROR': 'name already in use'}
                return Response(content, status=status.HTTP_409_CONFLICT)

        if "ipaddress" in request.data and "network" in request.data:
            content = {
                'ERROR': '\'ipaddress\' and \'network\' is mutually exclusive'
            }
            return Response(content, status=status.HTTP_400_BAD_REQUEST)

        # request.data is immutable
        hostdata = request.data.copy()

        if 'network' in hostdata:
            try:
                ipaddress.ip_network(hostdata['network'])
            except ValueError as error:
                content = {'ERROR': str(error)}
                return Response(content, status=status.HTTP_400_BAD_REQUEST)

            network = Network.objects.filter(
                network=hostdata['network']).first()
            if not network:
                content = {'ERROR': 'no such network'}
                return Response(content, status=status.HTTP_404_NOT_FOUND)

            ip = network.get_random_unused()
            if not ip:
                content = {'ERROR': 'no available IP in network'}
                return Response(content, status=status.HTTP_404_NOT_FOUND)

            hostdata['ipaddress'] = ip
            del hostdata['network']

        if 'ipaddress' in hostdata:
            ipkey = hostdata['ipaddress']
            del hostdata['ipaddress']
            host = Host()
            hostserializer = HostSerializer(host, data=hostdata)

            if hostserializer.is_valid(raise_exception=True):
                with transaction.atomic():
                    # XXX: must fix. perform_creates failes as it has no ipaddress and the
                    # the permissions fails for most users. Maybe a nested serializer should fix it?
                    #self.perform_create(hostserializer)
                    hostserializer.save()
                    self.save_log_create(hostserializer)
                    ipdata = {'host': host.pk, 'ipaddress': ipkey}
                    ip = Ipaddress()
                    ipserializer = IpaddressSerializer(ip, data=ipdata)
                    if ipserializer.is_valid(raise_exception=True):
                        self.perform_create(ipserializer)
                        location = request.path + host.name
                        return Response(status=status.HTTP_201_CREATED,
                                        headers={'Location': location})
        else:
            host = Host()
            hostserializer = HostSerializer(host, data=hostdata)
            if hostserializer.is_valid(raise_exception=True):
                self.perform_create(hostserializer)
                location = request.path + host.name
                return Response(status=status.HTTP_201_CREATED,
                                headers={'Location': location})
예제 #11
0
 def _add_ip(host, ipaddress):
     ip = Ipaddress(host=host, ipaddress=ipaddress)
     clean_and_save(ip)
예제 #12
0
class ModelIpaddressTestCase(TestCase):
    """This class defines the test suite for the Ipaddress model."""

    def setUp(self):
        """Define the test client and other test variables."""
        # Needs sample host and sample network to test properly
        self.host = Host.objects.create(name='host.example.org')

        self.ipaddress_sample = Ipaddress(host=self.host,
                                          ipaddress='192.168.202.123',
                                          macaddress='a4:34:d9:0e:88:b9')

        self.ipv6address_sample = Ipaddress(host=self.host,
                                            ipaddress='2001:db8::beef',
                                            macaddress='a4:34:d9:0e:88:b9')


    def test_model_can_create_ipaddress(self):
        """Test that the model is able to create an IP Address."""
        old_count = Ipaddress.objects.count()
        clean_and_save(self.ipaddress_sample)
        new_count = Ipaddress.objects.count()
        self.assertLess(old_count, new_count)
        str(self.ipaddress_sample)

    def test_model_can_create_ipv6address(self):
        """Test that the model is able to create an IPv6 Address."""
        old_count = Ipaddress.objects.count()
        clean_and_save(self.ipv6address_sample)
        new_count = Ipaddress.objects.count()
        self.assertLess(old_count, new_count)

    def test_model_can_change_ipaddress(self):
        """Test that the model is able to change an IP Address."""
        clean_and_save(self.ipaddress_sample)
        new_ipaddress = '192.168.202.124'
        self.ipaddress_sample.ipaddress = new_ipaddress
        clean_and_save(self.ipaddress_sample)
        updated_ipaddress = Ipaddress.objects.get(host=self.host).ipaddress
        self.assertEqual(new_ipaddress, updated_ipaddress)

    def test_model_can_change_ipv6address(self):
        """Test that the model is able to change an IPv6 Address."""
        clean_and_save(self.ipv6address_sample)
        new_ipv6address = '2001:db8::feed'
        self.ipv6address_sample.ipaddress = new_ipv6address
        clean_and_save(self.ipv6address_sample)
        updated_ipv6address = Ipaddress.objects.get(host=self.host).ipaddress
        self.assertEqual(new_ipv6address, updated_ipv6address)

    def test_model_can_delete_ipaddress(self):
        """Test that the model is able to delete an IP Address."""
        clean_and_save(self.ipaddress_sample)
        old_count = Ipaddress.objects.count()
        self.ipaddress_sample.delete()
        new_count = Ipaddress.objects.count()
        self.assertGreater(old_count, new_count)

    def test_model_can_delete_ipv6address(self):
        """Test that the model is able to delete an IPv6 Address."""
        clean_and_save(self.ipv6address_sample)
        old_count = Ipaddress.objects.count()
        self.ipv6address_sample.delete()
        new_count = Ipaddress.objects.count()
        self.assertGreater(old_count, new_count)