From 183e9d20294dec28df8ae5ea8ed7acc712adcd2b Mon Sep 17 00:00:00 2001 From: Xavier Fernandez Date: Fri, 6 Jun 2014 11:19:16 +0200 Subject: [PATCH] [1.7.x] Fixed #22771 -- Fixed test.Client.logout when using custom auth backend. Backport of 50b9313e0a from master --- django/test/client.py | 7 ++----- tests/test_client_regress/auth_backends.py | 20 ++++++++++++++++++++ tests/test_client_regress/tests.py | 19 +++++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 tests/test_client_regress/auth_backends.py diff --git a/django/test/client.py b/django/test/client.py index 9513dcd809..22c7b18b07 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -587,16 +587,13 @@ class Client(RequestFactory): Causes the authenticated user to be logged out. """ - from django.contrib.auth import get_user_model, logout + from django.contrib.auth import get_user, logout request = HttpRequest() engine = import_module(settings.SESSION_ENGINE) - UserModel = get_user_model() if self.session: request.session = self.session - uid = self.session.get("_auth_user_id") - if uid: - request.user = UserModel._default_manager.get(pk=uid) + request.user = get_user(request) else: request.session = engine.SessionStore() logout(request) diff --git a/tests/test_client_regress/auth_backends.py b/tests/test_client_regress/auth_backends.py new file mode 100644 index 0000000000..02a6d39611 --- /dev/null +++ b/tests/test_client_regress/auth_backends.py @@ -0,0 +1,20 @@ +from django.contrib.auth.backends import ModelBackend + +from .models import CustomUser + + +class CustomUserBackend(ModelBackend): + + def authenticate(self, username=None, password=None): + try: + user = CustomUser.custom_objects.get_by_natural_key(username) + if user.check_password(password): + return user + except CustomUser.DoesNotExist: + return None + + def get_user(self, user_id): + try: + return CustomUser.custom_objects.get(pk=user_id) + except CustomUser.DoesNotExist: + return None diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py index 996f70b667..604b07624f 100644 --- a/tests/test_client_regress/tests.py +++ b/tests/test_client_regress/tests.py @@ -1052,6 +1052,25 @@ class SessionTests(TestCase): user_logged_out.disconnect(listener) self.assertTrue(listener.executed) + @override_settings(AUTHENTICATION_BACKENDS=( + 'django.contrib.auth.backends.ModelBackend', + 'test_client_regress.auth_backends.CustomUserBackend')) + def test_logout_with_custom_auth_backend(self): + "Request a logout after logging in with custom authentication backend" + def listener(*args, **kwargs): + self.assertEqual(kwargs['sender'], CustomUser) + listener.executed = True + listener.executed = False + u = CustomUser.custom_objects.create(email='test@test.com') + u.set_password('password') + u.save() + + user_logged_out.connect(listener) + self.client.login(username='test@test.com', password='password') + self.client.logout() + user_logged_out.disconnect(listener) + self.assertTrue(listener.executed) + def test_logout_without_user(self): """Logout should send signal even if user not authenticated.""" def listener(user, *args, **kwargs):