def test_bad_nameserver_soa_state_case_3_2(self):
        # This is Case 3 ... with ptrs
        root_domain = create_fake_zone("32.in-addr.arpa", suffix="")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        soa = ns.domain.soa
        ns.domain.soa = None
        root_domain.soa = None  # Shit's getting cached
        ns.domain.save()
        soa.delete()

        # At his point we should have a domain pointed at no SOA record with no
        # records attached to it. It also has no child domains.

        # Add a record to the domain.

        ptr = PTR(name="asdf", ip_str="32.1.1.1", ip_type="4")
        ptr.save()

        s = SOA(primary="asdf.asdf", contact="asdf.asdf", description="asdf")
        s.save()
        root_domain.soa = s

        self.assertRaises(ValidationError, root_domain.save)
Example #2
0
 def test_chain_soa_domain_add(self):
     data = {'primary': "ns1.foo.com", 'contact': "email.foo.com"}
     soa = SOA(**data)
     soa.save()
     d0 = Domain(name='com')
     d0.save()
     d1 = Domain(name='foo.com', soa=soa)
     d1.save()
     self.assertTrue(soa == d1.soa)
     d2 = Domain(name='bar.foo.com', soa=soa)
     d2.save()
     self.assertTrue(soa == d2.soa)
     d3 = Domain(name='new.foo.com', soa=soa)
     d3.save()
     self.assertTrue(soa == d3.soa)
     d4 = Domain(name='far.bar.foo.com', soa=soa)
     d4.save()
     self.assertTrue(soa == d4.soa)
     d5 = Domain(name='tee.new.foo.com', soa=soa)
     d5.save()
     self.assertTrue(soa == d5.soa)
     d5.delete()
     d4.delete()
     self.assertTrue(soa == d1.soa)
     self.assertTrue(soa == d2.soa)
     self.assertTrue(soa == d3.soa)
 def test_chain_soa_domain_add(self):
     data = {'primary': "ns1.foo.com", 'contact': "email.foo.com"}
     soa = SOA(**data)
     soa.save()
     d0 = Domain(name='com')
     d0.save()
     d1 = Domain(name='foo.com', soa=soa)
     d1.save()
     self.assertTrue(soa == d1.soa)
     d2 = Domain(name='bar.foo.com', soa=soa)
     d2.save()
     self.assertTrue(soa == d2.soa)
     d3 = Domain(name='new.foo.com', soa=soa)
     d3.save()
     self.assertTrue(soa == d3.soa)
     d4 = Domain(name='far.bar.foo.com', soa=soa)
     d4.save()
     self.assertTrue(soa == d4.soa)
     d5 = Domain(name='tee.new.foo.com', soa=soa)
     d5.save()
     self.assertTrue(soa == d5.soa)
     d5.delete()
     d4.delete()
     self.assertTrue(soa == d1.soa)
     self.assertTrue(soa == d2.soa)
     self.assertTrue(soa == d3.soa)
    def test_bad_nameserver_soa_state_case_3_0(self):
        # This is Case 3
        root_domain = create_fake_zone("asdf30")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        soa = ns.domain.soa
        ns.domain.soa = None
        root_domain.soa = None  # Shit's getting cached
        ns.domain.save()
        soa.delete()

        # At his point we should have a domain pointed at no SOA record with no
        # records attached to it. It also has no child domains.

        # Add a record to the domain.
        a = AddressRecord(
            label='', domain=root_domain, ip_type="6", ip_str="1::")
        a.save()

        s = SOA(primary="asdf.asdf", contact="asdf.asdf", description="asdf")
        s.save()
        root_domain.soa = s

        self.assertRaises(ValidationError, root_domain.save)
Example #5
0
 def do_generic_add(self, primary, contact, retry, refresh, description):
     soa = SOA(primary=primary, contact=contact,
               retry=retry, refresh=refresh, description=description)
     soa.save()
     soa.save()
     rsoa = SOA.objects.filter(primary=primary, contact=contact,
                               retry=retry, refresh=refresh)
     self.assertTrue(len(rsoa) == 1)
     return soa
 def test_delete_with_domains(self):
     data = {'primary': "ns1asfdadsf.foo.com", 'contact': "email.foo.com"}
     soa = SOA(**data)
     soa.save()
     d0 = Domain(name='com')
     d0.save()
     d1 = Domain(name='foo.com', soa=soa)
     d1.soa = soa
     d1.save()
     self.assertRaises(ValidationError, soa.delete)
Example #7
0
 def test_delete_with_domains(self):
     data = {'primary': "ns1asfdadsf.foo.com", 'contact': "email.foo.com"}
     soa = SOA(**data)
     soa.save()
     d0 = Domain(name='com')
     d0.save()
     d1 = Domain(name='foo.com', soa=soa)
     d1.soa = soa
     d1.save()
     self.assertRaises(ValidationError, soa.delete)
 def test_add_invalid(self):
     data = {'primary': "daf..fff", 'contact': "foo.com"}
     soa = SOA(**data)
     self.assertRaises(ValidationError, soa.save)
     data = {'primary': 'foo.com', 'contact': 'dkfa..'}
     soa = SOA(**data)
     self.assertRaises(ValidationError, soa.save)
     data = {'primary': 'adf', 'contact': '*@#$;'}
     soa = SOA(**data)
     self.assertRaises(ValidationError, soa.save)
Example #9
0
def build_sample_domain():
    domain_name = ''
    for i in range(2):
        domain_name = random_label()
        domain = Domain(name=domain_name)
    soa = SOA(
        primary=random_label(), contact="asf", description=random_label())
    soa.save()
    domain.soa = soa
    domain.save()
    return domain
Example #10
0
class SOAViewTests(TestCase):
    def setUp(self):
        self.url_slug = 'soa'
        self.test_obj = SOA(primary=random_label(
        ), contact=random_label(), description=random_label())
        self.test_obj.save()

    def test_base_mozdns_app_soa(self):
        resp = self.client.get(MOZDNS_BASE_URL + "/%s/" % (self.url_slug),
                               follow=True)
        self.assertEqual(resp.status_code, 200)

    # url(r'^mozdns/nameserver/create$', NSCreateView.as_view()),
    def test_get_create_soa(self):
        resp = self.client.get(
            MOZDNS_BASE_URL + "/%s/create/" % (self.url_slug),
            follow=True)
        self.assertEqual(resp.status_code, 200)

    def test_post_create_soa(self):
        resp = self.client.post(MOZDNS_BASE_URL + "/%s/create/" %
                               (self.url_slug), self.post_data(), follow=True)
        self.assertTrue(resp.status_code in (302, 200))

        # url(r'^mozdns/nameserver/(?P<pk>[\w-]+)/update$',
        # NSUpdateView.as_view() ),
    def test_get_object_update_soa(self):
        resp = self.client.get(MOZDNS_BASE_URL + "/%s/%s/update/" %
                               (self.url_slug, self.test_obj.pk), follow=True)
        self.assertEqual(resp.status_code, 200)

    def test_post_object_update_soa(self):
        resp = self.client.post(MOZDNS_BASE_URL + "/%s/%s/update/" %
                                (self.url_slug, self.test_obj.pk),
                                self.post_data(), follow=True)
        self.assertTrue(resp.status_code in (302, 200))

    def test_get_object_details_soa(self):
        resp = self.client.get(MOZDNS_BASE_URL + "/%s/%s/" %
                               (self.url_slug, self.test_obj.pk), follow=True)
        self.assertEqual(resp.status_code, 200)

    def test_get_object_delete_soa(self):
        resp = self.client.get(MOZDNS_BASE_URL + "/%s/%s/delete/" %
                              (self.url_slug, self.test_obj.pk), follow=True)
        self.assertEqual(resp.status_code, 200)

    def post_data(self):
        return {'primary': random_label(), 'contact': random_label(), 'retry':
                '123', 'refresh': '123', 'description': random_label()}
 def do_generic_add(self, primary, contact, retry, refresh, description):
     soa = SOA(primary=primary,
               contact=contact,
               retry=retry,
               refresh=refresh,
               description=description)
     soa.save()
     soa.save()
     rsoa = SOA.objects.filter(primary=primary,
                               contact=contact,
                               retry=retry,
                               refresh=refresh)
     self.assertTrue(len(rsoa) == 1)
     return soa
Example #12
0
 def test_serial_less_than_date(self):
     # Case serial < date
     serial = '2111101111'
     date_str = '21111111'
     date = self.to_date(date_str)
     new_serial = SOA.calc_serial(serial, date)
     self.assertEqual(int(date_str + '00'), new_serial)
def migrate_soa(zone, root_domain_name):
    for (name, ttl, rdata) in zone.iterate_rdatas('SOA'):
        print str(name) + " SOA " + str(rdata)
        exists = SOA.objects.filter(minimum=rdata.minimum,
                contact=rdata.rname.to_text().strip('.'),
                primary=rdata.mname.to_text().strip('.'), description="SOA for"
                " {0}".format(root_domain_name))
        if exists:
            soa = exists[0]
        else:
            soa = SOA(serial=rdata.serial, minimum=rdata.minimum,
                    contact=rdata.rname.to_text().strip('.'),
                    primary=rdata.mname.to_text().strip('.'), description="SOA for"
                    " {0}".format(root_domain_name))
            soa.clean()
            soa.save()
    return soa
    def test_update_serial_no_dirty(self):
        # If we update the serial, the dirty bit shouldn't change.
        data = {
            'primary': "fakey.ns1.asdffoo.com",
            'contact': "adsffoopy.email.foo.com"
        }
        soa = SOA(**data)
        soa.save()  # new soa's are always dirty

        soa.dirty = False
        soa.save()

        soa.serial = soa.serial + 9
        soa.save()

        same_soa = SOA.objects.get(pk=soa.pk)
        self.assertFalse(same_soa.dirty)
    def test_add_soa(self):
        primary = "ns1.oregonstate.edu"
        contact = "admin.oregonstate.edu"
        retry = 1234
        refresh = 1234123
        description = "1"
        self.do_generic_add(primary,
                            contact,
                            retry,
                            refresh,
                            description=description)
        soa = SOA.objects.filter(primary=primary,
                                 contact=contact,
                                 retry=retry,
                                 refresh=refresh)
        soa[0].save()
        self.assertTrue(soa)
        soa[0].__repr__()
        soa = soa[0]
        self.assertTrue(soa.details())
        self.assertTrue(soa.get_absolute_url())
        self.assertTrue(soa.get_edit_url())
        self.assertTrue(soa.get_delete_url())

        primary = "do.com"
        contact = "admf.asdf"
        retry = 432152
        refresh = 1235146134
        description = "2"
        self.do_generic_add(primary,
                            contact,
                            retry,
                            refresh,
                            description=description)
        soa = SOA.objects.filter(primary=primary,
                                 contact=contact,
                                 retry=retry,
                                 refresh=refresh)
        self.assertTrue(soa)
        soa = soa[0]
        self.assertTrue(soa.details())
        self.assertTrue(soa.get_absolute_url())
        self.assertTrue(soa.get_edit_url())
        self.assertTrue(soa.get_delete_url())

        primary = "ns1.derp.com"
        contact = "admf.asdf"
        soa = SOA(primary=primary, contact=contact)
        soa.save()
        self.assertTrue(soa.serial and soa.expire and soa.retry
                        and soa.refresh)
        self.assertTrue(soa.details())
        self.assertTrue(soa.get_absolute_url())
        self.assertTrue(soa.get_edit_url())
        self.assertTrue(soa.get_delete_url())
Example #16
0
 def setUp(self):
     self.url_slug = 'domain'
     soa = SOA(primary=random_label(),
               contact=random_label(),
               description=random_label())
     self.test_obj = Domain(name=random_label())
     self.test_obj.save()
     self.test_obj.soa = soa
     self.test_obj.save()
Example #17
0
    def test_update_serial_no_dirty(self):
        # If we update the serial, the dirty bit shouldn't change.
        data = {'primary': "fakey.ns1.asdffoo.com", 'contact':
                "adsffoopy.email.foo.com"}
        soa = SOA(**data)
        soa.save()  # new soa's are always dirty

        soa.dirty = False
        soa.save()

        soa.serial = soa.serial + 9
        soa.save()

        same_soa = SOA.objects.get(pk=soa.pk)
        self.assertFalse(same_soa.dirty)
Example #18
0
    def test_soa_validators(self):
        m = self.create_domain(name='8')
        m.save()

        f_m = self.create_domain(name='8.2')
        f_m.save()

        n_f_m = self.create_domain(name='8.2.3')
        n_f_m.save()

        b_m = self.create_domain(name='8.3')
        b_m.save()

        s = SOA(primary="ns1.foo.com", contact="asdf", description="test")
        s.save()

        f_m.soa = s
        f_m.save()

        b_m.soa = s
        self.assertRaises(ValidationError, b_m.save)

        n_f_m = Domain.objects.get(pk=n_f_m.pk)  # Refresh object
        n_f_m.soa = s
        n_f_m.save()

        m.soa = s
        m.save()

        b_m = Domain.objects.get(pk=b_m.pk)  # Refresh object
        b_m.soa = s
        b_m.save()

        m.soa = None
        self.assertRaises(ValidationError, m.save)

        s2 = SOA(primary="ns1.foo.com", contact="asdf", description="test2")
        s2.save()

        m.soa = s2
        self.assertRaises(ValidationError, m.save)
Example #19
0
    def test_add_soa(self):
        primary = "ns1.oregonstate.edu"
        contact = "admin.oregonstate.edu"
        retry = 1234
        refresh = 1234123
        description = "1"
        self.do_generic_add(
            primary, contact, retry, refresh, description=description)
        soa = SOA.objects.filter(primary=primary, contact=contact,
                                 retry=retry, refresh=refresh)
        soa[0].save()
        self.assertTrue(soa)
        soa[0].__repr__()
        soa = soa[0]
        self.assertTrue(soa.details())
        self.assertTrue(soa.get_absolute_url())
        self.assertTrue(soa.get_edit_url())
        self.assertTrue(soa.get_delete_url())

        primary = "do.com"
        contact = "admf.asdf"
        retry = 432152
        refresh = 1235146134
        description = "2"
        self.do_generic_add(
            primary, contact, retry, refresh, description=description)
        soa = SOA.objects.filter(primary=primary, contact=contact,
                                 retry=retry, refresh=refresh)
        self.assertTrue(soa)
        soa = soa[0]
        self.assertTrue(soa.details())
        self.assertTrue(soa.get_absolute_url())
        self.assertTrue(soa.get_edit_url())
        self.assertTrue(soa.get_delete_url())

        primary = "ns1.derp.com"
        contact = "admf.asdf"
        soa = SOA(primary=primary, contact=contact)
        soa.save()
        self.assertTrue(
            soa.serial and soa.expire and soa.retry and soa.refresh)
        self.assertTrue(soa.details())
        self.assertTrue(soa.get_absolute_url())
        self.assertTrue(soa.get_edit_url())
        self.assertTrue(soa.get_delete_url())
    def test_soa_validators(self):
        m = self.create_domain(name="8")
        m.save()

        f_m = self.create_domain(name="8.2")
        f_m.save()

        n_f_m = self.create_domain(name="8.2.3")
        n_f_m.save()

        b_m = self.create_domain(name="8.3")
        b_m.save()

        s = SOA(primary="ns1.foo.com", contact="asdf", description="test")
        s.save()

        f_m.soa = s
        f_m.save()

        b_m.soa = s
        self.assertRaises(ValidationError, b_m.save)

        n_f_m = Domain.objects.get(pk=n_f_m.pk)  # Refresh object
        n_f_m.soa = s
        n_f_m.save()

        m.soa = s
        m.save()

        b_m = Domain.objects.get(pk=b_m.pk)  # Refresh object
        b_m.soa = s
        b_m.save()

        m.soa = None
        self.assertRaises(ValidationError, m.save)

        s2 = SOA(primary="ns1.foo.com", contact="asdf", description="test2")
        s2.save()

        m.soa = s2
        self.assertRaises(ValidationError, m.save)
Example #21
0
def migrate_soa(zone, root_domain_name):
    for (name, ttl, rdata) in zone.iterate_rdatas('SOA'):
        print str(name) + " SOA " + str(rdata)
        exists = SOA.objects.filter(minimum=rdata.minimum,
                                    contact=rdata.rname.to_text().strip('.'),
                                    primary=rdata.mname.to_text().strip('.'),
                                    description="SOA for"
                                    " {0}".format(root_domain_name))
        if exists:
            soa = exists[0]
        else:
            new_serial = int(datetime.datetime.now().strftime("%Y%m%d01"))
            soa = SOA(serial=new_serial,
                      minimum=rdata.minimum,
                      contact=rdata.rname.to_text().strip('.'),
                      primary=rdata.mname.to_text().strip('.'),
                      description="SOA for"
                      " {0}".format(root_domain_name))
            soa.clean()
            soa.save()
    return soa
Example #22
0
    def test_soa_validators(self):
        m = Domain(name='moo')
        m.save()

        f_m = Domain(name='foo.moo')
        f_m.save()

        n_f_m = Domain(name='noo.foo.moo')
        n_f_m.save()

        b_m = Domain(name='baz.moo')
        b_m.save()

        s = SOA(primary="ns1.foo.com", contact="asdf", description="test")
        s.save()

        f_m.soa = s
        f_m.save()

        b_m.soa = s
        self.assertRaises(ValidationError, b_m.save)

        n_f_m = Domain.objects.get(pk=n_f_m.pk)  # Refresh object
        n_f_m.soa = s
        n_f_m.save()

        m.soa = s
        m.save()

        b_m.soa = s
        b_m.save()

        m.soa = None
        self.assertRaises(ValidationError, m.save)

        s2 = SOA(primary="ns1.foo.com", contact="asdf", description="test2")
        s2.save()

        m.soa = s2
        self.assertRaises(ValidationError, m.save)
Example #23
0
def create_zone_ajax(request):
    """This view tries to create a new zone and returns an JSON with either
    'success' = True or 'success' = False and some errors.

    Throughout this function note that objects that are created are recorded,
    and if an error is caught, the previously created objects are deleted. This
    backing-out *may* be better handling by a transaction. Django has this sort
    of thing (middleware and decorators), but I'm in a time crunch so this
    manual deletetion will have to do.
    """

    qd = request.POST.copy()
    # See if the domain exists.
    # Fail if it already exists or if it's under a delegated domain.
    root_domain = qd.get('root_domain', None)

    if not root_domain:
        error = "Please specify a root_domain"
        return HttpResponse(json.dumps({'success': False, 'error': error}))

    if Domain.objects.filter(name=root_domain).exists():
        error = gt("<b>{0}</b> is already a domain. To make it a new zone, "
                   "assign it a newly created SOA.".format(root_domain))
        return HttpResponse(json.dumps({'success': False, 'error': error}))

    primary = qd.get('soa_primary', None)
    if not primary:
        error = "Please specify a primary nameserver for the SOA record."
        return HttpResponse(json.dumps({'success': False, 'error': error}))
    contact = qd.get('soa_contact', None)
    if not contact:
        error = "Please specify a contact address for the SOA record."
        return HttpResponse(json.dumps({'success': False, 'error': error}))
    contact.replace('@', '.')

    # Find all the NS entries
    nss = []
    number_re = re.compile('nameserver_(\d+)')
    private_view, _ = View.objects.get_or_create(name='private')
    public_view, _ = View.objects.get_or_create(name='public')

    for k, server in request.POST.iteritems():
        if k.startswith('nameserver_'):
            n = number_re.search(k)
            if not n:
                continue
            ns_number = n.groups()[0]
            views = []
            if qd.get('private_view_{0}'.format(ns_number), 'off') == 'on':
                views.append(private_view)
            if qd.get('public_view_{0}'.format(ns_number), 'off') == 'on':
                views.append(public_view)
            ttl = qd.get('ttl_{0}'.format(ns_number))
            if ttl and ttl.isdigit():
                ttl = int(ttl)
            else:
                ttl = None
            nss.append(
                (Nameserver(server=server, ttl=ttl), views)
            )

    if not nss:
        # They must create at least one nameserver
        error = gt("You must choose an authoritative nameserver to serve this "
                   "zone")
        return HttpResponse(json.dumps({'success': False, 'error': error}))

    # We want all domains created up to this point to inherit their
    # master_domain's soa. We will override the return domain's SOA.
    # Everything under this domain can be purgeable becase we will set this
    # domain to non-purgeable. This will also allow us to call prune tree.
    domain = ensure_domain(
        root_domain, purgeable=True, inherit_soa=False, force=True)

    soa = SOA(
        primary=primary, contact=contact,
        description="SOA for {0}".format(root_domain)
    )
    try:
        soa.save()
    except ValidationError, e:
        _clean_domain_tree(domain)
        return HttpResponse(json.dumps({'success': False,
                                        'error': e.messages[0]}))
Example #24
0
 def test_serial_greater_than_date(self):
     # Case serial > date
     serial = '2111111111'
     date = self.to_date('21111011')
     new_serial = SOA.calc_serial(serial, date)
     self.assertEqual(int(serial) + 1, new_serial)
Example #25
0
 def setUp(self):
     self.url_slug = 'soa'
     self.test_obj = SOA(primary=random_label(
     ), contact=random_label(), description=random_label())
     self.test_obj.save()
Example #26
0
 def test_serial_equal_date(self):
     # Case ==
     serial = '2111111111'
     date = self.to_date('21111111')
     new_serial = SOA.calc_serial(serial, date)
     self.assertEqual(int(serial) + 1, new_serial)
class SOAViewTests(TestCase):
    def setUp(self):
        self.url_slug = 'soa'
        self.test_obj = SOA(primary=random_label(),
                            contact=random_label(),
                            description=random_label())
        self.test_obj.save()

    def test_base_mozdns_app_soa(self):
        resp = self.client.get(MOZDNS_BASE_URL + "/%s/" % (self.url_slug),
                               follow=True)
        self.assertEqual(resp.status_code, 200)

    # url(r'^mozdns/nameserver/create$', NSCreateView.as_view()),
    def test_get_create_soa(self):
        resp = self.client.get(MOZDNS_BASE_URL + "/%s/create/" %
                               (self.url_slug),
                               follow=True)
        self.assertEqual(resp.status_code, 200)

    def test_post_create_soa(self):
        resp = self.client.post(MOZDNS_BASE_URL + "/%s/create/" %
                                (self.url_slug),
                                self.post_data(),
                                follow=True)
        self.assertTrue(resp.status_code in (302, 200))

        # url(r'^mozdns/nameserver/(?P<pk>[\w-]+)/update$',
        # NSUpdateView.as_view() ),
    def test_get_object_update_soa(self):
        resp = self.client.get(MOZDNS_BASE_URL + "/%s/%s/update/" %
                               (self.url_slug, self.test_obj.pk),
                               follow=True)
        self.assertEqual(resp.status_code, 200)

    def test_post_object_update_soa(self):
        resp = self.client.post(MOZDNS_BASE_URL + "/%s/%s/update/" %
                                (self.url_slug, self.test_obj.pk),
                                self.post_data(),
                                follow=True)
        self.assertTrue(resp.status_code in (302, 200))

    def test_get_object_details_soa(self):
        resp = self.client.get(MOZDNS_BASE_URL + "/%s/%s/" %
                               (self.url_slug, self.test_obj.pk),
                               follow=True)
        self.assertEqual(resp.status_code, 200)

    def test_get_object_delete_soa(self):
        resp = self.client.get(MOZDNS_BASE_URL + "/%s/%s/delete/" %
                               (self.url_slug, self.test_obj.pk),
                               follow=True)
        self.assertEqual(resp.status_code, 200)

    def post_data(self):
        return {
            'primary': random_label(),
            'contact': random_label(),
            'retry': '123',
            'refresh': '123',
            'description': random_label()
        }
 def setUp(self):
     self.url_slug = 'soa'
     self.test_obj = SOA(primary=random_label(),
                         contact=random_label(),
                         description=random_label())
     self.test_obj.save()