Ticket #10899: 10899.r16527.diff

File 10899.r16527.diff, 6.6 KB (added by prestontimmons, 4 years ago)

Updated patch

  • django/test/client.py

    diff --git a/django/test/client.py b/django/test/client.py
    index 4335f21..cf74b22 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.middleware.SessionMiddleware' in settings.MIDDLEWARE_CLASSES:
    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 self._session_store:
     389            if getattr(self._session_store, "modified"):
     390                self._session_store.save()
     391
     392            if 'django.contrib.sessions.middleware.SessionMiddleware' in settings.MIDDLEWARE_CLASSES:
     393                # Update the session cookie since the session key can change
     394                # due to login or logout and force the session to be reloaded
     395                # on next access.
     396                self.cookies[settings.SESSION_COOKIE_NAME] = \
     397                    self._session_store.session_key
     398                self._session_store = None
     399
    377400        # Curry a data dictionary into an instance of the template renderer
    378401        # callback function.
    379402        data = {}
    class Client(RequestFactory): 
    505528        user = authenticate(**credentials)
    506529        if user and user.is_active \
    507530                and 'django.contrib.sessions.middleware.SessionMiddleware' in settings.MIDDLEWARE_CLASSES:
    508             engine = import_module(settings.SESSION_ENGINE)
    509531
    510532            # Create a fake request to store login details.
    511533            request = HttpRequest()
    512             if self.session:
    513                 request.session = self.session
    514             else:
    515                 request.session = engine.SessionStore()
     534            request.session = self.session
    516535            login(request, user)
    517536
    518537            # Save the session values.
    class Client(RequestFactory): 
    545564        if session_cookie:
    546565            session.delete(session_key=session_cookie.value)
    547566        self.cookies = SimpleCookie()
     567        self._session_store = None
    548568
    549569    def _handle_redirects(self, response, **extra):
    550570        "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 6595c51..5a34376 100644
    a b can access these properties as part of a test condition. 
    10011001    A dictionary-like object containing session information. See the
    10021002    :doc:`session documentation</topics/http/sessions>` for full details.
    10031003
    1004     To modify the session and then save it, it must be stored in a variable
    1005     first (because a new ``SessionStore`` is created every time this property
    1006     is accessed)::
     1004.. versionadded:: 1.4
     1005
     1006    Similar to normal Django sessions, you can manipulate sessions directly.
     1007    The modified values will be saved on subsequent requests.
    10071008
    10081009        def test_something(self):
    1009             session = self.client.session
    1010             session['somekey'] = 'test'
    1011             session.save()
     1010            self.client.session['somekey'] = value
     1011            self.client.session['anotherkey'] = value
    10121012
    10131013.. _Cookie module documentation: http://docs.python.org/library/cookie.html
    10141014
  • 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 8e840e4..088b5d7 100644
    a b class SessionTests(TestCase): 
    704704        self.assertEqual(response.status_code, 200)
    705705        self.assertEqual(response.content, 'YES')
    706706
     707    def test_session_manipulation(self):
     708        # Check that the session can be edited as documented before #10899.
     709        session = self.client.session
     710        session["session_var"] = "foo"
     711        session.save()
     712
     713        response = self.client.get('/test_client_regress/check_session/')
     714        self.assertEqual(response.status_code, 200)
     715        self.assertEqual(response.content, 'foo')
     716
     717    def test_direct_session_manipulation(self):
     718        # Add a value to the session
     719        self.client.session['session_var'] = 'bar'
     720        self.assertEqual(self.client.session['session_var'], 'bar')
     721
     722        # Check that the session has been modified
     723        response = self.client.get('/test_client_regress/check_session/')
     724        self.assertEqual(response.status_code, 200)
     725        self.assertEqual(response.content, 'bar')
     726
     727        # Check that the session variable persists over login
     728        # when cycle_key() is called
     729        self.client.login(username='testclient', password='password')
     730        self.assertEqual(self.client.session['session_var'], 'bar')
     731
     732        response = self.client.get('/test_client_regress/check_session/')
     733        self.assertEqual(response.status_code, 200)
     734        self.assertEqual(response.content, 'bar')
     735
     736        # Check that new session is started after logout
     737        self.client.logout()
     738        self.assertEqual(self.client.session.get('session_var'), None)
     739
    707740    def test_logout(self):
    708741        """Logout should work whether the user is logged in or not (#9978)."""
    709742        self.client.logout()
    class SessionTests(TestCase): 
    712745        self.client.logout()
    713746        self.client.logout()
    714747
     748
    715749class RequestMethodTests(TestCase):
    716750    def test_get(self):
    717751        "Request a view via request method GET"
Back to Top