/tests/regressiontests/context_processors/tests.py

https://github.com/niran/django-old · Python · 123 lines · 93 code · 1 blank · 29 comment · 2 complexity · 7e92214252c164eac646b1a9774adf2e MD5 · raw file

  1. """
  2. Tests for Django's bundled context processors.
  3. """
  4. import warnings
  5. from django.conf import settings
  6. from django.contrib.auth import authenticate
  7. from django.db.models import Q
  8. from django.test import TestCase
  9. from django.template import Template
  10. class RequestContextProcessorTests(TestCase):
  11. """
  12. Tests for the ``django.core.context_processors.request`` processor.
  13. """
  14. urls = 'regressiontests.context_processors.urls'
  15. def test_request_attributes(self):
  16. """
  17. Test that the request object is available in the template and that its
  18. attributes can't be overridden by GET and POST parameters (#3828).
  19. """
  20. url = '/request_attrs/'
  21. # We should have the request object in the template.
  22. response = self.client.get(url)
  23. self.assertContains(response, 'Have request')
  24. # Test is_secure.
  25. response = self.client.get(url)
  26. self.assertContains(response, 'Not secure')
  27. response = self.client.get(url, {'is_secure': 'blah'})
  28. self.assertContains(response, 'Not secure')
  29. response = self.client.post(url, {'is_secure': 'blah'})
  30. self.assertContains(response, 'Not secure')
  31. # Test path.
  32. response = self.client.get(url)
  33. self.assertContains(response, url)
  34. response = self.client.get(url, {'path': '/blah/'})
  35. self.assertContains(response, url)
  36. response = self.client.post(url, {'path': '/blah/'})
  37. self.assertContains(response, url)
  38. class AuthContextProcessorTests(TestCase):
  39. """
  40. Tests for the ``django.contrib.auth.context_processors.auth`` processor
  41. """
  42. urls = 'regressiontests.context_processors.urls'
  43. fixtures = ['context-processors-users.xml']
  44. def setUp(self):
  45. warnings.filterwarnings('ignore', category=DeprecationWarning,
  46. module='django.contrib.auth.models')
  47. warnings.filterwarnings('ignore', category=DeprecationWarning,
  48. module='django.core.context_processors')
  49. def tearDown(self):
  50. warnings.resetwarnings()
  51. warnings.simplefilter('ignore', PendingDeprecationWarning)
  52. def test_session_not_accessed(self):
  53. """
  54. Tests that the session is not accessed simply by including
  55. the auth context processor
  56. """
  57. response = self.client.get('/auth_processor_no_attr_access/')
  58. self.assertContains(response, "Session not accessed")
  59. def test_session_is_accessed(self):
  60. """
  61. Tests that the session is accessed if the auth context processor
  62. is used and relevant attributes accessed.
  63. """
  64. response = self.client.get('/auth_processor_attr_access/')
  65. self.assertContains(response, "Session accessed")
  66. def test_perms_attrs(self):
  67. self.client.login(username='super', password='secret')
  68. response = self.client.get('/auth_processor_perms/')
  69. self.assertContains(response, "Has auth permissions")
  70. def test_message_attrs(self):
  71. self.client.login(username='super', password='secret')
  72. response = self.client.get('/auth_processor_messages/')
  73. self.assertContains(response, "Message 1")
  74. def test_user_attrs(self):
  75. """
  76. Test that the lazy objects returned behave just like the wrapped objects.
  77. """
  78. # These are 'functional' level tests for common use cases. Direct
  79. # testing of the implementation (SimpleLazyObject) is in the 'utils'
  80. # tests.
  81. self.client.login(username='super', password='secret')
  82. user = authenticate(username='super', password='secret')
  83. response = self.client.get('/auth_processor_user/')
  84. self.assertContains(response, "unicode: super")
  85. self.assertContains(response, "id: 100")
  86. self.assertContains(response, "username: super")
  87. # bug #12037 is tested by the {% url %} in the template:
  88. self.assertContains(response, "url: /userpage/super/")
  89. # See if this object can be used for queries where a Q() comparing
  90. # a user can be used with another Q() (in an AND or OR fashion).
  91. # This simulates what a template tag might do with the user from the
  92. # context. Note that we don't need to execute a query, just build it.
  93. #
  94. # The failure case (bug #12049) on Python 2.4 with a LazyObject-wrapped
  95. # User is a fatal TypeError: "function() takes at least 2 arguments
  96. # (0 given)" deep inside deepcopy().
  97. #
  98. # Python 2.5 and 2.6 succeeded, but logged internally caught exception
  99. # spew:
  100. #
  101. # Exception RuntimeError: 'maximum recursion depth exceeded while
  102. # calling a Python object' in <type 'exceptions.AttributeError'>
  103. # ignored"
  104. query = Q(user=response.context['user']) & Q(someflag=True)
  105. # Tests for user equality. This is hard because User defines
  106. # equality in a non-duck-typing way
  107. # See bug #12060
  108. self.assertEqual(response.context['user'], user)
  109. self.assertEqual(user, response.context['user'])