Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 7f91bdf2a957523003c1362dd6ec06479c8e8c02 1 parent 74ca8dd
authored February 09, 2012
26  django/contrib/auth/models.py
@@ -128,25 +128,31 @@ def natural_key(self):
128 128
 
129 129
 
130 130
 class UserManager(models.Manager):
131  
-    def create_user(self, username, email=None, password=None):
  131
+
  132
+    @classmethod
  133
+    def normalize_email(cls, email):
132 134
         """
133  
-        Creates and saves a User with the given username, email and password.
  135
+        Normalize the address by lowercasing the domain part of the email
  136
+        address.
134 137
         """
135  
-        now = timezone.now()
136  
-
137  
-        # Normalize the address by lowercasing the domain part of the email
138  
-        # address.
139 138
         email = email or ''
140 139
         try:
141  
-            email_name, domain_part = email.strip().split('@', 1)
  140
+            email_name, domain_part = email.strip().rsplit('@', 1)
142 141
         except ValueError:
143 142
             pass
144 143
         else:
145 144
             email = '@'.join([email_name, domain_part.lower()])
  145
+        return email
146 146
 
147  
-        user = self.model(username=username, email=email, is_staff=False,
148  
-                         is_active=True, is_superuser=False, last_login=now,
149  
-                         date_joined=now)
  147
+    def create_user(self, username, email=None, password=None):
  148
+        """
  149
+        Creates and saves a User with the given username, email and password.
  150
+        """
  151
+        now = timezone.now()
  152
+        email = UserManager.normalize_email(email)
  153
+        user = self.model(username=username, email=email,
  154
+                          is_staff=False, is_active=True, is_superuser=False,
  155
+                          last_login=now, date_joined=now)
150 156
 
151 157
         user.set_password(password)
152 158
         user.save(using=self._db)
3  django/contrib/auth/tests/__init__.py
@@ -11,7 +11,8 @@
11 11
     RemoteUserNoCreateTest, RemoteUserCustomTest)
12 12
 from django.contrib.auth.tests.management import GetDefaultUsernameTestCase
13 13
 from django.contrib.auth.tests.models import (ProfileTestCase, NaturalKeysTestCase,
14  
-    LoadDataWithoutNaturalKeysTestCase, LoadDataWithNaturalKeysTestCase)
  14
+    LoadDataWithoutNaturalKeysTestCase, LoadDataWithNaturalKeysTestCase,
  15
+    UserManagerTestCase)
15 16
 from django.contrib.auth.tests.hashers import TestUtilsHashPass
16 17
 from django.contrib.auth.tests.signals import SignalTestCase
17 18
 from django.contrib.auth.tests.tokens import TokenGeneratorTest
29  django/contrib/auth/tests/models.py
... ...
@@ -1,9 +1,12 @@
1 1
 from django.conf import settings
2 2
 from django.test import TestCase
3  
-from django.contrib.auth.models import Group, User, SiteProfileNotAvailable
  3
+from django.contrib.auth.models import (Group, User,
  4
+    SiteProfileNotAvailable, UserManager)
  5
+
4 6
 
5 7
 class ProfileTestCase(TestCase):
6 8
     fixtures = ['authtestdata.json']
  9
+
7 10
     def setUp(self):
8 11
         """Backs up the AUTH_PROFILE_MODULE"""
9 12
         self.old_AUTH_PROFILE_MODULE = getattr(settings,
@@ -59,8 +62,32 @@ def test_user_is_created_and_added_to_group(self):
59 62
 
60 63
 class LoadDataWithNaturalKeysTestCase(TestCase):
61 64
     fixtures = ['natural.json']
  65
+
62 66
     def test_user_is_created_and_added_to_group(self):
63 67
         user = User.objects.get(username='my_username')
64 68
         group = Group.objects.get(name='my_group')
65 69
         self.assertEquals(group, user.groups.get())
66 70
 
  71
+
  72
+class UserManagerTestCase(TestCase):
  73
+
  74
+    def test_create_user(self):
  75
+        email_lowercase = 'normal@normal.com'
  76
+        user = User.objects.create_user('user', email_lowercase)
  77
+        self.assertEquals(user.email, email_lowercase)
  78
+        self.assertEquals(user.username, 'user')
  79
+        self.assertEquals(user.password, '!')
  80
+
  81
+    def test_create_user_email_domain_normalize_rfc3696(self):
  82
+        # According to  http://tools.ietf.org/html/rfc3696#section-3
  83
+        # the "@" symbol can be part of the local part of an email address
  84
+        returned = UserManager.normalize_email(r'Abc\@DEF@EXAMPLE.com')
  85
+        self.assertEquals(returned, r'Abc\@DEF@example.com')
  86
+
  87
+    def test_create_user_email_domain_normalize(self):
  88
+        returned = UserManager.normalize_email('normal@DOMAIN.COM')
  89
+        self.assertEquals(returned, 'normal@domain.com')
  90
+
  91
+    def test_create_user_email_domain_normalize_with_whitespace(self):
  92
+        returned = UserManager.normalize_email('email\ with_whitespace@D.COM')
  93
+        self.assertEquals(returned, 'email\ with_whitespace@d.com')

0 notes on commit 7f91bdf

Please sign in to comment.
Something went wrong with that request. Please try again.