Fixed #28706 -- Moved AuthenticationFormn invalid login ValidationError to a method for reuse.
This commit is contained in:
parent
7fb913c805
commit
6ed347d851
|
@ -192,11 +192,7 @@ class AuthenticationForm(forms.Form):
|
||||||
if username is not None and password:
|
if username is not None and password:
|
||||||
self.user_cache = authenticate(self.request, username=username, password=password)
|
self.user_cache = authenticate(self.request, username=username, password=password)
|
||||||
if self.user_cache is None:
|
if self.user_cache is None:
|
||||||
raise forms.ValidationError(
|
raise self.get_invalid_login_error()
|
||||||
self.error_messages['invalid_login'],
|
|
||||||
code='invalid_login',
|
|
||||||
params={'username': self.username_field.verbose_name},
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
self.confirm_login_allowed(self.user_cache)
|
self.confirm_login_allowed(self.user_cache)
|
||||||
|
|
||||||
|
@ -227,6 +223,13 @@ class AuthenticationForm(forms.Form):
|
||||||
def get_user(self):
|
def get_user(self):
|
||||||
return self.user_cache
|
return self.user_cache
|
||||||
|
|
||||||
|
def get_invalid_login_error(self):
|
||||||
|
return forms.ValidationError(
|
||||||
|
self.error_messages['invalid_login'],
|
||||||
|
code='invalid_login',
|
||||||
|
params={'username': self.username_field.verbose_name},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class PasswordResetForm(forms.Form):
|
class PasswordResetForm(forms.Form):
|
||||||
email = forms.EmailField(label=_("Email"), max_length=254)
|
email = forms.EmailField(label=_("Email"), max_length=254)
|
||||||
|
|
|
@ -453,6 +453,17 @@ class AuthenticationFormTest(TestDataMixin, TestCase):
|
||||||
self.assertEqual(form.errors, {})
|
self.assertEqual(form.errors, {})
|
||||||
self.assertEqual(form.user_cache, user)
|
self.assertEqual(form.user_cache, user)
|
||||||
|
|
||||||
|
def test_get_invalid_login_error(self):
|
||||||
|
error = AuthenticationForm().get_invalid_login_error()
|
||||||
|
self.assertIsInstance(error, forms.ValidationError)
|
||||||
|
self.assertEqual(
|
||||||
|
error.message,
|
||||||
|
'Please enter a correct %(username)s and password. Note that both '
|
||||||
|
'fields may be case-sensitive.',
|
||||||
|
)
|
||||||
|
self.assertEqual(error.code, 'invalid_login')
|
||||||
|
self.assertEqual(error.params, {'username': 'username'})
|
||||||
|
|
||||||
|
|
||||||
class SetPasswordFormTest(TestDataMixin, TestCase):
|
class SetPasswordFormTest(TestDataMixin, TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue