test_categoriestracker.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. cache_versions = get_cache_versions()
  12. class AnonymousUser:
  13. is_authenticated = False
  14. is_anonymous = True
  15. class CategoriesTrackerTests(TestCase):
  16. def setUp(self):
  17. self.user = create_test_user("User", "user@example.com")
  18. self.user_acl = get_user_acl(self.user, cache_versions)
  19. self.category = Category.objects.get(slug="first-category")
  20. def test_falsy_value(self):
  21. """passing falsy value to readtracker causes no errors"""
  22. categoriestracker.make_read_aware(self.user, self.user_acl, None)
  23. categoriestracker.make_read_aware(self.user, self.user_acl, False)
  24. categoriestracker.make_read_aware(self.user, self.user_acl, [])
  25. def test_anon_thread_before_cutoff(self):
  26. """non-tracked thread is marked as read for anonymous users"""
  27. started_on = timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF)
  28. test.post_thread(self.category, started_on=started_on)
  29. categoriestracker.make_read_aware(AnonymousUser(), None, self.category)
  30. self.assertTrue(self.category.is_read)
  31. self.assertFalse(self.category.is_new)
  32. def test_anon_thread_after_cutoff(self):
  33. """tracked thread is marked as read for anonymous users"""
  34. test.post_thread(self.category, started_on=timezone.now())
  35. categoriestracker.make_read_aware(AnonymousUser(), None, self.category)
  36. self.assertTrue(self.category.is_read)
  37. self.assertFalse(self.category.is_new)
  38. def test_user_thread_before_cutoff(self):
  39. """non-tracked thread is marked as read for authenticated users"""
  40. started_on = timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF)
  41. test.post_thread(self.category, started_on=started_on)
  42. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  43. self.assertTrue(self.category.is_read)
  44. self.assertFalse(self.category.is_new)
  45. def test_user_unread_thread(self):
  46. """tracked thread is marked as unread for authenticated users"""
  47. test.post_thread(self.category, started_on=timezone.now())
  48. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  49. self.assertFalse(self.category.is_read)
  50. self.assertTrue(self.category.is_new)
  51. def test_user_created_after_thread(self):
  52. """tracked thread older than user is marked as read"""
  53. started_on = timezone.now() - timedelta(days=1)
  54. test.post_thread(self.category, started_on=started_on)
  55. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  56. self.assertTrue(self.category.is_read)
  57. self.assertFalse(self.category.is_new)
  58. def test_user_read_post(self):
  59. """tracked thread with read post marked as read"""
  60. thread = test.post_thread(self.category, started_on=timezone.now())
  61. poststracker.save_read(self.user, thread.first_post)
  62. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  63. self.assertTrue(self.category.is_read)
  64. self.assertFalse(self.category.is_new)
  65. def test_user_first_unread_last_read_post(self):
  66. """tracked thread with unread first and last read post marked as unread"""
  67. thread = test.post_thread(self.category, started_on=timezone.now())
  68. post = test.reply_thread(thread, posted_on=timezone.now())
  69. poststracker.save_read(self.user, post)
  70. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  71. self.assertFalse(self.category.is_read)
  72. self.assertTrue(self.category.is_new)
  73. def test_user_first_read_post_unread_event(self):
  74. """tracked thread with read first post and unread event"""
  75. thread = test.post_thread(self.category, started_on=timezone.now())
  76. poststracker.save_read(self.user, thread.first_post)
  77. test.reply_thread(thread, posted_on=timezone.now(), is_event=True)
  78. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  79. self.assertFalse(self.category.is_read)
  80. self.assertTrue(self.category.is_new)
  81. def test_user_hidden_event(self):
  82. """tracked thread with unread first post and hidden event"""
  83. thread = test.post_thread(self.category, started_on=timezone.now())
  84. test.reply_thread(
  85. thread, posted_on=timezone.now(), is_event=True, is_hidden=True
  86. )
  87. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  88. self.assertFalse(self.category.is_read)
  89. self.assertTrue(self.category.is_new)
  90. def test_user_first_read_post_hidden_event(self):
  91. """tracked thread with read first post and hidden event"""
  92. thread = test.post_thread(self.category, started_on=timezone.now())
  93. poststracker.save_read(self.user, thread.first_post)
  94. test.reply_thread(
  95. thread, posted_on=timezone.now(), is_event=True, is_hidden=True
  96. )
  97. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  98. self.assertTrue(self.category.is_read)
  99. self.assertFalse(self.category.is_new)
  100. def test_user_thread_before_cutoff_unread_post(self):
  101. """non-tracked thread is marked as unread for anonymous users"""
  102. started_on = timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF)
  103. test.post_thread(self.category, started_on=started_on)
  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_first_read_post_unapproved_post(self):
  108. """tracked thread with read first post and unapproved post"""
  109. thread = test.post_thread(self.category, started_on=timezone.now())
  110. poststracker.save_read(self.user, thread.first_post)
  111. test.reply_thread(thread, posted_on=timezone.now(), is_unapproved=True)
  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_own_post(self):
  116. """tracked thread with read first post and unapproved own post"""
  117. thread = test.post_thread(self.category, started_on=timezone.now())
  118. poststracker.save_read(self.user, thread.first_post)
  119. test.reply_thread(
  120. thread, posted_on=timezone.now(), poster=self.user, is_unapproved=True
  121. )
  122. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  123. self.assertFalse(self.category.is_read)
  124. self.assertTrue(self.category.is_new)
  125. def test_user_unapproved_thread_unread_post(self):
  126. """tracked unapproved thread"""
  127. test.post_thread(self.category, started_on=timezone.now(), is_unapproved=True)
  128. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  129. self.assertTrue(self.category.is_read)
  130. self.assertFalse(self.category.is_new)
  131. def test_user_unapproved_own_thread_unread_post(self):
  132. """tracked unapproved but visible thread"""
  133. test.post_thread(
  134. self.category,
  135. poster=self.user,
  136. started_on=timezone.now(),
  137. is_unapproved=True,
  138. )
  139. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  140. self.assertFalse(self.category.is_read)
  141. self.assertTrue(self.category.is_new)
  142. def test_user_hidden_thread_unread_post(self):
  143. """tracked hidden thread"""
  144. test.post_thread(self.category, started_on=timezone.now(), is_hidden=True)
  145. categoriestracker.make_read_aware(self.user, self.user_acl, self.category)
  146. self.assertTrue(self.category.is_read)
  147. self.assertFalse(self.category.is_new)