diff --git a/django/contrib/auth/middleware.py b/django/contrib/auth/middleware.py
a
|
b
|
|
13 | 13 | class AuthenticationMiddleware(object): |
14 | 14 | def process_request(self, request): |
15 | 15 | 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 |
17 | 23 | return None |
18 | 24 | |
19 | 25 | |
diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
a
|
b
|
|
12 | 12 | Context, Template, loader) |
13 | 13 | import django.template.context |
14 | 14 | from django.test import Client, TestCase |
15 | | from django.test.client import encode_file |
| 15 | from django.test.client import encode_file, RequestFactory |
16 | 16 | from django.test.utils import ContextList |
17 | 17 | |
18 | 18 | |
… |
… |
|
908 | 908 | response = self.client.get("/test_client_regress/raw_post_data/") |
909 | 909 | except AssertionError: |
910 | 910 | self.fail("Accessing request.raw_post_data from a view fetched with GET by the test client shouldn't fail.") |
| 911 | |
| 912 | |
| 913 | class 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 |