Ticket #10899: 10899_at_r16086.diff

File 10899_at_r16086.diff, 6.0 KB (added by Preston Timmons, 13 years ago)

Updated patch against r16086

  • django/test/client.py

    diff --git a/django/test/client.py b/django/test/client.py
    index 5cbc0ca..ea4d9ff 100644
    a b class Client(RequestFactory):  
    345345        super(Client, self).__init__(**defaults)
    346346        self.handler = ClientHandler(enforce_csrf_checks)
    347347        self.exc_info = None
     348        self._session_store = None
    348349
    349350    def store_exc_info(self, **kwargs):
    350351        """
    class Client(RequestFactory):  
    356357        """
    357358        Obtains the current session variables.
    358359        """
     360        if self._session_store:
     361            return self._session_store
     362
    359363        if 'django.contrib.sessions' in settings.INSTALLED_APPS:
    360364            engine = import_module(settings.SESSION_ENGINE)
    361             cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
     365            cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
    362366            if cookie:
    363                 return engine.SessionStore(cookie.value)
    364         return {}
     367                session_store = engine.SessionStore(cookie.value)
     368            else:
     369                session_store = engine.SessionStore()
     370                session_store.save()
     371                self.cookies[settings.SESSION_COOKIE_NAME] = \
     372                     session_store.session_key
     373            self._session_store = session_store
     374            return session_store
     375        else:
     376            return {}
    365377    session = property(_session)
    366378
    367 
    368379    def request(self, **request):
    369380        """
    370381        The master request method. Composes the environment dictionary
    class Client(RequestFactory):  
    374385        """
    375386        environ = self._base_environ(**request)
    376387
     388        if hasattr(self.session, "save"):
     389            # Save session modifications and update the session cookie since
     390            # the session key may change over login or logout. Also, force
     391            # the session to be reloaded on next access.
     392            self.session.save()
     393            self.cookies[settings.SESSION_COOKIE_NAME] = \
     394                self.session.session_key
     395            self._session_store = None
     396
    377397        # Curry a data dictionary into an instance of the template renderer
    378398        # callback function.
    379399        data = {}
    class Client(RequestFactory):  
    505525        user = authenticate(**credentials)
    506526        if user and user.is_active \
    507527                and 'django.contrib.sessions' in settings.INSTALLED_APPS:
    508             engine = import_module(settings.SESSION_ENGINE)
    509528
    510529            # Create a fake request to store login details.
    511530            request = HttpRequest()
    512             if self.session:
    513                 request.session = self.session
    514             else:
    515                 request.session = engine.SessionStore()
     531            request.session = self.session
    516532            login(request, user)
    517533
    518534            # Save the session values.
    class Client(RequestFactory):  
    545561        if session_cookie:
    546562            session.delete(session_key=session_cookie.value)
    547563        self.cookies = SimpleCookie()
     564        self._session_store = None
    548565
    549566    def _handle_redirects(self, response, **extra):
    550567        "Follows any redirects by requesting responses from the server using GET."
  • docs/topics/testing.txt

    diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt
    index fb9f6e5..005a336 100644
    a b can access these properties as part of a test condition.  
    10091009            session['somekey'] = 'test'
    10101010            session.save()
    10111011
     1012.. versionadded:: Development
     1013
     1014    Similar to normal Django sessions, you can manipulate sessions directly.
     1015    The modified values will be saved on subsequent requests.
     1016
     1017        def test_something(self):
     1018            self.client.session['somekey'] = value
     1019            self.client.session['anotherkey'] = value
     1020
    10121021.. _Cookie module documentation: http://docs.python.org/library/cookie.html
    10131022
    10141023Example
  • tests/regressiontests/test_client_regress/models.py

    diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
    index 6757695..40848a1 100644
    a b class SessionTests(TestCase):  
    691691        self.assertEqual(response.status_code, 200)
    692692        self.assertEqual(response.content, 'YES')
    693693
     694    def test_session_manipulation(self):
     695        # Check that the session can be edited as documented before #10899.
     696        session = self.client.session
     697        session["session_var"] = "foo"
     698        session.save()
     699
     700        response = self.client.get('/test_client_regress/check_session/')
     701        self.assertEqual(response.status_code, 200)
     702        self.assertEqual(response.content, 'foo')
     703
     704    def test_direct_session_manipulation(self):
     705        # Add a value to the session
     706        self.client.session['session_var'] = 'bar'
     707        self.assertEqual(self.client.session['session_var'], 'bar')
     708
     709        # Check that the session has been modified
     710        response = self.client.get('/test_client_regress/check_session/')
     711        self.assertEqual(response.status_code, 200)
     712        self.assertEqual(response.content, 'bar')
     713
     714        # Check that the session variable persists over login
     715        # when cycle_key() is called
     716        self.client.login(username='testclient', password='password')
     717        self.assertEqual(self.client.session['session_var'], 'bar')
     718
     719        response = self.client.get('/test_client_regress/check_session/')
     720        self.assertEqual(response.status_code, 200)
     721        self.assertEqual(response.content, 'bar')
     722
     723        # Check that new session is started after logout
     724        self.client.logout()
     725        self.assertEqual(self.client.session.get('session_var'), None)
     726
    694727    def test_logout(self):
    695728        """Logout should work whether the user is logged in or not (#9978)."""
    696729        self.client.logout()
    class SessionTests(TestCase):  
    699732        self.client.logout()
    700733        self.client.logout()
    701734
     735
    702736class RequestMethodTests(TestCase):
    703737    def test_get(self):
    704738        "Request a view via request method GET"
Back to Top