test_page_models.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. from django.test import Client
  2. from wagtail.test.utils import WagtailPageTests
  3. from wagtailcrx.models.page_models import (
  4. CoderedArticleIndexPage,
  5. CoderedArticlePage,
  6. CoderedEventIndexPage,
  7. CoderedEventPage,
  8. CoderedFormPage,
  9. CoderedLocationIndexPage,
  10. CoderedLocationPage,
  11. CoderedPage,
  12. CoderedStreamFormPage,
  13. CoderedWebPage,
  14. get_page_models
  15. )
  16. from wagtailcrx.models.snippet_models import Classifier, ClassifierTerm
  17. from wagtailcrx.tests.testapp.models import (
  18. ArticleIndexPage,
  19. ArticlePage,
  20. EventIndexPage,
  21. EventPage,
  22. FormPage,
  23. IndexTestPage,
  24. LocationIndexPage,
  25. LocationPage,
  26. StreamFormPage,
  27. WebPage
  28. )
  29. class BasicPageTestCase():
  30. """
  31. This is a testing mixin used to run common tests for basic versions of page types.
  32. """
  33. class Meta:
  34. abstract = True
  35. def setUp(self):
  36. self.client = Client()
  37. self.basic_page = self.model(
  38. title=str(self.model._meta.verbose_name)
  39. )
  40. self.homepage = WebPage.objects.get(url_path='/home/')
  41. self.homepage.add_child(instance=self.basic_page)
  42. def tearDown(self):
  43. self.basic_page.delete()
  44. def test_get(self):
  45. """
  46. Tests to make sure a basic version of the page serves a 200 from a GET request.
  47. """
  48. response = self.client.get(self.basic_page.url, follow=True)
  49. self.assertEqual(response.status_code, 200)
  50. class AbstractPageTestCase():
  51. """
  52. This is a testing mixin used to run common tests for abstract page types.
  53. """
  54. class Meta:
  55. abstract = True
  56. def test_not_available(self):
  57. """
  58. Tests to make sure the page is not creatable and not in our global
  59. list of page models.
  60. """
  61. self.assertFalse(self.model.is_creatable)
  62. self.assertFalse(self.model in get_page_models())
  63. class ConcretePageTestCase():
  64. """
  65. This is a testing mixin used to run common tests for concrete page types.
  66. """
  67. class Meta:
  68. abstract = True
  69. def test_is_available(self):
  70. """
  71. Tests to make sure the page is creatable and in our global list of
  72. page models.
  73. """
  74. self.assertTrue(self.model.is_creatable)
  75. self.assertTrue(self.model in get_page_models())
  76. class ConcreteBasicPageTestCase(ConcretePageTestCase, BasicPageTestCase):
  77. class Meta:
  78. abstract = True
  79. class ConcreteFormPageTestCase(ConcreteBasicPageTestCase):
  80. class Meta:
  81. abstract = True
  82. def test_post(self):
  83. """
  84. Tests to make sure a basic version of the page serves a 200 from a POST request.
  85. """
  86. response = self.client.post(self.basic_page.url, follow=True)
  87. self.assertEqual(response.status_code, 200)
  88. def test_spam(self):
  89. """
  90. Test to check if the default spam catching works.
  91. """
  92. response = self.client.post(self.basic_page.url, {'cr-decoy-comments': 'This is Spam'}, follow=True) # noqa
  93. messages = list(response.context['messages'])
  94. self.assertEqual(len(messages), 1)
  95. self.assertEqual(str(messages[0]), self.basic_page.get_spam_message())
  96. def test_not_spam(self):
  97. """
  98. Test to check if the default spam catching won't mark correct posts as spam.
  99. """
  100. response = self.client.post(self.basic_page.url)
  101. self.assertFalse(hasattr(response, 'is_spam'))
  102. class CoderedArticleIndexPageTestCase(AbstractPageTestCase, WagtailPageTests):
  103. model = CoderedArticleIndexPage
  104. class CoderedArticlePageTestCase(AbstractPageTestCase, WagtailPageTests):
  105. model = CoderedArticlePage
  106. class CoderedFormPageTestCase(AbstractPageTestCase, WagtailPageTests):
  107. model = CoderedFormPage
  108. class CoderedPageTestCase(WagtailPageTests):
  109. model = CoderedPage
  110. def test_not_available(self):
  111. self.assertFalse(self.model.is_creatable)
  112. self.assertTrue(self.model in get_page_models())
  113. class CoderedWebPageTestCase(AbstractPageTestCase, WagtailPageTests):
  114. model = CoderedWebPage
  115. class CoderedLocationIndexPageTestCase(AbstractPageTestCase, WagtailPageTests):
  116. model = CoderedLocationIndexPage
  117. class CoderedLocationPageTestCase(AbstractPageTestCase, WagtailPageTests):
  118. model = CoderedLocationPage
  119. class CoderedEventIndexPageTestCase(AbstractPageTestCase, WagtailPageTests):
  120. model = CoderedEventIndexPage
  121. class CoderedEventPageTestCase(AbstractPageTestCase, WagtailPageTests):
  122. model = CoderedEventPage
  123. class CoderedStreamFormPageTestCase(AbstractPageTestCase, WagtailPageTests):
  124. model = CoderedStreamFormPage
  125. # -- CONCRETE PAGES ------------------------------------------------------------
  126. class ArticlePageTestCase(ConcreteBasicPageTestCase, WagtailPageTests):
  127. model = ArticlePage
  128. class ArticleIndexPageTestCase(ConcreteBasicPageTestCase, WagtailPageTests):
  129. model = ArticleIndexPage
  130. class FormPageTestCase(ConcreteFormPageTestCase, WagtailPageTests):
  131. model = FormPage
  132. class WebPageTestCase(ConcreteBasicPageTestCase, WagtailPageTests):
  133. model = WebPage
  134. class EventIndexPageTestCase(ConcreteBasicPageTestCase, WagtailPageTests):
  135. model = EventIndexPage
  136. class EventPageTestCase(ConcreteBasicPageTestCase, WagtailPageTests):
  137. model = EventPage
  138. class LocationIndexPageTestCase(ConcreteBasicPageTestCase, WagtailPageTests):
  139. model = LocationIndexPage
  140. class LocationPageTestCase(ConcreteBasicPageTestCase, WagtailPageTests):
  141. model = LocationPage
  142. class StreamFormPageTestCase(ConcreteFormPageTestCase, WagtailPageTests):
  143. model = StreamFormPage
  144. # -- PAGES FOR TESTING SPECIFIC FUNCTIONALITY ----------------------------------
  145. class IndexTestCase(ConcreteBasicPageTestCase, WagtailPageTests):
  146. """
  147. Tests indexing features (show/sort/filter child pages).
  148. """
  149. model = IndexTestPage
  150. def setUp(self):
  151. super().setUp()
  152. # Create some child pages under this page.
  153. self.child_1 = WebPage(title=f"{self.basic_page.title} - Child 1")
  154. self.basic_page.add_child(instance=self.child_1)
  155. self.child_2 = WebPage(title=f"{self.basic_page.title} - Child 2")
  156. self.basic_page.add_child(instance=self.child_2)
  157. self.child_3 = WebPage(title=f"{self.basic_page.title} - Child 3")
  158. self.basic_page.add_child(instance=self.child_3)
  159. # Create some classifier terms for general purpose use.
  160. self.classifier = Classifier.objects.create(name="Classifier")
  161. self.term_a = ClassifierTerm.objects.create(
  162. classifier=self.classifier,
  163. name="Term A",
  164. sort_order=0,
  165. )
  166. self.term_b = ClassifierTerm.objects.create(
  167. classifier=self.classifier,
  168. name="Term B",
  169. sort_order=1,
  170. )
  171. def tearDown(self):
  172. super().tearDown()
  173. self.classifier.delete()
  174. def test_get_index_children(self):
  175. """
  176. Tests to make sure `get_index_children()` returns the correct queryset
  177. based on selected page settings.
  178. """
  179. # Test it without setting any options, ensure it is not broken.
  180. children = self.basic_page.get_index_children()
  181. self.assertIn(self.child_1, children)
  182. self.assertIn(self.child_2, children)
  183. self.assertIn(self.child_3, children)
  184. # Test index_order_by returns in the correct order.
  185. self.basic_page.index_order_by = "title"
  186. self.basic_page.save()
  187. children = self.basic_page.get_index_children()
  188. self.assertEqual(self.child_1, children[0])
  189. self.assertEqual(self.child_2, children[1])
  190. self.assertEqual(self.child_3, children[2])
  191. # Test index_order_by classifier returns in the correct order.
  192. self.basic_page.index_order_by_classifier = self.classifier
  193. self.basic_page.index_order_by = "title"
  194. self.basic_page.save()
  195. self.child_3.classifier_terms.add(self.term_a)
  196. self.child_3.save()
  197. self.child_1.classifier_terms.add(self.term_b)
  198. self.child_1.save()
  199. self.child_2.classifier_terms.add(self.term_b)
  200. self.child_2.save()
  201. children = self.basic_page.get_index_children()
  202. self.assertEqual(self.child_3, children[0])
  203. self.assertEqual(self.child_1, children[1])
  204. self.assertEqual(self.child_2, children[2])