test_context_processors.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from django.contrib.auth import authenticate
  2. from django.contrib.auth.context_processors import PermLookupDict, PermWrapper
  3. from django.contrib.auth.models import Permission, User
  4. from django.contrib.contenttypes.models import ContentType
  5. from django.db.models import Q
  6. from django.test import SimpleTestCase, TestCase, override_settings
  7. from .settings import AUTH_MIDDLEWARE, AUTH_TEMPLATES
  8. class MockUser:
  9. def has_module_perms(self, perm):
  10. if perm == 'mockapp':
  11. return True
  12. return False
  13. def has_perm(self, perm):
  14. if perm == 'mockapp.someperm':
  15. return True
  16. return False
  17. class PermWrapperTests(SimpleTestCase):
  18. """
  19. Test some details of the PermWrapper implementation.
  20. """
  21. class EQLimiterObject:
  22. """
  23. This object makes sure __eq__ will not be called endlessly.
  24. """
  25. def __init__(self):
  26. self.eq_calls = 0
  27. def __eq__(self, other):
  28. if self.eq_calls > 0:
  29. return True
  30. self.eq_calls += 1
  31. return False
  32. def test_permwrapper_in(self):
  33. """
  34. 'something' in PermWrapper works as expected.
  35. """
  36. perms = PermWrapper(MockUser())
  37. # Works for modules and full permissions.
  38. self.assertIn('mockapp', perms)
  39. self.assertNotIn('nonexisting', perms)
  40. self.assertIn('mockapp.someperm', perms)
  41. self.assertNotIn('mockapp.nonexisting', perms)
  42. def test_permlookupdict_in(self):
  43. """
  44. No endless loops if accessed with 'in' - refs #18979.
  45. """
  46. pldict = PermLookupDict(MockUser(), 'mockapp')
  47. with self.assertRaises(TypeError):
  48. self.EQLimiterObject() in pldict
  49. @override_settings(ROOT_URLCONF='auth_tests.urls', TEMPLATES=AUTH_TEMPLATES)
  50. class AuthContextProcessorTests(TestCase):
  51. """
  52. Tests for the ``django.contrib.auth.context_processors.auth`` processor
  53. """
  54. @classmethod
  55. def setUpTestData(cls):
  56. cls.superuser = User.objects.create_superuser(username='super', password='secret', email='super@example.com')
  57. @override_settings(MIDDLEWARE=AUTH_MIDDLEWARE)
  58. def test_session_not_accessed(self):
  59. """
  60. The session is not accessed simply by including
  61. the auth context processor
  62. """
  63. response = self.client.get('/auth_processor_no_attr_access/')
  64. self.assertContains(response, "Session not accessed")
  65. @override_settings(MIDDLEWARE=AUTH_MIDDLEWARE)
  66. def test_session_is_accessed(self):
  67. """
  68. The session is accessed if the auth context processor
  69. is used and relevant attributes accessed.
  70. """
  71. response = self.client.get('/auth_processor_attr_access/')
  72. self.assertContains(response, "Session accessed")
  73. def test_perms_attrs(self):
  74. u = User.objects.create_user(username='normal', password='secret')
  75. u.user_permissions.add(
  76. Permission.objects.get(
  77. content_type=ContentType.objects.get_for_model(Permission),
  78. codename='add_permission'))
  79. self.client.force_login(u)
  80. response = self.client.get('/auth_processor_perms/')
  81. self.assertContains(response, "Has auth permissions")
  82. self.assertContains(response, "Has auth.add_permission permissions")
  83. self.assertNotContains(response, "nonexisting")
  84. def test_perm_in_perms_attrs(self):
  85. u = User.objects.create_user(username='normal', password='secret')
  86. u.user_permissions.add(
  87. Permission.objects.get(
  88. content_type=ContentType.objects.get_for_model(Permission),
  89. codename='add_permission'))
  90. self.client.login(username='normal', password='secret')
  91. response = self.client.get('/auth_processor_perm_in_perms/')
  92. self.assertContains(response, "Has auth permissions")
  93. self.assertContains(response, "Has auth.add_permission permissions")
  94. self.assertNotContains(response, "nonexisting")
  95. def test_message_attrs(self):
  96. self.client.force_login(self.superuser)
  97. response = self.client.get('/auth_processor_messages/')
  98. self.assertContains(response, "Message 1")
  99. def test_user_attrs(self):
  100. """
  101. The lazy objects returned behave just like the wrapped objects.
  102. """
  103. # These are 'functional' level tests for common use cases. Direct
  104. # testing of the implementation (SimpleLazyObject) is in the 'utils'
  105. # tests.
  106. self.client.login(username='super', password='secret')
  107. user = authenticate(username='super', password='secret')
  108. response = self.client.get('/auth_processor_user/')
  109. self.assertContains(response, "unicode: super")
  110. self.assertContains(response, "id: %d" % self.superuser.pk)
  111. self.assertContains(response, "username: super")
  112. # bug #12037 is tested by the {% url %} in the template:
  113. self.assertContains(response, "url: /userpage/super/")
  114. # A Q() comparing a user and with another Q() (in an AND or OR fashion).
  115. Q(user=response.context['user']) & Q(someflag=True)
  116. # Tests for user equality. This is hard because User defines
  117. # equality in a non-duck-typing way
  118. # See bug #12060
  119. self.assertEqual(response.context['user'], user)
  120. self.assertEqual(user, response.context['user'])