From b60fd8722f305ec29c87f34d3fea262e56394ebd Mon Sep 17 00:00:00 2001 From: Natalia <124304+nessita@users.noreply.github.com> Date: Thu, 15 Aug 2024 10:21:32 -0300 Subject: Refs #35678 -- Split tests for BaseUserCreationForm when using a custom User model. This work also allows to subclass BaseUserCreationFormTest to reuse the tests and assertions for testing forms that extend BaseUserCreationForm, which is now used for UserCreationFormTest, increasing its coverage. --- tests/auth_tests/test_forms.py | 177 ++++++++++++++++++++++------------------- 1 file changed, 93 insertions(+), 84 deletions(-) diff --git a/tests/auth_tests/test_forms.py b/tests/auth_tests/test_forms.py index 3dd9324304..22b0aa6718 100644 --- a/tests/auth_tests/test_forms.py +++ b/tests/auth_tests/test_forms.py @@ -76,13 +76,16 @@ class ExtraValidationFormMixin: class BaseUserCreationFormTest(TestDataMixin, TestCase): + + form_class = BaseUserCreationForm + def test_user_already_exists(self): data = { "username": "testclient", "password1": "test123", "password2": "test123", } - form = BaseUserCreationForm(data) + form = self.form_class(data) self.assertFalse(form.is_valid()) self.assertEqual( form["username"].errors, @@ -95,7 +98,7 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase): "password1": "test123", "password2": "test123", } - form = BaseUserCreationForm(data) + form = self.form_class(data) self.assertFalse(form.is_valid()) validator = next( v @@ -111,7 +114,7 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase): "password1": "test123", "password2": "test", } - form = BaseUserCreationForm(data) + form = self.form_class(data) self.assertFalse(form.is_valid()) self.assertEqual( form["password2"].errors, [str(form.error_messages["password_mismatch"])] @@ -120,14 +123,14 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase): def test_both_passwords(self): # One (or both) passwords weren't given data = {"username": "jsmith"} - form = BaseUserCreationForm(data) + form = self.form_class(data) required_error = [str(Field.default_error_messages["required"])] self.assertFalse(form.is_valid()) self.assertEqual(form["password1"].errors, required_error) self.assertEqual(form["password2"].errors, required_error) data["password2"] = "test123" - form = UserCreationForm(data) + form = self.form_class(data) self.assertFalse(form.is_valid()) self.assertEqual(form["password1"].errors, required_error) self.assertEqual(form["password2"].errors, []) @@ -140,7 +143,7 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase): "password1": "test123", "password2": "test123", } - form = BaseUserCreationForm(data) + form = self.form_class(data) self.assertTrue(form.is_valid()) form.save(commit=False) self.assertEqual(password_changed.call_count, 0) @@ -154,7 +157,7 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase): "password1": "test123", "password2": "test123", } - form = BaseUserCreationForm(data) + form = self.form_class(data) self.assertTrue(form.is_valid()) u = form.save() self.assertEqual(u.username, "宝") @@ -168,7 +171,7 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase): "password1": "pwd2", "password2": "pwd2", } - form = BaseUserCreationForm(data) + form = self.form_class(data) self.assertTrue(form.is_valid()) user = form.save() self.assertNotEqual(user.username, ohm_username) @@ -195,7 +198,7 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase): "password1": "pwd2", "password2": "pwd2", } - form = BaseUserCreationForm(data) + form = self.form_class(data) self.assertFalse(form.is_valid()) self.assertEqual( form.errors["username"], ["A user with that username already exists."] @@ -221,11 +224,11 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase): ) def test_validates_password(self): data = { - "username": "testclient", - "password1": "testclient", - "password2": "testclient", + "username": "otherclient", + "password1": "otherclient", + "password2": "otherclient", } - form = BaseUserCreationForm(data) + form = self.form_class(data) self.assertFalse(form.is_valid()) self.assertEqual(len(form["password2"].errors), 2) self.assertIn( @@ -246,76 +249,13 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase): form = BaseUserCreationForm(data) self.assertIs(form.is_valid(), True, form.errors) - def test_custom_form(self): - class CustomUserCreationForm(BaseUserCreationForm): - class Meta(BaseUserCreationForm.Meta): - model = ExtensionUser - fields = UserCreationForm.Meta.fields + ("date_of_birth",) - - data = { - "username": "testclient", - "password1": "testclient", - "password2": "testclient", - "date_of_birth": "1988-02-24", - } - form = CustomUserCreationForm(data) - self.assertTrue(form.is_valid()) - - def test_custom_form_with_different_username_field(self): - class CustomUserCreationForm(BaseUserCreationForm): - class Meta(BaseUserCreationForm.Meta): - model = CustomUser - fields = ("email", "date_of_birth") - - data = { - "email": "test@client222.com", - "password1": "testclient", - "password2": "testclient", - "date_of_birth": "1988-02-24", - } - form = CustomUserCreationForm(data) - self.assertTrue(form.is_valid()) - - def test_custom_form_hidden_username_field(self): - class CustomUserCreationForm(BaseUserCreationForm): - class Meta(BaseUserCreationForm.Meta): - model = CustomUserWithoutIsActiveField - fields = ("email",) # without USERNAME_FIELD - - data = { - "email": "testclient@example.com", - "password1": "testclient", - "password2": "testclient", - } - form = CustomUserCreationForm(data) - self.assertTrue(form.is_valid()) - - def test_custom_form_saves_many_to_many_field(self): - class CustomUserCreationForm(BaseUserCreationForm): - class Meta(BaseUserCreationForm.Meta): - model = CustomUserWithM2M - fields = UserCreationForm.Meta.fields + ("orgs",) - - organization = Organization.objects.create(name="organization 1") - - data = { - "username": "testclient@example.com", - "password1": "testclient", - "password2": "testclient", - "orgs": [str(organization.pk)], - } - form = CustomUserCreationForm(data) - self.assertIs(form.is_valid(), True) - user = form.save(commit=True) - self.assertSequenceEqual(user.orgs.all(), [organization]) - def test_password_whitespace_not_stripped(self): data = { "username": "testuser", "password1": " testpassword ", "password2": " testpassword ", } - form = BaseUserCreationForm(data) + form = self.form_class(data) self.assertTrue(form.is_valid()) self.assertEqual(form.cleaned_data["password1"], data["password1"]) self.assertEqual(form.cleaned_data["password2"], data["password2"]) @@ -331,7 +271,7 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase): ] ) def test_password_help_text(self): - form = BaseUserCreationForm() + form = self.form_class() self.assertEqual( form.fields["password1"].help_text, "