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,
got_request_exception)
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.test import signals
from django.utils.functional import curry
@ -553,8 +553,8 @@ class Client(RequestFactory):
apps.is_installed('django.contrib.sessions')):
engine = import_module(settings.SESSION_ENGINE)
# Create a fake request that goes through request middleware
request = self.request().wsgi_request
# Create a fake request to store login details.
request = HttpRequest()
if self.session:
request.session = self.session
@ -588,9 +588,8 @@ class Client(RequestFactory):
Causes the authenticated user to be logged out.
"""
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)
UserModel = get_user_model()
if self.session:

View File

@ -802,11 +802,6 @@ Tests
:class:`~django.test.Client`. If ``True``, the request will be made
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
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
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()
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
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
~~~~~~~~~~~~~~~~~

View File

@ -7,7 +7,6 @@ from __future__ import unicode_literals
import os
import itertools
from django.conf import settings
from django.core.urlresolvers import reverse, NoReverseMatch
from django.template import (TemplateSyntaxError,
Context, Template, loader)
@ -804,11 +803,6 @@ class AssertFormsetErrorTests(TestCase):
**kwargs)
class ProcessedMiddleware(object):
def process_request(self, request):
request.has_been_processed = True
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
ROOT_URLCONF='test_client_regress.urls',)
class LoginTests(TestCase):
@ -830,24 +824,6 @@ class LoginTests(TestCase):
# default client.
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(
PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
@ -1348,32 +1324,12 @@ class UploadedFileEncodingTest(TestCase):
@override_settings(ROOT_URLCONF='test_client_regress.urls',)
class RequestHeadersTest(TestCase):
fixtures = ['testdata']
def test_client_headers(self):
"A test client can receive custom headers"
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.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):
"Test client headers are preserved through redirects"
response = self.client.get("/check_headers_redirect/", follow=True, HTTP_X_ARG_CHECK='Testing 123')