Ticket #14354: auth.3.diff

File auth.3.diff, 8.5 KB (added by laurentluce, 14 years ago)
  • django/contrib/auth/tests/__init__.py

     
    11from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest
    2 from django.contrib.auth.tests.basic import BASIC_TESTS
     2from django.contrib.auth.tests.basic import BasicTestCase
    33from django.contrib.auth.tests.decorators import LoginRequiredTestCase
    44from django.contrib.auth.tests.forms import UserCreationFormTest, AuthenticationFormTest, SetPasswordFormTest, PasswordChangeFormTest, UserChangeFormTest, PasswordResetFormTest
    55from django.contrib.auth.tests.remote_user \
     
    1212# The password for the fixture data users is 'password'
    1313
    1414__test__ = {
    15     'BASIC_TESTS': BASIC_TESTS,
    1615    'TOKEN_GENERATOR_TESTS': TOKEN_GENERATOR_TESTS,
    1716}
  • django/contrib/auth/tests/basic.py

     
     1from django.test import TestCase
     2from django.contrib.auth.models import User, AnonymousUser
     3from django.core.management import call_command
     4from StringIO import StringIO
    15
    2 BASIC_TESTS = """
    3 >>> from django.contrib.auth.models import User, AnonymousUser
    4 >>> u = User.objects.create_user('testuser', 'test@example.com', 'testpw')
    5 >>> u.has_usable_password()
    6 True
    7 >>> u.check_password('bad')
    8 False
    9 >>> u.check_password('testpw')
    10 True
    11 >>> u.set_unusable_password()
    12 >>> u.save()
    13 >>> u.check_password('testpw')
    14 False
    15 >>> u.has_usable_password()
    16 False
    17 >>> u2 = User.objects.create_user('testuser2', 'test2@example.com')
    18 >>> u2.has_usable_password()
    19 False
     6class BasicTestCase(TestCase):
     7    """
     8    Basic test case class for all basic test cases.
     9    """
    2010
    21 >>> u.is_authenticated()
    22 True
    23 >>> u.is_staff
    24 False
    25 >>> u.is_active
    26 True
    27 >>> u.is_superuser
    28 False
     11    def test_user(self):
     12        u = User.objects.create_user('testuser', 'test@example.com', 'testpw')
     13        self.assertTrue(u.has_usable_password())
     14        self.assertFalse(u.check_password('bad'))
     15        self.assertTrue(u.check_password('testpw'))
     16        u.set_unusable_password()
     17        u.save()
     18        self.assertFalse(u.check_password('testpw'))
     19        self.assertFalse(u.has_usable_password())
     20        u.set_password('testpw')
     21        self.assertTrue(u.check_password('testpw'))
     22        u.set_password(None)
     23        self.assertFalse(u.has_usable_password())
     24        self.assertTrue(u.is_authenticated())
     25        self.assertFalse(u.is_staff)
     26        self.assertTrue(u.is_active)
     27        self.assertFalse(u.is_superuser)
     28       
     29        u2 = User.objects.create_user('testuser2', 'test2@example.com')
     30        self.assertFalse(u.has_usable_password())
    2931
    30 >>> a = AnonymousUser()
    31 >>> a.is_authenticated()
    32 False
    33 >>> a.is_staff
    34 False
    35 >>> a.is_active
    36 False
    37 >>> a.is_superuser
    38 False
    39 >>> a.groups.all()
    40 []
    41 >>> a.user_permissions.all()
    42 []
     32    def test_anonymous_user(self):
     33        a = AnonymousUser()
     34        self.assertFalse(a.is_authenticated())
     35        self.assertFalse(a.is_staff)
     36        self.assertFalse(a.is_active)
     37        self.assertFalse(a.is_superuser)
     38        self.assertEquals(a.groups.all().count(), 0)
     39        self.assertEquals(a.user_permissions.all().count(), 0)
     40       
     41    def test_superuser(self):
     42        super = User.objects.create_superuser('super', 'super@example.com', 'super')
     43        self.assertTrue(super.is_superuser)
     44        self.assertTrue(super.is_active)
     45        self.assertTrue(super.is_staff)
    4346
    44 # superuser tests.
    45 >>> super = User.objects.create_superuser('super', 'super@example.com', 'super')
    46 >>> super.is_superuser
    47 True
    48 >>> super.is_active
    49 True
    50 >>> super.is_staff
    51 True
     47    def test_createsuperuser_management_command(self):
     48        new_io = StringIO()
     49        call_command("createsuperuser", interactive=False, username="joe", email="joe@somewhere.org", stdout=new_io)
     50        command_output = new_io.getvalue().strip()
     51        self.assertEqual(command_output, 'Superuser created successfully.')
     52        u = User.objects.get(username="joe")
     53        self.assertEquals(u.email, 'joe@somewhere.org')
     54        self.assertTrue(u.check_password(''))
     55        new_io = StringIO()
     56        call_command("createsuperuser", interactive=False, username="joe2", email="joe2@somewhere.org", verbosity=0, stdout=new_io)
     57        command_output = new_io.getvalue().strip()
     58        self.assertEqual(command_output, '')
     59        new_io = StringIO()
     60        call_command("createsuperuser", interactive=False, username="joe+admin@somewhere.org", email="joe@somewhere.org", stdout=new_io)
     61        u = User.objects.get(username="joe+admin@somewhere.org")
     62        self.assertEquals(u.email, 'joe@somewhere.org')
     63        self.assertTrue(u.check_password(''))
    5264
    53 #
    54 # Tests for createsuperuser management command.
    55 # It's nearly impossible to test the interactive mode -- a command test helper
    56 # would be needed (and *awesome*) -- so just test the non-interactive mode.
    57 # This covers most of the important validation, but not all.
    58 #
    59 >>> from django.core.management import call_command
    60 
    61 >>> call_command("createsuperuser", interactive=False, username="joe", email="joe@somewhere.org")
    62 Superuser created successfully.
    63 
    64 >>> u = User.objects.get(username="joe")
    65 >>> u.email
    66 u'joe@somewhere.org'
    67 >>> u.password
    68 u'!'
    69 >>> call_command("createsuperuser", interactive=False, username="joe+admin@somewhere.org", email="joe@somewhere.org")
    70 Superuser created successfully.
    71 
    72 >>> u = User.objects.get(username="joe+admin@somewhere.org")
    73 >>> u.email
    74 u'joe@somewhere.org'
    75 >>> u.password
    76 u'!'
    77 """
  • django/contrib/auth/models.py

     
    106106        """
    107107        Creates and saves a User with the given username, e-mail and password.
    108108        """
    109 
    110109        now = datetime.datetime.now()
    111110       
    112111        # Normalize the address by lowercasing the domain part of the email
     
    122121                         is_active=True, is_superuser=False, last_login=now,
    123122                         date_joined=now)
    124123
    125         if password:
    126             user.set_password(password)
    127         else:
    128             user.set_unusable_password()
     124        user.set_password(password)
    129125        user.save(using=self._db)
    130126        return user
    131127
     
    238234        return full_name.strip()
    239235
    240236    def set_password(self, raw_password):
    241         import random
    242         algo = 'sha1'
    243         salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5]
    244         hsh = get_hexdigest(algo, salt, raw_password)
    245         self.password = '%s$%s$%s' % (algo, salt, hsh)
     237        if raw_password is None:
     238            self.set_unusable_password()
     239        else:
     240            import random
     241            algo = 'sha1'
     242            salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5]
     243            hsh = get_hexdigest(algo, salt, raw_password)
     244            self.password = '%s$%s$%s' % (algo, salt, hsh)
    246245
    247246    def check_password(self, raw_password):
    248247        """
     
    265264        self.password = UNUSABLE_PASSWORD
    266265
    267266    def has_usable_password(self):
    268         return self.password != UNUSABLE_PASSWORD
     267        if self.password is None \
     268            or self.password == UNUSABLE_PASSWORD:
     269            return False
     270        else:
     271            return True
    269272
    270273    def get_group_permissions(self, obj=None):
    271274        """
  • django/contrib/auth/management/commands/createsuperuser.py

     
    4141        username = options.get('username', None)
    4242        email = options.get('email', None)
    4343        interactive = options.get('interactive')
     44        verbosity = int(options.get('verbosity', 1))
    4445       
    4546        # Do quick and dirty validation if --noinput
    4647        if not interactive:
     
    132133                sys.exit(1)
    133134       
    134135        User.objects.create_superuser(username, email, password)
    135         print "Superuser created successfully."
     136        if verbosity >= 1:
     137          self.stdout.write("Superuser created successfully.")
     138
Back to Top