From 1d20693fa67f314de08e51af57af6fb5460b7b0c Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 28 Apr 2014 18:41:16 -0400 Subject: [PATCH] [1.7.x] Revert "Fixed #15179 -- middlewares not applied for test client login()" This reverts commit 4fdd51b73240bf9c8d9472fcc45df699f0714755. See the ticket for concerns with this implementation; it will be revisited. Backport of aabceadd7d from master --- django/test/client.py | 9 +++---- docs/releases/1.7.txt | 5 ---- docs/topics/testing/tools.txt | 14 ---------- tests/test_client_regress/tests.py | 43 ------------------------------ 4 files changed, 4 insertions(+), 67 deletions(-) diff --git a/django/test/client.py b/django/test/client.py index e6a9593dfd..9513dcd809 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -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: diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index 3faac5fbbe..c27d5b96fa 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -802,11 +802,6 @@ Tests :class:`~django.test.Client`. If ``True``, the request will be made through HTTPS. -* Requests made with :meth:`Client.login() ` and - :meth:`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. diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt index 223fcd45a8..c973ffdc32 100644 --- a/docs/topics/testing/tools.txt +++ b/docs/topics/testing/tools.txt @@ -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`, @@ -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 ~~~~~~~~~~~~~~~~~ diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py index d25d36b09f..996f70b667 100644 --- a/tests/test_client_regress/tests.py +++ b/tests/test_client_regress/tests.py @@ -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) @@ -770,11 +769,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',)) class LoginTests(TestCase): urls = 'test_client_regress.urls' @@ -796,24 +790,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',), @@ -1314,7 +1290,6 @@ class UploadedFileEncodingTest(TestCase): class RequestHeadersTest(TestCase): urls = 'test_client_regress.urls' - fixtures = ['testdata'] def test_client_headers(self): "A test client can receive custom headers" @@ -1322,24 +1297,6 @@ class RequestHeadersTest(TestCase): 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')