test_threadparticipant_model.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from django.test import TestCase
  2. from django.utils import timezone
  3. from ...categories.models import Category
  4. from ...users.test import create_test_user
  5. from ..models import Post, Thread, ThreadParticipant
  6. class ThreadParticipantTests(TestCase):
  7. def setUp(self):
  8. datetime = timezone.now()
  9. self.category = Category.objects.all_categories()[:1][0]
  10. self.thread = Thread(
  11. category=self.category,
  12. started_on=datetime,
  13. starter_name="Tester",
  14. starter_slug="tester",
  15. last_post_on=datetime,
  16. last_poster_name="Tester",
  17. last_poster_slug="tester",
  18. )
  19. self.thread.set_title("Test thread")
  20. self.thread.save()
  21. post = Post.objects.create(
  22. category=self.category,
  23. thread=self.thread,
  24. poster_name="Tester",
  25. original="Hello! I am test message!",
  26. parsed="<p>Hello! I am test message!</p>",
  27. checksum="nope",
  28. posted_on=datetime,
  29. updated_on=datetime,
  30. )
  31. self.thread.first_post = post
  32. self.thread.last_post = post
  33. self.thread.save()
  34. def test_set_owner(self):
  35. """set_owner makes user thread owner"""
  36. user = create_test_user("User", "user@example.com")
  37. other_user = create_test_user("User2", "user2@example.com")
  38. ThreadParticipant.objects.set_owner(self.thread, user)
  39. self.assertEqual(self.thread.participants.count(), 1)
  40. participant = ThreadParticipant.objects.get(thread=self.thread, user=user)
  41. self.assertTrue(participant.is_owner)
  42. self.assertEqual(user, participant.user)
  43. # threads can't have more than one owner
  44. ThreadParticipant.objects.set_owner(self.thread, other_user)
  45. self.assertEqual(self.thread.participants.count(), 2)
  46. participant = ThreadParticipant.objects.get(thread=self.thread, user=user)
  47. self.assertFalse(participant.is_owner)
  48. self.assertEqual(ThreadParticipant.objects.filter(is_owner=True).count(), 1)
  49. def test_add_participants(self):
  50. """add_participant adds participant to thread"""
  51. users = [
  52. create_test_user("User", "user@example.com"),
  53. create_test_user("User2", "user2@example.com"),
  54. ]
  55. ThreadParticipant.objects.add_participants(self.thread, users)
  56. self.assertEqual(self.thread.participants.count(), 2)
  57. for user in users:
  58. participant = ThreadParticipant.objects.get(thread=self.thread, user=user)
  59. self.assertFalse(participant.is_owner)
  60. def test_remove_participant(self):
  61. """remove_participant deletes participant from thread"""
  62. user = create_test_user("User", "user@example.com")
  63. other_user = create_test_user("User2", "user2@example.com")
  64. ThreadParticipant.objects.add_participants(self.thread, [user])
  65. ThreadParticipant.objects.add_participants(self.thread, [other_user])
  66. self.assertEqual(self.thread.participants.count(), 2)
  67. ThreadParticipant.objects.remove_participant(self.thread, user)
  68. self.assertEqual(self.thread.participants.count(), 1)
  69. with self.assertRaises(ThreadParticipant.DoesNotExist):
  70. ThreadParticipant.objects.get(thread=self.thread, user=user)