Ticket #17504: django-17504.2.diff

File django-17504.2.diff, 4.9 KB (added by marw85, 13 years ago)
  • django/contrib/auth/models.py

    diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
    index 8064a4e..51ebd41 100644
    a b class Group(models.Model):  
    116116
    117117
    118118class UserManager(models.Manager):
    119     def create_user(self, username, email=None, password=None):
     119
     120    @staticmethod
     121    def normalize_email(email):
    120122        """
    121         Creates and saves a User with the given username, email and password.
     123        Normalize the address by lowercasing the domain part of the email
     124        address.
    122125        """
    123         now = timezone.now()
    124 
    125         # Normalize the address by lowercasing the domain part of the email
    126         # address.
    127126        email = email or ''
    128127        try:
    129             email_name, domain_part = email.strip().split('@', 1)
     128            email_name, domain_part = email.strip().rsplit('@', 1)
    130129        except ValueError:
    131130            pass
    132131        else:
    133132            email = '@'.join([email_name, domain_part.lower()])
     133        return email
     134
     135    def create_user(self, username, email=None, password=None):
     136        """
     137        Creates and saves a User with the given username, email and password.
     138        """
     139        now = timezone.now()
     140
     141        email = UserManager.normalize_email(email)
    134142
    135143        user = self.model(username=username, email=email, is_staff=False,
    136144                         is_active=True, is_superuser=False, last_login=now,
  • django/contrib/auth/tests/__init__.py

    diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py
    index 883e4c9..6bb6e68 100644
    a b from django.contrib.auth.tests.forms import (UserCreationFormTest,  
    1010from django.contrib.auth.tests.remote_user import (RemoteUserTest,
    1111    RemoteUserNoCreateTest, RemoteUserCustomTest)
    1212from django.contrib.auth.tests.management import GetDefaultUsernameTestCase
    13 from django.contrib.auth.tests.models import ProfileTestCase
     13from django.contrib.auth.tests.models import ProfileTestCase, UserManagerTestCase
    1414from django.contrib.auth.tests.hashers import TestUtilsHashPass
    1515from django.contrib.auth.tests.signals import SignalTestCase
    1616from django.contrib.auth.tests.tokens import TokenGeneratorTest
    17 from django.contrib.auth.tests.views import (AuthViewNamedURLTests, 
    18     PasswordResetTest, ChangePasswordTest, LoginTest, LogoutTest, 
     17from django.contrib.auth.tests.views import (AuthViewNamedURLTests,
     18    PasswordResetTest, ChangePasswordTest, LoginTest, LogoutTest,
    1919    LoginURLSettings)
    2020
    2121# The password for the fixture data users is 'password'
  • django/contrib/auth/tests/models.py

    diff --git a/django/contrib/auth/tests/models.py b/django/contrib/auth/tests/models.py
    index 754c6db..52e44c6 100644
    a b  
    11from django.conf import settings
    22from django.test import TestCase
    3 from django.contrib.auth.models import User, SiteProfileNotAvailable
     3from django.contrib.auth.models import User, SiteProfileNotAvailable, UserManager
     4
    45
    56class ProfileTestCase(TestCase):
    67    fixtures = ['authtestdata.json']
     8
    79    def setUp(self):
    810        """Backs up the AUTH_PROFILE_MODULE"""
    911        self.old_AUTH_PROFILE_MODULE = getattr(settings,
    class ProfileTestCase(TestCase):  
    2628        user = User.objects.get(username='testclient')
    2729        self.assertRaises(SiteProfileNotAvailable, user.get_profile)
    2830
    29         # Bad syntax in AUTH_PROFILE_MODULE: 
     31        # Bad syntax in AUTH_PROFILE_MODULE:
    3032        settings.AUTH_PROFILE_MODULE = 'foobar'
    3133        self.assertRaises(SiteProfileNotAvailable, user.get_profile)
    3234
    3335        # module that doesn't exist
    3436        settings.AUTH_PROFILE_MODULE = 'foo.bar'
    3537        self.assertRaises(SiteProfileNotAvailable, user.get_profile)
     38
     39
     40class UserManagerTestCase(TestCase):
     41
     42    def setUp(self):
     43        self.manager = UserManager()
     44        self.manager.contribute_to_class(User, 'name')
     45
     46    def test_create_user(self):
     47        email_lowercase = 'normal@normal.com'
     48        user = self.manager.create_user('user', email_lowercase)
     49        self.assertEquals(user.email, email_lowercase)
     50        self.assertEquals(user.username, 'user')
     51        self.assertEquals(user.password, '!')
     52
     53    def test_create_user_email_domain_normalize_rfc3696(self):
     54        # According to  http://tools.ietf.org/html/rfc3696#section-3
     55        # the "@" symbol can be part of the local part of an email address
     56        returned = UserManager.normalize_email(r'Abc\@DEF@EXAMPLE.com')
     57        self.assertEquals(returned, r'Abc\@DEF@example.com')
     58
     59    def test_create_user_email_domain_normalize(self):
     60        returned = UserManager.normalize_email('normal@DOMAIN.COM')
     61        self.assertEquals(returned, 'normal@domain.com')
     62
     63    def test_create_user_email_domain_normalize_with_whitespace(self):
     64        returned = UserManager.normalize_email('email\ with_whitespace@D.COM')
     65        self.assertEquals(returned, 'email\ with_whitespace@d.com')
Back to Top