Ticket #15929: 15929.diff

File 15929.diff, 2.3 KB (added by Ramiro Morales, 13 years ago)

Fix bu Luke Plant integrated with tests by m.vantellingen AT auto-interactive.nl and aaugustin

  • django/contrib/auth/middleware.py

    diff --git a/django/contrib/auth/middleware.py b/django/contrib/auth/middleware.py
    a b  
    1313class AuthenticationMiddleware(object):
    1414    def process_request(self, request):
    1515        assert hasattr(request, 'session'), "The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'."
    16         request.__class__.user = LazyUser()
     16
     17        # We dynamically subclass request.__class__ rather than monkey patch the
     18        # original class.
     19        class RequestWithUser(request.__class__):
     20            user = LazyUser()
     21
     22        request.__class__ = RequestWithUser
    1723        return None
    1824
    1925
  • 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
    a b  
    1212    Context, Template, loader)
    1313import django.template.context
    1414from django.test import Client, TestCase
    15 from django.test.client import encode_file
     15from django.test.client import encode_file, RequestFactory
    1616from django.test.utils import ContextList
    1717
    1818
     
    908908            response = self.client.get("/test_client_regress/raw_post_data/")
    909909        except AssertionError:
    910910            self.fail("Accessing request.raw_post_data from a view fetched with GET by the test client shouldn't fail.")
     911
     912
     913class RequestFactoryStateTest(TestCase):
     914    """Regression tests for #15929."""
     915
     916    def setUp(self):
     917        self.factory = RequestFactory()
     918
     919    def common_test_that_should_always_pass(self):
     920        request = self.factory.get('/')
     921        request.session = {}
     922        self.assertFalse(hasattr(request, 'user'))
     923
     924    def test_request(self):
     925        self.common_test_that_should_always_pass()
     926
     927    def test_request_after_client(self):
     928        # apart from the next line the three tests are identical
     929        self.client.get('/')
     930        self.common_test_that_should_always_pass() # Shouldn't fail
     931
     932    def test_request_after_client_2(self):
     933        # This test is executed after the previous one
     934        self.common_test_that_should_always_pass() # Shouldn't fail
Back to Top