Improved test coverage for django.contrib.auth.

This commit is contained in:
Anton Samarchyan 2017-03-01 17:29:50 -05:00 committed by Tim Graham
parent fede65260a
commit 7588d7e439
3 changed files with 54 additions and 6 deletions

View File

@ -583,20 +583,33 @@ class TypeErrorBackend:
raise TypeError raise TypeError
class TypeErrorBackendTest(TestCase): class SkippedBackend:
""" def authenticate(self):
A TypeError within a backend is propagated properly (#18171). # Doesn't accept any credentials so is skipped by authenticate().
""" pass
backend = 'auth_tests.test_auth_backends.TypeErrorBackend'
class AuthenticateTests(TestCase):
def setUp(self): def setUp(self):
self.user1 = User.objects.create_user('test', 'test@example.com', 'test') 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): def test_type_error_raised(self):
"""A TypeError within a backend is propagated properly (#18171)."""
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
authenticate(username='test', password='test') 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): class ImproperlyConfiguredUserModelTest(TestCase):
""" """

View File

@ -1,4 +1,5 @@
import builtins import builtins
import getpass
import sys import sys
from datetime import date from datetime import date
from io import StringIO from io import StringIO
@ -110,6 +111,28 @@ class ChangepasswordManagementCommandTestCase(TestCase):
self.stdout.close() self.stdout.close()
self.stderr.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') @mock.patch.object(changepassword.Command, '_get_pass', return_value='not qwerty')
def test_that_changepassword_command_changes_joes_password(self, mock_get_pass): def test_that_changepassword_command_changes_joes_password(self, mock_get_pass):
"Executing the changepassword management command should change joe's password" "Executing the changepassword management command should change joe's password"
@ -183,6 +206,11 @@ class MultiDBChangepasswordManagementCommandTestCase(TestCase):
) )
class CreatesuperuserManagementCommandTestCase(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): def test_basic_usage(self):
"Check the operation of the createsuperuser management command" "Check the operation of the createsuperuser management command"
# We can use the management command to create a superuser # We can use the management command to create a superuser

View File

@ -147,6 +147,13 @@ class UserManagerTestCase(TestCase):
password='test', is_staff=False, 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): class AbstractBaseUserTests(TestCase):