test_threadstracker.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.objectacl import add_acl_to_obj
  6. from misago.acl.useracl import get_user_acl
  7. from misago.categories.models import Category
  8. from misago.conf import settings
  9. from misago.conftest import get_cache_versions
  10. from misago.readtracker import poststracker, threadstracker
  11. from misago.readtracker.models import PostRead
  12. from misago.threads import testutils
  13. User = get_user_model()
  14. cache_versions = get_cache_versions()
  15. class AnonymousUser(object):
  16. is_authenticated = False
  17. is_anonymous = True
  18. class ThreadsTrackerTests(TestCase):
  19. def setUp(self):
  20. self.user = User.objects.create_user("UserA", "testa@user.com", "Pass.123")
  21. self.user_acl = get_user_acl(self.user, cache_versions)
  22. self.category = Category.objects.get(slug="first-category")
  23. add_acl_to_obj(self.user_acl, self.category)
  24. def test_falsy_value(self):
  25. """passing falsy value to readtracker causes no errors"""
  26. threadstracker.make_read_aware(self.user, self.user_acl, None)
  27. threadstracker.make_read_aware(self.user, self.user_acl, False)
  28. threadstracker.make_read_aware(self.user, self.user_acl, [])
  29. def test_anon_thread_before_cutoff(self):
  30. """non-tracked thread is marked as read for anonymous users"""
  31. started_on = timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF)
  32. thread = testutils.post_thread(self.category, started_on=started_on)
  33. threadstracker.make_read_aware(AnonymousUser(), None, thread)
  34. self.assertTrue(thread.is_read)
  35. self.assertFalse(thread.is_new)
  36. def test_anon_thread_after_cutoff(self):
  37. """tracked thread is marked as read for anonymous users"""
  38. thread = testutils.post_thread(self.category, started_on=timezone.now())
  39. threadstracker.make_read_aware(AnonymousUser(), None, thread)
  40. self.assertTrue(thread.is_read)
  41. self.assertFalse(thread.is_new)
  42. def test_user_thread_before_cutoff(self):
  43. """non-tracked thread is marked as read for authenticated users"""
  44. started_on = timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF)
  45. thread = testutils.post_thread(self.category, started_on=started_on)
  46. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  47. self.assertTrue(thread.is_read)
  48. self.assertFalse(thread.is_new)
  49. def test_user_unread_thread(self):
  50. """tracked thread is marked as unread for authenticated users"""
  51. thread = testutils.post_thread(self.category, started_on=timezone.now())
  52. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  53. self.assertFalse(thread.is_read)
  54. self.assertTrue(thread.is_new)
  55. def test_user_created_after_thread(self):
  56. """tracked thread older than user is marked as read"""
  57. started_on = timezone.now() - timedelta(days=1)
  58. thread = testutils.post_thread(self.category, started_on=started_on)
  59. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  60. self.assertTrue(thread.is_read)
  61. self.assertFalse(thread.is_new)
  62. def test_user_read_post(self):
  63. """tracked thread with read post marked as read"""
  64. thread = testutils.post_thread(self.category, started_on=timezone.now())
  65. poststracker.save_read(self.user, thread.first_post)
  66. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  67. self.assertTrue(thread.is_read)
  68. self.assertFalse(thread.is_new)
  69. def test_user_first_unread_last_read_post(self):
  70. """tracked thread with unread first and last read post marked as unread"""
  71. thread = testutils.post_thread(self.category, started_on=timezone.now())
  72. post = testutils.reply_thread(thread, posted_on=timezone.now())
  73. poststracker.save_read(self.user, post)
  74. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  75. self.assertFalse(thread.is_read)
  76. self.assertTrue(thread.is_new)
  77. def test_user_first_read_post_unread_event(self):
  78. """tracked thread with read first post and unread event"""
  79. thread = testutils.post_thread(self.category, started_on=timezone.now())
  80. poststracker.save_read(self.user, thread.first_post)
  81. testutils.reply_thread(thread, posted_on=timezone.now(), is_event=True)
  82. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  83. self.assertFalse(thread.is_read)
  84. self.assertTrue(thread.is_new)
  85. def test_user_hidden_event(self):
  86. """tracked thread with unread first post and hidden event"""
  87. thread = testutils.post_thread(self.category, started_on=timezone.now())
  88. testutils.reply_thread(
  89. thread, posted_on=timezone.now(), is_event=True, is_hidden=True
  90. )
  91. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  92. self.assertFalse(thread.is_read)
  93. self.assertTrue(thread.is_new)
  94. def test_user_first_read_post_hidden_event(self):
  95. """tracked thread with read first post and hidden event"""
  96. thread = testutils.post_thread(self.category, started_on=timezone.now())
  97. poststracker.save_read(self.user, thread.first_post)
  98. testutils.reply_thread(
  99. thread, posted_on=timezone.now(), is_event=True, is_hidden=True
  100. )
  101. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  102. self.assertTrue(thread.is_read)
  103. self.assertFalse(thread.is_new)
  104. def test_user_thread_before_cutoff_unread_post(self):
  105. """non-tracked thread is marked as unread for anonymous users"""
  106. started_on = timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF)
  107. thread = testutils.post_thread(self.category, started_on=started_on)
  108. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  109. self.assertTrue(thread.is_read)
  110. self.assertFalse(thread.is_new)
  111. def test_user_first_read_post_unapproved_post(self):
  112. """tracked thread with read first post and unapproved post"""
  113. thread = testutils.post_thread(self.category, started_on=timezone.now())
  114. poststracker.save_read(self.user, thread.first_post)
  115. testutils.reply_thread(thread, posted_on=timezone.now(), is_unapproved=True)
  116. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  117. self.assertTrue(thread.is_read)
  118. self.assertFalse(thread.is_new)
  119. def test_user_first_read_post_unapproved_own_post(self):
  120. """tracked thread with read first post and unapproved own post"""
  121. thread = testutils.post_thread(self.category, started_on=timezone.now())
  122. poststracker.save_read(self.user, thread.first_post)
  123. testutils.reply_thread(
  124. thread, posted_on=timezone.now(), poster=self.user, is_unapproved=True
  125. )
  126. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  127. self.assertFalse(thread.is_read)
  128. self.assertTrue(thread.is_new)