test_readtracker.py 12 KB

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