Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
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

@awsum

No description provided.

@mjtamlyn
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.

@awsum

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
@mjtamlyn 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
@awsum

@mjtamlyn thanks!

@mjtamlyn
Collaborator

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

@mjtamlyn
Collaborator

This patch will now need updating for custom user models.

@apollo13
Owner

Superseeded by #731.

@apollo13 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. @awsum
  2. @awsum

    Add tests.

    awsum authored
Commits on Sep 7, 2012
  1. @awsum
  2. @awsum

    Remove unneeded session.delete.

    awsum authored
  3. @awsum

    remove unused import

    awsum authored
This page is out of date. Refresh to see the latest.
View
20 django/test/client.py
@@ -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."
View
54 tests/regressiontests/test_client_regress/tests.py
@@ -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.