Revert "Fixed #15179 -- middlewares not applied for test client login()"

This reverts commit 4fdd51b732.

See the ticket for concerns with this implementation; it will be revisited.
This commit is contained in:
Tim Graham 2014-04-28 18:41:16 -04:00
parent 26b03f2526
commit aabceadd7d
4 changed files with 4 additions and 68 deletions

View File

@ -15,7 +15,7 @@ from django.core.handlers.wsgi import WSGIRequest
from django.core.signals import (request_started, request_finished, from django.core.signals import (request_started, request_finished,
got_request_exception) got_request_exception)
from django.db import close_old_connections from django.db import close_old_connections
from django.http import SimpleCookie, QueryDict from django.http import SimpleCookie, HttpRequest, QueryDict
from django.template import TemplateDoesNotExist from django.template import TemplateDoesNotExist
from django.test import signals from django.test import signals
from django.utils.functional import curry from django.utils.functional import curry
@ -553,8 +553,8 @@ class Client(RequestFactory):
apps.is_installed('django.contrib.sessions')): apps.is_installed('django.contrib.sessions')):
engine = import_module(settings.SESSION_ENGINE) engine = import_module(settings.SESSION_ENGINE)
# Create a fake request that goes through request middleware # Create a fake request to store login details.
request = self.request().wsgi_request request = HttpRequest()
if self.session: if self.session:
request.session = self.session request.session = self.session
@ -588,9 +588,8 @@ class Client(RequestFactory):
Causes the authenticated user to be logged out. Causes the authenticated user to be logged out.
""" """
from django.contrib.auth import get_user_model, logout from django.contrib.auth import get_user_model, logout
# Create a fake request that goes through request middleware
request = self.request().wsgi_request
request = HttpRequest()
engine = import_module(settings.SESSION_ENGINE) engine = import_module(settings.SESSION_ENGINE)
UserModel = get_user_model() UserModel = get_user_model()
if self.session: if self.session:

View File

@ -802,11 +802,6 @@ Tests
:class:`~django.test.Client`. If ``True``, the request will be made :class:`~django.test.Client`. If ``True``, the request will be made
through HTTPS. through HTTPS.
* Requests made with :meth:`Client.login() <django.test.Client.login>` and
:meth:`Client.logout() <django.test.Client.logout>` respect defaults defined
in :class:`~django.test.Client` instantiation and are processed through
middleware.
* :meth:`~django.test.TransactionTestCase.assertNumQueries` now prints * :meth:`~django.test.TransactionTestCase.assertNumQueries` now prints
out the list of executed queries if the assertion fails. out the list of executed queries if the assertion fails.

View File

@ -360,13 +360,6 @@ Use the ``django.test.Client`` class to make requests.
:meth:`~django.contrib.auth.models.UserManager.create_user` helper :meth:`~django.contrib.auth.models.UserManager.create_user` helper
method to create a new user with a correctly hashed password. method to create a new user with a correctly hashed password.
.. versionadded:: 1.7
Requests made with :meth:`~django.test.Client.login` go through the
request middleware. If you need to control the environment, you can
do so at :class:`~django.test.Client` instantiation or with the
`Client.defaults` attribute.
.. method:: Client.logout() .. method:: Client.logout()
If your site uses Django's :doc:`authentication system</topics/auth/index>`, If your site uses Django's :doc:`authentication system</topics/auth/index>`,
@ -377,13 +370,6 @@ Use the ``django.test.Client`` class to make requests.
and session data cleared to defaults. Subsequent requests will appear and session data cleared to defaults. Subsequent requests will appear
to come from an :class:`~django.contrib.auth.models.AnonymousUser`. to come from an :class:`~django.contrib.auth.models.AnonymousUser`.
.. versionadded:: 1.7
Requests made with :meth:`~django.test.Client.logout` go through the
request middleware. If you need to control the environment, you can
do so at :class:`~django.test.Client` instantiation or with the
`Client.defaults` attribute.
Testing responses Testing responses
~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~

View File

@ -7,7 +7,6 @@ from __future__ import unicode_literals
import os import os
import itertools import itertools
from django.conf import settings
from django.core.urlresolvers import reverse, NoReverseMatch from django.core.urlresolvers import reverse, NoReverseMatch
from django.template import (TemplateSyntaxError, from django.template import (TemplateSyntaxError,
Context, Template, loader) Context, Template, loader)
@ -804,11 +803,6 @@ class AssertFormsetErrorTests(TestCase):
**kwargs) **kwargs)
class ProcessedMiddleware(object):
def process_request(self, request):
request.has_been_processed = True
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',), @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
ROOT_URLCONF='test_client_regress.urls',) ROOT_URLCONF='test_client_regress.urls',)
class LoginTests(TestCase): class LoginTests(TestCase):
@ -830,24 +824,6 @@ class LoginTests(TestCase):
# default client. # default client.
self.assertRedirects(response, "http://testserver/get_view/") self.assertRedirects(response, "http://testserver/get_view/")
@override_settings(
MIDDLEWARE_CLASSES=list(settings.MIDDLEWARE_CLASSES) +
['test_client_regress.tests.ProcessedMiddleware'])
def test_request_middleware(self):
"Check that the request middleware is executed on login request"
def listener(sender, signal, **kwargs):
request = kwargs['request']
self.assertTrue(hasattr(request, 'has_been_processed'))
# Unlike other Client request performing methods, login and logout don't
# return the response, therefore we must use signals to get it
user_logged_in.connect(listener)
try:
self.client.login(username='testclient', password='password')
finally:
user_logged_in.disconnect(listener)
@override_settings( @override_settings(
PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',), PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
@ -1348,32 +1324,12 @@ class UploadedFileEncodingTest(TestCase):
@override_settings(ROOT_URLCONF='test_client_regress.urls',) @override_settings(ROOT_URLCONF='test_client_regress.urls',)
class RequestHeadersTest(TestCase): class RequestHeadersTest(TestCase):
fixtures = ['testdata']
def test_client_headers(self): def test_client_headers(self):
"A test client can receive custom headers" "A test client can receive custom headers"
response = self.client.get("/check_headers/", HTTP_X_ARG_CHECK='Testing 123') response = self.client.get("/check_headers/", HTTP_X_ARG_CHECK='Testing 123')
self.assertEqual(response.content, b"HTTP_X_ARG_CHECK: Testing 123") self.assertEqual(response.content, b"HTTP_X_ARG_CHECK: Testing 123")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
def test_client_login_headers(self):
"Test client headers are used in login"
client = Client(HTTP_HOST='different')
def listener(sender, signal, **kwargs):
request = kwargs['request']
self.assertEqual(request.get_host(), 'different')
# Unlike other Client request performing methods, login and logout don't
# return the response, therefore we must use signals to get it
user_logged_in.connect(listener)
try:
client.login(username='testclient', password='password')
finally:
user_logged_in.disconnect(listener)
def test_client_headers_redirect(self): def test_client_headers_redirect(self):
"Test client headers are preserved through redirects" "Test client headers are preserved through redirects"
response = self.client.get("/check_headers_redirect/", follow=True, HTTP_X_ARG_CHECK='Testing 123') response = self.client.get("/check_headers_redirect/", follow=True, HTTP_X_ARG_CHECK='Testing 123')