Code

Ticket #17504: django-17504.2.diff

File django-17504.2.diff, 4.9 KB (added by marw85, 2 years ago)
Line 
1diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
2index 8064a4e..51ebd41 100644
3--- a/django/contrib/auth/models.py
4+++ b/django/contrib/auth/models.py
5@@ -116,21 +116,29 @@ class Group(models.Model):
6 
7 
8 class UserManager(models.Manager):
9-    def create_user(self, username, email=None, password=None):
10+
11+    @staticmethod
12+    def normalize_email(email):
13         """
14-        Creates and saves a User with the given username, email and password.
15+        Normalize the address by lowercasing the domain part of the email
16+        address.
17         """
18-        now = timezone.now()
19-
20-        # Normalize the address by lowercasing the domain part of the email
21-        # address.
22         email = email or ''
23         try:
24-            email_name, domain_part = email.strip().split('@', 1)
25+            email_name, domain_part = email.strip().rsplit('@', 1)
26         except ValueError:
27             pass
28         else:
29             email = '@'.join([email_name, domain_part.lower()])
30+        return email
31+
32+    def create_user(self, username, email=None, password=None):
33+        """
34+        Creates and saves a User with the given username, email and password.
35+        """
36+        now = timezone.now()
37+
38+        email = UserManager.normalize_email(email)
39 
40         user = self.model(username=username, email=email, is_staff=False,
41                          is_active=True, is_superuser=False, last_login=now,
42diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py
43index 883e4c9..6bb6e68 100644
44--- a/django/contrib/auth/tests/__init__.py
45+++ b/django/contrib/auth/tests/__init__.py
46@@ -10,12 +10,12 @@ from django.contrib.auth.tests.forms import (UserCreationFormTest,
47 from django.contrib.auth.tests.remote_user import (RemoteUserTest,
48     RemoteUserNoCreateTest, RemoteUserCustomTest)
49 from django.contrib.auth.tests.management import GetDefaultUsernameTestCase
50-from django.contrib.auth.tests.models import ProfileTestCase
51+from django.contrib.auth.tests.models import ProfileTestCase, UserManagerTestCase
52 from django.contrib.auth.tests.hashers import TestUtilsHashPass
53 from django.contrib.auth.tests.signals import SignalTestCase
54 from django.contrib.auth.tests.tokens import TokenGeneratorTest
55-from django.contrib.auth.tests.views import (AuthViewNamedURLTests,
56-    PasswordResetTest, ChangePasswordTest, LoginTest, LogoutTest,
57+from django.contrib.auth.tests.views import (AuthViewNamedURLTests,
58+    PasswordResetTest, ChangePasswordTest, LoginTest, LogoutTest,
59     LoginURLSettings)
60 
61 # The password for the fixture data users is 'password'
62diff --git a/django/contrib/auth/tests/models.py b/django/contrib/auth/tests/models.py
63index 754c6db..52e44c6 100644
64--- a/django/contrib/auth/tests/models.py
65+++ b/django/contrib/auth/tests/models.py
66@@ -1,9 +1,11 @@
67 from django.conf import settings
68 from django.test import TestCase
69-from django.contrib.auth.models import User, SiteProfileNotAvailable
70+from django.contrib.auth.models import User, SiteProfileNotAvailable, UserManager
71+
72 
73 class ProfileTestCase(TestCase):
74     fixtures = ['authtestdata.json']
75+
76     def setUp(self):
77         """Backs up the AUTH_PROFILE_MODULE"""
78         self.old_AUTH_PROFILE_MODULE = getattr(settings,
79@@ -26,10 +28,38 @@ class ProfileTestCase(TestCase):
80         user = User.objects.get(username='testclient')
81         self.assertRaises(SiteProfileNotAvailable, user.get_profile)
82 
83-        # Bad syntax in AUTH_PROFILE_MODULE:
84+        # Bad syntax in AUTH_PROFILE_MODULE:
85         settings.AUTH_PROFILE_MODULE = 'foobar'
86         self.assertRaises(SiteProfileNotAvailable, user.get_profile)
87 
88         # module that doesn't exist
89         settings.AUTH_PROFILE_MODULE = 'foo.bar'
90         self.assertRaises(SiteProfileNotAvailable, user.get_profile)
91+
92+
93+class UserManagerTestCase(TestCase):
94+
95+    def setUp(self):
96+        self.manager = UserManager()
97+        self.manager.contribute_to_class(User, 'name')
98+
99+    def test_create_user(self):
100+        email_lowercase = 'normal@normal.com'
101+        user = self.manager.create_user('user', email_lowercase)
102+        self.assertEquals(user.email, email_lowercase)
103+        self.assertEquals(user.username, 'user')
104+        self.assertEquals(user.password, '!')
105+
106+    def test_create_user_email_domain_normalize_rfc3696(self):
107+        # According to  http://tools.ietf.org/html/rfc3696#section-3
108+        # the "@" symbol can be part of the local part of an email address
109+        returned = UserManager.normalize_email(r'Abc\@DEF@EXAMPLE.com')
110+        self.assertEquals(returned, r'Abc\@DEF@example.com')
111+
112+    def test_create_user_email_domain_normalize(self):
113+        returned = UserManager.normalize_email('normal@DOMAIN.COM')
114+        self.assertEquals(returned, 'normal@domain.com')
115+
116+    def test_create_user_email_domain_normalize_with_whitespace(self):
117+        returned = UserManager.normalize_email('email\ with_whitespace@D.COM')
118+        self.assertEquals(returned, 'email\ with_whitespace@d.com')