浏览代码

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.
Natalia 7 月之前
父节点
当前提交
b60fd8722f
共有 1 个文件被更改,包括 93 次插入84 次删除
  1. 93 84
      tests/auth_tests/test_forms.py

+ 93 - 84
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,
             "<ul><li>"
@@ -340,7 +280,7 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase):
         )
 
     def test_password_extra_validations(self):
-        class ExtraValidationForm(ExtraValidationFormMixin, BaseUserCreationForm):
+        class ExtraValidationForm(ExtraValidationFormMixin, self.form_class):
             def clean_password1(self):
                 return self.failing_helper("password1")
 
@@ -370,8 +310,8 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase):
         BaseUserCreationForm password validation uses all of the form's data.
         """
 
-        class CustomUserCreationForm(BaseUserCreationForm):
-            class Meta(BaseUserCreationForm.Meta):
+        class CustomUserCreationForm(self.form_class):
+            class Meta(self.form_class.Meta):
                 model = User
                 fields = ("username", "email", "first_name", "last_name")
 
@@ -404,13 +344,13 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase):
         self.assertIs(form.is_valid(), True, form.errors)
 
     def test_username_field_autocapitalize_none(self):
-        form = BaseUserCreationForm()
+        form = self.form_class()
         self.assertEqual(
             form.fields["username"].widget.attrs.get("autocapitalize"), "none"
         )
 
     def test_html_autocomplete_attributes(self):
-        form = BaseUserCreationForm()
+        form = self.form_class()
         tests = (
             ("username", "username"),
             ("password1", "new-password"),
@@ -434,7 +374,76 @@ class BaseUserCreationFormTest(TestDataMixin, TestCase):
         self.assertFalse(u.has_usable_password())
 
 
-class UserCreationFormTest(TestDataMixin, TestCase):
+class CustomUserCreationFormTest(TestDataMixin, TestCase):
+
+    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])
+
+
+class UserCreationFormTest(BaseUserCreationFormTest):
+
+    form_class = UserCreationForm
+
     def test_case_insensitive_username(self):
         data = {
             "username": "TeStClIeNt",