Fixed #5189 -- Added logout method to test Client. Thanks, Jakub Wisniowski <restless.being@gmail.com>.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5916 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2007-08-17 14:20:25 +00:00
parent 55c0b35acf
commit 8dff1cd91d
4 changed files with 42 additions and 3 deletions

View File

@ -284,6 +284,7 @@ answer newbie questions, and generally made Django that much better:
charly.wilhelm@gmail.com charly.wilhelm@gmail.com
Rachel Willmer <http://www.willmer.com/kb/> Rachel Willmer <http://www.willmer.com/kb/>
Gary Wilson <gary.wilson@gmail.com> Gary Wilson <gary.wilson@gmail.com>
Jakub Wiśniowski <restless.being@gmail.com>
wojtek wojtek
ye7cakf02@sneakemail.com ye7cakf02@sneakemail.com
ymasuda@ethercube.com ymasuda@ethercube.com

View File

@ -253,3 +253,14 @@ class Client:
else: else:
return False return False
def logout(self):
"""Removes the authenticated user's cookies.
Causes the authenticated user to be logged out.
"""
try:
Session.objects.get(session_key=self.cookies['sessionid'].value).delete()
except KeyError:
pass
self.cookies = SimpleCookie()

View File

@ -550,6 +550,17 @@ Once you have a ``Client`` instance, you can call any of the following methods:
conditions. You'll need to create users as part of the test suite -- either conditions. You'll need to create users as part of the test suite -- either
manually (using the Django model API) or with a test fixture. manually (using the Django model API) or with a test fixture.
``logout()``
**New in Django development version**
If your site uses Django's `authentication system`_, the ``logout()``
method can be used to simulate the effect of a user logging out of
your site.
After you call this method, the test client will have all the cookies and
session data cleared to defaults. Subsequent requests will appear to
come from an AnonymousUser.
.. _authentication system: ../authentication/ .. _authentication system: ../authentication/
.. _authentication backend: ../authentication/#other-authentication-sources .. _authentication backend: ../authentication/#other-authentication-sources

View File

@ -246,6 +246,22 @@ class ClientTest(TestCase):
login = self.client.login(username='inactive', password='password') login = self.client.login(username='inactive', password='password')
self.failIf(login) self.failIf(login)
def test_logout(self):
# Log in
self.client.login(username='testclient', password='password')
# Request a page that requires a login
response = self.client.get('/test_client/login_protected_view/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['user'].username, 'testclient')
# Log out
self.client.logout()
# Request a page that requires a login
response = self.client.get('/test_client/login_protected_view/')
self.assertRedirects(response, '/accounts/login/')
def test_session_modifying_view(self): def test_session_modifying_view(self):
"Request a page that modifies the session" "Request a page that modifies the session"
# Session value isn't set initially # Session value isn't set initially