Ticket #8122: 8122-rmaster.diff

File 8122-rmaster.diff, 7.9 KB (added by Ed Henderson, 10 years ago)

Updated the diff to the latest master

  • django/contrib/sessions/backends/base.py

    diff --git a/django/contrib/sessions/backends/base.py b/django/contrib/sessions/backends/base.py
    index 3b24b8d..d734351 100644
    a b class SessionBase(object):  
    3232    """
    3333    Base class for all Session classes.
    3434    """
    35     TEST_COOKIE_NAME = 'testcookie'
    36     TEST_COOKIE_VALUE = 'worked'
    3735
    3836    def __init__(self, session_key=None):
    3937        self._session_key = session_key
    4038        self.accessed = False
    4139        self.modified = False
     40        self.cookie_received = (session_key is not None)
    4241        self.serializer = import_string(settings.SESSION_SERIALIZER)
    4342
    4443    def __contains__(self, key):
    class SessionBase(object):  
    7170            return value
    7271
    7372    def set_test_cookie(self):
    74         self[self.TEST_COOKIE_NAME] = self.TEST_COOKIE_VALUE
     73        from warnings import warn
     74        warn('set_test_cookie() is deprecated. It is no longer required.')
    7575
    7676    def test_cookie_worked(self):
    77         return self.get(self.TEST_COOKIE_NAME) == self.TEST_COOKIE_VALUE
     77        from warnings import warn
     78        warn('test_cookie_worked() is deprecated. Use request.cookie_received')
     79        return self.cookie_received
    7880
    7981    def delete_test_cookie(self):
    80         del self[self.TEST_COOKIE_NAME]
     82        from warnings import warn
     83        warn('delete_test_cookie() is deprecated. It is no longer required.')
    8184
    8285    def _hash(self, value):
    8386        key_salt = "django.contrib.sessions" + self.__class__.__name__
  • django/contrib/sessions/middleware.py

    diff --git a/django/contrib/sessions/middleware.py b/django/contrib/sessions/middleware.py
    index 69ca669..f1af3de 100644
    a b class SessionMiddleware(object):  
    3636                if accessed:
    3737                    patch_vary_headers(response, ('Cookie',))
    3838                if modified or settings.SESSION_SAVE_EVERY_REQUEST:
    39                     if request.session.get_expire_at_browser_close():
    40                         max_age = None
    41                         expires = None
    42                     else:
    43                         max_age = request.session.get_expiry_age()
    44                         expires_time = time.time() + max_age
    45                         expires = cookie_date(expires_time)
    46                     # Save the session data and refresh the client cookie.
    47                     # Skip session save for 500 responses, refs #3881.
    48                     if response.status_code != 500:
    49                         request.session.save()
    50                         response.set_cookie(settings.SESSION_COOKIE_NAME,
    51                                 request.session.session_key, max_age=max_age,
    52                                 expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
    53                                 path=settings.SESSION_COOKIE_PATH,
    54                                 secure=settings.SESSION_COOKIE_SECURE or None,
    55                                 httponly=settings.SESSION_COOKIE_HTTPONLY or None)
     39                    # Save the session data and refresh the client cookie
     40                    request.session.save()
     41                    self.set_session_cookie(request, response,
     42                            request.session.session_key)
     43                elif not request.session.cookie_received:
     44                    # Set a temporary cookie
     45                    self.set_session_cookie(request, response, '')
     46                           
    5647        return response
     48
     49    def set_session_cookie(self, request, response, value):
     50        if request.session.get_expire_at_browser_close():
     51            max_age = None
     52            expires = None
     53        else:
     54            max_age = request.session.get_expiry_age()
     55            expires_time = time.time() + max_age
     56            expires = cookie_date(expires_time)
     57        response.set_cookie(settings.SESSION_COOKIE_NAME, value,
     58                max_age=max_age, expires=expires,
     59                domain=settings.SESSION_COOKIE_DOMAIN,
     60                path=settings.SESSION_COOKIE_PATH,
     61                secure=settings.SESSION_COOKIE_SECURE or None)
  • new file tests/regressiontests/sessions_regress/models.py

    diff --git a/tests/regressiontests/sessions_regress/models.py b/tests/regressiontests/sessions_regress/models.py
    new file mode 100644
    index 0000000..71abcc5
    - +  
     1# models.py file for tests to run.
  • new file tests/regressiontests/sessions_regress/tests.py

    diff --git a/tests/regressiontests/sessions_regress/tests.py b/tests/regressiontests/sessions_regress/tests.py
    new file mode 100644
    index 0000000..8f68963
    - +  
     1# -*- coding: utf-8 -*-
     2
     3from django.test import TestCase
     4from django.http import HttpRequest, HttpResponse
     5from django.contrib.sessions.middleware import SessionMiddleware
     6from django.conf import settings
     7
     8class SessionMiddlewareTest(TestCase):
     9    def test_no_cookie(self):
     10        """
     11        Tests that the session middleware works when the client does not send
     12        a session cookie.
     13        """
     14        middleware = SessionMiddleware()
     15        request = HttpRequest()
     16        middleware.process_request(request)
     17        assert not request.session.cookie_received
     18        response = middleware.process_response(request, HttpResponse())
     19        assert settings.SESSION_COOKIE_NAME in response.cookies
     20        assert response.cookies[settings.SESSION_COOKIE_NAME].value == ''
     21
     22    def test_empty_cookie(self):
     23        """
     24        Tests that the session middleware works when the client sends an empty
     25        session cookie.
     26        """
     27        middleware = SessionMiddleware()
     28        request = HttpRequest()
     29        request.COOKIES[settings.SESSION_COOKIE_NAME] = ''
     30        middleware.process_request(request)
     31        assert request.session.cookie_received
     32        response = middleware.process_response(request, HttpResponse())
     33        assert not settings.SESSION_COOKIE_NAME in response.cookies
     34
     35    def test_valid_cookie(self):
     36        """
     37        Tests that the session middleware works when the client sends a valid
     38        session cookie.
     39        """
     40        # First get valid session id
     41        middleware = SessionMiddleware()
     42        request = HttpRequest()
     43        middleware.process_request(request)
     44        request.session['test'] = 'test'
     45        response = middleware.process_response(request, HttpResponse())
     46        sessionid = response.cookies[settings.SESSION_COOKIE_NAME].value
     47        assert sessionid != ''
     48        # Now check behavior for valid session id
     49        request = HttpRequest()
     50        request.COOKIES[settings.SESSION_COOKIE_NAME] = sessionid
     51        middleware.process_request(request)
     52        assert request.session.cookie_received
     53        assert request.session['test'] == 'test'
     54        response = middleware.process_response(request, HttpResponse())
     55        assert not settings.SESSION_COOKIE_NAME in response.cookies
     56
     57    def test_invalid_cookie(self):
     58        """
     59        Tests that the session middleware works when the client sends an
     60        invalid session cookie.
     61        """
     62        # First get valid session id
     63        middleware = SessionMiddleware()
     64        request = HttpRequest()
     65        middleware.process_request(request)
     66        request.session['test'] = 'test'
     67        response = middleware.process_response(request, HttpResponse())
     68        sessionid = response.cookies[settings.SESSION_COOKIE_NAME].value
     69        assert sessionid != ''
     70        # Now check behavior for invalid session id
     71        request = HttpRequest()
     72        request.COOKIES[settings.SESSION_COOKIE_NAME] = 'invalid'
     73        middleware.process_request(request)
     74        assert request.session.cookie_received
     75        assert 'test' not in request.session
     76        response = middleware.process_response(request, HttpResponse())
     77        assert settings.SESSION_COOKIE_NAME in response.cookies
     78        assert response.cookies[settings.SESSION_COOKIE_NAME] != ''
     79        assert response.cookies[settings.SESSION_COOKIE_NAME] != sessionid
Back to Top