Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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
Owner

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 @@
13 13
     from urlparse import urlparse, urlsplit
14 14
 
15 15
 from django.conf import settings
16  
-from django.contrib.auth import authenticate, login
  16
+from django.contrib.auth import authenticate, login, logout
  17
+from django.contrib.auth.signals import user_logged_out
1
Marc Tamlyn Owner

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
Owner

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

Marc Tamlyn
Owner

This patch will now need updating for custom user models.

Florian Apolloner
Owner

Superseeded by #731.

Florian Apolloner apollo13 closed this February 24, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
20  django/test/client.py
@@ -13,7 +13,7 @@
13 13
     from urlparse import urlparse, urlsplit
14 14
 
15 15
 from django.conf import settings
16  
-from django.contrib.auth import authenticate, login
  16
+from django.contrib.auth import authenticate, login, logout
17 17
 from django.core.handlers.base import BaseHandler
18 18
 from django.core.handlers.wsgi import WSGIRequest
19 19
 from django.core.signals import got_request_exception
@@ -519,12 +519,18 @@ def logout(self):
519 519
 
520 520
         Causes the authenticated user to be logged out.
521 521
         """
522  
-        session = import_module(settings.SESSION_ENGINE).SessionStore()
523  
-        session_cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
524  
-        if session_cookie:
525  
-            session.delete(session_key=session_cookie.value)
526  
-        self.cookies = SimpleCookie()
527  
-
  522
+        from django.contrib.auth.models import User
  523
+        request = HttpRequest()
  524
+        engine = import_module(settings.SESSION_ENGINE)
  525
+        if self.session:
  526
+            request.session = self.session
  527
+            uid = self.session.get("_auth_user_id")
  528
+            if uid:
  529
+                request.user = User.objects.get(pk=uid)
  530
+        else:
  531
+            request.session = engine.SessionStore()
  532
+        logout(request)
  533
+        
528 534
     def _handle_redirects(self, response, **extra):
529 535
         "Follows any redirects by requesting responses from the server using GET."
530 536
 
54  tests/regressiontests/test_client_regress/tests.py
@@ -17,6 +17,7 @@
17 17
 from django.test.utils import ContextList, override_settings, str_prefix
18 18
 from django.template.response import SimpleTemplateResponse
19 19
 from django.http import HttpResponse
  20
+from django.contrib.auth.signals import user_logged_out, user_logged_in
20 21
 
21 22
 
22 23
 @override_settings(
@@ -743,6 +744,59 @@ def test_logout(self):
743 744
         self.client.logout()
744 745
         self.client.logout()
745 746
 
  747
+    def test_logout_with_user(self):
  748
+        """Logout should send user_logged_out signal if user was logged in."""
  749
+        def listener(*args, **kwargs):
  750
+            listener.executed = True
  751
+        listener.executed = False
  752
+
  753
+        user_logged_out.connect(listener)
  754
+        self.client.login(username='testclient', password='password')
  755
+        self.client.logout()
  756
+        user_logged_out.disconnect(listener)
  757
+
  758
+        self.assertTrue(listener.executed)
  759
+
  760
+    def test_logout_without_user(self):
  761
+        """Logout should send signal even if user not authenticated."""
  762
+        def listener(user, *args, **kwargs):
  763
+            listener.user = user
  764
+            listener.executed = True
  765
+        listener.executed = False
  766
+
  767
+        user_logged_out.connect(listener)
  768
+        self.client.login(username='incorrect', password='password')
  769
+        self.client.logout()
  770
+        user_logged_out.disconnect(listener)
  771
+
  772
+        self.assertTrue(listener.executed)
  773
+        self.assertIsNone(listener.user)
  774
+
  775
+    def test_login_with_user(self):
  776
+        """Login should send user_logged_in signal on successful login."""
  777
+        def listener(*args, **kwargs):
  778
+            listener.executed = True
  779
+        listener.executed = False
  780
+
  781
+        user_logged_in.connect(listener)
  782
+        self.client.login(username='testclient', password='password')
  783
+        user_logged_out.disconnect(listener)
  784
+
  785
+        self.assertTrue(listener.executed)
  786
+
  787
+    def test_login_without_signal(self):
  788
+        """Login shouldn't send signal if user wasn't logged in"""
  789
+        def listener(*args, **kwargs):
  790
+            listener.executed = True
  791
+        listener.executed = False
  792
+
  793
+        user_logged_in.connect(listener)
  794
+        self.client.login(username='incorrect', password='password')
  795
+        user_logged_in.disconnect(listener)
  796
+
  797
+        self.assertFalse(listener.executed)
  798
+
  799
+
746 800
 class RequestMethodTests(TestCase):
747 801
     def test_get(self):
748 802
         "Request a view via request method GET"
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.