Ticket #5189: client.patch

File client.patch, 2.6 KB (added by Jakub Wiśniowski, 17 years ago)
  • django/test/client.py

     
    253253        else:
    254254            return False
    255255
     256    def logout(self):
     257        """Removes the authenticated user's cookies.
     258
     259        Causes the authenticated user to be logged out.
     260        """
     261        try:
     262            Session.objects.get(session_key=self.cookies['sessionid'].value).delete()
     263        except KeyError:
     264            pass
     265
     266        self.cookies = SimpleCookie()
     267 No newline at end of file
  • tests/modeltests/test_client/models.py

     
    246246        login = self.client.login(username='inactive', password='password')
    247247        self.failIf(login)
    248248
     249    def test_logout(self):
     250       # Log in
     251        self.client.login(username='testclient', password='password')
     252
     253        # Request a page that requires a login
     254        response = self.client.get('/test_client/login_protected_view/')
     255        self.assertEqual(response.status_code, 200)
     256        self.assertEqual(response.context['user'].username, 'testclient')
     257
     258       # Log out
     259       self.client.logout()
     260
     261       # Request a page that requires a login
     262       response = self.client.get('/test_client/login_protected_view/')
     263       self.assertRedirects(response, '/accounts/login/')
     264
    249265    def test_session_modifying_view(self):
    250266        "Request a page that modifies the session"
    251267        # Session value isn't set initially
  • docs/testing.txt

     
    550550    conditions. You'll need to create users as part of the test suite -- either
    551551    manually (using the Django model API) or with a test fixture.
    552552
     553``logout()``
     554    **New in Django development version**
     555
     556    If your site uses Django's `authentication system`_ and you deal with
     557    logging in users using test client's ``login()`` method you can also use
     558    client's ``logout()`` method to simulate the effect of logging a user out from
     559    the site.
     560
     561    After you call this method, the test client will have all the cookies and
     562    session data cleared to defaults. Your current user will become an
     563    Anonymous User.
     564
     565
    553566.. _authentication system: ../authentication/
    554567.. _authentication backend: ../authentication/#other-authentication-sources
Back to Top