test_readtracker.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. from datetime import timedelta
  2. from django.contrib.auth import get_user_model
  3. from django.test import TestCase
  4. from django.utils import timezone
  5. from misago.acl import add_acl
  6. from misago.categories.models import Category
  7. from misago.readtracker import categoriestracker, threadstracker
  8. from misago.threads import testutils
  9. from misago.users.models import AnonymousUser
  10. UserModel = get_user_model()
  11. class ReadTrackerTests(TestCase):
  12. def setUp(self):
  13. self.categories = list(Category.objects.all_categories()[:1])
  14. self.category = self.categories[0]
  15. self.user = UserModel.objects.create_user("Bob", "bob@test.com", "Pass.123")
  16. self.anon = AnonymousUser()
  17. def post_thread(self, datetime):
  18. return testutils.post_thread(category=self.category, started_on=datetime)
  19. class CategoriesTrackerTests(ReadTrackerTests):
  20. def test_anon_empty_category_read(self):
  21. """anon users content is always read"""
  22. categoriestracker.make_read_aware(self.anon, self.categories)
  23. self.assertIsNone(self.category.last_post_on)
  24. self.assertTrue(self.category.is_read)
  25. def test_anon_category_recent_reply_read(self):
  26. """anon users content is always read"""
  27. categoriestracker.make_read_aware(self.anon, self.categories)
  28. self.category.last_post_on = timezone.now()
  29. self.assertTrue(self.category.is_read)
  30. def test_empty_category_is_read(self):
  31. """empty category is read for signed in user"""
  32. categoriestracker.make_read_aware(self.user, self.categories)
  33. self.assertTrue(self.category.is_read)
  34. def test_make_read_aware_sets_read_flag_for_empty_category(self):
  35. """make_read_aware sets read flag on empty category"""
  36. categoriestracker.make_read_aware(self.anon, self.categories)
  37. self.assertTrue(self.category.is_read)
  38. categoriestracker.make_read_aware(self.user, self.categories)
  39. self.assertTrue(self.category.is_read)
  40. def test_make_read_aware_sets_read_flag_for_category_old_thread(self):
  41. """make_read_aware sets read flag on category with old thread"""
  42. self.category.last_post_on = self.user.joined_on - timedelta(days=1)
  43. categoriestracker.make_read_aware(self.user, self.categories)
  44. self.assertTrue(self.category.is_read)
  45. def test_make_read_aware_sets_unread_flag_for_category_new_thread(self):
  46. """make_read_aware sets unread flag on category with new thread"""
  47. self.category.last_post_on = self.user.joined_on + timedelta(days=1)
  48. categoriestracker.make_read_aware(self.user, self.categories)
  49. self.assertFalse(self.category.is_read)
  50. def test_sync_record_for_empty_category(self):
  51. """sync_record sets read flag on empty category"""
  52. add_acl(self.user, self.categories)
  53. categoriestracker.sync_record(self.user, self.category)
  54. self.user.categoryread_set.get(category=self.category)
  55. categoriestracker.make_read_aware(self.user, self.categories)
  56. self.assertTrue(self.category.is_read)
  57. def test_sync_record_for_category_old_thread_and_reply(self):
  58. """
  59. sync_record sets read flag on category with old thread,
  60. then changes flag to unread when new reply is posted
  61. """
  62. self.post_thread(self.user.joined_on - timedelta(days=1))
  63. add_acl(self.user, self.categories)
  64. categoriestracker.sync_record(self.user, self.category)
  65. self.user.categoryread_set.get(category=self.category)
  66. categoriestracker.make_read_aware(self.user, self.categories)
  67. self.assertTrue(self.category.is_read)
  68. self.post_thread(self.user.joined_on + timedelta(days=1))
  69. categoriestracker.sync_record(self.user, self.category)
  70. categoriestracker.make_read_aware(self.user, self.categories)
  71. self.assertFalse(self.category.is_read)
  72. def test_sync_record_for_category_new_thread(self):
  73. """
  74. sync_record sets read flag on category with old thread,
  75. then keeps flag to unread when new reply is posted
  76. """
  77. self.post_thread(self.user.joined_on + timedelta(days=1))
  78. add_acl(self.user, self.categories)
  79. categoriestracker.sync_record(self.user, self.category)
  80. self.user.categoryread_set.get(category=self.category)
  81. categoriestracker.make_read_aware(self.user, self.categories)
  82. self.assertFalse(self.category.is_read)
  83. self.post_thread(self.user.joined_on + timedelta(days=1))
  84. categoriestracker.sync_record(self.user, self.category)
  85. categoriestracker.make_read_aware(self.user, self.categories)
  86. self.assertFalse(self.category.is_read)
  87. def test_sync_record_for_category_deleted_threads(self):
  88. """unread category reverts to read after its emptied"""
  89. self.post_thread(self.user.joined_on + timedelta(days=1))
  90. self.post_thread(self.user.joined_on + timedelta(days=1))
  91. self.post_thread(self.user.joined_on + timedelta(days=1))
  92. add_acl(self.user, self.categories)
  93. categoriestracker.sync_record(self.user, self.category)
  94. categoriestracker.make_read_aware(self.user, self.categories)
  95. self.assertFalse(self.category.is_read)
  96. self.category.thread_set.all().delete()
  97. self.category.synchronize()
  98. self.category.save()
  99. categoriestracker.make_read_aware(self.user, self.categories)
  100. self.assertTrue(self.category.is_read)
  101. def test_sync_record_for_category_many_threads(self):
  102. """sync_record sets unread flag on category with many threads"""
  103. self.post_thread(self.user.joined_on + timedelta(days=1))
  104. self.post_thread(self.user.joined_on - timedelta(days=1))
  105. self.post_thread(self.user.joined_on + timedelta(days=1))
  106. self.post_thread(self.user.joined_on - timedelta(days=1))
  107. add_acl(self.user, self.categories)
  108. categoriestracker.sync_record(self.user, self.category)
  109. self.user.categoryread_set.get(category=self.category)
  110. categoriestracker.make_read_aware(self.user, self.categories)
  111. self.assertFalse(self.category.is_read)
  112. self.post_thread(self.user.joined_on + timedelta(days=1))
  113. categoriestracker.sync_record(self.user, self.category)
  114. categoriestracker.make_read_aware(self.user, self.categories)
  115. self.assertFalse(self.category.is_read)
  116. def test_sync_record_for_category_threads_behind_cutoff(self):
  117. """
  118. sync_record sets read flag on category with only thread being behind cutoff
  119. """
  120. self.post_thread(timezone.now() - timedelta(days=180))
  121. read_thread = self.post_thread(timezone.now())
  122. threadstracker.make_read_aware(self.user, read_thread)
  123. threadstracker.read_thread(self.user, read_thread, read_thread.last_post)
  124. category = Category.objects.get(pk=self.category.pk)
  125. categoriestracker.make_read_aware(self.user, [category])
  126. self.assertTrue(category.is_read)
  127. def test_read_leaf_category(self):
  128. """read_category reads leaf category for user"""
  129. categoriestracker.read_category(self.user, self.category)
  130. self.assertTrue(self.user.categoryread_set.get(category=self.category))
  131. def test_read_root_category(self):
  132. """read_category reads its subcategories for user"""
  133. root_category = Category.objects.root_category()
  134. categoriestracker.read_category(self.user, root_category)
  135. child_read = self.user.categoryread_set.get(category=self.category)
  136. self.assertTrue(child_read.last_read_on > timezone.now() - timedelta(seconds=3))
  137. def test_read_category_prunes_threadreads(self):
  138. """read_category prunes threadreads in this category"""
  139. thread = self.post_thread(timezone.now())
  140. threadstracker.make_read_aware(self.user, thread)
  141. threadstracker.read_thread(self.user, thread, thread.last_post)
  142. self.assertTrue(self.user.threadread_set.exists())
  143. categoriestracker.read_category(self.user, self.category)
  144. self.assertTrue(self.user.categoryread_set.get(category=self.category))
  145. self.assertFalse(self.user.threadread_set.exists())
  146. class ThreadsTrackerTests(ReadTrackerTests):
  147. def setUp(self):
  148. super(ThreadsTrackerTests, self).setUp()
  149. self.thread = self.post_thread(timezone.now() - timedelta(days=10))
  150. def reply_thread(self, is_hidden=False, is_unapproved=False):
  151. self.post = testutils.reply_thread(
  152. thread=self.thread,
  153. is_hidden=is_hidden,
  154. is_unapproved=is_unapproved,
  155. posted_on=timezone.now()
  156. )
  157. return self.post
  158. def test_thread_read_for_guest(self):
  159. """threads are always read for guests"""
  160. threadstracker.make_read_aware(self.anon, self.thread)
  161. self.assertTrue(self.thread.is_read)
  162. self.reply_thread()
  163. threadstracker.make_read_aware(self.anon, [self.thread])
  164. self.assertTrue(self.thread.is_read)
  165. def test_thread_read_for_user(self):
  166. """thread is read for user"""
  167. threadstracker.make_read_aware(self.user, self.thread)
  168. self.assertTrue(self.thread.is_read)
  169. def test_thread_replied_unread_for_user(self):
  170. """replied thread is unread for user"""
  171. self.reply_thread()
  172. threadstracker.make_read_aware(self.user, self.thread)
  173. self.assertFalse(self.thread.is_read)
  174. def test_thread_read(self):
  175. """thread read flag is set for user, then its set as unread by reply"""
  176. self.reply_thread()
  177. add_acl(self.user, self.categories)
  178. threadstracker.make_read_aware(self.user, self.thread)
  179. self.assertFalse(self.thread.is_read)
  180. threadstracker.read_thread(self.user, self.thread, self.post)
  181. threadstracker.make_read_aware(self.user, self.thread)
  182. self.assertTrue(self.thread.is_read)
  183. categoriestracker.make_read_aware(self.user, self.categories)
  184. self.assertTrue(self.category.is_read)
  185. self.thread.last_post_on = timezone.now()
  186. self.thread.save()
  187. self.category.synchronize()
  188. self.category.save()
  189. self.reply_thread()
  190. threadstracker.make_read_aware(self.user, self.thread)
  191. self.assertFalse(self.thread.is_read)
  192. categoriestracker.make_read_aware(self.user, self.categories)
  193. self.assertFalse(self.category.is_read)
  194. posts = [post for post in self.thread.post_set.order_by('id')]
  195. threadstracker.make_posts_read_aware(self.user, self.thread, posts)
  196. for post in posts[:-1]:
  197. self.assertTrue(post.is_read)
  198. self.assertFalse(posts[-1].is_read)
  199. def test_thread_read_category_cutoff(self):
  200. """thread read is handled when category cutoff is present"""
  201. self.reply_thread()
  202. add_acl(self.user, self.categories)
  203. threadstracker.make_read_aware(self.user, self.thread)
  204. self.assertFalse(self.thread.is_read)
  205. categoriestracker.read_category(self.user, self.category)
  206. threadstracker.make_read_aware(self.user, self.thread)
  207. self.assertTrue(self.thread.is_read)
  208. categoriestracker.make_read_aware(self.user, self.categories)
  209. self.assertTrue(self.category.is_read)
  210. posts = list(self.thread.post_set.order_by('id'))
  211. threadstracker.make_posts_read_aware(self.user, self.thread, posts)
  212. for post in posts:
  213. self.assertTrue(post.is_read)
  214. # post reply
  215. self.reply_thread()
  216. # test if only last post is unread
  217. posts = list(self.thread.post_set.order_by('id'))
  218. threadstracker.make_read_aware(self.user, self.thread)
  219. threadstracker.make_posts_read_aware(self.user, self.thread, posts)
  220. for post in posts[:-1]:
  221. self.assertTrue(post.is_read)
  222. self.assertTrue(posts[-1].is_new)
  223. # last post read will change readstate of categories
  224. threadstracker.make_read_aware(self.user, self.thread)
  225. threadstracker.read_thread(self.user, self.thread, posts[-1])
  226. categoriestracker.make_read_aware(self.user, self.categories)
  227. self.assertTrue(self.category.is_read)