test_participants.py 7.4 KB

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