tests.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. TEMPLATE_CONTEXT_PROCESSORS=('django.template.context_processors.request',),
  8. )
  9. class RequestContextProcessorTests(TestCase):
  10. """
  11. Tests for the ``django.template.context_processors.request`` processor.
  12. """
  13. def test_request_attributes(self):
  14. """
  15. Test that the request object is available in the template and that its
  16. attributes can't be overridden by GET and POST parameters (#3828).
  17. """
  18. url = '/request_attrs/'
  19. # We should have the request object in the template.
  20. response = self.client.get(url)
  21. self.assertContains(response, 'Have request')
  22. # Test is_secure.
  23. response = self.client.get(url)
  24. self.assertContains(response, 'Not secure')
  25. response = self.client.get(url, {'is_secure': 'blah'})
  26. self.assertContains(response, 'Not secure')
  27. response = self.client.post(url, {'is_secure': 'blah'})
  28. self.assertContains(response, 'Not secure')
  29. # Test path.
  30. response = self.client.get(url)
  31. self.assertContains(response, url)
  32. response = self.client.get(url, {'path': '/blah/'})
  33. self.assertContains(response, url)
  34. response = self.client.post(url, {'path': '/blah/'})
  35. self.assertContains(response, url)
  36. @override_settings(
  37. DEBUG=True,
  38. INTERNAL_IPS=('127.0.0.1',),
  39. ROOT_URLCONF='context_processors.urls',
  40. TEMPLATE_CONTEXT_PROCESSORS=('django.template.context_processors.debug',),
  41. )
  42. class DebugContextProcessorTests(TestCase):
  43. """
  44. Tests for the ``django.template.context_processors.debug`` processor.
  45. """
  46. def test_debug(self):
  47. url = '/debug/'
  48. # We should have the debug flag in the template.
  49. response = self.client.get(url)
  50. self.assertContains(response, 'Have debug')
  51. # And now we should not
  52. with override_settings(DEBUG=False):
  53. response = self.client.get(url)
  54. self.assertNotContains(response, 'Have debug')
  55. def test_sql_queries(self):
  56. """
  57. Test whether sql_queries represents the actual amount
  58. of queries executed. (#23364)
  59. """
  60. url = '/debug/'
  61. response = self.client.get(url)
  62. self.assertContains(response, 'First query list: 0')
  63. self.assertContains(response, 'Second query list: 1')
  64. # Check we have not actually memoized connection.queries
  65. self.assertContains(response, 'Third query list: 2')