test_participants.py 7.6 KB

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