diff options
| author | Berker Peksag <berker.peksag@gmail.com> | 2016-02-14 17:30:01 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-02-17 10:42:54 -0500 |
| commit | f78892f2de1e986f00d23581dc006e90e26abd8f (patch) | |
| tree | a5db18a810724cdd93bfe3e2f2a7bf2fc29239ce /tests | |
| parent | 61f1b6ff219a72902c81135ad4fb1db190f103f6 (diff) | |
[1.9.x] Refs #19353 -- Added tests for using custom user models with built-in auth forms.
Also updated topics/auth/customizing.txt to reflect that subclasses of
UserCreationForm and UserChangeForm can be used with custom user models.
Thanks Baptiste Mispelon for the initial documentation.
Backport of f0425c72601f466c6a71518749c6d15b94945514 from master
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/auth_tests/test_forms.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/auth_tests/test_forms.py b/tests/auth_tests/test_forms.py index 79f0f65628..918fe3801a 100644 --- a/tests/auth_tests/test_forms.py +++ b/tests/auth_tests/test_forms.py @@ -10,6 +10,7 @@ from django.contrib.auth.forms import ( SetPasswordForm, UserChangeForm, UserCreationForm, ) from django.contrib.auth.models import User +from django.contrib.auth.tests.custom_user import ExtensionUser from django.contrib.sites.models import Site from django.core import mail from django.core.mail import EmailMultiAlternatives @@ -153,6 +154,21 @@ class UserCreationFormTest(TestDataMixin, TestCase): form['password2'].errors ) + def test_custom_form(self): + class CustomUserCreationForm(UserCreationForm): + class Meta(UserCreationForm.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()) + @override_settings(USE_TZ=False, PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher']) class AuthenticationFormTest(TestDataMixin, TestCase): @@ -441,6 +457,24 @@ class UserChangeFormTest(TestDataMixin, TestCase): # value to render correctly self.assertEqual(form.initial['password'], form['password'].value()) + def test_custom_form(self): + class CustomUserChangeForm(UserChangeForm): + class Meta(UserChangeForm.Meta): + model = ExtensionUser + fields = ('username', 'password', 'date_of_birth',) + + user = User.objects.get(username='testclient') + data = { + 'username': 'testclient', + 'password': 'testclient', + 'date_of_birth': '1998-02-24', + } + form = CustomUserChangeForm(data, instance=user) + self.assertTrue(form.is_valid()) + form.save() + self.assertEqual(form.cleaned_data['username'], 'testclient') + self.assertEqual(form.cleaned_data['date_of_birth'], datetime.date(1998, 2, 24)) + @override_settings( PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'], |
