test_participants.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from django.contrib.auth import get_user_model
  2. from django.test import TestCase
  3. from django.utils import timezone
  4. from misago.forums.models import Forum
  5. from misago.threads.models import Thread, ThreadParticipant, Post
  6. from misago.threads.participants import (thread_has_participants,
  7. make_thread_participants_aware,
  8. set_thread_owner,
  9. set_user_unread_private_threads_sync,
  10. add_owner,
  11. remove_participant
  12. )
  13. class ParticipantsTests(TestCase):
  14. def setUp(self):
  15. datetime = timezone.now()
  16. self.forum = Forum.objects.filter(role="forum")[:1][0]
  17. self.thread = Thread(
  18. forum=self.forum,
  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. self.thread.set_title("Test thread")
  26. self.thread.save()
  27. post = Post.objects.create(
  28. forum=self.forum,
  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. self.thread.first_post = post
  38. self.thread.last_post = post
  39. self.thread.save()
  40. def test_thread_has_participants(self):
  41. """thread_has_participants returns true if thread has participants"""
  42. User = get_user_model()
  43. user = User.objects.create_user(
  44. "Bob", "bob@boberson.com", "Pass.123")
  45. other_user = User.objects.create_user(
  46. "Bob2", "bob2@boberson.com", "Pass.123")
  47. self.assertFalse(thread_has_participants(self.thread))
  48. ThreadParticipant.objects.add_participant(self.thread, user)
  49. self.assertTrue(thread_has_participants(self.thread))
  50. ThreadParticipant.objects.add_participant(self.thread, other_user)
  51. self.assertTrue(thread_has_participants(self.thread))
  52. self.thread.threadparticipant_set.all().delete()
  53. self.assertFalse(thread_has_participants(self.thread))
  54. def test_make_thread_participants_aware(self):
  55. """
  56. make_thread_participants_aware sets participants_list and participant
  57. adnotations on thread model
  58. """
  59. User = get_user_model()
  60. user = User.objects.create_user(
  61. "Bob", "bob@boberson.com", "Pass.123")
  62. other_user = User.objects.create_user(
  63. "Bob2", "bob2@boberson.com", "Pass.123")
  64. self.assertFalse(hasattr(self.thread, 'participants_list'))
  65. self.assertFalse(hasattr(self.thread, 'participant'))
  66. make_thread_participants_aware(user, self.thread)
  67. self.assertTrue(hasattr(self.thread, 'participants_list'))
  68. self.assertTrue(hasattr(self.thread, 'participant'))
  69. self.assertEqual(self.thread.participants_list, [])
  70. self.assertIsNone(self.thread.participant)
  71. ThreadParticipant.objects.add_participant(self.thread, user, True)
  72. ThreadParticipant.objects.add_participant(self.thread, other_user)
  73. make_thread_participants_aware(user, self.thread)
  74. self.assertEqual(self.thread.participant.user, user)
  75. for participant in self.thread.participants_list:
  76. if participant.user == user:
  77. break
  78. else:
  79. self.fail("thread.participants_list didn't contain user")
  80. def test_set_thread_owner(self):
  81. """set_thread_owner sets user as thread owner"""
  82. User = get_user_model()
  83. user = User.objects.create_user(
  84. "Bob", "bob@boberson.com", "Pass.123")
  85. set_thread_owner(self.thread, user)
  86. owner = self.thread.threadparticipant_set.get(is_owner=True)
  87. self.assertEqual(user, owner.user)
  88. def test_set_user_unread_private_threads_sync(self):
  89. """
  90. set_user_unread_private_threads_sync sets sync_unread_private_threads
  91. flag on user model to true
  92. """
  93. User = get_user_model()
  94. user = User.objects.create_user(
  95. "Bob", "bob@boberson.com", "Pass.123")
  96. self.assertFalse(user.sync_unread_private_threads)
  97. set_user_unread_private_threads_sync(user)
  98. self.assertTrue(user.sync_unread_private_threads)
  99. db_user = User.objects.get(pk=user.pk)
  100. self.assertTrue(db_user.sync_unread_private_threads)
  101. def test_add_owner(self):
  102. """add_owner adds user as thread owner"""
  103. User = get_user_model()
  104. user = User.objects.create_user(
  105. "Bob", "bob@boberson.com", "Pass.123")
  106. add_owner(self.thread, user)
  107. self.assertTrue(user.sync_unread_private_threads)
  108. owner = self.thread.threadparticipant_set.get(is_owner=True)
  109. self.assertEqual(user, owner.user)
  110. def test_remove_participant(self):
  111. """remove_participant removes user from thread"""
  112. User = get_user_model()
  113. user = User.objects.create_user(
  114. "Bob", "bob@boberson.com", "Pass.123")
  115. add_owner(self.thread, user)
  116. remove_participant(self.thread, user)
  117. with self.assertRaises(ThreadParticipant.DoesNotExist):
  118. self.thread.threadparticipant_set.get(user=user)
  119. set_user_unread_private_threads_sync(user)
  120. self.assertTrue(user.sync_unread_private_threads)
  121. db_user = User.objects.get(pk=user.pk)
  122. self.assertTrue(db_user.sync_unread_private_threads)