test_gotoviews.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.users.testutils import AuthenticatedUserTestCase
  7. from .. import testutils
  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(response['location'], GOTO_URL % (self.thread.get_absolute_url(), self.thread.first_post_id))
  21. response = self.client.get(response['location'])
  22. self.assertContains(response, self.thread.first_post.get_absolute_url())
  23. def test_goto_last_post_on_page(self):
  24. """last post on page redirect url is valid"""
  25. for i in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL - 1):
  26. post = testutils.reply_thread(self.thread)
  27. response = self.client.get(post.get_absolute_url())
  28. self.assertEqual(response.status_code, 302)
  29. self.assertEqual(response['location'], GOTO_URL % (self.thread.get_absolute_url(), post.pk))
  30. response = self.client.get(response['location'])
  31. self.assertContains(response, post.get_absolute_url())
  32. def test_goto_first_post_on_next_page(self):
  33. """first post on next page redirect url is valid"""
  34. for i in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL):
  35. post = testutils.reply_thread(self.thread)
  36. response = self.client.get(post.get_absolute_url())
  37. self.assertEqual(response.status_code, 302)
  38. self.assertEqual(response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 2, post.pk))
  39. response = self.client.get(response['location'])
  40. self.assertContains(response, post.get_absolute_url())
  41. def test_goto_first_post_on_page_three_out_of_five(self):
  42. """first post on next page redirect url is valid"""
  43. posts = []
  44. for i in range(settings.MISAGO_POSTS_PER_PAGE * 4 - 1):
  45. post = testutils.reply_thread(self.thread)
  46. posts.append(post)
  47. post = posts[settings.MISAGO_POSTS_PER_PAGE * 2 - 3]
  48. response = self.client.get(post.get_absolute_url())
  49. self.assertEqual(response.status_code, 302)
  50. self.assertEqual(response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 3, post.pk))
  51. response = self.client.get(response['location'])
  52. self.assertContains(response, post.get_absolute_url())
  53. def test_goto_first_event_on_page_three_out_of_five(self):
  54. """event redirect url is valid"""
  55. posts = []
  56. for i in range(settings.MISAGO_POSTS_PER_PAGE * 4 - 1):
  57. post = testutils.reply_thread(self.thread)
  58. posts.append(post)
  59. post = posts[settings.MISAGO_POSTS_PER_PAGE * 2 - 2]
  60. self.thread.has_events = True
  61. self.thread.save()
  62. post.is_event = True
  63. post.save()
  64. response = self.client.get(post.get_absolute_url())
  65. self.assertEqual(response.status_code, 302)
  66. self.assertEqual(response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 3, post.pk))
  67. response = self.client.get(response['location'])
  68. self.assertContains(response, post.get_absolute_url())
  69. class GotoLastTests(GotoViewTestCase):
  70. def test_goto_last_post(self):
  71. """first post redirect url is valid"""
  72. response = self.client.get(self.thread.get_last_post_url())
  73. self.assertEqual(response.status_code, 302)
  74. self.assertEqual(response['location'], GOTO_URL % (self.thread.get_absolute_url(), self.thread.first_post_id))
  75. response = self.client.get(response['location'])
  76. self.assertContains(response, self.thread.last_post.get_absolute_url())
  77. def test_goto_last_post_on_page(self):
  78. """last post on page redirect url is valid"""
  79. for i in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL - 1):
  80. post = testutils.reply_thread(self.thread)
  81. response = self.client.get(self.thread.get_last_post_url())
  82. self.assertEqual(response.status_code, 302)
  83. self.assertEqual(response['location'], GOTO_URL % (self.thread.get_absolute_url(), post.pk))
  84. response = self.client.get(response['location'])
  85. self.assertContains(response, post.get_absolute_url())
  86. class GotoNewTests(GotoViewTestCase):
  87. def test_goto_first_post(self):
  88. """first unread post redirect url is valid"""
  89. response = self.client.get(self.thread.get_new_post_url())
  90. self.assertEqual(response.status_code, 302)
  91. self.assertEqual(response['location'], GOTO_URL % (self.thread.get_absolute_url(), self.thread.first_post_id))
  92. def test_goto_first_new_post(self):
  93. """first unread post redirect url in already read thread is valid"""
  94. make_thread_read_aware(self.user, self.thread)
  95. read_thread(self.user, self.thread, self.thread.last_post)
  96. post = testutils.reply_thread(self.thread, posted_on=timezone.now())
  97. for i in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL - 1):
  98. testutils.reply_thread(self.thread, posted_on=timezone.now())
  99. response = self.client.get(self.thread.get_new_post_url())
  100. self.assertEqual(response.status_code, 302)
  101. self.assertEqual(response['location'], GOTO_URL % (self.thread.get_absolute_url(), post.pk))
  102. def test_goto_first_new_post_on_next_page(self):
  103. """first unread post redirect url in already read multipage thread is valid"""
  104. for i in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL):
  105. testutils.reply_thread(self.thread, posted_on=timezone.now())
  106. make_thread_read_aware(self.user, self.thread)
  107. read_thread(self.user, self.thread, self.thread.last_post)
  108. post = testutils.reply_thread(self.thread, posted_on=timezone.now())
  109. for i in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL - 1):
  110. testutils.reply_thread(self.thread, posted_on=timezone.now())
  111. response = self.client.get(self.thread.get_new_post_url())
  112. self.assertEqual(response.status_code, 302)
  113. self.assertEqual(response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 2, post.pk))
  114. def test_goto_first_new_post_in_read_thread(self):
  115. """goto new in read thread points to last post"""
  116. for i in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL):
  117. post = testutils.reply_thread(self.thread, posted_on=timezone.now())
  118. make_thread_read_aware(self.user, self.thread)
  119. read_thread(self.user, self.thread, self.thread.last_post)
  120. response = self.client.get(self.thread.get_new_post_url())
  121. self.assertEqual(response.status_code, 302)
  122. self.assertEqual(response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 2, post.pk))
  123. def test_guest_goto_first_new_post_in_thread(self):
  124. """guest goto new in read thread points to last post"""
  125. for i in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL):
  126. post = testutils.reply_thread(self.thread, posted_on=timezone.now())
  127. self.logout_user()
  128. response = self.client.get(self.thread.get_new_post_url())
  129. self.assertEqual(response.status_code, 302)
  130. self.assertEqual(response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 2, post.pk))
  131. class GotoUnapprovedTests(GotoViewTestCase):
  132. def grant_permission(self):
  133. self.user.acl['categories'][self.category.pk]['can_approve_content'] = 1
  134. override_acl(self.user, self.user.acl)
  135. def test_view_validates_permission(self):
  136. """view validates permission to see unapproved posts"""
  137. response = self.client.get(self.thread.get_unapproved_post_url())
  138. self.assertContains(response, "You need permission to approve content", status_code=403)
  139. self.grant_permission()
  140. response = self.client.get(self.thread.get_unapproved_post_url())
  141. self.assertEqual(response.status_code, 302)
  142. def test_view_handles_no_unapproved_posts(self):
  143. """if thread has no unapproved posts, redirect to last post"""
  144. self.grant_permission()
  145. response = self.client.get(self.thread.get_unapproved_post_url())
  146. self.assertEqual(response.status_code, 302)
  147. self.assertEqual(response['location'], GOTO_URL % (self.thread.get_absolute_url(), self.thread.first_post_id))
  148. def test_vie_handles_unapproved_posts(self):
  149. """if thread has unapproved posts, redirect to first of them"""
  150. for i in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL):
  151. testutils.reply_thread(self.thread, posted_on=timezone.now())
  152. make_thread_read_aware(self.user, self.thread)
  153. read_thread(self.user, self.thread, self.thread.last_post)
  154. post = testutils.reply_thread(self.thread, is_unapproved=True, posted_on=timezone.now())
  155. for i in range(settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL - 1):
  156. testutils.reply_thread(self.thread, posted_on=timezone.now())
  157. self.grant_permission()
  158. response = self.client.get(self.thread.get_new_post_url())
  159. self.assertEqual(response.status_code, 302)
  160. self.assertEqual(response['location'], GOTO_PAGE_URL % (self.thread.get_absolute_url(), 2, post.pk))