def test_save_sets_appropriate_ranges(self):
        num_forms = random.randint(1, 10)
        name = ' '.join([str(random.randint(0, 10)) for i in range(10)])
        data = {
            'name': name,
            'current_ip': 'x',
            '_formset-TOTAL_FORMS': '%d' % num_forms,
            '_formset-INITIAL_FORMS': '0',
            '_formset-MAX_FORMS': '',
        }

        for i in range(num_forms):
            data['_formset-%d-ip' % i] = fmt_ip(random.randint(0, 0xFFFFFFFF))
            data['_formset-%d-cidr' % i] = random.randint(0, 32)

        wlf = WhitelistForm(None, None, data)

        wlf.is_valid()

        self.assertTrue(wlf.is_valid())

        wl = wlf.save()

        for i in range(num_forms):
            self.assertTrue(wl.range_set.filter(
                ip=data['_formset-%d-ip' % i],
                cidr=data['_formset-%d-cidr' % i]
            ).exists())
    def test_save_sets_appropriate_field_values(self):
        name = ' '.join([str(random.randint(0, 10)) for i in range(10)])
        data = {
            'name': name,
            'current_ip': 'x',
            '_formset-TOTAL_FORMS': '0',
            '_formset-INITIAL_FORMS': '0',
            '_formset-MAX_FORMS': '0',
        }
        wl = Whitelist.objects.create(
            name='asdf',
            slug='asdf',
        )
        wlf = WhitelistForm(None, wl, data)
        wlf.is_valid()
        wlf.save()

        wl = Whitelist.objects.get(pk=wl.pk)

        self.assertEqual(wl.name, name)
        self.assertEqual(wl.slug, slugify(name))
 def test_save_returns_created_whitelist(self):
     name = ' '.join([str(random.randint(0, 10)) for i in range(10)])
     data = {
         'name': name,
         'current_ip': 'x',
         '_formset-TOTAL_FORMS': '0',
         '_formset-INITIAL_FORMS': '0',
         '_formset-MAX_FORMS': '0',
     }
     wl = Whitelist.objects.create(
         name='asdf',
         slug='asdf',
     )
     wlf = WhitelistForm(None, wl, data)
     wlf.is_valid()
     self.assertEqual(wlf.save().pk, wl.pk)