test_views.py 5.5 KB

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