2
0

tests.py 2.4 KB

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