test_participants.py 5.6 KB

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