Ticket #15110: modify_create_user_and_tests.patch

File modify_create_user_and_tests.patch, 3.9 KB (added by jorgeecardona, 14 years ago)

Modify create_user and unit test.

  • django/contrib/auth/models.py

    From 19bf7746cc0eec3e5c871d21ab4972b39a884667 Mon Sep 17 00:00:00 2001
    From: Jorge Eduardo Cardona <jorgeecardona@gmail.com>
    Date: Tue, 18 Jan 2011 16:25:43 -0500
    Subject: [PATCH 1/2] Modify create_user to receive a site object
    
    ---
     django/contrib/auth/models.py         |    4 ++--
     django/contrib/auth/tests/__init__.py |    2 +-
     2 files changed, 3 insertions(+), 3 deletions(-)
    
    diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
    index f51ba3b..2657e86 100644
    a b class Group(models.Model):  
    112112        return self.name
    113113
    114114class UserManager(models.Manager):
    115     def create_user(self, username, email, password=None):
     115    def create_user(self, username, email, password=None, site=None):
    116116        """
    117117        Creates and saves a User with the given username, e-mail and password.
    118118        """
    class UserManager(models.Manager):  
    129129
    130130        user = self.model(username=username, email=email, is_staff=False,
    131131                         is_active=True, is_superuser=False, last_login=now,
    132                          date_joined=now)
     132                         date_joined=now, site=site)
    133133
    134134        user.set_password(password)
    135135        user.save(using=self._db)
  • django/contrib/auth/tests/__init__.py

    diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py
    index 3a8f55b..3f0f3db 100644
    a b from django.contrib.auth.tests.tokens import TokenGeneratorTest  
    1414from django.contrib.auth.tests.views import (PasswordResetTest,
    1515    ChangePasswordTest, LoginTest, LogoutTest, LoginURLSettings)
    1616from django.contrib.auth.tests.permissions import TestAuthPermissions
    17 
     17from django.contrib.auth.tests.multitenancy import MultitenancyTests
    1818# The password for the fixture data users is 'password'
  • new file django/contrib/auth/tests/multitenancy.py

    -- 
    1.7.1
    
    
    From 9f86e2b835c67a10b8ac73e7deb4258e9bd45fab Mon Sep 17 00:00:00 2001
    From: Jorge Eduardo Cardona <jorgeecardona@gmail.com>
    Date: Tue, 18 Jan 2011 16:32:21 -0500
    Subject: [PATCH 2/2] Add the tests for multitenancy
    
    ---
     django/contrib/auth/tests/multitenancy.py |   39 +++++++++++++++++++++++++++++
     1 files changed, 39 insertions(+), 0 deletions(-)
     create mode 100644 django/contrib/auth/tests/multitenancy.py
    
    diff --git a/django/contrib/auth/tests/multitenancy.py b/django/contrib/auth/tests/multitenancy.py
    new file mode 100644
    index 0000000..e681d04
    - +  
     1from django.db import IntegrityError
     2from django.conf import settings
     3from django.test import TestCase
     4from django.contrib.sites.models import Site
     5from django.contrib.auth.models import User
     6
     7
     8class MultitenancyTests(TestCase):
     9
     10    def setUp(self):
     11        site = Site(id=settings.SITE_ID,
     12                    domain="example.com",
     13                    name="example.com")
     14        site.save()
     15
     16    def test_user_with_current_site(self):
     17        "Check that an users can be created in a site"
     18        u = User.objects.create_user('testuser', 'text@example.com', 'testpw')
     19        self.assertEqual(u.site, None)
     20
     21        # Asign site.
     22        u.site = Site.objects.get_current()
     23        u.save()
     24        self.assertEqual(u.site, Site.objects.get_current())
     25
     26        # Add a new user to None site.
     27        u = User.objects.create_user('testuser', 'text@example.com', 'testpw')
     28        self.assertEqual(u.site, None)
     29
     30    def test_add_user_duplicated_in_site(self):
     31
     32        # Add a new user with same username to current site.
     33        u = User.objects.create_user('testuser', 'text@example.com', 'testpw',
     34                                     Site.objects.get_current())
     35        self.assertEqual(u.site, Site.objects.get_current())
     36        self.assertRaises(IntegrityError, User.objects.create_user,
     37                          'testuser', 'text@example.com', 'testpw',
     38                           Site.objects.get_current())
     39
Back to Top