def test_create_invalid(self, form_data): error_desc = form_data.pop('error', None) form = UserASForm(user=get_testuser(), data=form_data) self.assertFalse(form.is_valid()) if error_desc: error_descs = _get_forms_error_descs(form) self.assertIn(error_desc, error_descs)
def test_create_check(self, form_data): # Create a UserAS with the given data create_form = UserASForm(user=get_testuser(), data=form_data) self.assertIsNotNone(create_form.as_table()) self.assertTrue(create_form.is_valid(), create_form.errors) user_as = create_form.save() self.assertIsNotNone(user_as.pk) links = [iface.link().pk for iface in user_as.interfaces.all()] self.assertEqual(len(links), form_data['form-TOTAL_FORMS']) # Submittng the ~same data on the previously created instance should have no changes form_data_2 = _get_form_fields_for_edit(form_data, user_as) edit_form = UserASForm(user=get_testuser(), instance=user_as, data=form_data_2) self.assertFalse(edit_form.has_changed(), _get_forms_change_data(edit_form)) self.assertTrue(edit_form.is_valid(), edit_form.errors) # Also, if we save the updated form, nothing should change user_as_edited = edit_form.save() self.assertEqual(user_as.pk, user_as_edited.pk) links_edited = [iface.link().pk for iface in user_as.interfaces.all()] self.assertEqual(links, links_edited)
def test_create_valid(self, form_data): """ Check that the form is valid with the given values as user input, and saving the form results in a new UserAS object. """ form = UserASForm(user=get_testuser(), data=form_data) error_descs = _get_forms_error_descs(form) self.assertTrue(form.is_valid(), error_descs) user_as = form.save() self.assertIsNotNone(user_as)
def test_create_valid(self, form_data): """ Check that the form is valid with the given values as user input, and saving the form results in a new UserAS object. """ form = UserASForm(user=get_testuser(), data=form_data) error_descs = _get_forms_error_descs(form) self.assertTrue(form.is_valid(), error_descs) user_as = form.save() self.assertIsNotNone(user_as) if form_data.get("user-as-become_user_ap", "") == "on": self.assertTrue( AttachmentPoint.objects.filter(AS=user_as).count() > 0)
def test_create_valid(self, **kwargs): """ Check that the form is valid with the given values as user input, and saving the form results in a new UserAS object. """ form_data = self._get_initial_dict() form_data.update(**kwargs) form = UserASForm(user=get_testuser(), data=form_data) self.assertTrue(form.is_valid(), form.errors) user_as = form.save() self.assertIsNotNone(user_as)
def _get_initial_dict(self): """ Return a dict containing the initial value for each UserAS form field """ # Create a form instance, only to extract the initial values form = UserASForm(user=get_testuser()) return {key: field.initial for (key, field) in form.fields.items()}
def _get_forms_error_descs(form: UserASForm) -> str: """ Return all the fields and non fields errors in the `UserASForm`, as well as all the fields, non fields, and non form errors of the `AttachmentConfForm` """ formset = form.attachment_conf_form_set return '\n'.join( map(str, [ form.errors, form.non_field_errors(), formset.errors, formset.non_form_errors() ]))
def test_create_too_many(self): """ Submitting the creation form with quota exceeded leads to a form-error. """ form_data = self._get_initial_dict() form_data.update(**self.valid_form_params[0].kwargs) for i in range(get_testuser().max_num_ases()): form = UserASForm(user=get_testuser(), data=form_data) # Check that form is valid, otherwise this test will not make sense self.assertTrue(form.is_valid(), form.errors) form.save() form = UserASForm(user=get_testuser(), data=form_data) self.assertFalse(form.is_valid()) self.assertTrue(any(e.find("quota exceeded") >= 0 for e in form.non_field_errors()), form.errors)
def test_edit(self, initial_data, new_data): # Create a UserAS with the given data create_form = UserASForm(user=get_testuser(), data=initial_data) self.assertIsNotNone(create_form.as_table()) self.assertTrue(create_form.is_valid(), create_form.errors) user_as = create_form.save() self.assertIsNotNone(user_as) # Submittng the new data on the previously created instance should change it edit_form_1_data = _get_form_fields_for_edit(new_data, user_as) edit_form_1 = UserASForm(user=get_testuser(), instance=user_as, data=edit_form_1_data) rendered_1 = edit_form_1.as_table() self.assertIsNotNone(rendered_1) rendered_formset_1 = edit_form_1.attachment_conf_form_set.as_table() self.assertIsNotNone(rendered_formset_1) self.assertTrue(edit_form_1.is_valid(), edit_form_1.errors) if initial_data == new_data: self.assertFalse(edit_form_1.has_changed(), _get_forms_change_data(edit_form_1)) else: self.assertTrue(edit_form_1.has_changed()) user_as_edited_1 = edit_form_1.save() self.assertEqual(user_as.pk, user_as_edited_1.pk) links_edited_1 = [ iface.link().pk for iface in user_as.interfaces.all() ] self.assertEqual(len(links_edited_1), new_data['form-TOTAL_FORMS']) if initial_data == new_data: return # no need to do this _again_ # Submittng the ~same data on the previously edited instance should have no further changes edit_form_2_data = _get_form_fields_for_edit(new_data, user_as) edit_form_2 = UserASForm(user=get_testuser(), instance=user_as, data=edit_form_2_data) self.assertFalse(edit_form_2.has_changed(), _get_forms_change_data(edit_form_2)) self.assertTrue(edit_form_2.is_valid(), edit_form_2.errors) links_edited_2 = [ iface.link().pk for iface in user_as.interfaces.all() ] self.assertEqual(links_edited_1, links_edited_2)
def test_render_create(self): form = UserASForm(user=get_testuser()) self.assertIsNotNone(form.as_table())
def test_edit_render(self, **kwargs): """ The form can be instantiated with a (freshly created) UserAS. The instantiated form should not show any changes if the same data is submitted. """ form_data = self._get_initial_dict() form_data.update(**kwargs) # Create a UserAS with the given data create_form = UserASForm(user=get_testuser(), data=form_data) self.assertIsNotNone(create_form.as_table()) self.assertTrue(create_form.is_valid(), create_form.errors) user_as = create_form.save() self.assertIsNotNone(user_as) # Check that the form can be instantiated for the object just created # and check that the forms initial values are the same as the # previously submitted values. edit_form_1 = UserASForm(user=get_testuser(), instance=user_as, data=form_data) self.assertIsNotNone(edit_form_1.as_table()) self.assertTrue(edit_form_1.is_valid(), edit_form_1.errors) self.assertFalse(edit_form_1.has_changed(), edit_form_1.changed_data) user_as_edited_1 = edit_form_1.save() self.assertEqual(user_as.pk, user_as_edited_1.pk) # Do it again! edit_form_2 = UserASForm(user=get_testuser(), instance=user_as, data=form_data) self.assertTrue(edit_form_2.is_valid(), edit_form_2.errors) self.assertFalse(edit_form_2.has_changed(), edit_form_2.changed_data) user_as_edited_2 = edit_form_2.save() self.assertEqual(user_as.pk, user_as_edited_2.pk)
def test_create_invalid(self, **kwargs): form_data = self._get_initial_dict() form_data.update(**kwargs) form = UserASForm(user=get_testuser(), data=form_data) self.assertFalse(form.is_valid())