Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #17504 -- Fixed normalization of email addresses that have '@' …

…in the name when calling `User.objects.create_user`. Thanks, marw85.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17482 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 875a5ea8d47c2c10432572fec788a81217073a4f 1 parent ec5e2f0
@jezdez jezdez authored
View
26 django/contrib/auth/models.py
@@ -128,25 +128,31 @@ def natural_key(self):
class UserManager(models.Manager):
- def create_user(self, username, email=None, password=None):
+
+ @classmethod
+ def normalize_email(cls, email):
"""
- Creates and saves a User with the given username, email and password.
+ Normalize the address by lowercasing the domain part of the email
+ address.
"""
- now = timezone.now()
-
- # Normalize the address by lowercasing the domain part of the email
- # address.
email = email or ''
try:
- email_name, domain_part = email.strip().split('@', 1)
+ email_name, domain_part = email.strip().rsplit('@', 1)
except ValueError:
pass
else:
email = '@'.join([email_name, domain_part.lower()])
+ return email
- user = self.model(username=username, email=email, is_staff=False,
- is_active=True, is_superuser=False, last_login=now,
- date_joined=now)
+ def create_user(self, username, email=None, password=None):
+ """
+ Creates and saves a User with the given username, email and password.
+ """
+ now = timezone.now()
+ email = UserManager.normalize_email(email)
+ user = self.model(username=username, email=email,
+ is_staff=False, is_active=True, is_superuser=False,
+ last_login=now, date_joined=now)
user.set_password(password)
user.save(using=self._db)
View
3  django/contrib/auth/tests/__init__.py
@@ -11,7 +11,8 @@
RemoteUserNoCreateTest, RemoteUserCustomTest)
from django.contrib.auth.tests.management import GetDefaultUsernameTestCase
from django.contrib.auth.tests.models import (ProfileTestCase, NaturalKeysTestCase,
- LoadDataWithoutNaturalKeysTestCase, LoadDataWithNaturalKeysTestCase)
+ LoadDataWithoutNaturalKeysTestCase, LoadDataWithNaturalKeysTestCase,
+ UserManagerTestCase)
from django.contrib.auth.tests.hashers import TestUtilsHashPass
from django.contrib.auth.tests.signals import SignalTestCase
from django.contrib.auth.tests.tokens import TokenGeneratorTest
View
29 django/contrib/auth/tests/models.py
@@ -1,9 +1,12 @@
from django.conf import settings
from django.test import TestCase
-from django.contrib.auth.models import Group, User, SiteProfileNotAvailable
+from django.contrib.auth.models import (Group, User,
+ SiteProfileNotAvailable, UserManager)
+
class ProfileTestCase(TestCase):
fixtures = ['authtestdata.json']
+
def setUp(self):
"""Backs up the AUTH_PROFILE_MODULE"""
self.old_AUTH_PROFILE_MODULE = getattr(settings,
@@ -59,8 +62,32 @@ def test_user_is_created_and_added_to_group(self):
class LoadDataWithNaturalKeysTestCase(TestCase):
fixtures = ['natural.json']
+
def test_user_is_created_and_added_to_group(self):
user = User.objects.get(username='my_username')
group = Group.objects.get(name='my_group')
self.assertEquals(group, user.groups.get())
+
+class UserManagerTestCase(TestCase):
+
+ def test_create_user(self):
+ email_lowercase = 'normal@normal.com'
+ user = User.objects.create_user('user', email_lowercase)
+ self.assertEquals(user.email, email_lowercase)
+ self.assertEquals(user.username, 'user')
+ self.assertEquals(user.password, '!')
+
+ def test_create_user_email_domain_normalize_rfc3696(self):
+ # According to http://tools.ietf.org/html/rfc3696#section-3
+ # the "@" symbol can be part of the local part of an email address
+ returned = UserManager.normalize_email(r'Abc\@DEF@EXAMPLE.com')
+ self.assertEquals(returned, r'Abc\@DEF@example.com')
+
+ def test_create_user_email_domain_normalize(self):
+ returned = UserManager.normalize_email('normal@DOMAIN.COM')
+ self.assertEquals(returned, 'normal@domain.com')
+
+ def test_create_user_email_domain_normalize_with_whitespace(self):
+ returned = UserManager.normalize_email('email\ with_whitespace@D.COM')
+ self.assertEquals(returned, 'email\ with_whitespace@d.com')
Please sign in to comment.
Something went wrong with that request. Please try again.