test_threadparticipant_model.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.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_delete_participant(self):
  36. """delete_participant deletes participant from thread"""
  37. User = get_user_model()
  38. user = User.objects.create_user(
  39. "Bob", "bob@boberson.com", "Pass.123")
  40. other_user = User.objects.create_user(
  41. "Bob2", "bob2@boberson.com", "Pass.123")
  42. ThreadParticipant.objects.add_participant(self.thread, user)
  43. ThreadParticipant.objects.add_participant(self.thread, other_user)
  44. self.assertEqual(self.thread.participants.count(), 2)
  45. ThreadParticipant.objects.remove_participant(self.thread, user)
  46. self.assertEqual(self.thread.participants.count(), 1)
  47. with self.assertRaises(ThreadParticipant.DoesNotExist):
  48. participation = ThreadParticipant.objects.get(
  49. thread=self.thread, user=user)
  50. def test_add_participant(self):
  51. """add_participant adds participant to thread"""
  52. User = get_user_model()
  53. user = User.objects.create_user(
  54. "Bob", "bob@boberson.com", "Pass.123")
  55. ThreadParticipant.objects.add_participant(self.thread, user)
  56. self.assertEqual(self.thread.participants.count(), 1)
  57. participation = ThreadParticipant.objects.get(
  58. thread=self.thread, user=user)
  59. self.assertFalse(participation.is_owner)
  60. ThreadParticipant.objects.add_participant(self.thread, user)
  61. self.assertEqual(self.thread.participants.count(), 2)
  62. def test_set_owner(self):
  63. """set_owner makes user thread owner"""
  64. User = get_user_model()
  65. user = User.objects.create_user(
  66. "Bob", "bob@boberson.com", "Pass.123")
  67. ThreadParticipant.objects.set_owner(self.thread, user)
  68. self.assertEqual(self.thread.participants.count(), 1)
  69. participation = ThreadParticipant.objects.get(
  70. thread=self.thread, user=user)
  71. self.assertTrue(participation.is_owner)
  72. self.assertEqual(user, participation.user)
  73. other_user = User.objects.create_user(
  74. "Bob2", "bob2@boberson.com", "Pass.123")
  75. ThreadParticipant.objects.set_owner(self.thread, other_user)
  76. self.assertEqual(self.thread.participants.count(), 2)
  77. participation = ThreadParticipant.objects.get(
  78. thread=self.thread, user=user)
  79. self.assertFalse(participation.is_owner)