test_privatethread_patch_api.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import json
  2. from django.contrib.auth import get_user_model
  3. from django.core import mail
  4. from misago.acl.testutils import override_acl
  5. from .. import testutils
  6. from ..models import ThreadParticipant
  7. from .test_privatethreads import PrivateThreadsTestCase
  8. class PrivateThreadPatchApiTestCase(PrivateThreadsTestCase):
  9. def setUp(self):
  10. super(PrivateThreadPatchApiTestCase, self).setUp()
  11. self.thread = testutils.post_thread(self.category, poster=self.user)
  12. self.api_link = self.thread.get_api_url()
  13. def patch(self, api_link, ops):
  14. return self.client.patch(
  15. api_link, json.dumps(ops), content_type="application/json")
  16. class PrivateThreadAddParticipantApiTests(PrivateThreadPatchApiTestCase):
  17. def setUp(self):
  18. super(PrivateThreadAddParticipantApiTests, self).setUp()
  19. User = get_user_model()
  20. self.other_user = get_user_model().objects.create_user(
  21. 'BobBoberson', 'bob@boberson.com', 'pass123')
  22. def test_add_participant_not_owner(self):
  23. """non-owner can't add participant"""
  24. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  25. response = self.patch(self.api_link, [
  26. {'op': 'add', 'path': 'participants', 'value': self.user.username}
  27. ])
  28. self.assertContains(
  29. response, "be thread owner to add new participants to it", status_code=400)
  30. def test_add_nonexistant_user(self):
  31. """can't user two times"""
  32. ThreadParticipant.objects.set_owner(self.thread, self.user)
  33. response = self.patch(self.api_link, [
  34. {'op': 'add', 'path': 'participants', 'value': 'InvalidUser'}
  35. ])
  36. self.assertContains(response, "No user with such name exists.", status_code=400)
  37. def test_add_already_participant(self):
  38. """can't add user that is already participant"""
  39. ThreadParticipant.objects.set_owner(self.thread, self.user)
  40. response = self.patch(self.api_link, [
  41. {'op': 'add', 'path': 'participants', 'value': self.user.username}
  42. ])
  43. self.assertContains(
  44. response, "This user is already thread participant", status_code=400)
  45. def test_add_blocking_user(self):
  46. """can't add user that is already participant"""
  47. ThreadParticipant.objects.set_owner(self.thread, self.user)
  48. self.other_user.blocks.add(self.user)
  49. response = self.patch(self.api_link, [
  50. {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
  51. ])
  52. self.assertContains(response, "BobBoberson is blocking you.", status_code=400)
  53. def test_add_too_many_users(self):
  54. """can't add user that is already participant"""
  55. ThreadParticipant.objects.set_owner(self.thread, self.user)
  56. User = get_user_model()
  57. for i in range(self.user.acl['max_private_thread_participants']):
  58. user = User.objects.create_user(
  59. 'User{}'.format(i), 'user{}@example.com'.format(i), 'Pass.123')
  60. ThreadParticipant.objects.add_participants(self.thread, [user])
  61. response = self.patch(self.api_link, [
  62. {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
  63. ])
  64. self.assertContains(
  65. response, "You can't add any more new users to this thread.", status_code=400)
  66. def test_add_user(self):
  67. """adding user to thread add user to thread as participant, sets event and emails him"""
  68. ThreadParticipant.objects.set_owner(self.thread, self.user)
  69. response = self.patch(self.api_link, [
  70. {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
  71. ])
  72. self.assertEqual(response.json()['participant'], {
  73. 'id': self.other_user.id,
  74. 'username': self.other_user.username,
  75. 'avatar_hash': self.other_user.avatar_hash,
  76. 'url': self.other_user.get_absolute_url(),
  77. 'is_owner': False,
  78. })
  79. # event was set on thread
  80. event = self.thread.post_set.order_by('id').last()
  81. self.assertTrue(event.is_event)
  82. self.assertTrue(event.event_type, 'added_participant')
  83. # notification about new private thread was sent to other user
  84. self.assertEqual(len(mail.outbox), 1)
  85. email = mail.outbox[-1]
  86. self.assertIn(self.user.username, email.subject)
  87. self.assertIn(self.thread.title, email.subject)
  88. class PrivateThreadRemoveParticipantApiTests(PrivateThreadPatchApiTestCase):
  89. pass
  90. class PrivateThreadTakeOverApiTests(PrivateThreadPatchApiTestCase):
  91. pass