test_gotoviews.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. from django.utils import timezone
  2. from misago.acl.testutils import override_acl
  3. from misago.categories.models import Category
  4. from misago.conf import settings
  5. from misago.readtracker.threadstracker import make_thread_read_aware, read_thread
  6. from misago.threads import testutils
  7. from misago.users.testutils import AuthenticatedUserTestCase
  8. GOTO_URL = '%s#post-%s'
  9. GOTO_PAGE_URL = '%s%s/#post-%s'
  10. class GotoViewTestCase(AuthenticatedUserTestCase):
  11. def setUp(self):
  12. super(GotoViewTestCase, self).setUp()
  13. self.category = Category.objects.get(slug='first-category')
  14. self.thread = testutils.post_thread(category=self.category)
  15. class GotoPostTests(GotoViewTestCase):
  16. def test_goto_first_post(self):
  17. """first post redirect url is valid"""
  18. response = self.client.get(self.thread.first_post.get_absolute_url())
  19. self.assertEqual(response.status_code, 302)
  20. self.assertEqual(
  21. response['location'],
  22. GOTO_URL % (self.thread.get_absolute_url(), self.thread.first_post_id)
  23. )
  24. response = self.client.get(response['location'])
  25. self.assertContains(response, self.thread.first_post.get_absolute_url())
  26. def test_goto_last_post_on_page(self):
  27. """last post on page redirect url is valid"""
  28. for _ in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL - 1):
  29. post = testutils.reply_thread(self.thread)
  30. response = self.client.get(post.get_absolute_url())
  31. self.assertEqual(response.status_code, 302)
  32. self.assertEqual(
  33. response['location'], GOTO_URL % (self.thread.get_absolute_url(), post.pk)
  34. )
  35. response = self.client.get(response['location'])
  36. self.assertContains(response, post.get_absolute_url())
  37. def test_goto_first_post_on_next_page(self):
  38. """first post on next page redirect url is valid"""
  39. for _ in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL):
  40. post = testutils.reply_thread(self.thread)
  41. response = self.client.get(post.get_absolute_url())
  42. self.assertEqual(response.status_code, 302)
  43. self.assertEqual(
  44. response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 2, post.pk)
  45. )
  46. response = self.client.get(response['location'])
  47. self.assertContains(response, post.get_absolute_url())
  48. def test_goto_first_post_on_page_three_out_of_five(self):
  49. """first post on next page redirect url is valid"""
  50. posts = []
  51. for _ in range(settings.MISAGO_POSTS_PER_PAGE * 4 - 1):
  52. post = testutils.reply_thread(self.thread)
  53. posts.append(post)
  54. post = posts[settings.MISAGO_POSTS_PER_PAGE * 2 - 3]
  55. response = self.client.get(post.get_absolute_url())
  56. self.assertEqual(response.status_code, 302)
  57. self.assertEqual(
  58. response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 3, post.pk)
  59. )
  60. response = self.client.get(response['location'])
  61. self.assertContains(response, post.get_absolute_url())
  62. def test_goto_first_event_on_page_three_out_of_five(self):
  63. """event redirect url is valid"""
  64. posts = []
  65. for _ in range(settings.MISAGO_POSTS_PER_PAGE * 4 - 1):
  66. post = testutils.reply_thread(self.thread)
  67. posts.append(post)
  68. post = posts[settings.MISAGO_POSTS_PER_PAGE * 2 - 2]
  69. self.thread.has_events = True
  70. self.thread.save()
  71. post.is_event = True
  72. post.save()
  73. response = self.client.get(post.get_absolute_url())
  74. self.assertEqual(response.status_code, 302)
  75. self.assertEqual(
  76. response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 3, post.pk)
  77. )
  78. response = self.client.get(response['location'])
  79. self.assertContains(response, post.get_absolute_url())
  80. class GotoLastTests(GotoViewTestCase):
  81. def test_goto_last_post(self):
  82. """first post redirect url is valid"""
  83. response = self.client.get(self.thread.get_last_post_url())
  84. self.assertEqual(response.status_code, 302)
  85. self.assertEqual(
  86. response['location'],
  87. GOTO_URL % (self.thread.get_absolute_url(), self.thread.first_post_id)
  88. )
  89. response = self.client.get(response['location'])
  90. self.assertContains(response, self.thread.last_post.get_absolute_url())
  91. def test_goto_last_post_on_page(self):
  92. """last post on page redirect url is valid"""
  93. for _ in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL - 1):
  94. post = testutils.reply_thread(self.thread)
  95. response = self.client.get(self.thread.get_last_post_url())
  96. self.assertEqual(response.status_code, 302)
  97. self.assertEqual(
  98. response['location'], GOTO_URL % (self.thread.get_absolute_url(), post.pk)
  99. )
  100. response = self.client.get(response['location'])
  101. self.assertContains(response, post.get_absolute_url())
  102. class GotoNewTests(GotoViewTestCase):
  103. def test_goto_first_post(self):
  104. """first unread post redirect url is valid"""
  105. response = self.client.get(self.thread.get_new_post_url())
  106. self.assertEqual(response.status_code, 302)
  107. self.assertEqual(
  108. response['location'],
  109. GOTO_URL % (self.thread.get_absolute_url(), self.thread.first_post_id)
  110. )
  111. def test_goto_first_new_post(self):
  112. """first unread post redirect url in already read thread is valid"""
  113. make_thread_read_aware(self.user, self.thread)
  114. read_thread(self.user, self.thread, self.thread.last_post)
  115. post = testutils.reply_thread(self.thread, posted_on=timezone.now())
  116. for _ in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL - 1):
  117. testutils.reply_thread(self.thread, posted_on=timezone.now())
  118. response = self.client.get(self.thread.get_new_post_url())
  119. self.assertEqual(response.status_code, 302)
  120. self.assertEqual(
  121. response['location'], GOTO_URL % (self.thread.get_absolute_url(), post.pk)
  122. )
  123. def test_goto_first_new_post_on_next_page(self):
  124. """first unread post redirect url in already read multipage thread is valid"""
  125. for _ in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL):
  126. testutils.reply_thread(self.thread, posted_on=timezone.now())
  127. make_thread_read_aware(self.user, self.thread)
  128. read_thread(self.user, self.thread, self.thread.last_post)
  129. post = testutils.reply_thread(self.thread, posted_on=timezone.now())
  130. for _ in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL - 1):
  131. testutils.reply_thread(self.thread, posted_on=timezone.now())
  132. response = self.client.get(self.thread.get_new_post_url())
  133. self.assertEqual(response.status_code, 302)
  134. self.assertEqual(
  135. response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 2, post.pk)
  136. )
  137. def test_goto_first_new_post_in_read_thread(self):
  138. """goto new in read thread points to last post"""
  139. for _ in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL):
  140. post = testutils.reply_thread(self.thread, posted_on=timezone.now())
  141. make_thread_read_aware(self.user, self.thread)
  142. read_thread(self.user, self.thread, self.thread.last_post)
  143. response = self.client.get(self.thread.get_new_post_url())
  144. self.assertEqual(response.status_code, 302)
  145. self.assertEqual(
  146. response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 2, post.pk)
  147. )
  148. def test_guest_goto_first_new_post_in_thread(self):
  149. """guest goto new in read thread points to last post"""
  150. for _ in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL):
  151. post = testutils.reply_thread(self.thread, posted_on=timezone.now())
  152. self.logout_user()
  153. response = self.client.get(self.thread.get_new_post_url())
  154. self.assertEqual(response.status_code, 302)
  155. self.assertEqual(
  156. response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 2, post.pk)
  157. )
  158. class GotoUnapprovedTests(GotoViewTestCase):
  159. def grant_permission(self):
  160. self.user.acl_cache['categories'][self.category.pk]['can_approve_content'] = 1
  161. override_acl(self.user, self.user.acl_cache)
  162. def test_view_validates_permission(self):
  163. """view validates permission to see unapproved posts"""
  164. response = self.client.get(self.thread.get_unapproved_post_url())
  165. self.assertContains(response, "You need permission to approve content", status_code=403)
  166. self.grant_permission()
  167. response = self.client.get(self.thread.get_unapproved_post_url())
  168. self.assertEqual(response.status_code, 302)
  169. def test_view_handles_no_unapproved_posts(self):
  170. """if thread has no unapproved posts, redirect to last post"""
  171. self.grant_permission()
  172. response = self.client.get(self.thread.get_unapproved_post_url())
  173. self.assertEqual(response.status_code, 302)
  174. self.assertEqual(
  175. response['location'],
  176. GOTO_URL % (self.thread.get_absolute_url(), self.thread.first_post_id)
  177. )
  178. def test_vie_handles_unapproved_posts(self):
  179. """if thread has unapproved posts, redirect to first of them"""
  180. for _ in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL):
  181. testutils.reply_thread(self.thread, posted_on=timezone.now())
  182. make_thread_read_aware(self.user, self.thread)
  183. read_thread(self.user, self.thread, self.thread.last_post)
  184. post = testutils.reply_thread(self.thread, is_unapproved=True, posted_on=timezone.now())
  185. for _ in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL - 1):
  186. testutils.reply_thread(self.thread, posted_on=timezone.now())
  187. self.grant_permission()
  188. response = self.client.get(self.thread.get_new_post_url())
  189. self.assertEqual(response.status_code, 302)
  190. self.assertEqual(
  191. response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 2, post.pk)
  192. )