test_categoriestracker.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. from datetime import timedelta
  2. from django.test import TestCase
  3. from django.utils import timezone
  4. from .. import categoriestracker, poststracker
  5. from ...acl.useracl import get_user_acl
  6. from ...categories.models import Category
  7. from ...conf import settings
  8. from ...conftest import get_cache_versions
  9. from ...threads import test
  10. from ...users.test import create_test_user
  11. from ..models import PostRead
  12. cache_versions = get_cache_versions()
  13. class AnonymousUser(object):
  14. is_authenticated = False
  15. is_anonymous = True
  16. class CategoriesTrackerTests(TestCase):
  17. def setUp(self):
  18. self.user = create_test_user("User", "user@example.com")
  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. test.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. test.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. test.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. test.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. test.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 = test.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 = test.post_thread(self.category, started_on=timezone.now())
  69. post = test.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 = test.post_thread(self.category, started_on=timezone.now())
  77. poststracker.save_read(self.user, thread.first_post)
  78. test.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 = test.post_thread(self.category, started_on=timezone.now())
  85. test.reply_thread(
  86. thread, posted_on=timezone.now(), is_event=True, is_hidden=True
  87. )
  88. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  89. self.assertFalse(self.category.is_read)
  90. self.assertTrue(self.category.is_new)
  91. def test_user_first_read_post_hidden_event(self):
  92. """tracked thread with read first post and hidden event"""
  93. thread = test.post_thread(self.category, started_on=timezone.now())
  94. poststracker.save_read(self.user, thread.first_post)
  95. test.reply_thread(
  96. thread, posted_on=timezone.now(), is_event=True, is_hidden=True
  97. )
  98. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  99. self.assertTrue(self.category.is_read)
  100. self.assertFalse(self.category.is_new)
  101. def test_user_thread_before_cutoff_unread_post(self):
  102. """non-tracked thread is marked as unread for anonymous users"""
  103. started_on = timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF)
  104. test.post_thread(self.category, started_on=started_on)
  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_first_read_post_unapproved_post(self):
  109. """tracked thread with read first post and unapproved post"""
  110. thread = test.post_thread(self.category, started_on=timezone.now())
  111. poststracker.save_read(self.user, thread.first_post)
  112. test.reply_thread(thread, posted_on=timezone.now(), is_unapproved=True)
  113. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  114. self.assertTrue(self.category.is_read)
  115. self.assertFalse(self.category.is_new)
  116. def test_user_first_read_post_unapproved_own_post(self):
  117. """tracked thread with read first post and unapproved own post"""
  118. thread = test.post_thread(self.category, started_on=timezone.now())
  119. poststracker.save_read(self.user, thread.first_post)
  120. test.reply_thread(
  121. thread, posted_on=timezone.now(), poster=self.user, is_unapproved=True
  122. )
  123. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  124. self.assertFalse(self.category.is_read)
  125. self.assertTrue(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 = test.post_thread(self.category, started_on=timezone.now())
  129. poststracker.save_read(self.user, thread.first_post)
  130. test.reply_thread(
  131. thread, posted_on=timezone.now(), poster=self.user, is_unapproved=True
  132. )
  133. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  134. self.assertFalse(self.category.is_read)
  135. self.assertTrue(self.category.is_new)
  136. def test_user_unapproved_thread_unread_post(self):
  137. """tracked unapproved thread"""
  138. thread = test.post_thread(
  139. self.category, started_on=timezone.now(), is_unapproved=True
  140. )
  141. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  142. self.assertTrue(self.category.is_read)
  143. self.assertFalse(self.category.is_new)
  144. def test_user_unapproved_own_thread_unread_post(self):
  145. """tracked unapproved but visible thread"""
  146. thread = test.post_thread(
  147. self.category,
  148. poster=self.user,
  149. started_on=timezone.now(),
  150. is_unapproved=True,
  151. )
  152. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  153. self.assertFalse(self.category.is_read)
  154. self.assertTrue(self.category.is_new)
  155. def test_user_hidden_thread_unread_post(self):
  156. """tracked hidden thread"""
  157. thread = test.post_thread(
  158. self.category, started_on=timezone.now(), is_hidden=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)