mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Fixed #24855 -- Allowed using contrib.auth.login() without credentials.
Added an optional `backend` argument to login().
This commit is contained in:
@@ -605,6 +605,14 @@ class ImportedModelBackend(ModelBackend):
|
||||
pass
|
||||
|
||||
|
||||
class CustomModelBackend(ModelBackend):
|
||||
pass
|
||||
|
||||
|
||||
class OtherModelBackend(ModelBackend):
|
||||
pass
|
||||
|
||||
|
||||
class ImportedBackendTests(TestCase):
|
||||
"""
|
||||
#23925 - The backend path added to the session should be the same
|
||||
@@ -622,3 +630,38 @@ class ImportedBackendTests(TestCase):
|
||||
request = HttpRequest()
|
||||
request.session = self.client.session
|
||||
self.assertEqual(request.session[BACKEND_SESSION_KEY], self.backend)
|
||||
|
||||
|
||||
class SelectingBackendTests(TestCase):
|
||||
backend = 'auth_tests.test_auth_backends.CustomModelBackend'
|
||||
other_backend = 'auth_tests.test_auth_backends.OtherModelBackend'
|
||||
username = 'username'
|
||||
password = 'password'
|
||||
|
||||
def assertBackendInSession(self, backend):
|
||||
request = HttpRequest()
|
||||
request.session = self.client.session
|
||||
self.assertEqual(request.session[BACKEND_SESSION_KEY], backend)
|
||||
|
||||
@override_settings(AUTHENTICATION_BACKENDS=[backend])
|
||||
def test_backend_path_login_without_authenticate_single_backend(self):
|
||||
user = User.objects.create_user(self.username, 'email', self.password)
|
||||
self.client._login(user)
|
||||
self.assertBackendInSession(self.backend)
|
||||
|
||||
@override_settings(AUTHENTICATION_BACKENDS=[backend, other_backend])
|
||||
def test_backend_path_login_without_authenticate_multiple_backends(self):
|
||||
user = User.objects.create_user(self.username, 'email', self.password)
|
||||
expected_message = (
|
||||
'You have multiple authentication backends configured and '
|
||||
'therefore must provide the `backend` argument or set the '
|
||||
'`backend` attribute on the user.'
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, expected_message):
|
||||
self.client._login(user)
|
||||
|
||||
@override_settings(AUTHENTICATION_BACKENDS=[backend, other_backend])
|
||||
def test_backend_path_login_with_explicit_backends(self):
|
||||
user = User.objects.create_user(self.username, 'email', self.password)
|
||||
self.client._login(user, self.other_backend)
|
||||
self.assertBackendInSession(self.other_backend)
|
||||
|
||||
Reference in New Issue
Block a user