test_context_processors.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. from django.urls import reverse
  2. from misago.legal.context_processors import legal_links
  3. from misago.legal.models import Agreement
  4. from misago.users.testutils import AuthenticatedUserTestCase
  5. class MockRequest(object):
  6. def __init__(self, user):
  7. self.user = user
  8. self.frontend_context = {}
  9. def get_host(self):
  10. return 'testhost.com'
  11. class PrivacyPolicyTests(AuthenticatedUserTestCase):
  12. def setUp(self):
  13. super(PrivacyPolicyTests, self).setUp()
  14. Agreement.objects.invalidate_cache()
  15. def tearDown(self):
  16. Agreement.objects.invalidate_cache()
  17. def test_context_processor_no_policy(self):
  18. """context processor has no TOS link"""
  19. context_dict = legal_links(MockRequest(self.user))
  20. self.assertEqual(context_dict, {
  21. 'TERMS_OF_SERVICE_URL': None,
  22. 'PRIVACY_POLICY_URL': None,
  23. 'misago_agreement': None,
  24. })
  25. def test_context_processor_misago_policy(self):
  26. """context processor has TOS link to Misago view"""
  27. Agreement.objects.create(
  28. type=Agreement.TYPE_PRIVACY,
  29. text='Lorem ipsum',
  30. is_active=True,
  31. )
  32. context_dict = legal_links(MockRequest(self.user))
  33. self.assertEqual(context_dict, {
  34. 'TERMS_OF_SERVICE_URL': None,
  35. 'PRIVACY_POLICY_URL': reverse('misago:privacy-policy'),
  36. 'misago_agreement': {
  37. 'type': 'Privacy policy',
  38. 'title': 'Privacy policy',
  39. 'link': None,
  40. 'text': '<p>Lorem ipsum</p>',
  41. },
  42. })
  43. def test_context_processor_remote_policy(self):
  44. """context processor has TOS link to remote url"""
  45. agreement = Agreement.objects.create(
  46. type=Agreement.TYPE_PRIVACY,
  47. link='http://test.com',
  48. is_active=True,
  49. )
  50. context_dict = legal_links(MockRequest(self.user))
  51. self.assertEqual(context_dict, {
  52. 'TERMS_OF_SERVICE_URL': None,
  53. 'PRIVACY_POLICY_URL': 'http://test.com',
  54. 'misago_agreement': {
  55. 'type': 'Privacy policy',
  56. 'title': 'Privacy policy',
  57. 'link': 'http://test.com',
  58. 'text': None,
  59. },
  60. })
  61. # set misago view too
  62. agreement.text = 'Lorem ipsum'
  63. agreement.save()
  64. context_dict = legal_links(MockRequest(self.user))
  65. self.assertEqual(context_dict, {
  66. 'TERMS_OF_SERVICE_URL': None,
  67. 'PRIVACY_POLICY_URL': 'http://test.com',
  68. 'misago_agreement': {
  69. 'type': 'Privacy policy',
  70. 'title': 'Privacy policy',
  71. 'link': 'http://test.com',
  72. 'text': '<p>Lorem ipsum</p>',
  73. },
  74. })
  75. class TermsOfServiceTests(AuthenticatedUserTestCase):
  76. def setUp(self):
  77. super(TermsOfServiceTests, self).setUp()
  78. Agreement.objects.invalidate_cache()
  79. def tearDown(self):
  80. Agreement.objects.invalidate_cache()
  81. def test_context_processor_no_tos(self):
  82. """context processor has no TOS link"""
  83. context_dict = legal_links(MockRequest(self.user))
  84. self.assertEqual(context_dict, {
  85. 'TERMS_OF_SERVICE_URL': None,
  86. 'PRIVACY_POLICY_URL': None,
  87. 'misago_agreement': None,
  88. })
  89. def test_context_processor_misago_tos(self):
  90. """context processor has TOS link to Misago view"""
  91. Agreement.objects.create(
  92. type=Agreement.TYPE_TOS,
  93. text='Lorem ipsum',
  94. is_active=True,
  95. )
  96. context_dict = legal_links(MockRequest(self.user))
  97. self.assertEqual(context_dict, {
  98. 'TERMS_OF_SERVICE_URL': reverse('misago:terms-of-service'),
  99. 'PRIVACY_POLICY_URL': None,
  100. 'misago_agreement': {
  101. 'type': 'Terms of service',
  102. 'title': 'Terms of service',
  103. 'link': None,
  104. 'text': '<p>Lorem ipsum</p>',
  105. }
  106. })
  107. def test_context_processor_remote_tos(self):
  108. """context processor has TOS link to remote url"""
  109. agreement = Agreement.objects.create(
  110. type=Agreement.TYPE_TOS,
  111. link='http://test.com',
  112. is_active=True,
  113. )
  114. context_dict = legal_links(MockRequest(self.user))
  115. self.assertEqual(context_dict, {
  116. 'TERMS_OF_SERVICE_URL': 'http://test.com',
  117. 'PRIVACY_POLICY_URL': None,
  118. 'misago_agreement': {
  119. 'type': 'Terms of service',
  120. 'title': 'Terms of service',
  121. 'link': 'http://test.com',
  122. 'text': None,
  123. }
  124. })
  125. # set misago view too
  126. agreement.text = 'Lorem ipsum'
  127. agreement.save()
  128. context_dict = legal_links(MockRequest(self.user))
  129. self.assertEqual(context_dict, {
  130. 'TERMS_OF_SERVICE_URL': 'http://test.com',
  131. 'PRIVACY_POLICY_URL': None,
  132. 'misago_agreement': {
  133. 'type': 'Terms of service',
  134. 'title': 'Terms of service',
  135. 'link': 'http://test.com',
  136. 'text': '<p>Lorem ipsum</p>',
  137. },
  138. })