Django

Code

Changeset 5916

Show
Ignore:
Timestamp:
08/17/07 09:20:25 (1 year ago)
Author:
russellm
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r5895 r5916  
    285285    Rachel Willmer <http://www.willmer.com/kb/> 
    286286    Gary Wilson <gary.wilson@gmail.com> 
     287    Jakub Wiśniowski <restless.being@gmail.com> 
    287288    wojtek 
    288289    ye7cakf02@sneakemail.com 
  • django/trunk/django/test/client.py

    r5741 r5916  
    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() 
  • django/trunk/docs/testing.txt

    r5910 r5916  
    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`_, the ``logout()`` 
     557    method can be used to simulate the effect of a user logging out of 
     558    your site. 
     559 
     560    After you call this method, the test client will have all the cookies and 
     561    session data cleared to defaults. Subsequent requests will appear to 
     562    come from an AnonymousUser. 
     563 
    553564.. _authentication system: ../authentication/ 
    554565.. _authentication backend: ../authentication/#other-authentication-sources 
     
    759770      flush the database, returning the database to the state it was in 
    760771      directly after ``syncdb`` was called. 
    761       
     772 
    762773    * Then, all the named fixtures are installed. In this example, Django will 
    763774      install any JSON fixture named ``mammals``, followed by any fixture named 
     
    844855mail server, if you're running one.) 
    845856 
    846 During test running, each outgoing e-mail is saved in  
     857During test running, each outgoing e-mail is saved in 
    847858``django.core.mail.outbox``. This is a simple list of all `EmailMessage`_ 
    848859instances that have been sent. It does not exist under normal execution 
     
    978989    ``autoclobber`` describes the behavior that will occur if a database with 
    979990    the same name as the test database is discovered: 
    980      
     991 
    981992        * If ``autoclobber`` is ``False``, the user will be asked to approve 
    982993          destroying the existing database. ``sys.exit`` is called if the user 
  • django/trunk/tests/modeltests/test_client/models.py

    r5876 r5916  
    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"