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

File fixed-auth-logout-with-redirect.diff, 3.6 KB (added by Felipe 'chronos' Prenholato, 16 years ago)

logout redirects like login view, with tests

  • django/contrib/auth/views.py

     
    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)
  • django/contrib/auth/tests/views.py

     
    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.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_redirect_argument(self):
     37        "Logout with next_page option redirects to specified resource"
     38        self.login()
     39        response = self.client.get('/logout/?next=/login/')
     40        self.assertEqual(response.status_code, 302)
     41        self.assert_(response['Location'].endswith('/login/'))
     42        self.confirm_logged_out()
     43
     44
    1045class PasswordResetTest(TestCase):
    1146    fixtures = ['authtestdata.json']
    1247    urls = 'django.contrib.auth.urls'
  • django/contrib/auth/tests/__init__.py

     
    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
Back to Top