test_participants.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. User = 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. User.objects.create_user("Bob", "bob@boberson.com", "Pass.123"),
  45. User.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 = User.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  58. other_user = User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123")
  59. self.assertFalse(hasattr(self.thread, "participants_list"))
  60. self.assertFalse(hasattr(self.thread, "participant"))
  61. make_participants_aware(user, [self.thread])
  62. self.assertFalse(hasattr(self.thread, "participants_list"))
  63. self.assertTrue(hasattr(self.thread, "participant"))
  64. self.assertIsNone(self.thread.participant)
  65. ThreadParticipant.objects.set_owner(self.thread, user)
  66. ThreadParticipant.objects.add_participants(self.thread, [other_user])
  67. make_participants_aware(user, [self.thread])
  68. self.assertFalse(hasattr(self.thread, "participants_list"))
  69. self.assertEqual(self.thread.participant.user, user)
  70. def test_make_thread_participants_aware(self):
  71. """
  72. make_participants_aware sets participants_list and participant
  73. annotations on thread model
  74. """
  75. user = User.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  76. other_user = User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123")
  77. self.assertFalse(hasattr(self.thread, "participants_list"))
  78. self.assertFalse(hasattr(self.thread, "participant"))
  79. make_participants_aware(user, self.thread)
  80. self.assertTrue(hasattr(self.thread, "participants_list"))
  81. self.assertTrue(hasattr(self.thread, "participant"))
  82. self.assertEqual(self.thread.participants_list, [])
  83. self.assertIsNone(self.thread.participant)
  84. ThreadParticipant.objects.set_owner(self.thread, user)
  85. ThreadParticipant.objects.add_participants(self.thread, [other_user])
  86. make_participants_aware(user, self.thread)
  87. self.assertEqual(self.thread.participant.user, user)
  88. for participant in self.thread.participants_list:
  89. if participant.user == user:
  90. break
  91. else:
  92. self.fail("thread.participants_list didn't contain user")
  93. def test_set_owner(self):
  94. """set_owner sets user as thread owner"""
  95. user = User.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  96. set_owner(self.thread, user)
  97. owner = self.thread.threadparticipant_set.get(is_owner=True)
  98. self.assertEqual(user, owner.user)
  99. def test_set_users_unread_private_threads_sync(self):
  100. """
  101. set_users_unread_private_threads_sync sets sync_unread_private_threads
  102. flag on users provided to true
  103. """
  104. users = [
  105. User.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123"),
  106. User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123"),
  107. ]
  108. set_users_unread_private_threads_sync(users=users)
  109. for user in users:
  110. User.objects.get(pk=user.pk, sync_unread_private_threads=True)
  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. User.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123"),
  118. User.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. User.objects.get(pk=user.pk, sync_unread_private_threads=True)
  124. def test_set_participants_users_unread_private_threads_sync(self):
  125. """
  126. set_users_unread_private_threads_sync sets sync_unread_private_threads
  127. flag on users and participants provided to true
  128. """
  129. users = [User.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123")]
  130. participants = [ThreadParticipant(user=u) for u in users]
  131. users.append(User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123"))
  132. set_users_unread_private_threads_sync(users=users, participants=participants)
  133. for user in users:
  134. User.objects.get(pk=user.pk, sync_unread_private_threads=True)
  135. def test_set_users_unread_private_threads_sync_exclude_user(self):
  136. """exclude_user kwarg works"""
  137. users = [
  138. User.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123"),
  139. User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123"),
  140. ]
  141. set_users_unread_private_threads_sync(users=users, exclude_user=users[0])
  142. self.assertFalse(User.objects.get(pk=users[0].pk).sync_unread_private_threads)
  143. self.assertTrue(User.objects.get(pk=users[1].pk).sync_unread_private_threads)
  144. def test_set_users_unread_private_threads_sync_noop(self):
  145. """excluding only user is noop"""
  146. user = User.objects.create_user("Bob1", "bob1@boberson.com", "Pass.123")
  147. with self.assertNumQueries(0):
  148. set_users_unread_private_threads_sync(users=[user], exclude_user=user)
  149. self.assertFalse(User.objects.get(pk=user.pk).sync_unread_private_threads)