From 2790cf482d5e5e428759c5e8e6e63db3c740f389 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 18 Oct 2010 23:58:06 +0000 Subject: [PATCH] Fixed #13842 -- Added tests to verify that XViewMiddleware works with class-based views. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14269 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../regressiontests/special_headers/tests.py | 45 +++++++++++++------ tests/regressiontests/special_headers/urls.py | 3 +- .../regressiontests/special_headers/views.py | 6 ++- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/tests/regressiontests/special_headers/tests.py b/tests/regressiontests/special_headers/tests.py index f304bfa9320..4e2741b6dcf 100644 --- a/tests/regressiontests/special_headers/tests.py +++ b/tests/regressiontests/special_headers/tests.py @@ -8,33 +8,52 @@ class SpecialHeadersTest(TestCase): user = User.objects.get(username='super') response = self.client.get('/special_headers/article/1/') # import pdb; pdb.set_trace() - self.failUnless('X-Object-Type' not in response) + self.assertFalse('X-Object-Type' in response) self.client.login(username='super', password='secret') response = self.client.get('/special_headers/article/1/') - self.failUnless('X-Object-Type' in response) + self.assertTrue('X-Object-Type' in response) user.is_staff = False user.save() response = self.client.get('/special_headers/article/1/') - self.failUnless('X-Object-Type' not in response) + self.assertFalse('X-Object-Type' in response) user.is_staff = True user.is_active = False user.save() response = self.client.get('/special_headers/article/1/') - self.failUnless('X-Object-Type' not in response) + self.assertFalse('X-Object-Type' in response) - def test_xview(self): + def test_xview_func(self): user = User.objects.get(username='super') - response = self.client.head('/special_headers/xview/') - self.failUnless('X-View' not in response) + response = self.client.head('/special_headers/xview/func/') + self.assertFalse('X-View' in response) self.client.login(username='super', password='secret') - response = self.client.head('/special_headers/xview/') - self.failUnless('X-View' in response) + response = self.client.head('/special_headers/xview/func/') + self.assertTrue('X-View' in response) + self.assertEqual(response['X-View'], 'regressiontests.special_headers.views.xview') user.is_staff = False user.save() - response = self.client.head('/special_headers/xview/') - self.failUnless('X-View' not in response) + response = self.client.head('/special_headers/xview/func/') + self.assertFalse('X-View' in response) user.is_staff = True user.is_active = False user.save() - response = self.client.head('/special_headers/xview/') - self.failUnless('X-View' not in response) + response = self.client.head('/special_headers/xview/func/') + self.assertFalse('X-View' in response) + + def test_xview_class(self): + user = User.objects.get(username='super') + response = self.client.head('/special_headers/xview/class/') + self.assertFalse('X-View' in response) + self.client.login(username='super', password='secret') + response = self.client.head('/special_headers/xview/class/') + self.assertTrue('X-View' in response) + self.assertEqual(response['X-View'], 'regressiontests.special_headers.views.XViewClass') + user.is_staff = False + user.save() + response = self.client.head('/special_headers/xview/class/') + self.assertFalse('X-View' in response) + user.is_staff = True + user.is_active = False + user.save() + response = self.client.head('/special_headers/xview/class/') + self.assertFalse('X-View' in response) diff --git a/tests/regressiontests/special_headers/urls.py b/tests/regressiontests/special_headers/urls.py index 721f60ad949..c6b05d62664 100644 --- a/tests/regressiontests/special_headers/urls.py +++ b/tests/regressiontests/special_headers/urls.py @@ -6,5 +6,6 @@ import views urlpatterns = patterns('', (r'^article/(?P\d+)/$', object_detail, {'queryset': Article.objects.all()}), - (r'^xview/$', views.xview), + (r'^xview/func/$', views.xview_dec(views.xview)), + (r'^xview/class/$', views.xview_dec(views.XViewClass.as_view())), ) diff --git a/tests/regressiontests/special_headers/views.py b/tests/regressiontests/special_headers/views.py index 7a012038a1a..7ec99ad4e1f 100644 --- a/tests/regressiontests/special_headers/views.py +++ b/tests/regressiontests/special_headers/views.py @@ -1,10 +1,14 @@ # -*- coding:utf-8 -*- from django.http import HttpResponse from django.utils.decorators import decorator_from_middleware +from django.views.generic import View from django.middleware.doc import XViewMiddleware xview_dec = decorator_from_middleware(XViewMiddleware) def xview(request): return HttpResponse() -xview = xview_dec(xview) + +class XViewClass(View): + def get(request): + return HttpResponse()