Fixed #15929 - test.client.RequestFactory keeps state/AuthMiddleware does monkey patching

Thanks to m.vantellingen for the report and tests, and to aaugustin for
work on the tests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16297 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Luke Plant 2011-05-30 16:33:23 +00:00
parent de3b58d626
commit dc4c2f3add
2 changed files with 37 additions and 2 deletions

View File

@ -13,7 +13,13 @@ class LazyUser(object):
class AuthenticationMiddleware(object): class AuthenticationMiddleware(object):
def process_request(self, request): def process_request(self, request):
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'." 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'."
request.__class__.user = LazyUser()
# We dynamically subclass request.__class__ rather than monkey patch the
# original class.
class RequestWithUser(request.__class__):
user = LazyUser()
request.__class__ = RequestWithUser
return None return None

View File

@ -12,7 +12,7 @@ from django.template import (TemplateDoesNotExist, TemplateSyntaxError,
Context, Template, loader) Context, Template, loader)
import django.template.context import django.template.context
from django.test import Client, TestCase from django.test import Client, TestCase
from django.test.client import encode_file from django.test.client import encode_file, RequestFactory
from django.test.utils import ContextList from django.test.utils import ContextList
@ -908,3 +908,32 @@ class RawPostDataTest(TestCase):
response = self.client.get("/test_client_regress/raw_post_data/") response = self.client.get("/test_client_regress/raw_post_data/")
except AssertionError: except AssertionError:
self.fail("Accessing request.raw_post_data from a view fetched with GET by the test client shouldn't fail.") self.fail("Accessing request.raw_post_data from a view fetched with GET by the test client shouldn't fail.")
class RequestFactoryStateTest(TestCase):
"""Regression tests for #15929."""
# These tests are checking that certain middleware don't change certain
# global state. Alternatively, from the point of view of a test, they are
# ensuring test isolation behaviour. So, unusually, it doesn't make sense to
# run the tests individually, and if any are failing it is confusing to run
# them with any other set of tests.
def setUp(self):
self.factory = RequestFactory()
def common_test_that_should_always_pass(self):
request = self.factory.get('/')
request.session = {}
self.assertFalse(hasattr(request, 'user'))
def test_request(self):
self.common_test_that_should_always_pass()
def test_request_after_client(self):
# apart from the next line the three tests are identical
self.client.get('/')
self.common_test_that_should_always_pass()
def test_request_after_client_2(self):
# This test is executed after the previous one
self.common_test_that_should_always_pass()