test_participants.py 7.6 KB

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