Ticket #14354: auth.diff

File auth.diff, 6.9 KB (added by laurentluce, 5 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
    14
    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
     5class BasicTestCase(TestCase):
     6    """
     7    Basic test case class for all basic test cases.
     8    """
    209
    21 >>> u.is_authenticated()
    22 True
    23 >>> u.is_staff
    24 False
    25 >>> u.is_active
    26 True
    27 >>> u.is_superuser
    28 False
     10    def setUp(self):
     11        pass
     12   
     13    def tearDown(self):
     14        pass
    2915
    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 []
     16    def test_user(self):
     17        u = User.objects.create_user('testuser', 'test@example.com', 'testpw')
     18        self.assertTrue(u.has_usable_password())
     19        self.assertFalse(u.check_password('bad'))
     20        self.assertTrue(u.check_password('testpw'))
     21        u.set_unusable_password()
     22        u.save()
     23        self.assertFalse(u.check_password('testpw'))
     24        self.assertFalse(u.has_usable_password())
     25        u.set_password(None)
     26        self.assertFalse(u.has_usable_password())
     27        self.assertTrue(u.is_authenticated())
     28        self.assertFalse(u.is_staff)
     29        self.assertTrue(u.is_active)
     30        self.assertFalse(u.is_superuser)
     31       
     32        u2 = User.objects.create_user('testuser2', 'test2@example.com')
     33        self.assertFalse(u.has_usable_password())
    4334
    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
     35    def test_anonymous_user(self):
     36        a = AnonymousUser()
     37        self.assertFalse(a.is_authenticated())
     38        self.assertFalse(a.is_staff)
     39        self.assertFalse(a.is_active)
     40        self.assertFalse(a.is_superuser)
     41        self.assertEquals(a.groups.all().count(), 0)
     42        self.assertEquals(a.user_permissions.all().count(), 0)
     43       
     44    def test_superuser(self):
     45        super = User.objects.create_superuser('super', 'super@example.com', 'super')
     46        self.assertTrue(super.is_superuser)
     47        self.assertTrue(super.is_active)
     48        self.assertTrue(super.is_staff)
    5249
    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
     50    def test_createsuperuser_management_command(self):
     51        call_command("createsuperuser", interactive=False, username="joe", email="joe@somewhere.org")
     52        u = User.objects.get(username="joe")
     53        self.assertEquals(u.email, 'joe@somewhere.org')
     54        self.assertEquals(u.password, '!')
     55        call_command("createsuperuser", interactive=False, username="joe+admin@somewhere.org", email="joe@somewhere.org")
     56        u = User.objects.get(username="joe+admin@somewhere.org")
     57        self.assertEquals(u.email, 'joe@somewhere.org')
     58        self.assertEquals(u.password, '!')
    6059
    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

     
    122122                         is_active=True, is_superuser=False, last_login=now,
    123123                         date_joined=now)
    124124
    125         if password:
    126             user.set_password(password)
    127         else:
    128             user.set_unusable_password()
     125        user.set_password(password)
    129126        user.save(using=self._db)
    130127        return user
    131128
     
    238235        return full_name.strip()
    239236
    240237    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)
     238        if not raw_password:
     239            self.set_unusable_password()
     240        else:
     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)
    246246
    247247    def check_password(self, raw_password):
    248248        """
     
    265265        self.password = UNUSABLE_PASSWORD
    266266
    267267    def has_usable_password(self):
    268         return self.password != UNUSABLE_PASSWORD
     268        if not self.password \
     269            or self.password == UNUSABLE_PASSWORD:
     270            return False
     271        else:
     272            return True
    269273
    270274    def get_group_permissions(self, obj=None):
    271275        """
Back to Top