2
0

test_page_modeladmin.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. from django.contrib.auth import get_user_model
  2. from django.contrib.auth.models import Group, Permission
  3. from django.test import TestCase
  4. from wagtail.core.models import GroupPagePermission, Page
  5. from wagtail.tests.testapp.models import BusinessIndex, EventCategory, EventPage
  6. from wagtail.tests.utils import WagtailTestUtils
  7. class TestIndexView(TestCase, WagtailTestUtils):
  8. fixtures = ['test_specific.json']
  9. def setUp(self):
  10. self.login()
  11. def get(self, **params):
  12. return self.client.get('/admin/tests/eventpage/', params)
  13. def test_simple(self):
  14. response = self.get()
  15. self.assertEqual(response.status_code, 200)
  16. # There are four event pages in the test data
  17. self.assertEqual(response.context['result_count'], 4)
  18. # User has add permission
  19. self.assertEqual(response.context['user_can_create'], True)
  20. def test_filter(self):
  21. # Filter by audience
  22. response = self.get(audience__exact='public')
  23. self.assertEqual(response.status_code, 200)
  24. # Only three of the event page in the test data are 'public'
  25. self.assertEqual(response.context['result_count'], 3)
  26. for eventpage in response.context['object_list']:
  27. self.assertEqual(eventpage.audience, 'public')
  28. def test_search(self):
  29. response = self.get(q='Someone')
  30. self.assertEqual(response.status_code, 200)
  31. # There are two eventpage's where the title contains 'Someone'
  32. self.assertEqual(response.context['result_count'], 1)
  33. def test_ordering(self):
  34. response = self.get(o='0.1')
  35. self.assertEqual(response.status_code, 200)
  36. # There should still be four results
  37. self.assertEqual(response.context['result_count'], 4)
  38. class TestExcludeFromExplorer(TestCase, WagtailTestUtils):
  39. fixtures = ['modeladmintest_test.json']
  40. def setUp(self):
  41. self.login()
  42. def test_attribute_effects_explorer(self):
  43. # The two VenuePages should appear in the venuepage list
  44. response = self.client.get('/admin/modeladmintest/venuepage/')
  45. self.assertContains(response, "Santa's Grotto")
  46. self.assertContains(response, "Santa's Workshop")
  47. # But when viewing the children of 'Christmas' event in explorer
  48. response = self.client.get('/admin/pages/4/')
  49. self.assertNotContains(response, "Santa's Grotto")
  50. self.assertNotContains(response, "Santa's Workshop")
  51. # But the other test page should...
  52. self.assertContains(response, "Claim your free present!")
  53. class TestCreateView(TestCase, WagtailTestUtils):
  54. fixtures = ['test_specific.json']
  55. def setUp(self):
  56. self.login()
  57. def test_redirect_to_choose_parent(self):
  58. # When more than one possible parent page exists, redirect to choose_parent
  59. response = self.client.get('/admin/tests/eventpage/create/')
  60. self.assertRedirects(response, '/admin/tests/eventpage/choose_parent/')
  61. def test_one_parent_exists(self):
  62. # Create a BusinessIndex page that BusinessChild can exist under
  63. homepage = Page.objects.get(url_path='/home/')
  64. business_index = BusinessIndex(title='Business Index')
  65. homepage.add_child(instance=business_index)
  66. # When one possible parent page exists, redirect straight to the page create view
  67. response = self.client.get('/admin/tests/businesschild/create/')
  68. expected_path = '/admin/pages/add/tests/businesschild/%d/' % business_index.pk
  69. expected_next_path = '/admin/tests/businesschild/'
  70. self.assertRedirects(response, '%s?next=%s' % (expected_path, expected_next_path))
  71. class TestInspectView(TestCase, WagtailTestUtils):
  72. fixtures = ['test_specific.json']
  73. def setUp(self):
  74. self.login()
  75. def get(self, id):
  76. return self.client.get('/admin/tests/eventpage/inspect/%d/' % id)
  77. def test_simple(self):
  78. response = self.get(4)
  79. self.assertEqual(response.status_code, 200)
  80. def test_title_present(self):
  81. """
  82. The page title should appear three times. Once in the header, and two times
  83. in the field listing (as the actual title and as the draft title)
  84. """
  85. response = self.get(4)
  86. self.assertContains(response, 'Christmas', 3)
  87. def test_manytomany_output(self):
  88. """
  89. Because ManyToMany fields are output InspectView by default, the
  90. `categories` for the event should output as a comma separated list
  91. once populated.
  92. """
  93. eventpage = EventPage.objects.get(pk=4)
  94. free_category = EventCategory.objects.create(name='Free')
  95. child_friendly_category = EventCategory.objects.create(name='Child-friendly')
  96. eventpage.categories = (free_category, child_friendly_category)
  97. eventpage.save()
  98. response = self.get(4)
  99. self.assertContains(response, '<dd>Free, Child-friendly</dd>', html=True)
  100. def test_false_values_displayed(self):
  101. """
  102. Boolean fields with False values should display False, rather than the
  103. value of `get_empty_value_display()`. For this page, those should be
  104. `locked`, `expired` and `has_unpublished_changes`
  105. """
  106. response = self.get(4)
  107. self.assertContains(response, '<dd>False</dd>', count=3, html=True)
  108. def test_location_present(self):
  109. """
  110. The location should appear once, in the field listing
  111. """
  112. response = self.get(4)
  113. self.assertContains(response, 'The North Pole', 1)
  114. def test_non_existent(self):
  115. response = self.get(100)
  116. self.assertEqual(response.status_code, 404)
  117. class TestEditView(TestCase, WagtailTestUtils):
  118. fixtures = ['test_specific.json']
  119. def setUp(self):
  120. self.login()
  121. def get(self, obj_id):
  122. return self.client.get('/admin/tests/eventpage/edit/%d/' % obj_id)
  123. def test_simple(self):
  124. response = self.get(4)
  125. expected_path = '/admin/pages/4/edit/'
  126. expected_next_path = '/admin/tests/eventpage/'
  127. self.assertRedirects(response, '%s?next=%s' % (expected_path, expected_next_path))
  128. def test_non_existent(self):
  129. response = self.get(100)
  130. self.assertEqual(response.status_code, 404)
  131. class TestDeleteView(TestCase, WagtailTestUtils):
  132. fixtures = ['test_specific.json']
  133. def setUp(self):
  134. self.login()
  135. def get(self, obj_id):
  136. return self.client.get('/admin/tests/eventpage/delete/%d/' % obj_id)
  137. def test_simple(self):
  138. response = self.get(4)
  139. expected_path = '/admin/pages/4/delete/'
  140. expected_next_path = '/admin/tests/eventpage/'
  141. self.assertRedirects(response, '%s?next=%s' % (expected_path, expected_next_path))
  142. class TestChooseParentView(TestCase, WagtailTestUtils):
  143. fixtures = ['test_specific.json']
  144. def setUp(self):
  145. self.login()
  146. def test_simple(self):
  147. response = self.client.get('/admin/tests/eventpage/choose_parent/')
  148. self.assertEqual(response.status_code, 200)
  149. def test_no_parent_exists(self):
  150. response = self.client.get('/admin/tests/businesschild/choose_parent/')
  151. self.assertEqual(response.status_code, 403)
  152. def test_post(self):
  153. response = self.client.post('/admin/tests/eventpage/choose_parent/', {
  154. 'parent_page': 2,
  155. })
  156. expected_path = '/admin/pages/add/tests/eventpage/2/'
  157. expected_next_path = '/admin/tests/eventpage/'
  158. self.assertRedirects(response, '%s?next=%s' % (expected_path, expected_next_path))
  159. class TestChooseParentViewForNonSuperuser(TestCase, WagtailTestUtils):
  160. fixtures = ['test_specific.json']
  161. def setUp(self):
  162. homepage = Page.objects.get(url_path='/home/')
  163. business_index = BusinessIndex(
  164. title='Public Business Index',
  165. draft_title='Public Business Index',
  166. )
  167. homepage.add_child(instance=business_index)
  168. another_business_index = BusinessIndex(
  169. title='Another Business Index',
  170. draft_title='Another Business Index',
  171. )
  172. homepage.add_child(instance=another_business_index)
  173. secret_business_index = BusinessIndex(
  174. title='Private Business Index',
  175. draft_title='Private Business Index',
  176. )
  177. homepage.add_child(instance=secret_business_index)
  178. business_editors = Group.objects.create(name='Business editors')
  179. business_editors.permissions.add(Permission.objects.get(codename='access_admin'))
  180. GroupPagePermission.objects.create(
  181. group=business_editors,
  182. page=business_index,
  183. permission_type='add'
  184. )
  185. GroupPagePermission.objects.create(
  186. group=business_editors,
  187. page=another_business_index,
  188. permission_type='add'
  189. )
  190. user = get_user_model().objects._create_user(username='test2', email='test2@email.com', password='password', is_staff=True, is_superuser=False)
  191. user.groups.add(business_editors)
  192. # Login
  193. self.client.login(username='test2', password='password')
  194. def test_simple(self):
  195. response = self.client.get('/admin/tests/businesschild/choose_parent/')
  196. self.assertEqual(response.status_code, 200)
  197. self.assertContains(response, 'Public Business Index')
  198. self.assertNotContains(response, 'Private Business Index')
  199. class TestEditorAccess(TestCase):
  200. fixtures = ['test_specific.json']
  201. expected_status_code = 403
  202. def login(self):
  203. # Create a user
  204. user = get_user_model().objects._create_user(username='test2', email='test2@email.com', password='password', is_staff=True, is_superuser=False)
  205. user.groups.add(Group.objects.get(pk=2))
  206. # Login
  207. self.client.login(username='test2', password='password')
  208. return user
  209. def setUp(self):
  210. self.login()
  211. def test_delete_permitted(self):
  212. response = self.client.get('/admin/tests/eventpage/delete/4/')
  213. self.assertEqual(response.status_code, self.expected_status_code)
  214. class TestModeratorAccess(TestCase):
  215. fixtures = ['test_specific.json']
  216. expected_status_code = 302
  217. def login(self):
  218. # Create a user
  219. user = get_user_model().objects._create_user(username='test3', email='test3@email.com', password='password', is_staff=True, is_superuser=False)
  220. user.groups.add(Group.objects.get(pk=1))
  221. # Login
  222. self.client.login(username='test2', password='password')
  223. return user
  224. def setUp(self):
  225. self.login()
  226. def test_delete_permitted(self):
  227. response = self.client.get('/admin/tests/eventpage/delete/4/')
  228. self.assertEqual(response.status_code, self.expected_status_code)