1. """
    
  2. Tests for Django's bundled context processors.
    
  3. """
    
  4. from django.test import SimpleTestCase, TestCase, override_settings
    
  5. 
    
  6. 
    
  7. @override_settings(
    
  8.     ROOT_URLCONF="context_processors.urls",
    
  9.     TEMPLATES=[
    
  10.         {
    
  11.             "BACKEND": "django.template.backends.django.DjangoTemplates",
    
  12.             "APP_DIRS": True,
    
  13.             "OPTIONS": {
    
  14.                 "context_processors": [
    
  15.                     "django.template.context_processors.request",
    
  16.                 ],
    
  17.             },
    
  18.         }
    
  19.     ],
    
  20. )
    
  21. class RequestContextProcessorTests(SimpleTestCase):
    
  22.     """
    
  23.     Tests for the ``django.template.context_processors.request`` processor.
    
  24.     """
    
  25. 
    
  26.     def test_request_attributes(self):
    
  27.         """
    
  28.         The request object is available in the template and that its
    
  29.         attributes can't be overridden by GET and POST parameters (#3828).
    
  30.         """
    
  31.         url = "/request_attrs/"
    
  32.         # We should have the request object in the template.
    
  33.         response = self.client.get(url)
    
  34.         self.assertContains(response, "Have request")
    
  35.         # Test is_secure.
    
  36.         response = self.client.get(url)
    
  37.         self.assertContains(response, "Not secure")
    
  38.         response = self.client.get(url, {"is_secure": "blah"})
    
  39.         self.assertContains(response, "Not secure")
    
  40.         response = self.client.post(url, {"is_secure": "blah"})
    
  41.         self.assertContains(response, "Not secure")
    
  42.         # Test path.
    
  43.         response = self.client.get(url)
    
  44.         self.assertContains(response, url)
    
  45.         response = self.client.get(url, {"path": "/blah/"})
    
  46.         self.assertContains(response, url)
    
  47.         response = self.client.post(url, {"path": "/blah/"})
    
  48.         self.assertContains(response, url)
    
  49. 
    
  50. 
    
  51. @override_settings(
    
  52.     DEBUG=True,
    
  53.     INTERNAL_IPS=["127.0.0.1"],
    
  54.     ROOT_URLCONF="context_processors.urls",
    
  55.     TEMPLATES=[
    
  56.         {
    
  57.             "BACKEND": "django.template.backends.django.DjangoTemplates",
    
  58.             "APP_DIRS": True,
    
  59.             "OPTIONS": {
    
  60.                 "context_processors": [
    
  61.                     "django.template.context_processors.debug",
    
  62.                 ],
    
  63.             },
    
  64.         }
    
  65.     ],
    
  66. )
    
  67. class DebugContextProcessorTests(TestCase):
    
  68.     """
    
  69.     Tests for the ``django.template.context_processors.debug`` processor.
    
  70.     """
    
  71. 
    
  72.     databases = {"default", "other"}
    
  73. 
    
  74.     def test_debug(self):
    
  75.         url = "/debug/"
    
  76.         # We should have the debug flag in the template.
    
  77.         response = self.client.get(url)
    
  78.         self.assertContains(response, "Have debug")
    
  79. 
    
  80.         # And now we should not
    
  81.         with override_settings(DEBUG=False):
    
  82.             response = self.client.get(url)
    
  83.             self.assertNotContains(response, "Have debug")
    
  84. 
    
  85.     def test_sql_queries(self):
    
  86.         """
    
  87.         Test whether sql_queries represents the actual amount
    
  88.         of queries executed. (#23364)
    
  89.         """
    
  90.         url = "/debug/"
    
  91.         response = self.client.get(url)
    
  92.         self.assertContains(response, "First query list: 0")
    
  93.         self.assertContains(response, "Second query list: 1")
    
  94.         # Check we have not actually memoized connection.queries
    
  95.         self.assertContains(response, "Third query list: 2")
    
  96.         # Check queries for DB connection 'other'
    
  97.         self.assertContains(response, "Fourth query list: 3")