test_threadparticipant_model.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. 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. 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. )
  32. self.thread.first_post = post
  33. self.thread.last_post = post
  34. self.thread.save()
  35. def test_set_owner(self):
  36. """set_owner makes user thread owner"""
  37. User = get_user_model()
  38. user = User.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  39. other_user = User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123")
  40. ThreadParticipant.objects.set_owner(self.thread, user)
  41. self.assertEqual(self.thread.participants.count(), 1)
  42. participant = ThreadParticipant.objects.get(thread=self.thread, user=user)
  43. self.assertTrue(participant.is_owner)
  44. self.assertEqual(user, participant.user)
  45. # threads can't have more than one owner
  46. ThreadParticipant.objects.set_owner(self.thread, other_user)
  47. self.assertEqual(self.thread.participants.count(), 2)
  48. participant = ThreadParticipant.objects.get(thread=self.thread, user=user)
  49. self.assertFalse(participant.is_owner)
  50. self.assertEqual(ThreadParticipant.objects.filter(is_owner=True).count(), 1)
  51. def test_add_participants(self):
  52. """add_participant adds participant to thread"""
  53. User = get_user_model()
  54. users = [
  55. User.objects.create_user("Bob", "bob@boberson.com", "Pass.123"),
  56. User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123"),
  57. ]
  58. ThreadParticipant.objects.add_participants(self.thread, users)
  59. self.assertEqual(self.thread.participants.count(), 2)
  60. for user in users:
  61. participant = ThreadParticipant.objects.get(thread=self.thread, user=user)
  62. self.assertFalse(participant.is_owner)
  63. def test_remove_participant(self):
  64. """remove_participant deletes participant from thread"""
  65. User = get_user_model()
  66. user = User.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  67. other_user = User.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123")
  68. ThreadParticipant.objects.add_participants(self.thread, [user])
  69. ThreadParticipant.objects.add_participants(self.thread, [other_user])
  70. self.assertEqual(self.thread.participants.count(), 2)
  71. ThreadParticipant.objects.remove_participant(self.thread, user)
  72. self.assertEqual(self.thread.participants.count(), 1)
  73. with self.assertRaises(ThreadParticipant.DoesNotExist):
  74. participant = ThreadParticipant.objects.get(
  75. thread=self.thread, user=user)