test_threadparticipant_model.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. poster_ip='127.0.0.1',
  27. original="Hello! I am test message!",
  28. parsed="<p>Hello! I am test message!</p>",
  29. checksum="nope",
  30. posted_on=datetime,
  31. updated_on=datetime
  32. )
  33. self.thread.first_post = post
  34. self.thread.last_post = post
  35. self.thread.save()
  36. def test_set_owner(self):
  37. """set_owner makes user thread owner"""
  38. user = UserModel.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  39. other_user = UserModel.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. users = [
  54. UserModel.objects.create_user("Bob", "bob@boberson.com", "Pass.123"),
  55. UserModel.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123"),
  56. ]
  57. ThreadParticipant.objects.add_participants(self.thread, users)
  58. self.assertEqual(self.thread.participants.count(), 2)
  59. for user in users:
  60. participant = ThreadParticipant.objects.get(thread=self.thread, user=user)
  61. self.assertFalse(participant.is_owner)
  62. def test_remove_participant(self):
  63. """remove_participant deletes participant from thread"""
  64. user = UserModel.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  65. other_user = UserModel.objects.create_user("Bob2", "bob2@boberson.com", "Pass.123")
  66. ThreadParticipant.objects.add_participants(self.thread, [user])
  67. ThreadParticipant.objects.add_participants(self.thread, [other_user])
  68. self.assertEqual(self.thread.participants.count(), 2)
  69. ThreadParticipant.objects.remove_participant(self.thread, user)
  70. self.assertEqual(self.thread.participants.count(), 1)
  71. with self.assertRaises(ThreadParticipant.DoesNotExist):
  72. participant = ThreadParticipant.objects.get(
  73. thread=self.thread, user=user)