test_participants.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. add_owner,
  8. make_thread_participants_aware,
  9. remove_participant,
  10. set_thread_owner,
  11. set_user_unread_private_threads_sync,
  12. thread_has_participants
  13. )
  14. class ParticipantsTests(TestCase):
  15. def setUp(self):
  16. datetime = timezone.now()
  17. self.category = Category.objects.all_categories()[:1][0]
  18. self.thread = Thread(
  19. category=self.category,
  20. started_on=datetime,
  21. starter_name='Tester',
  22. starter_slug='tester',
  23. last_post_on=datetime,
  24. last_poster_name='Tester',
  25. last_poster_slug='tester'
  26. )
  27. self.thread.set_title("Test thread")
  28. self.thread.save()
  29. post = Post.objects.create(
  30. category=self.category,
  31. thread=self.thread,
  32. poster_name='Tester',
  33. poster_ip='127.0.0.1',
  34. original="Hello! I am test message!",
  35. parsed="<p>Hello! I am test message!</p>",
  36. checksum="nope",
  37. posted_on=datetime,
  38. updated_on=datetime
  39. )
  40. self.thread.first_post = post
  41. self.thread.last_post = post
  42. self.thread.save()
  43. def test_thread_has_participants(self):
  44. """thread_has_participants returns true if thread has participants"""
  45. User = get_user_model()
  46. user = User.objects.create_user(
  47. "Bob", "bob@boberson.com", "Pass.123")
  48. other_user = User.objects.create_user(
  49. "Bob2", "bob2@boberson.com", "Pass.123")
  50. self.assertFalse(thread_has_participants(self.thread))
  51. ThreadParticipant.objects.add_participant(self.thread, user)
  52. self.assertTrue(thread_has_participants(self.thread))
  53. ThreadParticipant.objects.add_participant(self.thread, other_user)
  54. self.assertTrue(thread_has_participants(self.thread))
  55. self.thread.threadparticipant_set.all().delete()
  56. self.assertFalse(thread_has_participants(self.thread))
  57. def test_make_thread_participants_aware(self):
  58. """
  59. make_thread_participants_aware sets participants_list and participant
  60. adnotations on thread model
  61. """
  62. User = get_user_model()
  63. user = User.objects.create_user(
  64. "Bob", "bob@boberson.com", "Pass.123")
  65. other_user = User.objects.create_user(
  66. "Bob2", "bob2@boberson.com", "Pass.123")
  67. self.assertFalse(hasattr(self.thread, 'participants_list'))
  68. self.assertFalse(hasattr(self.thread, 'participant'))
  69. make_thread_participants_aware(user, self.thread)
  70. self.assertTrue(hasattr(self.thread, 'participants_list'))
  71. self.assertTrue(hasattr(self.thread, 'participant'))
  72. self.assertEqual(self.thread.participants_list, [])
  73. self.assertIsNone(self.thread.participant)
  74. ThreadParticipant.objects.add_participant(self.thread, user, True)
  75. ThreadParticipant.objects.add_participant(self.thread, other_user)
  76. make_thread_participants_aware(user, self.thread)
  77. self.assertEqual(self.thread.participant.user, user)
  78. for participant in self.thread.participants_list:
  79. if participant.user == user:
  80. break
  81. else:
  82. self.fail("thread.participants_list didn't contain user")
  83. def test_set_thread_owner(self):
  84. """set_thread_owner sets user as thread owner"""
  85. User = get_user_model()
  86. user = User.objects.create_user(
  87. "Bob", "bob@boberson.com", "Pass.123")
  88. set_thread_owner(self.thread, user)
  89. owner = self.thread.threadparticipant_set.get(is_owner=True)
  90. self.assertEqual(user, owner.user)
  91. def test_set_user_unread_private_threads_sync(self):
  92. """
  93. set_user_unread_private_threads_sync sets sync_unread_private_threads
  94. flag on user model to true
  95. """
  96. User = get_user_model()
  97. user = User.objects.create_user(
  98. "Bob", "bob@boberson.com", "Pass.123")
  99. self.assertFalse(user.sync_unread_private_threads)
  100. set_user_unread_private_threads_sync(user)
  101. self.assertTrue(user.sync_unread_private_threads)
  102. db_user = User.objects.get(pk=user.pk)
  103. self.assertTrue(db_user.sync_unread_private_threads)
  104. def test_add_owner(self):
  105. """add_owner adds user as thread owner"""
  106. User = get_user_model()
  107. user = User.objects.create_user(
  108. "Bob", "bob@boberson.com", "Pass.123")
  109. add_owner(self.thread, user)
  110. self.assertTrue(user.sync_unread_private_threads)
  111. owner = self.thread.threadparticipant_set.get(is_owner=True)
  112. self.assertEqual(user, owner.user)
  113. def test_remove_participant(self):
  114. """remove_participant removes user from thread"""
  115. User = get_user_model()
  116. user = User.objects.create_user(
  117. "Bob", "bob@boberson.com", "Pass.123")
  118. add_owner(self.thread, user)
  119. remove_participant(self.thread, user)
  120. with self.assertRaises(ThreadParticipant.DoesNotExist):
  121. self.thread.threadparticipant_set.get(user=user)
  122. set_user_unread_private_threads_sync(user)
  123. self.assertTrue(user.sync_unread_private_threads)
  124. db_user = User.objects.get(pk=user.pk)
  125. self.assertTrue(db_user.sync_unread_private_threads)