test_categoriestracker.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.useracl import get_user_acl
  6. from misago.categories.models import Category
  7. from misago.conf import settings
  8. from misago.readtracker import poststracker, categoriestracker
  9. from misago.readtracker.models import PostRead
  10. from misago.threads import testutils
  11. User = get_user_model()
  12. cache_versions = {"acl": "abcdefgh"}
  13. class AnonymousUser(object):
  14. is_authenticated = False
  15. is_anonymous = True
  16. class CategoriesTrackerTests(TestCase):
  17. def setUp(self):
  18. self.user = User.objects.create_user("UserA", "testa@user.com", 'Pass.123')
  19. self.user_acl = get_user_acl(self.user, cache_versions)
  20. self.category = Category.objects.get(slug='first-category')
  21. def test_falsy_value(self):
  22. """passing falsy value to readtracker causes no errors"""
  23. categoriestracker.make_read_aware(self.user, self.user_acl, None)
  24. categoriestracker.make_read_aware(self.user, self.user_acl, False)
  25. categoriestracker.make_read_aware(self.user, self.user_acl, [])
  26. def test_anon_thread_before_cutoff(self):
  27. """non-tracked thread is marked as read for anonymous users"""
  28. started_on = timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF)
  29. testutils.post_thread(self.category, started_on=started_on)
  30. categoriestracker.make_read_aware(AnonymousUser(), None, self.category)
  31. self.assertTrue(self.category.is_read)
  32. self.assertFalse(self.category.is_new)
  33. def test_anon_thread_after_cutoff(self):
  34. """tracked thread is marked as read for anonymous users"""
  35. testutils.post_thread(self.category, started_on=timezone.now())
  36. categoriestracker.make_read_aware(AnonymousUser(), None, self.category)
  37. self.assertTrue(self.category.is_read)
  38. self.assertFalse(self.category.is_new)
  39. def test_user_thread_before_cutoff(self):
  40. """non-tracked thread is marked as read for authenticated users"""
  41. started_on = timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF)
  42. testutils.post_thread(self.category, started_on=started_on)
  43. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  44. self.assertTrue(self.category.is_read)
  45. self.assertFalse(self.category.is_new)
  46. def test_user_unread_thread(self):
  47. """tracked thread is marked as unread for authenticated users"""
  48. testutils.post_thread(self.category, started_on=timezone.now())
  49. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  50. self.assertFalse(self.category.is_read)
  51. self.assertTrue(self.category.is_new)
  52. def test_user_created_after_thread(self):
  53. """tracked thread older than user is marked as read"""
  54. started_on = timezone.now() - timedelta(days=1)
  55. testutils.post_thread(self.category, started_on=started_on)
  56. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  57. self.assertTrue(self.category.is_read)
  58. self.assertFalse(self.category.is_new)
  59. def test_user_read_post(self):
  60. """tracked thread with read post marked as read"""
  61. thread = testutils.post_thread(self.category, started_on=timezone.now())
  62. poststracker.save_read(self.user, thread.first_post)
  63. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  64. self.assertTrue(self.category.is_read)
  65. self.assertFalse(self.category.is_new)
  66. def test_user_first_unread_last_read_post(self):
  67. """tracked thread with unread first and last read post marked as unread"""
  68. thread = testutils.post_thread(self.category, started_on=timezone.now())
  69. post = testutils.reply_thread(thread, posted_on=timezone.now())
  70. poststracker.save_read(self.user, post)
  71. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  72. self.assertFalse(self.category.is_read)
  73. self.assertTrue(self.category.is_new)
  74. def test_user_first_read_post_unread_event(self):
  75. """tracked thread with read first post and unread event"""
  76. thread = testutils.post_thread(self.category, started_on=timezone.now())
  77. poststracker.save_read(self.user, thread.first_post)
  78. testutils.reply_thread(thread, posted_on=timezone.now(), is_event=True)
  79. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  80. self.assertFalse(self.category.is_read)
  81. self.assertTrue(self.category.is_new)
  82. def test_user_hidden_event(self):
  83. """tracked thread with unread first post and hidden event"""
  84. thread = testutils.post_thread(self.category, started_on=timezone.now())
  85. testutils.reply_thread(
  86. thread,
  87. posted_on=timezone.now(),
  88. is_event=True,
  89. is_hidden=True,
  90. )
  91. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  92. self.assertFalse(self.category.is_read)
  93. self.assertTrue(self.category.is_new)
  94. def test_user_first_read_post_hidden_event(self):
  95. """tracked thread with read first post and hidden event"""
  96. thread = testutils.post_thread(self.category, started_on=timezone.now())
  97. poststracker.save_read(self.user, thread.first_post)
  98. testutils.reply_thread(
  99. thread,
  100. posted_on=timezone.now(),
  101. is_event=True,
  102. is_hidden=True,
  103. )
  104. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  105. self.assertTrue(self.category.is_read)
  106. self.assertFalse(self.category.is_new)
  107. def test_user_thread_before_cutoff_unread_post(self):
  108. """non-tracked thread is marked as unread for anonymous users"""
  109. started_on = timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF)
  110. testutils.post_thread(self.category, started_on=started_on)
  111. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  112. self.assertTrue(self.category.is_read)
  113. self.assertFalse(self.category.is_new)
  114. def test_user_first_read_post_unapproved_post(self):
  115. """tracked thread with read first post and unapproved post"""
  116. thread = testutils.post_thread(self.category, started_on=timezone.now())
  117. poststracker.save_read(self.user, thread.first_post)
  118. testutils.reply_thread(
  119. thread,
  120. posted_on=timezone.now(),
  121. is_unapproved=True,
  122. )
  123. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  124. self.assertTrue(self.category.is_read)
  125. self.assertFalse(self.category.is_new)
  126. def test_user_first_read_post_unapproved_own_post(self):
  127. """tracked thread with read first post and unapproved own post"""
  128. thread = testutils.post_thread(self.category, started_on=timezone.now())
  129. poststracker.save_read(self.user, thread.first_post)
  130. testutils.reply_thread(
  131. thread,
  132. posted_on=timezone.now(),
  133. poster=self.user,
  134. is_unapproved=True,
  135. )
  136. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  137. self.assertFalse(self.category.is_read)
  138. self.assertTrue(self.category.is_new)
  139. def test_user_first_read_post_unapproved_own_post(self):
  140. """tracked thread with read first post and unapproved own post"""
  141. thread = testutils.post_thread(self.category, started_on=timezone.now())
  142. poststracker.save_read(self.user, thread.first_post)
  143. testutils.reply_thread(
  144. thread,
  145. posted_on=timezone.now(),
  146. poster=self.user,
  147. is_unapproved=True,
  148. )
  149. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  150. self.assertFalse(self.category.is_read)
  151. self.assertTrue(self.category.is_new)
  152. def test_user_unapproved_thread_unread_post(self):
  153. """tracked unapproved thread"""
  154. thread = testutils.post_thread(
  155. self.category,
  156. started_on=timezone.now(),
  157. is_unapproved=True,
  158. )
  159. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  160. self.assertTrue(self.category.is_read)
  161. self.assertFalse(self.category.is_new)
  162. def test_user_unapproved_own_thread_unread_post(self):
  163. """tracked unapproved but visible thread"""
  164. thread = testutils.post_thread(
  165. self.category,
  166. poster=self.user,
  167. started_on=timezone.now(),
  168. is_unapproved=True,
  169. )
  170. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  171. self.assertFalse(self.category.is_read)
  172. self.assertTrue(self.category.is_new)
  173. def test_user_hidden_thread_unread_post(self):
  174. """tracked hidden thread"""
  175. thread = testutils.post_thread(
  176. self.category,
  177. started_on=timezone.now(),
  178. is_hidden=True,
  179. )
  180. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  181. self.assertTrue(self.category.is_read)
  182. self.assertFalse(self.category.is_new)