diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py
index b1cedc9..fb2a7c0 100644
a
|
b
|
class UserCreationForm(forms.ModelForm):
|
78 | 78 | model = User |
79 | 79 | fields = ("username",) |
80 | 80 | |
| 81 | def clean_username(self): |
| 82 | username = self.cleaned_data.get('username') |
| 83 | if User.objects.filter(username__iexact=username).exists(): |
| 84 | raise forms.ValidationError( |
| 85 | User._meta.get_field('username').error_messages['unique'], |
| 86 | code='unique', |
| 87 | ) |
| 88 | return username |
| 89 | |
81 | 90 | def clean_password2(self): |
82 | 91 | password1 = self.cleaned_data.get("password1") |
83 | 92 | password2 = self.cleaned_data.get("password2") |
diff --git a/tests/auth_tests/test_forms.py b/tests/auth_tests/test_forms.py
index aa0a6af..5cddbe7 100644
a
|
b
|
class UserCreationFormTest(TestDataMixin, TestCase):
|
78 | 78 | self.assertEqual(form["username"].errors, |
79 | 79 | [force_text(User._meta.get_field('username').error_messages['unique'])]) |
80 | 80 | |
| 81 | def test_case_insensitive_user_already_exists(self): |
| 82 | data = { |
| 83 | 'username': 'Testclient', |
| 84 | 'password1': 'test123', |
| 85 | 'password2': 'test123', |
| 86 | } |
| 87 | form = UserCreationForm(data) |
| 88 | self.assertFalse(form.is_valid()) |
| 89 | self.assertEqual(form["username"].errors, |
| 90 | [force_text(User._meta.get_field('username').error_messages['unique'])]) |
| 91 | |
81 | 92 | def test_invalid_data(self): |
82 | 93 | data = { |
83 | 94 | 'username': 'jsmith!', |
… |
… |
class UserCreationFormTest(TestDataMixin, TestCase):
|
140 | 151 | ]) |
141 | 152 | def test_validates_password(self): |
142 | 153 | data = { |
143 | | 'username': 'testclient', |
144 | | 'password1': 'testclient', |
145 | | 'password2': 'testclient', |
| 154 | 'username': 'anothertestclient', |
| 155 | 'password1': 'anothertestclient', |
| 156 | 'password2': 'anothertestclient', |
146 | 157 | } |
147 | 158 | form = UserCreationForm(data) |
148 | 159 | self.assertFalse(form.is_valid()) |