mirror of
https://github.com/django/django.git
synced 2025-01-03 06:55:47 +00:00
Fixed #26951 -- Allowed AuthenticationForm to work with a username of 0.
This commit is contained in:
parent
74bb013cc1
commit
975a76a964
@ -186,7 +186,7 @@ class AuthenticationForm(forms.Form):
|
|||||||
username = self.cleaned_data.get('username')
|
username = self.cleaned_data.get('username')
|
||||||
password = self.cleaned_data.get('password')
|
password = self.cleaned_data.get('password')
|
||||||
|
|
||||||
if username and password:
|
if username is not None and password:
|
||||||
self.user_cache = authenticate(username=username, password=password)
|
self.user_cache = authenticate(username=username, password=password)
|
||||||
if self.user_cache is None:
|
if self.user_cache is None:
|
||||||
raise forms.ValidationError(
|
raise forms.ValidationError(
|
||||||
|
@ -6,9 +6,10 @@ from .invalid_models import CustomUserNonUniqueUsername
|
|||||||
from .is_active import IsActiveTestUser1
|
from .is_active import IsActiveTestUser1
|
||||||
from .uuid_pk import UUIDUser
|
from .uuid_pk import UUIDUser
|
||||||
from .with_foreign_key import CustomUserWithFK, Email
|
from .with_foreign_key import CustomUserWithFK, Email
|
||||||
|
from .with_integer_username import IntegerUsernameUser
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'CustomUser', 'CustomUserWithoutIsActiveField', 'CustomPermissionsUser',
|
'CustomUser', 'CustomUserWithoutIsActiveField', 'CustomPermissionsUser',
|
||||||
'CustomUserWithFK', 'Email', 'ExtensionUser', 'IsActiveTestUser1',
|
'CustomUserWithFK', 'Email', 'ExtensionUser', 'IsActiveTestUser1',
|
||||||
'UUIDUser', 'CustomUserNonUniqueUsername',
|
'UUIDUser', 'CustomUserNonUniqueUsername', 'IntegerUsernameUser'
|
||||||
)
|
)
|
||||||
|
23
tests/auth_tests/models/with_integer_username.py
Normal file
23
tests/auth_tests/models/with_integer_username.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class IntegerUsernameUserManager(BaseUserManager):
|
||||||
|
def create_user(self, username, password):
|
||||||
|
user = self.model(username=username)
|
||||||
|
user.set_password(password)
|
||||||
|
user.save(using=self._db)
|
||||||
|
return user
|
||||||
|
|
||||||
|
def get_by_natural_key(self, username):
|
||||||
|
return self.get(username=username)
|
||||||
|
|
||||||
|
|
||||||
|
class IntegerUsernameUser(AbstractBaseUser):
|
||||||
|
username = models.IntegerField()
|
||||||
|
password = models.CharField(max_length=255)
|
||||||
|
|
||||||
|
USERNAME_FIELD = 'username'
|
||||||
|
REQUIRED_FIELDS = ['username', 'password']
|
||||||
|
|
||||||
|
objects = IntegerUsernameUserManager()
|
@ -15,7 +15,7 @@ from django.contrib.auth.models import User
|
|||||||
from django.contrib.sites.models import Site
|
from django.contrib.sites.models import Site
|
||||||
from django.core import mail
|
from django.core import mail
|
||||||
from django.core.mail import EmailMultiAlternatives
|
from django.core.mail import EmailMultiAlternatives
|
||||||
from django.forms.fields import CharField, Field
|
from django.forms.fields import CharField, Field, IntegerField
|
||||||
from django.test import SimpleTestCase, TestCase, mock, override_settings
|
from django.test import SimpleTestCase, TestCase, mock, override_settings
|
||||||
from django.utils import six, translation
|
from django.utils import six, translation
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
@ -23,6 +23,7 @@ from django.utils.text import capfirst
|
|||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
|
|
||||||
from .models.custom_user import CustomUser, ExtensionUser
|
from .models.custom_user import CustomUser, ExtensionUser
|
||||||
|
from .models.with_integer_username import IntegerUsernameUser
|
||||||
from .settings import AUTH_TEMPLATES
|
from .settings import AUTH_TEMPLATES
|
||||||
|
|
||||||
|
|
||||||
@ -351,6 +352,23 @@ class AuthenticationFormTest(TestDataMixin, TestCase):
|
|||||||
form.is_valid() # Not necessary to have valid credentails for the test.
|
form.is_valid() # Not necessary to have valid credentails for the test.
|
||||||
self.assertEqual(form.cleaned_data['password'], data['password'])
|
self.assertEqual(form.cleaned_data['password'], data['password'])
|
||||||
|
|
||||||
|
@override_settings(AUTH_USER_MODEL='auth_tests.IntegerUsernameUser')
|
||||||
|
def test_integer_username(self):
|
||||||
|
class CustomAuthenticationForm(AuthenticationForm):
|
||||||
|
username = IntegerField()
|
||||||
|
|
||||||
|
user = IntegerUsernameUser.objects.create_user(username=0, password='pwd')
|
||||||
|
data = {
|
||||||
|
'username': 0,
|
||||||
|
'password': 'pwd',
|
||||||
|
}
|
||||||
|
form = CustomAuthenticationForm(None, data)
|
||||||
|
self.assertTrue(form.is_valid())
|
||||||
|
self.assertEqual(form.cleaned_data['username'], data['username'])
|
||||||
|
self.assertEqual(form.cleaned_data['password'], data['password'])
|
||||||
|
self.assertEqual(form.errors, {})
|
||||||
|
self.assertEqual(form.user_cache, user)
|
||||||
|
|
||||||
|
|
||||||
class SetPasswordFormTest(TestDataMixin, TestCase):
|
class SetPasswordFormTest(TestDataMixin, TestCase):
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user