Code

Ticket #7299: django_xview_middleware.diff

File django_xview_middleware.diff, 2.6 KB (added by ishikawa_takanori, 6 years ago)
Line 
1Index: django/middleware/doc.py
2===================================================================
3--- django/middleware/doc.py    (revision 7547)
4+++ django/middleware/doc.py    (working copy)
5@@ -12,7 +12,7 @@
6         indicating the view function.  This is used by the documentation module
7         to lookup the view function for an arbitrary page.
8         """
9-        if request.method == 'HEAD' and (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS or (request.user.is_authenticated() and request.user.is_staff)):
10+        if request.method == 'HEAD' and (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS or (hasattr(request, 'user') and request.user.is_authenticated() and request.user.is_staff)):
11             response = http.HttpResponse()
12             response['X-View'] = "%s.%s" % (view_func.__module__, view_func.__name__)
13             return response
14Index: tests/regressiontests/middleware/tests.py
15===================================================================
16--- tests/regressiontests/middleware/tests.py   (revision 7547)
17+++ tests/regressiontests/middleware/tests.py   (working copy)
18@@ -3,7 +3,10 @@
19 from django.test import TestCase
20 from django.http import HttpRequest
21 from django.middleware.common import CommonMiddleware
22+from django.middleware.doc import XViewMiddleware
23+from django.views.generic.simple import direct_to_template
24 from django.conf import settings
25+from django.conf import global_settings
26 
27 class CommonMiddlewareTest(TestCase):
28     def _get_request(self, path):
29@@ -89,3 +92,30 @@
30         self.assertEquals(
31             r['Location'],
32             'http://testserver/middleware/needsquoting%23/')
33+
34+
35+class XViewMiddlewareTest(TestCase):
36+    def tearDown(self):
37+        settings.INTERNAL_IPS = global_settings.INTERNAL_IPS
38+
39+    def test_internal_ips(self):
40+        """
41+        Tests that request method is HEAD and the IP is internal.
42+        """
43+        settings.INTERNAL_IPS = ('127.0.0.1',)
44+        request = HttpRequest()
45+        request.method, request.path = 'HEAD', "/middleware/xview"
46+       
47+        request.META = {
48+            'SERVER_NAME': 'testserver',
49+            'SERVER_PORT': 80,
50+            'REMOTE_ADDR': settings.INTERNAL_IPS[0],
51+        }
52+        response = XViewMiddleware().process_view(request, direct_to_template, (), {})
53+        self.assertEquals(response.status_code, 200)
54+        self.assertEquals(response['X-View'], 'django.views.generic.simple.direct_to_template')
55+       
56+        # No INTERNAL_IPS
57+        request.META['REMOTE_ADDR'] = '127.0.0.2'
58+        response = XViewMiddleware().process_view(request, direct_to_template, (), {})
59+        self.assert_(response is None)