test_participants.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. 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. poster_ip='127.0.0.1',
  33. original="Hello! I am test message!",
  34. parsed="<p>Hello! I am test message!</p>",
  35. checksum="nope",
  36. posted_on=datetime,
  37. updated_on=datetime
  38. )
  39. self.thread.first_post = post
  40. self.thread.last_post = post
  41. self.thread.save()
  42. def test_has_participants(self):
  43. """has_participants returns true if thread has participants"""
  44. users = [
  45. UserModel.objects.create_user("Bob", "bob@boberson.com", "Pass.123"),
  46. UserModel.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 = UserModel.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  59. other_user = UserModel.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123")
  60. self.assertFalse(hasattr(self.thread, 'participants_list'))
  61. self.assertFalse(hasattr(self.thread, 'participant'))
  62. make_participants_aware(user, [self.thread])
  63. self.assertFalse(hasattr(self.thread, 'participants_list'))
  64. self.assertTrue(hasattr(self.thread, 'participant'))
  65. self.assertIsNone(self.thread.participant)
  66. ThreadParticipant.objects.set_owner(self.thread, user)
  67. ThreadParticipant.objects.add_participants(self.thread, [other_user])
  68. make_participants_aware(user, [self.thread])
  69. self.assertFalse(hasattr(self.thread, 'participants_list'))
  70. self.assertEqual(self.thread.participant.user, user)
  71. def test_make_thread_participants_aware(self):
  72. """
  73. make_participants_aware sets participants_list and participant
  74. annotations on thread model
  75. """
  76. user = UserModel.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  77. other_user = UserModel.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123")
  78. self.assertFalse(hasattr(self.thread, 'participants_list'))
  79. self.assertFalse(hasattr(self.thread, 'participant'))
  80. make_participants_aware(user, self.thread)
  81. self.assertTrue(hasattr(self.thread, 'participants_list'))
  82. self.assertTrue(hasattr(self.thread, 'participant'))
  83. self.assertEqual(self.thread.participants_list, [])
  84. self.assertIsNone(self.thread.participant)
  85. ThreadParticipant.objects.set_owner(self.thread, user)
  86. ThreadParticipant.objects.add_participants(self.thread, [other_user])
  87. make_participants_aware(user, self.thread)
  88. self.assertEqual(self.thread.participant.user, user)
  89. for participant in self.thread.participants_list:
  90. if participant.user == user:
  91. break
  92. else:
  93. self.fail("thread.participants_list didn't contain user")
  94. def test_set_owner(self):
  95. """set_owner sets user as thread owner"""
  96. user = UserModel.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  97. set_owner(self.thread, user)
  98. owner = self.thread.threadparticipant_set.get(is_owner=True)
  99. self.assertEqual(user, owner.user)
  100. def test_set_users_unread_private_threads_sync(self):
  101. """
  102. set_users_unread_private_threads_sync sets sync_unread_private_threads
  103. flag on users provided to true
  104. """
  105. users = [
  106. UserModel.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123"),
  107. UserModel.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123"),
  108. ]
  109. set_users_unread_private_threads_sync(users=users)
  110. for user in users:
  111. UserModel.objects.get(pk=user.pk, sync_unread_private_threads=True)
  112. def test_set_participants_unread_private_threads_sync(self):
  113. """
  114. set_users_unread_private_threads_sync sets sync_unread_private_threads
  115. flag on participants provided to true
  116. """
  117. users = [
  118. UserModel.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123"),
  119. UserModel.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123"),
  120. ]
  121. participants = [ThreadParticipant(user=u) for u in users]
  122. set_users_unread_private_threads_sync(participants=participants)
  123. for user in users:
  124. UserModel.objects.get(pk=user.pk, sync_unread_private_threads=True)
  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 = [
  131. UserModel.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123"),
  132. ]
  133. participants = [ThreadParticipant(user=u) for u in users]
  134. users.append(UserModel.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123"))
  135. set_users_unread_private_threads_sync(users=users, participants=participants)
  136. for user in users:
  137. UserModel.objects.get(pk=user.pk, sync_unread_private_threads=True)
  138. def test_set_users_unread_private_threads_sync_exclude_user(self):
  139. """exclude_user kwarg works"""
  140. users = [
  141. UserModel.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123"),
  142. UserModel.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123")
  143. ]
  144. set_users_unread_private_threads_sync(users=users, exclude_user=users[0])
  145. self.assertFalse(UserModel.objects.get(pk=users[0].pk).sync_unread_private_threads)
  146. self.assertTrue(UserModel.objects.get(pk=users[1].pk).sync_unread_private_threads)
  147. def test_set_users_unread_private_threads_sync_noop(self):
  148. """excluding only user is noop"""
  149. user = UserModel.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123")
  150. with self.assertNumQueries(0):
  151. set_users_unread_private_threads_sync(users=[user], exclude_user=user)
  152. self.assertFalse(UserModel.objects.get(pk=user.pk).sync_unread_private_threads)