From 859cd7c6b43bf70e2852eda10f635c70feeb397f Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Thu, 5 Nov 2020 01:40:41 -0800 Subject: Fixed #22276 -- Fixed crash when formset management form is invalid. Co-authored-by: Patryk Zawadzki --- tests/forms_tests/tests/test_formsets.py | 68 +++++++++++++++++++++++++++++--- tests/model_formsets/tests.py | 9 ++--- 2 files changed, 66 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/forms_tests/tests/test_formsets.py b/tests/forms_tests/tests/test_formsets.py index a11c183f86..889560aa74 100644 --- a/tests/forms_tests/tests/test_formsets.py +++ b/tests/forms_tests/tests/test_formsets.py @@ -1300,13 +1300,69 @@ ArticleFormSet = formset_factory(ArticleForm) class TestIsBoundBehavior(SimpleTestCase): - def test_no_data_raises_validation_error(self): - msg = ( - 'ManagementForm data is missing or has been tampered with. ' - 'Missing fields: form-TOTAL_FORMS, form-INITIAL_FORMS' + def test_no_data_error(self): + formset = ArticleFormSet({}) + self.assertIs(formset.is_valid(), False) + self.assertEqual( + formset.non_form_errors(), + [ + 'ManagementForm data is missing or has been tampered with. ' + 'Missing fields: form-TOTAL_FORMS, form-INITIAL_FORMS. ' + 'You may need to file a bug report if the issue persists.', + ], + ) + self.assertEqual(formset.errors, []) + # Can still render the formset. + self.assertEqual( + str(formset), + '' + '' + '' + '' + '' + '' + '\n' ) - with self.assertRaisesMessage(ValidationError, msg): - ArticleFormSet({}).is_valid() + + def test_management_form_invalid_data(self): + data = { + 'form-TOTAL_FORMS': 'two', + 'form-INITIAL_FORMS': 'one', + } + formset = ArticleFormSet(data) + self.assertIs(formset.is_valid(), False) + self.assertEqual( + formset.non_form_errors(), + [ + 'ManagementForm data is missing or has been tampered with. ' + 'Missing fields: form-TOTAL_FORMS, form-INITIAL_FORMS. ' + 'You may need to file a bug report if the issue persists.', + ], + ) + self.assertEqual(formset.errors, []) + # Can still render the formset. + self.assertEqual( + str(formset), + '' + '' + '' + '' + '' + '' + '\n', + ) + + def test_customize_management_form_error(self): + formset = ArticleFormSet({}, error_messages={'missing_management_form': 'customized'}) + self.assertIs(formset.is_valid(), False) + self.assertEqual(formset.non_form_errors(), ['customized']) + self.assertEqual(formset.errors, []) def test_with_management_data_attrs_work_fine(self): data = { diff --git a/tests/model_formsets/tests.py b/tests/model_formsets/tests.py index aaf76fa7ec..9c06d45339 100644 --- a/tests/model_formsets/tests.py +++ b/tests/model_formsets/tests.py @@ -4,7 +4,7 @@ from datetime import date from decimal import Decimal from django import forms -from django.core.exceptions import ImproperlyConfigured, ValidationError +from django.core.exceptions import ImproperlyConfigured from django.db import models from django.forms.models import ( BaseModelFormSet, _get_foreign_key, inlineformset_factory, @@ -1783,11 +1783,10 @@ class ModelFormsetTest(TestCase): [{'id': ['Select a valid choice. That choice is not one of the available choices.']}], ) - def test_initial_form_count_empty_data_raises_validation_error(self): + def test_initial_form_count_empty_data(self): AuthorFormSet = modelformset_factory(Author, fields='__all__') - msg = 'ManagementForm data is missing or has been tampered with' - with self.assertRaisesMessage(ValidationError, msg): - AuthorFormSet({}).initial_form_count() + formset = AuthorFormSet({}) + self.assertEqual(formset.initial_form_count(), 0) class TestModelFormsetOverridesTroughFormMeta(TestCase): -- cgit v1.3