test_categoriestracker.py 9.2 KB

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