From 0cac4fbf699bb6a3de5f4a48c6e047a4dc6c2df7 Mon Sep 17 00:00:00 2001 From: Bojan Mihelac Date: Mon, 4 Feb 2013 16:50:15 +0100 Subject: [PATCH] Fixed #18356 -- Gave the test client signals.template_rendered call a unique dispatch_uid This prevents the test client context from being lost when the client is used in a nested fashion. --- AUTHORS | 1 + django/test/client.py | 5 +++-- tests/test_client_regress/tests.py | 8 ++++++++ tests/test_client_regress/urls.py | 1 + tests/test_client_regress/views.py | 13 ++++++++++++- 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 15f3e8dbf42..9300c07a4ff 100644 --- a/AUTHORS +++ b/AUTHORS @@ -418,6 +418,7 @@ answer newbie questions, and generally made Django that much better: Christian Metts michal@plovarna.cz Justin Michalicek + Bojan Mihelac Slawek Mikula Katie Miller Shawn Milochik diff --git a/django/test/client.py b/django/test/client.py index 754d4d73f83..ae7d7a5fb79 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -406,7 +406,8 @@ class Client(RequestFactory): # callback function. data = {} on_template_render = curry(store_rendered_templates, data) - signals.template_rendered.connect(on_template_render, dispatch_uid="template-render") + signal_uid = "template-render-%s" % id(request) + signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid) # Capture exceptions created by the handler. got_request_exception.connect(self.store_exc_info, dispatch_uid="request-exception") try: @@ -452,7 +453,7 @@ class Client(RequestFactory): return response finally: - signals.template_rendered.disconnect(dispatch_uid="template-render") + signals.template_rendered.disconnect(dispatch_uid=signal_uid) got_request_exception.disconnect(dispatch_uid="request-exception") def get(self, path, data={}, follow=False, **extra): diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py index 67e66fa52d5..857d6d83c02 100644 --- a/tests/test_client_regress/tests.py +++ b/tests/test_client_regress/tests.py @@ -925,6 +925,14 @@ class ContextTests(TestCase): finally: django.template.context._standard_context_processors = None + def test_nested_requests(self): + """ + response.context is not lost when view call another view. + """ + response = self.client.get("/test_client_regress/nested_view/") + self.assertEqual(response.context.__class__, Context) + self.assertEqual(response.context['nested'], 'yes') + @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',)) class SessionTests(TestCase): diff --git a/tests/test_client_regress/urls.py b/tests/test_client_regress/urls.py index 6a0b330e02c..1bde10315d8 100644 --- a/tests/test_client_regress/urls.py +++ b/tests/test_client_regress/urls.py @@ -11,6 +11,7 @@ urlpatterns = patterns('', (r'^request_data/$', views.request_data), (r'^request_data_extended/$', views.request_data, {'template':'extended.html', 'data':'bacon'}), url(r'^arg_view/(?P.+)/$', views.view_with_argument, name='arg_view'), + url(r'^nested_view/$', views.nested_view, name='nested_view'), (r'^login_protected_redirect_view/$', views.login_protected_redirect_view), (r'^redirects/$', RedirectView.as_view(url='/test_client_regress/redirects/further/')), (r'^redirects/further/$', RedirectView.as_view(url='/test_client_regress/redirects/further/more/')), diff --git a/tests/test_client_regress/views.py b/tests/test_client_regress/views.py index 71e5b526e5a..d7c54cf2f72 100644 --- a/tests/test_client_regress/views.py +++ b/tests/test_client_regress/views.py @@ -5,8 +5,10 @@ from django.contrib.auth.decorators import login_required from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render_to_response from django.core.serializers.json import DjangoJSONEncoder -from django.test.client import CONTENT_TYPE_RE from django.template import RequestContext +from django.test import Client +from django.test.client import CONTENT_TYPE_RE +from django.test.utils import setup_test_environment class CustomTestException(Exception): @@ -52,6 +54,15 @@ def view_with_argument(request, name): else: return HttpResponse('Howdy, %s' % name) +def nested_view(request): + """ + A view that uses test client to call another view. + """ + setup_test_environment() + c = Client() + c.get("/test_client_regress/no_template_view") + return render_to_response('base.html', {'nested':'yes'}) + def login_protected_redirect_view(request): "A view that redirects all requests to the GET view" return HttpResponseRedirect('/test_client_regress/get_view/')