test_threadparticipant_model.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. class ThreadParticipantTests(TestCase):
  7. def setUp(self):
  8. datetime = timezone.now()
  9. self.category = Category.objects.filter(role='forum')[: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. self.thread.set_title("Test thread")
  19. self.thread.save()
  20. post = Post.objects.create(
  21. category=self.category,
  22. thread=self.thread,
  23. poster_name='Tester',
  24. poster_ip='127.0.0.1',
  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. self.thread.first_post = post
  31. self.thread.last_post = post
  32. self.thread.save()
  33. def test_delete_participant(self):
  34. """delete_participant deletes participant from thread"""
  35. User = get_user_model()
  36. user = User.objects.create_user(
  37. "Bob", "bob@boberson.com", "Pass.123")
  38. other_user = User.objects.create_user(
  39. "Bob2", "bob2@boberson.com", "Pass.123")
  40. ThreadParticipant.objects.add_participant(self.thread, user)
  41. ThreadParticipant.objects.add_participant(self.thread, other_user)
  42. self.assertEqual(self.thread.participants.count(), 2)
  43. ThreadParticipant.objects.remove_participant(self.thread, user)
  44. self.assertEqual(self.thread.participants.count(), 1)
  45. with self.assertRaises(ThreadParticipant.DoesNotExist):
  46. participation = ThreadParticipant.objects.get(
  47. thread=self.thread, user=user)
  48. def test_add_participant(self):
  49. """add_participant adds participant to thread"""
  50. User = get_user_model()
  51. user = User.objects.create_user(
  52. "Bob", "bob@boberson.com", "Pass.123")
  53. ThreadParticipant.objects.add_participant(self.thread, user)
  54. self.assertEqual(self.thread.participants.count(), 1)
  55. participation = ThreadParticipant.objects.get(
  56. thread=self.thread, user=user)
  57. self.assertFalse(participation.is_owner)
  58. ThreadParticipant.objects.add_participant(self.thread, user)
  59. self.assertEqual(self.thread.participants.count(), 2)
  60. def test_set_owner(self):
  61. """set_owner makes user thread owner"""
  62. User = get_user_model()
  63. user = User.objects.create_user(
  64. "Bob", "bob@boberson.com", "Pass.123")
  65. ThreadParticipant.objects.set_owner(self.thread, user)
  66. self.assertEqual(self.thread.participants.count(), 1)
  67. participation = ThreadParticipant.objects.get(
  68. thread=self.thread, user=user)
  69. self.assertTrue(participation.is_owner)
  70. self.assertEqual(user, participation.user)
  71. other_user = User.objects.create_user(
  72. "Bob2", "bob2@boberson.com", "Pass.123")
  73. ThreadParticipant.objects.set_owner(self.thread, other_user)
  74. self.assertEqual(self.thread.participants.count(), 2)
  75. participation = ThreadParticipant.objects.get(
  76. thread=self.thread, user=user)
  77. self.assertFalse(participation.is_owner)