Improved test coverage for django.contrib.auth.
This commit is contained in:
parent
fede65260a
commit
7588d7e439
|
@ -583,20 +583,33 @@ class TypeErrorBackend:
|
|||
raise TypeError
|
||||
|
||||
|
||||
class TypeErrorBackendTest(TestCase):
|
||||
"""
|
||||
A TypeError within a backend is propagated properly (#18171).
|
||||
"""
|
||||
backend = 'auth_tests.test_auth_backends.TypeErrorBackend'
|
||||
class SkippedBackend:
|
||||
def authenticate(self):
|
||||
# Doesn't accept any credentials so is skipped by authenticate().
|
||||
pass
|
||||
|
||||
|
||||
class AuthenticateTests(TestCase):
|
||||
def setUp(self):
|
||||
self.user1 = User.objects.create_user('test', 'test@example.com', 'test')
|
||||
|
||||
@override_settings(AUTHENTICATION_BACKENDS=[backend])
|
||||
@override_settings(AUTHENTICATION_BACKENDS=['auth_tests.test_auth_backends.TypeErrorBackend'])
|
||||
def test_type_error_raised(self):
|
||||
"""A TypeError within a backend is propagated properly (#18171)."""
|
||||
with self.assertRaises(TypeError):
|
||||
authenticate(username='test', password='test')
|
||||
|
||||
@override_settings(AUTHENTICATION_BACKENDS=(
|
||||
'auth_tests.test_auth_backends.SkippedBackend',
|
||||
'django.contrib.auth.backends.ModelBackend',
|
||||
))
|
||||
def test_skips_backends_without_arguments(self):
|
||||
"""
|
||||
A backend (SkippedBackend) is ignored if it doesn't accept the
|
||||
credentials as arguments.
|
||||
"""
|
||||
self.assertEqual(authenticate(username='test', password='test'), self.user1)
|
||||
|
||||
|
||||
class ImproperlyConfiguredUserModelTest(TestCase):
|
||||
"""
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import builtins
|
||||
import getpass
|
||||
import sys
|
||||
from datetime import date
|
||||
from io import StringIO
|
||||
|
@ -110,6 +111,28 @@ class ChangepasswordManagementCommandTestCase(TestCase):
|
|||
self.stdout.close()
|
||||
self.stderr.close()
|
||||
|
||||
@mock.patch.object(getpass, 'getpass', return_value='password')
|
||||
def test_get_pass(self, mock_get_pass):
|
||||
call_command('changepassword', username='joe', stdout=self.stdout)
|
||||
self.assertIs(User.objects.get(username='joe').check_password('password'), True)
|
||||
|
||||
@mock.patch.object(getpass, 'getpass', return_value='')
|
||||
def test_get_pass_no_input(self, mock_get_pass):
|
||||
with self.assertRaisesMessage(CommandError, 'aborted'):
|
||||
call_command('changepassword', username='joe', stdout=self.stdout)
|
||||
|
||||
@mock.patch.object(changepassword.Command, '_get_pass', return_value='new_password')
|
||||
def test_system_username(self, mock_get_pass):
|
||||
"""The system username is used if --username isn't provided."""
|
||||
username = getpass.getuser()
|
||||
User.objects.create_user(username=username, password='qwerty')
|
||||
call_command('changepassword', stdout=self.stdout)
|
||||
self.assertIs(User.objects.get(username=username).check_password('new_password'), True)
|
||||
|
||||
def test_nonexistent_username(self):
|
||||
with self.assertRaisesMessage(CommandError, "user 'test' does not exist"):
|
||||
call_command('changepassword', username='test', stdout=self.stdout)
|
||||
|
||||
@mock.patch.object(changepassword.Command, '_get_pass', return_value='not qwerty')
|
||||
def test_that_changepassword_command_changes_joes_password(self, mock_get_pass):
|
||||
"Executing the changepassword management command should change joe's password"
|
||||
|
@ -183,6 +206,11 @@ class MultiDBChangepasswordManagementCommandTestCase(TestCase):
|
|||
)
|
||||
class CreatesuperuserManagementCommandTestCase(TestCase):
|
||||
|
||||
def test_no_email_argument(self):
|
||||
new_io = StringIO()
|
||||
with self.assertRaisesMessage(CommandError, 'You must use --email with --noinput.'):
|
||||
call_command('createsuperuser', interactive=False, username='joe', stdout=new_io)
|
||||
|
||||
def test_basic_usage(self):
|
||||
"Check the operation of the createsuperuser management command"
|
||||
# We can use the management command to create a superuser
|
||||
|
|
|
@ -147,6 +147,13 @@ class UserManagerTestCase(TestCase):
|
|||
password='test', is_staff=False,
|
||||
)
|
||||
|
||||
def test_make_random_password(self):
|
||||
allowed_chars = 'abcdefg'
|
||||
password = UserManager().make_random_password(5, allowed_chars)
|
||||
self.assertEqual(len(password), 5)
|
||||
for char in password:
|
||||
self.assertIn(char, allowed_chars)
|
||||
|
||||
|
||||
class AbstractBaseUserTests(TestCase):
|
||||
|
||||
|
|
Loading…
Reference in New Issue