def test_view_updates_existing_whitelist_on_valid_form(self): wl = Whitelist.objects.create(name='anything', slug='anything') ViewDispatch.view = WhitelistEditor(lambda *a: wl) formset = WhitelistEditor.form_class(None, None).formset formset_data = formset.management_form.initial data = dict([ ('%s-%s' % (formset.prefix, key), value if value is not None else '') for key, value in formset_data.iteritems() ]) data['name'] = 'random' data['current_ip'] = '0.0.0.0' num_whitelists = len(Whitelist.objects.all()) with self.settings(**self.test_settings): resp = self.client.post('/', data) self.assertEqual(resp.status_code, 302) self.assertEqual(resp['Location'], 'http://testserver/') self.assertEqual(len(Whitelist.objects.all()), num_whitelists) new_wl = Whitelist.objects.get(pk=wl.pk) self.assertEqual(new_wl.name, 'random') self.assertEqual(new_wl.slug, 'random')
def test_view_returns_form_with_errors_on_bad_post(self): wl = Whitelist.objects.create(name='anything', slug='anything') ViewDispatch.view = WhitelistEditor(lambda *a: wl) formset = WhitelistEditor.form_class(None, None).formset formset_data = formset.management_form.initial data = dict([ ('%s-%s' % (formset.prefix, key), value if value is not None else '') for key, value in formset_data.iteritems() ]) with self.settings(**self.test_settings): resp = self.client.post('/', data) self.assertEqual(resp.status_code, 200) self.assertTrue(len(resp.context['form'].errors))