Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Ticket #18924. Made test.Client.logout send user_logged_out signal. #322

Closed
wants to merge 5 commits into from

3 participants

Pavel Ponomarev Marc Tamlyn Florian Apolloner
Pavel Ponomarev

No description provided.

Marc Tamlyn
Collaborator

Does this have an associated ticket?

It seems to me it would be preferable to implement this in a similar way to the login method and call the django.contrib.auth.logout function. I think that will make the tests pass as you've written them, but will also keep the code base a bit cleaner.

Pavel Ponomarev

Created ticket #18924 for this.

Reusing django.contrib.auth.logout is indeed a good idea, added commit here. Also after looking at method closely found out that behaviour for not authorized User was incorrect, also cover that in last commit.

django/test/client.py
@@ -13,7 +13,8 @@
from urlparse import urlparse, urlsplit
from django.conf import settings
-from django.contrib.auth import authenticate, login
+from django.contrib.auth import authenticate, login, logout
+from django.contrib.auth.signals import user_logged_out
Marc Tamlyn Collaborator
mjtamlyn added a note

This line is now an unused import

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Pavel Ponomarev

@mjtamlyn thanks!

Marc Tamlyn
Collaborator

I made a little comment on the ticket about release notes as well, but I'll mark it as RFC.

Marc Tamlyn
Collaborator

This patch will now need updating for custom user models.

Florian Apolloner
Owner

Superseeded by #731.

Florian Apolloner apollo13 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 4, 2012
  1. Pavel Ponomarev
  2. Pavel Ponomarev

    Add tests.

    awsum authored
Commits on Sep 7, 2012
  1. Pavel Ponomarev
  2. Pavel Ponomarev

    Remove unneeded session.delete.

    awsum authored
  3. Pavel Ponomarev

    remove unused import

    awsum authored
This page is out of date. Refresh to see the latest.
20 django/test/client.py
View
@@ -13,7 +13,7 @@
from urlparse import urlparse, urlsplit
from django.conf import settings
-from django.contrib.auth import authenticate, login
+from django.contrib.auth import authenticate, login, logout
from django.core.handlers.base import BaseHandler
from django.core.handlers.wsgi import WSGIRequest
from django.core.signals import got_request_exception
@@ -519,12 +519,18 @@ def logout(self):
Causes the authenticated user to be logged out.
"""
- session = import_module(settings.SESSION_ENGINE).SessionStore()
- session_cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
- if session_cookie:
- session.delete(session_key=session_cookie.value)
- self.cookies = SimpleCookie()
-
+ from django.contrib.auth.models import User
+ request = HttpRequest()
+ engine = import_module(settings.SESSION_ENGINE)
+ if self.session:
+ request.session = self.session
+ uid = self.session.get("_auth_user_id")
+ if uid:
+ request.user = User.objects.get(pk=uid)
+ else:
+ request.session = engine.SessionStore()
+ logout(request)
+
def _handle_redirects(self, response, **extra):
"Follows any redirects by requesting responses from the server using GET."
54 tests/regressiontests/test_client_regress/tests.py
View
@@ -17,6 +17,7 @@
from django.test.utils import ContextList, override_settings, str_prefix
from django.template.response import SimpleTemplateResponse
from django.http import HttpResponse
+from django.contrib.auth.signals import user_logged_out, user_logged_in
@override_settings(
@@ -743,6 +744,59 @@ def test_logout(self):
self.client.logout()
self.client.logout()
+ def test_logout_with_user(self):
+ """Logout should send user_logged_out signal if user was logged in."""
+ def listener(*args, **kwargs):
+ listener.executed = True
+ listener.executed = False
+
+ user_logged_out.connect(listener)
+ self.client.login(username='testclient', 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):
+ listener.user = user
+ listener.executed = True
+ listener.executed = False
+
+ user_logged_out.connect(listener)
+ self.client.login(username='incorrect', password='password')
+ self.client.logout()
+ user_logged_out.disconnect(listener)
+
+ self.assertTrue(listener.executed)
+ self.assertIsNone(listener.user)
+
+ def test_login_with_user(self):
+ """Login should send user_logged_in signal on successful login."""
+ def listener(*args, **kwargs):
+ listener.executed = True
+ listener.executed = False
+
+ user_logged_in.connect(listener)
+ self.client.login(username='testclient', password='password')
+ user_logged_out.disconnect(listener)
+
+ self.assertTrue(listener.executed)
+
+ def test_login_without_signal(self):
+ """Login shouldn't send signal if user wasn't logged in"""
+ def listener(*args, **kwargs):
+ listener.executed = True
+ listener.executed = False
+
+ user_logged_in.connect(listener)
+ self.client.login(username='incorrect', password='password')
+ user_logged_in.disconnect(listener)
+
+ self.assertFalse(listener.executed)
+
+
class RequestMethodTests(TestCase):
def test_get(self):
"Request a view via request method GET"
Something went wrong with that request. Please try again.