Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #14354 -- Normalized the handling of empty/null passwords in co…
…ntrib.auth. This also updates the createsuperuser command to be more testable, and migrates some auth doctests. Thanks to berryp for the report, and Laurent Luce for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14053 bcc190cf-cafb-0310-a4f2-bffc1f526a37
- Loading branch information
1 parent
71a4c47
commit 8755fb1
Showing
5 changed files
with
109 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,77 +1,92 @@ | |||
from django.test import TestCase | |||
from django.contrib.auth.models import User, AnonymousUser | |||
from django.core.management import call_command | |||
from StringIO import StringIO | |||
|
|
||
BASIC_TESTS = """ | class BasicTestCase(TestCase): | ||
>>> from django.contrib.auth.models import User, AnonymousUser | def test_user(self): | ||
>>> u = User.objects.create_user('testuser', 'test@example.com', 'testpw') | "Check that users can be created and can set their password" | ||
>>> u.has_usable_password() | u = User.objects.create_user('testuser', 'test@example.com', 'testpw') | ||
True | self.assertTrue(u.has_usable_password()) | ||
>>> u.check_password('bad') | self.assertFalse(u.check_password('bad')) | ||
False | self.assertTrue(u.check_password('testpw')) | ||
>>> u.check_password('testpw') | |||
True | |||
>>> u.set_unusable_password() | |||
>>> u.save() | |||
>>> u.check_password('testpw') | |||
False | |||
>>> u.has_usable_password() | |||
False | |||
>>> u2 = User.objects.create_user('testuser2', 'test2@example.com') | |||
>>> u2.has_usable_password() | |||
False | |||
|
|
||
>>> u.is_authenticated() | # Check we can manually set an unusable password | ||
True | u.set_unusable_password() | ||
>>> u.is_staff | u.save() | ||
False | self.assertFalse(u.check_password('testpw')) | ||
>>> u.is_active | self.assertFalse(u.has_usable_password()) | ||
True | u.set_password('testpw') | ||
>>> u.is_superuser | self.assertTrue(u.check_password('testpw')) | ||
False | u.set_password(None) | ||
self.assertFalse(u.has_usable_password()) | |||
|
|
||
>>> a = AnonymousUser() | # Check authentication/permissions | ||
>>> a.is_authenticated() | self.assertTrue(u.is_authenticated()) | ||
False | self.assertFalse(u.is_staff) | ||
>>> a.is_staff | self.assertTrue(u.is_active) | ||
False | self.assertFalse(u.is_superuser) | ||
>>> a.is_active | |||
False | |||
>>> a.is_superuser | |||
False | |||
>>> a.groups.all() | |||
[] | |||
>>> a.user_permissions.all() | |||
[] | |||
|
|
||
# superuser tests. | # Check API-based user creation with no password | ||
>>> super = User.objects.create_superuser('super', 'super@example.com', 'super') | u2 = User.objects.create_user('testuser2', 'test2@example.com') | ||
>>> super.is_superuser | self.assertFalse(u.has_usable_password()) | ||
True | |||
>>> super.is_active | |||
True | |||
>>> super.is_staff | |||
True | |||
|
|
||
# | def test_anonymous_user(self): | ||
# Tests for createsuperuser management command. | "Check the properties of the anonymous user" | ||
# It's nearly impossible to test the interactive mode -- a command test helper | a = AnonymousUser() | ||
# would be needed (and *awesome*) -- so just test the non-interactive mode. | self.assertFalse(a.is_authenticated()) | ||
# This covers most of the important validation, but not all. | self.assertFalse(a.is_staff) | ||
# | self.assertFalse(a.is_active) | ||
>>> from django.core.management import call_command | self.assertFalse(a.is_superuser) | ||
self.assertEqual(a.groups.all().count(), 0) | |||
self.assertEqual(a.user_permissions.all().count(), 0) | |||
|
|
||
>>> call_command("createsuperuser", interactive=False, username="joe", email="joe@somewhere.org") | def test_superuser(self): | ||
Superuser created successfully. | "Check the creation and properties of a superuser" | ||
super = User.objects.create_superuser('super', 'super@example.com', 'super') | |||
self.assertTrue(super.is_superuser) | |||
self.assertTrue(super.is_active) | |||
self.assertTrue(super.is_staff) | |||
|
|
||
>>> u = User.objects.get(username="joe") | def test_createsuperuser_management_command(self): | ||
>>> u.email | "Check the operation of the createsuperuser management command" | ||
u'joe@somewhere.org' | # We can use the management command to create a superuser | ||
>>> u.password | new_io = StringIO() | ||
u'!' | call_command("createsuperuser", | ||
>>> call_command("createsuperuser", interactive=False, username="joe+admin@somewhere.org", email="joe@somewhere.org") | interactive=False, | ||
Superuser created successfully. | username="joe", | ||
email="joe@somewhere.org", | |||
stdout=new_io | |||
) | |||
command_output = new_io.getvalue().strip() | |||
self.assertEqual(command_output, 'Superuser created successfully.') | |||
u = User.objects.get(username="joe") | |||
self.assertEquals(u.email, 'joe@somewhere.org') | |||
self.assertTrue(u.check_password('')) | |||
|
|||
# We can supress output on the management command | |||
new_io = StringIO() | |||
call_command("createsuperuser", | |||
interactive=False, | |||
username="joe2", | |||
email="joe2@somewhere.org", | |||
verbosity=0, | |||
stdout=new_io | |||
) | |||
command_output = new_io.getvalue().strip() | |||
self.assertEqual(command_output, '') | |||
u = User.objects.get(username="joe2") | |||
self.assertEquals(u.email, 'joe2@somewhere.org') | |||
self.assertTrue(u.check_password('')) | |||
|
|||
new_io = StringIO() | |||
call_command("createsuperuser", | |||
interactive=False, | |||
username="joe+admin@somewhere.org", | |||
email="joe@somewhere.org", | |||
stdout=new_io | |||
) | |||
u = User.objects.get(username="joe+admin@somewhere.org") | |||
self.assertEquals(u.email, 'joe@somewhere.org') | |||
self.assertTrue(u.check_password('')) | |||
|
|
||
>>> u = User.objects.get(username="joe+admin@somewhere.org") | |||
>>> u.email | |||
u'joe@somewhere.org' | |||
>>> u.password | |||
u'!' | |||
""" |