Ticket #10460: auth-logout-with-redirect.diff
File auth-logout-with-redirect.diff, 5.5 KB (added by , 16 years ago) |
---|
-
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 1 1 from django.contrib.auth.tests.basic import BASIC_TESTS 2 2 from django.contrib.auth.tests.views \ 3 import PasswordResetTest, ChangePasswordTest 3 import PasswordResetTest, ChangePasswordTest, 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 -
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 2 from django.conf.urls.defaults import * 3 4 urlpatterns = 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 3 3 import re 4 4 5 5 from django.conf import settings 6 from django.contrib.auth import SESSION_KEY 6 7 from django.contrib.auth.models import User 7 8 from django.test import TestCase 8 9 from django.core import mail 9 10 11 class 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 10 61 class PasswordResetTest(TestCase): 11 62 fixtures = ['authtestdata.json'] 12 63 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= 42 42 }, context_instance=RequestContext(request)) 43 43 login = never_cache(login) 44 44 45 def logout(request, next_page=None, template_name='registration/logged_out.html' ):45 def logout(request, next_page=None, template_name='registration/logged_out.html', redirect_field_name=REDIRECT_FIELD_NAME): 46 46 "Logs out the user and displays 'You are logged out' message." 47 47 from django.contrib.auth import logout 48 48 logout(request) 49 49 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)) 51 55 else: 52 56 # Redirect to this page until the session has been cleared. 53 57 return HttpResponseRedirect(next_page or request.path)