test_threadparticipant_model.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from datetime import timedelta
  2. from django.contrib.auth import get_user_model
  3. from django.test import TestCase
  4. from django.utils import timezone
  5. from misago.forums.models import Forum
  6. from misago.threads.models import Thread, ThreadParticipant, Post
  7. class ThreadParticipantTests(TestCase):
  8. def setUp(self):
  9. datetime = timezone.now()
  10. self.forum = Forum.objects.filter(role="forum")[:1][0]
  11. self.thread = Thread(
  12. forum=self.forum,
  13. started_on=datetime,
  14. starter_name='Tester',
  15. starter_slug='tester',
  16. last_post_on=datetime,
  17. last_poster_name='Tester',
  18. last_poster_slug='tester')
  19. self.thread.set_title("Test thread")
  20. self.thread.save()
  21. post = Post.objects.create(
  22. forum=self.forum,
  23. thread=self.thread,
  24. poster_name='Tester',
  25. poster_ip='127.0.0.1',
  26. original="Hello! I am test message!",
  27. parsed="<p>Hello! I am test message!</p>",
  28. checksum="nope",
  29. posted_on=datetime,
  30. updated_on=datetime)
  31. self.thread.first_post = post
  32. self.thread.last_post = post
  33. self.thread.save()
  34. def test_delete_participant(self):
  35. """delete_participant deletes participant from thread"""
  36. User = get_user_model()
  37. user = User.objects.create_user(
  38. "Bob", "bob@boberson.com", "Pass.123")
  39. other_user = User.objects.create_user(
  40. "Bob2", "bob2@boberson.com", "Pass.123")
  41. ThreadParticipant.objects.add_participant(self.thread, user)
  42. ThreadParticipant.objects.add_participant(self.thread, other_user)
  43. self.assertEqual(self.thread.participants.count(), 2)
  44. ThreadParticipant.objects.delete_participant(self.thread, user)
  45. self.assertEqual(self.thread.participants.count(), 1)
  46. with self.assertRaises(ThreadParticipant.DoesNotExist):
  47. participation = ThreadParticipant.objects.get(
  48. thread=self.thread, user=user)
  49. def test_add_participant(self):
  50. """add_participant adds participant to thread"""
  51. User = get_user_model()
  52. user = User.objects.create_user(
  53. "Bob", "bob@boberson.com", "Pass.123")
  54. ThreadParticipant.objects.add_participant(self.thread, user)
  55. self.assertEqual(self.thread.participants.count(), 1)
  56. participation = ThreadParticipant.objects.get(
  57. thread=self.thread, user=user)
  58. self.assertTrue(participation.is_active)
  59. self.assertFalse(participation.is_removed)
  60. self.assertFalse(participation.is_owner)
  61. self.assertEqual(user, participation.user)
  62. ThreadParticipant.objects.add_participant(self.thread, user)
  63. self.assertEqual(self.thread.participants.count(), 1)
  64. def test_set_owner(self):
  65. """set_owner makes user thread owner"""
  66. User = get_user_model()
  67. user = User.objects.create_user(
  68. "Bob", "bob@boberson.com", "Pass.123")
  69. ThreadParticipant.objects.set_owner(self.thread, user)
  70. self.assertEqual(self.thread.participants.count(), 1)
  71. participation = ThreadParticipant.objects.get(
  72. thread=self.thread, user=user)
  73. self.assertFalse(participation.is_active)
  74. self.assertFalse(participation.is_removed)
  75. self.assertTrue(participation.is_owner)
  76. self.assertEqual(user, participation.user)
  77. other_user = User.objects.create_user(
  78. "Bob2", "bob2@boberson.com", "Pass.123")
  79. ThreadParticipant.objects.set_owner(self.thread, other_user)
  80. def test_remove_participant(self):
  81. """remove_participant flags participant as removed"""
  82. User = get_user_model()
  83. user = User.objects.create_user(
  84. "Bob", "bob@boberson.com", "Pass.123")
  85. ThreadParticipant.objects.add_participant(self.thread, user)
  86. self.assertEqual(self.thread.participants.count(), 1)
  87. ThreadParticipant.objects.remove_participant(self.thread, user)
  88. self.assertEqual(self.thread.participants.count(), 1)
  89. participation = ThreadParticipant.objects.get(
  90. thread=self.thread, user=user)
  91. self.assertFalse(participation.is_active)
  92. self.assertTrue(participation.is_removed)
  93. self.assertFalse(participation.is_owner)
  94. self.assertEqual(user, participation.user)
  95. self.assertEqual(self.thread.last_post_on, participation.last_post_on)
  96. self.assertEqual(self.thread.last_poster_id,
  97. participation.last_poster_id)
  98. self.assertEqual(self.thread.last_poster_name,
  99. participation.last_poster_name)