test_threadstracker.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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,
  90. posted_on=timezone.now(),
  91. is_event=True,
  92. is_hidden=True,
  93. )
  94. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  95. self.assertFalse(thread.is_read)
  96. self.assertTrue(thread.is_new)
  97. def test_user_first_read_post_hidden_event(self):
  98. """tracked thread with read first post and hidden event"""
  99. thread = testutils.post_thread(self.category, started_on=timezone.now())
  100. poststracker.save_read(self.user, thread.first_post)
  101. testutils.reply_thread(
  102. thread,
  103. posted_on=timezone.now(),
  104. is_event=True,
  105. is_hidden=True,
  106. )
  107. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  108. self.assertTrue(thread.is_read)
  109. self.assertFalse(thread.is_new)
  110. def test_user_thread_before_cutoff_unread_post(self):
  111. """non-tracked thread is marked as unread for anonymous users"""
  112. started_on = timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF)
  113. thread = testutils.post_thread(self.category, started_on=started_on)
  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_post(self):
  118. """tracked thread with read first post and unapproved 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,
  123. posted_on=timezone.now(),
  124. is_unapproved=True,
  125. )
  126. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  127. self.assertTrue(thread.is_read)
  128. self.assertFalse(thread.is_new)
  129. def test_user_first_read_post_unapproved_own_post(self):
  130. """tracked thread with read first post and unapproved own post"""
  131. thread = testutils.post_thread(self.category, started_on=timezone.now())
  132. poststracker.save_read(self.user, thread.first_post)
  133. testutils.reply_thread(
  134. thread,
  135. posted_on=timezone.now(),
  136. poster=self.user,
  137. is_unapproved=True,
  138. )
  139. threadstracker.make_read_aware(self.user, self.user_acl, thread)
  140. self.assertFalse(thread.is_read)
  141. self.assertTrue(thread.is_new)