test_participants.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. from django.test import TestCase
  2. from django.utils import timezone
  3. from ...categories.models import Category
  4. from ...users.test import create_test_user
  5. from ..models import Post, Thread, ThreadParticipant
  6. from ..participants import (
  7. has_participants,
  8. make_participants_aware,
  9. set_owner,
  10. set_users_unread_private_threads_sync,
  11. )
  12. class ParticipantsTests(TestCase):
  13. def setUp(self):
  14. datetime = timezone.now()
  15. self.category = Category.objects.all_categories()[:1][0]
  16. self.thread = Thread(
  17. category=self.category,
  18. started_on=datetime,
  19. starter_name="Tester",
  20. starter_slug="tester",
  21. last_post_on=datetime,
  22. last_poster_name="Tester",
  23. last_poster_slug="tester",
  24. )
  25. self.thread.set_title("Test thread")
  26. self.thread.save()
  27. post = Post.objects.create(
  28. category=self.category,
  29. thread=self.thread,
  30. poster_name="Tester",
  31. original="Hello! I am test message!",
  32. parsed="<p>Hello! I am test message!</p>",
  33. checksum="nope",
  34. posted_on=datetime,
  35. updated_on=datetime,
  36. )
  37. self.thread.first_post = post
  38. self.thread.last_post = post
  39. self.thread.save()
  40. def test_has_participants(self):
  41. """has_participants returns true if thread has participants"""
  42. users = [
  43. create_test_user("User", "user@example.com"),
  44. create_test_user("OtherUser", "otheruser@example.com"),
  45. ]
  46. self.assertFalse(has_participants(self.thread))
  47. ThreadParticipant.objects.add_participants(self.thread, users)
  48. self.assertTrue(has_participants(self.thread))
  49. self.thread.threadparticipant_set.all().delete()
  50. self.assertFalse(has_participants(self.thread))
  51. def test_make_threads_participants_aware(self):
  52. """
  53. make_participants_aware sets participants_list and participant
  54. annotations on list of threads
  55. """
  56. user = create_test_user("User", "user@example.com")
  57. other_user = create_test_user("OtherUser", "otheruser@example.com")
  58. self.assertFalse(hasattr(self.thread, "participants_list"))
  59. self.assertFalse(hasattr(self.thread, "participant"))
  60. make_participants_aware(user, [self.thread])
  61. self.assertFalse(hasattr(self.thread, "participants_list"))
  62. self.assertTrue(hasattr(self.thread, "participant"))
  63. self.assertIsNone(self.thread.participant)
  64. ThreadParticipant.objects.set_owner(self.thread, user)
  65. ThreadParticipant.objects.add_participants(self.thread, [other_user])
  66. make_participants_aware(user, [self.thread])
  67. self.assertFalse(hasattr(self.thread, "participants_list"))
  68. self.assertEqual(self.thread.participant.user, user)
  69. def test_make_thread_participants_aware(self):
  70. """
  71. make_participants_aware sets participants_list and participant
  72. annotations on thread model
  73. """
  74. user = create_test_user("User", "user@example.com")
  75. other_user = create_test_user("OtherUser", "otheruser@example.com")
  76. self.assertFalse(hasattr(self.thread, "participants_list"))
  77. self.assertFalse(hasattr(self.thread, "participant"))
  78. make_participants_aware(user, self.thread)
  79. self.assertTrue(hasattr(self.thread, "participants_list"))
  80. self.assertTrue(hasattr(self.thread, "participant"))
  81. self.assertEqual(self.thread.participants_list, [])
  82. self.assertIsNone(self.thread.participant)
  83. ThreadParticipant.objects.set_owner(self.thread, user)
  84. ThreadParticipant.objects.add_participants(self.thread, [other_user])
  85. make_participants_aware(user, self.thread)
  86. self.assertEqual(self.thread.participant.user, user)
  87. for participant in self.thread.participants_list:
  88. if participant.user == user:
  89. break
  90. else:
  91. self.fail("thread.participants_list didn't contain user")
  92. def test_set_owner(self):
  93. """set_owner sets user as thread owner"""
  94. user = create_test_user("User", "user@example.com")
  95. set_owner(self.thread, user)
  96. owner = self.thread.threadparticipant_set.get(is_owner=True)
  97. self.assertEqual(user, owner.user)
  98. def test_set_users_unread_private_threads_sync(self):
  99. """
  100. set_users_unread_private_threads_sync sets sync_unread_private_threads
  101. flag on users provided to true
  102. """
  103. users = [
  104. create_test_user("User", "user@example.com"),
  105. create_test_user("OtherUser", "otheruser@example.com"),
  106. ]
  107. set_users_unread_private_threads_sync(users=users)
  108. for user in users:
  109. user.refresh_from_db()
  110. assert user.sync_unread_private_threads
  111. def test_set_participants_unread_private_threads_sync(self):
  112. """
  113. set_users_unread_private_threads_sync sets sync_unread_private_threads
  114. flag on participants provided to true
  115. """
  116. users = [
  117. create_test_user("User", "user@example.com"),
  118. create_test_user("OtherUser", "otheruser@example.com"),
  119. ]
  120. participants = [ThreadParticipant(user=u) for u in users]
  121. set_users_unread_private_threads_sync(participants=participants)
  122. for user in users:
  123. user.refresh_from_db()
  124. assert user.sync_unread_private_threads
  125. def test_set_participants_users_unread_private_threads_sync(self):
  126. """
  127. set_users_unread_private_threads_sync sets sync_unread_private_threads
  128. flag on users and participants provided to true
  129. """
  130. users = [create_test_user("User", "user@example.com")]
  131. participants = [ThreadParticipant(user=u) for u in users]
  132. users.append(create_test_user("OtherUser", "otheruser@example.com"))
  133. set_users_unread_private_threads_sync(users=users, participants=participants)
  134. for user in users:
  135. user.refresh_from_db()
  136. assert user.sync_unread_private_threads
  137. def test_set_users_unread_private_threads_sync_exclude_user(self):
  138. """exclude_user kwarg works"""
  139. users = [
  140. create_test_user("User", "user@example.com"),
  141. create_test_user("OtherUser", "otheruser@example.com"),
  142. ]
  143. set_users_unread_private_threads_sync(users=users, exclude_user=users[0])
  144. [i.refresh_from_db() for i in users]
  145. assert users[0].sync_unread_private_threads is False
  146. assert users[1].sync_unread_private_threads
  147. def test_set_users_unread_private_threads_sync_noop(self):
  148. """excluding only user is noop"""
  149. user = create_test_user("User", "user@example.com")
  150. with self.assertNumQueries(0):
  151. set_users_unread_private_threads_sync(users=[user], exclude_user=user)
  152. user.refresh_from_db()
  153. assert user.sync_unread_private_threads is False