test_goto.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from django.conf import settings
  2. from misago.acl import add_acl
  3. from misago.categories.models import Category
  4. from misago.readtracker import threadstracker
  5. from misago.users.testutils import AuthenticatedUserTestCase
  6. from misago.threads import goto
  7. from misago.threads.permissions import exclude_invisible_posts
  8. from misago.threads.testutils import post_thread, reply_thread
  9. POSTS_PER_PAGE = settings.MISAGO_POSTS_PER_PAGE
  10. THREAD_TAIL = settings.MISAGO_THREAD_TAIL
  11. MAX_PAGE_LEN = POSTS_PER_PAGE + THREAD_TAIL
  12. class MockThreadsCounter(object):
  13. def set(self, *args, **kwargs):
  14. pass
  15. def decrease(self, *args, **kwargs):
  16. pass
  17. class GotoTests(AuthenticatedUserTestCase):
  18. def setUp(self):
  19. super(GotoTests, self).setUp()
  20. self.category = Category.objects.all_categories().filter(role='forum')[:1][0]
  21. self.category.labels = []
  22. self.thread = post_thread(self.category)
  23. add_acl(self.user, self.category)
  24. add_acl(self.user, self.thread)
  25. def test_get_thread_pages(self):
  26. """get_thread_pages returns valid count of pages for given positions"""
  27. self.assertEqual(goto.get_thread_pages(1), 1)
  28. self.assertEqual(goto.get_thread_pages(POSTS_PER_PAGE), 1)
  29. self.assertEqual(goto.get_thread_pages(MAX_PAGE_LEN), 1)
  30. self.assertEqual(goto.get_thread_pages(MAX_PAGE_LEN + 1), 2)
  31. self.assertEqual(goto.get_thread_pages(POSTS_PER_PAGE * 2 - 1), 2)
  32. self.assertEqual(goto.get_thread_pages(POSTS_PER_PAGE * 2), 2)
  33. self.assertEqual(goto.get_thread_pages(POSTS_PER_PAGE * 3), 3)
  34. self.assertEqual(goto.get_thread_pages(
  35. POSTS_PER_PAGE * 5 + THREAD_TAIL - 1), 5)
  36. def test_get_post_page(self):
  37. """get_post_page returns valid page number for given queryset"""
  38. self.assertEqual(goto.get_post_page(1, self.thread.post_set), 1)
  39. # fill out page
  40. [reply_thread(self.thread) for p in xrange(MAX_PAGE_LEN - 1)]
  41. self.assertEqual(
  42. goto.get_post_page(MAX_PAGE_LEN, self.thread.post_set), 1)
  43. # add 2 posts, adding second page
  44. [reply_thread(self.thread) for p in xrange(2)]
  45. self.assertEqual(
  46. goto.get_post_page(MAX_PAGE_LEN + 2, self.thread.post_set), 2)
  47. def test_hashed_reverse(self):
  48. """hashed_reverse returns complete url for given post"""
  49. url = goto.hashed_reverse(self.thread, self.thread.first_post)
  50. url_formats = self.thread.get_absolute_url(), self.thread.first_post_id
  51. self.assertEqual(url, '%s#post-%s' % url_formats)
  52. url = goto.hashed_reverse(self.thread, self.thread.first_post, 4)
  53. url_formats = self.thread.get_absolute_url(), self.thread.first_post_id
  54. self.assertEqual(url, '%s4/#post-%s' % url_formats)
  55. def test_last(self):
  56. """last returns link to last post in thread"""
  57. url_last = goto.last(self.thread, self.thread.post_set)
  58. url_formats = self.thread.get_absolute_url(), self.thread.last_post_id
  59. self.assertEqual(url_last, '%s#post-%s' % url_formats)
  60. # add posts to reach page limit
  61. [reply_thread(self.thread) for p in xrange(MAX_PAGE_LEN - 1)]
  62. url_last = goto.last(self.thread, self.thread.post_set)
  63. url_formats = self.thread.get_absolute_url(), self.thread.last_post_id
  64. self.assertEqual(url_last, '%s#post-%s' % url_formats)
  65. # add 2 posts to add second page to thread
  66. [reply_thread(self.thread) for p in xrange(2)]
  67. url_last = goto.last(self.thread, self.thread.post_set)
  68. url_formats = self.thread.get_absolute_url(), self.thread.last_post_id
  69. self.assertEqual(url_last, '%s2/#post-%s' % url_formats)
  70. def test_get_post_link(self):
  71. """get_post_link returns link to specified post"""
  72. post_link = goto.get_post_link(
  73. 1, self.thread.post_set, self.thread, self.thread.last_post)
  74. last_link = goto.last(self.thread, self.thread.post_set)
  75. self.assertEqual(post_link, last_link)
  76. # add posts to add extra page to thread
  77. [reply_thread(self.thread) for p in xrange(MAX_PAGE_LEN)]
  78. post_link = goto.get_post_link(
  79. MAX_PAGE_LEN + 1,
  80. self.thread.post_set, self.thread, self.thread.last_post)
  81. last_link = goto.last(self.thread, self.thread.post_set)
  82. self.assertEqual(post_link, last_link)
  83. def test_new(self):
  84. """new returns link to first unread post"""
  85. self.user.new_threads = MockThreadsCounter()
  86. self.user.unread_threads = MockThreadsCounter()
  87. post_link = goto.new(self.user, self.thread, self.thread.post_set)
  88. last_link = goto.last(self.thread, self.thread.post_set)
  89. self.assertEqual(post_link, last_link)
  90. # add extra page to thread, then read them
  91. [reply_thread(self.thread) for p in xrange(MAX_PAGE_LEN)]
  92. threadstracker.read_thread(
  93. self.user, self.thread, self.thread.last_post)
  94. # add extra unread posts
  95. first_unread = reply_thread(self.thread)
  96. [reply_thread(self.thread) for p in xrange(20)]
  97. new_link = goto.new(self.user, self.thread, self.thread.post_set)
  98. post_link = goto.get_post_link(
  99. MAX_PAGE_LEN + 21, self.thread.post_set, self.thread, first_unread)
  100. self.assertEqual(new_link, post_link)
  101. # read thread
  102. threadstracker.read_thread(
  103. self.user, self.thread, self.thread.last_post)
  104. # assert new() points to last reply
  105. post_link = goto.new(self.user, self.thread, self.thread.post_set)
  106. last_link = goto.last(self.thread, self.thread.post_set)
  107. self.assertEqual(post_link, last_link)
  108. def test_post(self):
  109. """post returns link to post given"""
  110. thread = self.thread
  111. post_link = goto.post(thread, thread.post_set, thread.last_post)
  112. last_link = goto.last(self.thread, self.thread.post_set)
  113. self.assertEqual(post_link, last_link)
  114. # add 24 posts
  115. [reply_thread(self.thread) for p in xrange(24)]
  116. post_link = goto.post(thread, thread.post_set, thread.last_post)
  117. last_link = goto.last(self.thread, self.thread.post_set)
  118. self.assertEqual(post_link, last_link)