diff options
| author | Paulo Poiati <paulogpoiati@gmail.com> | 2015-07-05 17:54:25 -0300 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-01-07 08:56:07 -0500 |
| commit | b6433866682baac35a953e59298dff7f399ac49b (patch) | |
| tree | b0a6a9ce9edd6099b1b79744c5087b5783d994ff /tests/auth_tests | |
| parent | bd3c2900fc0e6863924d3b27f0713aa3022c061d (diff) | |
Fixed #24855 -- Allowed using contrib.auth.login() without credentials.
Added an optional `backend` argument to login().
Diffstat (limited to 'tests/auth_tests')
| -rw-r--r-- | tests/auth_tests/test_auth_backends.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/auth_tests/test_auth_backends.py b/tests/auth_tests/test_auth_backends.py index 6ded9f2eb8..c0ccbb3971 100644 --- a/tests/auth_tests/test_auth_backends.py +++ b/tests/auth_tests/test_auth_backends.py @@ -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) |
