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_clean_sets_slug_to_slugified_name(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', } wlf = WhitelistForm(None, None, data) wlf.is_valid() self.assertEqual(wlf.cleaned_data['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)
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_is_valid_validates_formset(self): data = { 'name': 'x', 'current_ip': 'x', '_formset-TOTAL_FORMS': '0', '_formset-INITIAL_FORMS': '0', '_formset-MAX_FORMS': '0', } wlf = WhitelistForm(None, None, data) wlf.formset = type('X', (object,), {'is_valid':lambda *a: True, 'forms': []})() self.assertTrue(wlf.is_valid()) wlf = WhitelistForm(None, None, data) wlf.formset = type('X', (object,), {'is_valid':lambda *a: False, 'forms': []})() self.assertFalse(wlf.is_valid())