mirror of https://github.com/django/django.git
[1.7.x] Revert "Fixed #15179 -- middlewares not applied for test client login()"
This reverts commit4fdd51b732
. See the ticket for concerns with this implementation; it will be revisited. Backport ofaabceadd7d
from master
This commit is contained in:
parent
edca57817f
commit
1d20693fa6
|
@ -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:
|
||||||
|
|
|
@ -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.
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
@ -770,11 +769,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',))
|
||||||
class LoginTests(TestCase):
|
class LoginTests(TestCase):
|
||||||
urls = 'test_client_regress.urls'
|
urls = 'test_client_regress.urls'
|
||||||
|
@ -796,24 +790,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',),
|
||||||
|
@ -1314,7 +1290,6 @@ class UploadedFileEncodingTest(TestCase):
|
||||||
|
|
||||||
class RequestHeadersTest(TestCase):
|
class RequestHeadersTest(TestCase):
|
||||||
urls = 'test_client_regress.urls'
|
urls = 'test_client_regress.urls'
|
||||||
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"
|
||||||
|
@ -1322,24 +1297,6 @@ class RequestHeadersTest(TestCase):
|
||||||
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')
|
||||||
|
|
Loading…
Reference in New Issue