From fc8f097117af7ada616fad20ae5b417fcf740413 Mon Sep 17 00:00:00 2001 From: Ben Demboski Date: Fri, 5 Aug 2016 17:18:12 -0700 Subject: [PATCH] Fixed #27027 -- Restored Client.force_login() defaulting to the first auth backend. --- django/test/client.py | 3 +++ docs/releases/1.10.1.txt | 4 ++++ tests/test_client/tests.py | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/django/test/client.py b/django/test/client.py index dc813716d6..2b5840b49e 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -626,6 +626,9 @@ class Client(RequestFactory): return False def force_login(self, user, backend=None): + if backend is None: + backend = settings.AUTHENTICATION_BACKENDS[0] + user.backend = backend self._login(user, backend) def _login(self, user, backend=None): diff --git a/docs/releases/1.10.1.txt b/docs/releases/1.10.1.txt index 89fc2b2b19..2950c06042 100644 --- a/docs/releases/1.10.1.txt +++ b/docs/releases/1.10.1.txt @@ -33,3 +33,7 @@ Bugfixes * Prevented the ``migrate`` command from raising ``InconsistentMigrationHistory`` in the presence of unapplied squashed migrations (:ticket:`27004`). + +* Fixed a regression in ``Client.force_login()`` which required specifying a + ``backend`` rather than automatically using the first one if multiple + backends are configured (:ticket:`27027`). diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py index 497df1591c..d017fa42b1 100644 --- a/tests/test_client/tests.py +++ b/tests/test_client/tests.py @@ -532,12 +532,30 @@ class ClientTest(TestCase): # Log in self.client.force_login(self.u1, backend='test_client.auth_backends.TestClientBackend') + self.assertEqual(self.u1.backend, 'test_client.auth_backends.TestClientBackend') # Request a page that requires a login response = self.client.get('/login_protected_view/') self.assertEqual(response.status_code, 200) self.assertEqual(response.context['user'].username, 'testclient') + @override_settings( + AUTHENTICATION_BACKENDS=[ + 'django.contrib.auth.backends.ModelBackend', + 'test_client.auth_backends.TestClientBackend', + ], + ) + def test_force_login_without_backend(self): + """ + force_login() without passing a backend and with multiple backends + configured should automatically use the first backend. + """ + self.client.force_login(self.u1) + response = self.client.get('/login_protected_view/') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context['user'].username, 'testclient') + self.assertEqual(self.u1.backend, 'django.contrib.auth.backends.ModelBackend') + @override_settings(SESSION_ENGINE="django.contrib.sessions.backends.signed_cookies") def test_logout_cookie_sessions(self): self.test_logout()