Code

Ticket #14354: auth.2.diff

File auth.2.diff, 8.4 KB (added by laurentluce, 4 years ago)
Line 
1Index: django/contrib/auth/tests/__init__.py
2===================================================================
3--- django/contrib/auth/tests/__init__.py       (revision 13984)
4+++ django/contrib/auth/tests/__init__.py       (working copy)
5@@ -1,5 +1,5 @@
6 from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest
7-from django.contrib.auth.tests.basic import BASIC_TESTS
8+from django.contrib.auth.tests.basic import BasicTestCase
9 from django.contrib.auth.tests.decorators import LoginRequiredTestCase
10 from django.contrib.auth.tests.forms import UserCreationFormTest, AuthenticationFormTest, SetPasswordFormTest, PasswordChangeFormTest, UserChangeFormTest, PasswordResetFormTest
11 from django.contrib.auth.tests.remote_user \
12@@ -12,6 +12,5 @@
13 # The password for the fixture data users is 'password'
14 
15 __test__ = {
16-    'BASIC_TESTS': BASIC_TESTS,
17     'TOKEN_GENERATOR_TESTS': TOKEN_GENERATOR_TESTS,
18 }
19Index: django/contrib/auth/tests/basic.py
20===================================================================
21--- django/contrib/auth/tests/basic.py  (revision 13984)
22+++ django/contrib/auth/tests/basic.py  (working copy)
23@@ -1,77 +1,62 @@
24+from django.test import TestCase
25+from django.contrib.auth.models import User, AnonymousUser
26+from django.core.management import call_command
27+from StringIO import StringIO
28 
29-BASIC_TESTS = """
30->>> from django.contrib.auth.models import User, AnonymousUser
31->>> u = User.objects.create_user('testuser', 'test@example.com', 'testpw')
32->>> u.has_usable_password()
33-True
34->>> u.check_password('bad')
35-False
36->>> u.check_password('testpw')
37-True
38->>> u.set_unusable_password()
39->>> u.save()
40->>> u.check_password('testpw')
41-False
42->>> u.has_usable_password()
43-False
44->>> u2 = User.objects.create_user('testuser2', 'test2@example.com')
45->>> u2.has_usable_password()
46-False
47+class BasicTestCase(TestCase):
48+    """
49+    Basic test case class for all basic test cases.
50+    """
51 
52->>> u.is_authenticated()
53-True
54->>> u.is_staff
55-False
56->>> u.is_active
57-True
58->>> u.is_superuser
59-False
60+    def test_user(self):
61+        u = User.objects.create_user('testuser', 'test@example.com', 'testpw')
62+        self.assertTrue(u.has_usable_password())
63+        self.assertFalse(u.check_password('bad'))
64+        self.assertTrue(u.check_password('testpw'))
65+        u.set_unusable_password()
66+        u.save()
67+        self.assertFalse(u.check_password('testpw'))
68+        self.assertFalse(u.has_usable_password())
69+        u.set_password(None)
70+        self.assertFalse(u.has_usable_password())
71+        self.assertTrue(u.is_authenticated())
72+        self.assertFalse(u.is_staff)
73+        self.assertTrue(u.is_active)
74+        self.assertFalse(u.is_superuser)
75+       
76+        u2 = User.objects.create_user('testuser2', 'test2@example.com')
77+        self.assertFalse(u.has_usable_password())
78 
79->>> a = AnonymousUser()
80->>> a.is_authenticated()
81-False
82->>> a.is_staff
83-False
84->>> a.is_active
85-False
86->>> a.is_superuser
87-False
88->>> a.groups.all()
89-[]
90->>> a.user_permissions.all()
91-[]
92+    def test_anonymous_user(self):
93+        a = AnonymousUser()
94+        self.assertFalse(a.is_authenticated())
95+        self.assertFalse(a.is_staff)
96+        self.assertFalse(a.is_active)
97+        self.assertFalse(a.is_superuser)
98+        self.assertEquals(a.groups.all().count(), 0)
99+        self.assertEquals(a.user_permissions.all().count(), 0)
100+       
101+    def test_superuser(self):
102+        super = User.objects.create_superuser('super', 'super@example.com', 'super')
103+        self.assertTrue(super.is_superuser)
104+        self.assertTrue(super.is_active)
105+        self.assertTrue(super.is_staff)
106 
107-# superuser tests.
108->>> super = User.objects.create_superuser('super', 'super@example.com', 'super')
109->>> super.is_superuser
110-True
111->>> super.is_active
112-True
113->>> super.is_staff
114-True
115+    def test_createsuperuser_management_command(self):
116+        new_io = StringIO()
117+        call_command("createsuperuser", interactive=False, username="joe", email="joe@somewhere.org", stdout=new_io)
118+        command_output = new_io.getvalue().strip()
119+        self.assertEqual(command_output, 'Superuser created successfully.')
120+        u = User.objects.get(username="joe")
121+        self.assertEquals(u.email, 'joe@somewhere.org')
122+        self.assertTrue(u.check_password(''))
123+        new_io = StringIO()
124+        call_command("createsuperuser", interactive=False, username="joe2", email="joe2@somewhere.org", verbosity=0, stdout=new_io)
125+        command_output = new_io.getvalue().strip()
126+        self.assertEqual(command_output, '')
127+        new_io = StringIO()
128+        call_command("createsuperuser", interactive=False, username="joe+admin@somewhere.org", email="joe@somewhere.org", stdout=new_io)
129+        u = User.objects.get(username="joe+admin@somewhere.org")
130+        self.assertEquals(u.email, 'joe@somewhere.org')
131+        self.assertTrue(u.check_password(''))
132 
133-#
134-# Tests for createsuperuser management command.
135-# It's nearly impossible to test the interactive mode -- a command test helper
136-# would be needed (and *awesome*) -- so just test the non-interactive mode.
137-# This covers most of the important validation, but not all.
138-#
139->>> from django.core.management import call_command
140-
141->>> call_command("createsuperuser", interactive=False, username="joe", email="joe@somewhere.org")
142-Superuser created successfully.
143-
144->>> u = User.objects.get(username="joe")
145->>> u.email
146-u'joe@somewhere.org'
147->>> u.password
148-u'!'
149->>> call_command("createsuperuser", interactive=False, username="joe+admin@somewhere.org", email="joe@somewhere.org")
150-Superuser created successfully.
151-
152->>> u = User.objects.get(username="joe+admin@somewhere.org")
153->>> u.email
154-u'joe@somewhere.org'
155->>> u.password
156-u'!'
157-"""
158Index: django/contrib/auth/models.py
159===================================================================
160--- django/contrib/auth/models.py       (revision 13984)
161+++ django/contrib/auth/models.py       (working copy)
162@@ -106,7 +106,6 @@
163         """
164         Creates and saves a User with the given username, e-mail and password.
165         """
166-
167         now = datetime.datetime.now()
168         
169         # Normalize the address by lowercasing the domain part of the email
170@@ -122,10 +121,7 @@
171                          is_active=True, is_superuser=False, last_login=now,
172                          date_joined=now)
173 
174-        if password:
175-            user.set_password(password)
176-        else:
177-            user.set_unusable_password()
178+        user.set_password(password)
179         user.save(using=self._db)
180         return user
181 
182@@ -238,11 +234,14 @@
183         return full_name.strip()
184 
185     def set_password(self, raw_password):
186-        import random
187-        algo = 'sha1'
188-        salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5]
189-        hsh = get_hexdigest(algo, salt, raw_password)
190-        self.password = '%s$%s$%s' % (algo, salt, hsh)
191+        if raw_password is None:
192+            self.set_unusable_password()
193+        else:
194+            import random
195+            algo = 'sha1'
196+            salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5]
197+            hsh = get_hexdigest(algo, salt, raw_password)
198+            self.password = '%s$%s$%s' % (algo, salt, hsh)
199 
200     def check_password(self, raw_password):
201         """
202@@ -265,7 +264,11 @@
203         self.password = UNUSABLE_PASSWORD
204 
205     def has_usable_password(self):
206-        return self.password != UNUSABLE_PASSWORD
207+        if self.password is None \
208+            or self.password == UNUSABLE_PASSWORD:
209+            return False
210+        else:
211+            return True
212 
213     def get_group_permissions(self, obj=None):
214         """
215Index: django/contrib/auth/management/commands/createsuperuser.py
216===================================================================
217--- django/contrib/auth/management/commands/createsuperuser.py  (revision 13984)
218+++ django/contrib/auth/management/commands/createsuperuser.py  (working copy)
219@@ -41,6 +41,7 @@
220         username = options.get('username', None)
221         email = options.get('email', None)
222         interactive = options.get('interactive')
223+        verbosity = int(options.get('verbosity', 1))
224         
225         # Do quick and dirty validation if --noinput
226         if not interactive:
227@@ -132,4 +133,6 @@
228                 sys.exit(1)
229         
230         User.objects.create_superuser(username, email, password)
231-        print "Superuser created successfully."
232+        if verbosity >= 1:
233+          self.stdout.write("Superuser created successfully.")
234+