test_gotoviews.py 8.2 KB

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