test_threadstracker.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. from datetime import timedelta
  2. from django.test import TestCase
  3. from django.utils import timezone
  4. from .. import poststracker, threadstracker
  5. from ...acl.objectacl import add_acl_to_obj
  6. from ...acl.useracl import get_user_acl
  7. from ...categories.models import Category
  8. from ...conf import settings
  9. from ...conftest import get_cache_versions
  10. from ...threads import test
  11. from ...users.test import create_test_user
  12. cache_versions = get_cache_versions()
  13. class AnonymousUser:
  14. is_authenticated = False
  15. is_anonymous = True
  16. class ThreadsTrackerTests(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. add_acl_to_obj(self.user_acl, self.category)
  22. def test_falsy_value(self):
  23. """passing falsy value to readtracker causes no errors"""
  24. threadstracker.make_read_aware(self.user, self.user_acl, None)
  25. threadstracker.make_read_aware(self.user, self.user_acl, False)
  26. threadstracker.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. thread = test.post_thread(self.category, started_on=started_on)
  31. threadstracker.make_read_aware(AnonymousUser(), None, thread)
  32. self.assertTrue(thread.is_read)
  33. self.assertFalse(thread.is_new)
  34. def test_anon_thread_after_cutoff(self):
  35. """tracked thread is marked as read for anonymous users"""
  36. thread = test.post_thread(self.category, started_on=timezone.now())
  37. threadstracker.make_read_aware(AnonymousUser(), None, thread)
  38. self.assertTrue(thread.is_read)
  39. self.assertFalse(thread.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. thread = test.post_thread(self.category, started_on=started_on)
  44. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  45. self.assertTrue(thread.is_read)
  46. self.assertFalse(thread.is_new)
  47. def test_user_unread_thread(self):
  48. """tracked thread is marked as unread for authenticated users"""
  49. thread = test.post_thread(self.category, started_on=timezone.now())
  50. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  51. self.assertFalse(thread.is_read)
  52. self.assertTrue(thread.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. thread = test.post_thread(self.category, started_on=started_on)
  57. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  58. self.assertTrue(thread.is_read)
  59. self.assertFalse(thread.is_new)
  60. def test_user_read_post(self):
  61. """tracked thread with read post marked as read"""
  62. thread = test.post_thread(self.category, started_on=timezone.now())
  63. poststracker.save_read(self.user, thread.first_post)
  64. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  65. self.assertTrue(thread.is_read)
  66. self.assertFalse(thread.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 = test.post_thread(self.category, started_on=timezone.now())
  70. post = test.reply_thread(thread, posted_on=timezone.now())
  71. poststracker.save_read(self.user, post)
  72. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  73. self.assertFalse(thread.is_read)
  74. self.assertTrue(thread.is_new)
  75. def test_user_first_read_post_unread_event(self):
  76. """tracked thread with read first post and unread event"""
  77. thread = test.post_thread(self.category, started_on=timezone.now())
  78. poststracker.save_read(self.user, thread.first_post)
  79. test.reply_thread(thread, posted_on=timezone.now(), is_event=True)
  80. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  81. self.assertFalse(thread.is_read)
  82. self.assertTrue(thread.is_new)
  83. def test_user_hidden_event(self):
  84. """tracked thread with unread first post and hidden event"""
  85. thread = test.post_thread(self.category, started_on=timezone.now())
  86. test.reply_thread(
  87. thread, posted_on=timezone.now(), is_event=True, is_hidden=True
  88. )
  89. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  90. self.assertFalse(thread.is_read)
  91. self.assertTrue(thread.is_new)
  92. def test_user_first_read_post_hidden_event(self):
  93. """tracked thread with read first post and hidden event"""
  94. thread = test.post_thread(self.category, started_on=timezone.now())
  95. poststracker.save_read(self.user, thread.first_post)
  96. test.reply_thread(
  97. thread, posted_on=timezone.now(), is_event=True, is_hidden=True
  98. )
  99. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  100. self.assertTrue(thread.is_read)
  101. self.assertFalse(thread.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. thread = test.post_thread(self.category, started_on=started_on)
  106. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  107. self.assertTrue(thread.is_read)
  108. self.assertFalse(thread.is_new)
  109. def test_user_first_read_post_unapproved_post(self):
  110. """tracked thread with read first post and unapproved post"""
  111. thread = test.post_thread(self.category, started_on=timezone.now())
  112. poststracker.save_read(self.user, thread.first_post)
  113. test.reply_thread(thread, posted_on=timezone.now(), is_unapproved=True)
  114. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  115. self.assertTrue(thread.is_read)
  116. self.assertFalse(thread.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 = test.post_thread(self.category, started_on=timezone.now())
  120. poststracker.save_read(self.user, thread.first_post)
  121. test.reply_thread(
  122. thread, posted_on=timezone.now(), poster=self.user, is_unapproved=True
  123. )
  124. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  125. self.assertFalse(thread.is_read)
  126. self.assertTrue(thread.is_new)