test_participants.py 7.5 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 ..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. poster_ip='127.0.0.1',
  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. User = get_user_model()
  44. users = [
  45. User.objects.create_user("Bob", "bob@boberson.com", "Pass.123"),
  46. User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123"),
  47. ]
  48. self.assertFalse(has_participants(self.thread))
  49. ThreadParticipant.objects.add_participants(self.thread, users)
  50. self.assertTrue(has_participants(self.thread))
  51. self.thread.threadparticipant_set.all().delete()
  52. self.assertFalse(has_participants(self.thread))
  53. def test_make_threads_participants_aware(self):
  54. """
  55. make_participants_aware sets participants_list and participant
  56. annotations on list of threads
  57. """
  58. User = get_user_model()
  59. user = User.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  60. other_user = User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123")
  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 = get_user_model()
  78. user = User.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  79. other_user = User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123")
  80. self.assertFalse(hasattr(self.thread, 'participants_list'))
  81. self.assertFalse(hasattr(self.thread, 'participant'))
  82. make_participants_aware(user, self.thread)
  83. self.assertTrue(hasattr(self.thread, 'participants_list'))
  84. self.assertTrue(hasattr(self.thread, 'participant'))
  85. self.assertEqual(self.thread.participants_list, [])
  86. self.assertIsNone(self.thread.participant)
  87. ThreadParticipant.objects.set_owner(self.thread, user)
  88. ThreadParticipant.objects.add_participants(self.thread, [other_user])
  89. make_participants_aware(user, self.thread)
  90. self.assertEqual(self.thread.participant.user, user)
  91. for participant in self.thread.participants_list:
  92. if participant.user == user:
  93. break
  94. else:
  95. self.fail("thread.participants_list didn't contain user")
  96. def test_set_owner(self):
  97. """set_owner sets user as thread owner"""
  98. User = get_user_model()
  99. user = User.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. User = get_user_model()
  109. users = [
  110. User.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123"),
  111. User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123"),
  112. ]
  113. set_users_unread_private_threads_sync(users=users)
  114. for user in users:
  115. User.objects.get(pk=user.pk, sync_unread_private_threads=True)
  116. def test_set_participants_unread_private_threads_sync(self):
  117. """
  118. set_users_unread_private_threads_sync sets sync_unread_private_threads
  119. flag on participants provided to true
  120. """
  121. User = get_user_model()
  122. users = [
  123. User.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123"),
  124. User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123"),
  125. ]
  126. participants = [ThreadParticipant(user=u) for u in users]
  127. set_users_unread_private_threads_sync(participants=participants)
  128. for user in users:
  129. User.objects.get(pk=user.pk, sync_unread_private_threads=True)
  130. def test_set_participants_users_unread_private_threads_sync(self):
  131. """
  132. set_users_unread_private_threads_sync sets sync_unread_private_threads
  133. flag on users and participants provided to true
  134. """
  135. User = get_user_model()
  136. users = [
  137. User.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123"),
  138. ]
  139. participants = [ThreadParticipant(user=u) for u in users]
  140. users.append(User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123"))
  141. set_users_unread_private_threads_sync(users=users, participants=participants)
  142. for user in users:
  143. User.objects.get(pk=user.pk, sync_unread_private_threads=True)
  144. def test_set_users_unread_private_threads_sync_exclude_user(self):
  145. """exclude_user kwarg works"""
  146. User = get_user_model()
  147. users = [
  148. User.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123"),
  149. User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123")
  150. ]
  151. set_users_unread_private_threads_sync(users=users, exclude_user=users[0])
  152. self.assertFalse(User.objects.get(pk=users[0].pk).sync_unread_private_threads)
  153. self.assertTrue(User.objects.get(pk=users[1].pk).sync_unread_private_threads)
  154. def test_set_users_unread_private_threads_sync_noop(self):
  155. """excluding only user is noop"""
  156. User = get_user_model()
  157. user = User.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123")
  158. with self.assertNumQueries(0):
  159. set_users_unread_private_threads_sync(users=[user], exclude_user=user)
  160. self.assertFalse(User.objects.get(pk=user.pk).sync_unread_private_threads)