test_categoriestracker.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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, posted_on=timezone.now(), is_event=True, is_hidden=True
  88. )
  89. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  90. self.assertFalse(self.category.is_read)
  91. self.assertTrue(self.category.is_new)
  92. def test_user_first_read_post_hidden_event(self):
  93. """tracked thread with read first post and hidden event"""
  94. thread = testutils.post_thread(self.category, started_on=timezone.now())
  95. poststracker.save_read(self.user, thread.first_post)
  96. testutils.reply_thread(
  97. thread, posted_on=timezone.now(), is_event=True, is_hidden=True
  98. )
  99. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  100. self.assertTrue(self.category.is_read)
  101. self.assertFalse(self.category.is_new)
  102. def test_user_thread_before_cutoff_unread_post(self):
  103. """non-tracked thread is marked as unread for anonymous users"""
  104. started_on = timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF)
  105. testutils.post_thread(self.category, started_on=started_on)
  106. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  107. self.assertTrue(self.category.is_read)
  108. self.assertFalse(self.category.is_new)
  109. def test_user_first_read_post_unapproved_post(self):
  110. """tracked thread with read first post and unapproved post"""
  111. thread = testutils.post_thread(self.category, started_on=timezone.now())
  112. poststracker.save_read(self.user, thread.first_post)
  113. testutils.reply_thread(thread, posted_on=timezone.now(), is_unapproved=True)
  114. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  115. self.assertTrue(self.category.is_read)
  116. self.assertFalse(self.category.is_new)
  117. def test_user_first_read_post_unapproved_own_post(self):
  118. """tracked thread with read first post and unapproved own post"""
  119. thread = testutils.post_thread(self.category, started_on=timezone.now())
  120. poststracker.save_read(self.user, thread.first_post)
  121. testutils.reply_thread(
  122. thread, posted_on=timezone.now(), poster=self.user, is_unapproved=True
  123. )
  124. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  125. self.assertFalse(self.category.is_read)
  126. self.assertTrue(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, posted_on=timezone.now(), poster=self.user, is_unapproved=True
  133. )
  134. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  135. self.assertFalse(self.category.is_read)
  136. self.assertTrue(self.category.is_new)
  137. def test_user_unapproved_thread_unread_post(self):
  138. """tracked unapproved thread"""
  139. thread = testutils.post_thread(
  140. self.category, started_on=timezone.now(), is_unapproved=True
  141. )
  142. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  143. self.assertTrue(self.category.is_read)
  144. self.assertFalse(self.category.is_new)
  145. def test_user_unapproved_own_thread_unread_post(self):
  146. """tracked unapproved but visible thread"""
  147. thread = testutils.post_thread(
  148. self.category,
  149. poster=self.user,
  150. started_on=timezone.now(),
  151. is_unapproved=True,
  152. )
  153. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  154. self.assertFalse(self.category.is_read)
  155. self.assertTrue(self.category.is_new)
  156. def test_user_hidden_thread_unread_post(self):
  157. """tracked hidden thread"""
  158. thread = testutils.post_thread(
  159. self.category, started_on=timezone.now(), is_hidden=True
  160. )
  161. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  162. self.assertTrue(self.category.is_read)
  163. self.assertFalse(self.category.is_new)