Ticket #10460: auth-logout-with-redirect.diff

File auth-logout-with-redirect.diff, 5.5 KB (added by steingrd, 15 years ago)

logout view redirects like login view, with tests

  • django/contrib/auth/tests/__init__.py

    diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py
    index e878d43..f8adf2c 100644
    a b  
    11from django.contrib.auth.tests.basic import BASIC_TESTS
    22from django.contrib.auth.tests.views \
    3         import PasswordResetTest, ChangePasswordTest
     3        import PasswordResetTest, ChangePasswordTest, LogoutTest
    44from django.contrib.auth.tests.forms import FORM_TESTS
    55from django.contrib.auth.tests.remote_user \
    66        import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest
  • new file django/contrib/auth/tests/urls.py

    diff --git a/django/contrib/auth/tests/urls.py b/django/contrib/auth/tests/urls.py
    new file mode 100644
    index 0000000..8133c3f
    - +  
     1
     2from django.conf.urls.defaults import *
     3
     4urlpatterns = patterns('',
     5    (r'^login/$', 'django.contrib.auth.views.login'),
     6    (r'^logout/$', 'django.contrib.auth.views.logout'),
     7    (r'^password_change/$', 'django.contrib.auth.views.password_change'),
     8    (r'^password_change/done/$', 'django.contrib.auth.views.password_change_done'),
     9    (r'^password_reset/$', 'django.contrib.auth.views.password_reset'),
     10    (r'^password_reset/done/$', 'django.contrib.auth.views.password_reset_done'),
     11    (r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm'),
     12    (r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete'),
     13
     14    # special urls for test cases
     15    (r'^logout/custom_query/$', 'django.contrib.auth.views.logout', dict(redirect_field_name='follow')),
     16    (r'^logout/next_page/$', 'django.contrib.auth.views.logout', dict(next_page='/somewhere/')),
     17)
     18
  • django/contrib/auth/tests/views.py

    diff --git a/django/contrib/auth/tests/views.py b/django/contrib/auth/tests/views.py
    index ec98cf0..2a7bd02 100644
    a b import os  
    33import re
    44
    55from django.conf import settings
     6from django.contrib.auth import SESSION_KEY
    67from django.contrib.auth.models import User
    78from django.test import TestCase
    89from django.core import mail
    910
     11class LogoutTest(TestCase):
     12    fixtures = ['authtestdata.json']
     13    urls = 'django.contrib.auth.tests.urls'
     14
     15    def login(self, password='password'):
     16        response = self.client.post('/login/', {
     17            'username': 'testclient',
     18            'password': password
     19            }
     20        )
     21        self.assertEquals(response.status_code, 302)
     22        self.assert_(response['Location'].endswith(settings.LOGIN_REDIRECT_URL))
     23        self.assert_(SESSION_KEY in self.client.session)
     24
     25    def confirm_logged_out(self):
     26        self.assert_(SESSION_KEY not in self.client.session)
     27
     28    def test_logout_default(self):
     29        "Logout without next_page option renders the default template"
     30        self.login()
     31        response = self.client.get('/logout/')
     32        self.assertEquals(200, response.status_code)
     33        self.assert_('Logged out' in response.content)
     34        self.confirm_logged_out()
     35
     36    def test_logout_with_next_page_specified(self):
     37        "Logout with next_page option given redirects to specified resource"
     38        self.login()
     39        response = self.client.get('/logout/next_page/')
     40        self.assertEqual(response.status_code, 302)
     41        self.assert_(response['Location'].endswith('/somewhere/'))
     42        self.confirm_logged_out()
     43
     44    def test_logout_with_redirect_argument(self):
     45        "Logout with query string redirects to specified resource"
     46        self.login()
     47        response = self.client.get('/logout/?next=/login/')
     48        self.assertEqual(response.status_code, 302)
     49        self.assert_(response['Location'].endswith('/login/'))
     50        self.confirm_logged_out()
     51
     52    def test_logout_with_custom_redirect_argument(self):
     53        "Logout with custom query string redirects to specified resource"
     54        self.login()
     55        response = self.client.get('/logout/custom_query/?follow=/somewhere/')
     56        self.assertEqual(response.status_code, 302)
     57        self.assert_(response['Location'].endswith('/somewhere/'))
     58        self.confirm_logged_out()
     59
     60
    1061class PasswordResetTest(TestCase):
    1162    fixtures = ['authtestdata.json']
    1263    urls = 'django.contrib.auth.urls'
  • django/contrib/auth/views.py

    diff --git a/django/contrib/auth/views.py b/django/contrib/auth/views.py
    index e1f0d43..7877dc8 100644
    a b def login(request, template_name='registration/login.html', redirect_field_name=  
    4242    }, context_instance=RequestContext(request))
    4343login = never_cache(login)
    4444
    45 def logout(request, next_page=None, template_name='registration/logged_out.html'):
     45def logout(request, next_page=None, template_name='registration/logged_out.html', redirect_field_name=REDIRECT_FIELD_NAME):
    4646    "Logs out the user and displays 'You are logged out' message."
    4747    from django.contrib.auth import logout
    4848    logout(request)
    4949    if next_page is None:
    50         return render_to_response(template_name, {'title': _('Logged out')}, context_instance=RequestContext(request))
     50        redirect_to = request.REQUEST.get(redirect_field_name, '')
     51        if redirect_to:
     52            return HttpResponseRedirect(redirect_to)
     53        else:
     54            return render_to_response(template_name, {'title': _('Logged out')}, context_instance=RequestContext(request))
    5155    else:
    5256        # Redirect to this page until the session has been cleared.
    5357        return HttpResponseRedirect(next_page or request.path)
Back to Top