tests.py 2.9 KB

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