test_participants.py 5.5 KB

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