test_threadparticipant_model.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Post, Thread, ThreadParticipant
  6. UserModel = get_user_model()
  7. class ThreadParticipantTests(TestCase):
  8. def setUp(self):
  9. datetime = timezone.now()
  10. self.category = Category.objects.all_categories()[:1][0]
  11. self.thread = Thread(
  12. category=self.category,
  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. )
  20. self.thread.set_title("Test thread")
  21. self.thread.save()
  22. post = Post.objects.create(
  23. category=self.category,
  24. thread=self.thread,
  25. poster_name="Tester",
  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 = UserModel.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  38. other_user = UserModel.objects.create_user(
  39. "Bob2", "bob2@boberson.com", "Pass.123"
  40. )
  41. ThreadParticipant.objects.set_owner(self.thread, user)
  42. self.assertEqual(self.thread.participants.count(), 1)
  43. participant = ThreadParticipant.objects.get(thread=self.thread, user=user)
  44. self.assertTrue(participant.is_owner)
  45. self.assertEqual(user, participant.user)
  46. # threads can't have more than one owner
  47. ThreadParticipant.objects.set_owner(self.thread, other_user)
  48. self.assertEqual(self.thread.participants.count(), 2)
  49. participant = ThreadParticipant.objects.get(thread=self.thread, user=user)
  50. self.assertFalse(participant.is_owner)
  51. self.assertEqual(ThreadParticipant.objects.filter(is_owner=True).count(), 1)
  52. def test_add_participants(self):
  53. """add_participant adds participant to thread"""
  54. users = [
  55. UserModel.objects.create_user("Bob", "bob@boberson.com", "Pass.123"),
  56. UserModel.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 = UserModel.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  66. other_user = UserModel.objects.create_user(
  67. "Bob2", "bob2@boberson.com", "Pass.123"
  68. )
  69. ThreadParticipant.objects.add_participants(self.thread, [user])
  70. ThreadParticipant.objects.add_participants(self.thread, [other_user])
  71. self.assertEqual(self.thread.participants.count(), 2)
  72. ThreadParticipant.objects.remove_participant(self.thread, user)
  73. self.assertEqual(self.thread.participants.count(), 1)
  74. with self.assertRaises(ThreadParticipant.DoesNotExist):
  75. ThreadParticipant.objects.get(thread=self.thread, user=user)