Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10460: the logout view can now redirect like the rest of the a…

…uth views. Thanks, chronos and steingrd.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10332 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3e6f4674e2fc190c2116d77066d18f42bd3bcbae 1 parent 35a1f22
Jacob Kaplan-Moss authored April 01, 2009
3  django/contrib/auth/tests/__init__.py
... ...
@@ -1,6 +1,6 @@
1 1
 from django.contrib.auth.tests.basic import BASIC_TESTS
2 2
 from django.contrib.auth.tests.views \
3  
-        import PasswordResetTest, ChangePasswordTest, LoginTest
  3
+        import PasswordResetTest, ChangePasswordTest, LoginTest, LogoutTest
4 4
 from django.contrib.auth.tests.forms import FORM_TESTS
5 5
 from django.contrib.auth.tests.remote_user \
6 6
         import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest
@@ -15,4 +15,5 @@
15 15
     'TOKEN_GENERATOR_TESTS': TOKEN_GENERATOR_TESTS,
16 16
     'CHANGEPASSWORD_TESTS': ChangePasswordTest,
17 17
     'LOGIN_TESTS': LoginTest,
  18
+    'LOGOUT_TESTS': LogoutTest,
18 19
 }
9  django/contrib/auth/tests/urls.py
... ...
@@ -0,0 +1,9 @@
  1
+from django.conf.urls.defaults import patterns
  2
+from django.contrib.auth.urls import urlpatterns
  3
+
  4
+# special urls for auth test cases
  5
+urlpatterns += patterns('',
  6
+    (r'^logout/custom_query/$', 'django.contrib.auth.views.logout', dict(redirect_field_name='follow')),
  7
+    (r'^logout/next_page/$', 'django.contrib.auth.views.logout', dict(next_page='/somewhere/')),
  8
+)
  9
+
52  django/contrib/auth/tests/views.py
... ...
@@ -1,8 +1,8 @@
1  
-
2 1
 import os
3 2
 import re
4 3
 
5 4
 from django.conf import settings
  5
+from django.contrib.auth import SESSION_KEY
6 6
 from django.contrib.auth.forms import AuthenticationForm
7 7
 from django.contrib.sites.models import Site, RequestSite
8 8
 from django.contrib.auth.models import User
@@ -184,4 +184,52 @@ def test_current_site_in_context_after_login(self):
184 184
         self.assertEquals(response.context['site_name'], site.name)
185 185
         self.assert_(isinstance(response.context['form'], AuthenticationForm), 
186 186
                      'Login form is not an AuthenticationForm')
187  
-        
  187
+        
  188
+class LogoutTest(TestCase):
  189
+    fixtures = ['authtestdata.json']
  190
+    urls = 'django.contrib.auth.tests.urls'
  191
+
  192
+    def login(self, password='password'):
  193
+        response = self.client.post('/login/', {
  194
+            'username': 'testclient',
  195
+            'password': password
  196
+            }
  197
+        )
  198
+        self.assertEquals(response.status_code, 302)
  199
+        self.assert_(response['Location'].endswith(settings.LOGIN_REDIRECT_URL))
  200
+        self.assert_(SESSION_KEY in self.client.session)
  201
+
  202
+    def confirm_logged_out(self):
  203
+        self.assert_(SESSION_KEY not in self.client.session)
  204
+
  205
+    def test_logout_default(self):
  206
+        "Logout without next_page option renders the default template"
  207
+        self.login()
  208
+        response = self.client.get('/logout/')
  209
+        self.assertEquals(200, response.status_code)
  210
+        self.assert_('Logged out' in response.content)
  211
+        self.confirm_logged_out()
  212
+
  213
+    def test_logout_with_next_page_specified(self): 
  214
+        "Logout with next_page option given redirects to specified resource"
  215
+        self.login()
  216
+        response = self.client.get('/logout/next_page/')
  217
+        self.assertEqual(response.status_code, 302)
  218
+        self.assert_(response['Location'].endswith('/somewhere/'))
  219
+        self.confirm_logged_out()
  220
+
  221
+    def test_logout_with_redirect_argument(self):
  222
+        "Logout with query string redirects to specified resource"
  223
+        self.login()
  224
+        response = self.client.get('/logout/?next=/login/')
  225
+        self.assertEqual(response.status_code, 302)
  226
+        self.assert_(response['Location'].endswith('/login/'))
  227
+        self.confirm_logged_out()
  228
+
  229
+    def test_logout_with_custom_redirect_argument(self):
  230
+        "Logout with custom query string redirects to specified resource"
  231
+        self.login()
  232
+        response = self.client.get('/logout/custom_query/?follow=/somewhere/')
  233
+        self.assertEqual(response.status_code, 302)
  234
+        self.assert_(response['Location'].endswith('/somewhere/'))
  235
+        self.confirm_logged_out()
10  django/contrib/auth/views.py
@@ -43,12 +43,18 @@ def login(request, template_name='registration/login.html', redirect_field_name=
43 43
     }, context_instance=RequestContext(request))
44 44
 login = never_cache(login)
45 45
 
46  
-def logout(request, next_page=None, template_name='registration/logged_out.html'):
  46
+def logout(request, next_page=None, template_name='registration/logged_out.html', redirect_field_name=REDIRECT_FIELD_NAME):
47 47
     "Logs out the user and displays 'You are logged out' message."
48 48
     from django.contrib.auth import logout
49 49
     logout(request)
50 50
     if next_page is None:
51  
-        return render_to_response(template_name, {'title': _('Logged out')}, context_instance=RequestContext(request))
  51
+        redirect_to = request.REQUEST.get(redirect_field_name, '')
  52
+        if redirect_to:
  53
+            return HttpResponseRedirect(redirect_to)
  54
+        else:
  55
+            return render_to_response(template_name, {
  56
+                'title': _('Logged out')
  57
+            }, context_instance=RequestContext(request))
52 58
     else:
53 59
         # Redirect to this page until the session has been cleared.
54 60
         return HttpResponseRedirect(next_page or request.path)

0 notes on commit 3e6f467

Please sign in to comment.
Something went wrong with that request. Please try again.