test_privatethread_patch_api.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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_empty_username(self):
  31. """path validates username"""
  32. ThreadParticipant.objects.set_owner(self.thread, self.user)
  33. response = self.patch(self.api_link, [
  34. {'op': 'add', 'path': 'participants', 'value': ''}
  35. ])
  36. self.assertContains(
  37. response, "You have to enter new participant's username.", status_code=400)
  38. def test_add_nonexistant_user(self):
  39. """can't user two times"""
  40. ThreadParticipant.objects.set_owner(self.thread, self.user)
  41. response = self.patch(self.api_link, [
  42. {'op': 'add', 'path': 'participants', 'value': 'InvalidUser'}
  43. ])
  44. self.assertContains(response, "No user with such name exists.", status_code=400)
  45. def test_add_already_participant(self):
  46. """can't add user that is already participant"""
  47. ThreadParticipant.objects.set_owner(self.thread, self.user)
  48. response = self.patch(self.api_link, [
  49. {'op': 'add', 'path': 'participants', 'value': self.user.username}
  50. ])
  51. self.assertContains(
  52. response, "This user is already thread participant", status_code=400)
  53. def test_add_blocking_user(self):
  54. """can't add user that is already participant"""
  55. ThreadParticipant.objects.set_owner(self.thread, self.user)
  56. self.other_user.blocks.add(self.user)
  57. response = self.patch(self.api_link, [
  58. {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
  59. ])
  60. self.assertContains(response, "BobBoberson is blocking you.", status_code=400)
  61. def test_add_too_many_users(self):
  62. """can't add user that is already participant"""
  63. ThreadParticipant.objects.set_owner(self.thread, self.user)
  64. User = get_user_model()
  65. for i in range(self.user.acl['max_private_thread_participants']):
  66. user = User.objects.create_user(
  67. 'User{}'.format(i), 'user{}@example.com'.format(i), 'Pass.123')
  68. ThreadParticipant.objects.add_participants(self.thread, [user])
  69. response = self.patch(self.api_link, [
  70. {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
  71. ])
  72. self.assertContains(
  73. response, "You can't add any more new users to this thread.", status_code=400)
  74. def test_add_user(self):
  75. """adding user to thread add user to thread as participant, sets event and emails him"""
  76. ThreadParticipant.objects.set_owner(self.thread, self.user)
  77. response = self.patch(self.api_link, [
  78. {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
  79. ])
  80. self.assertEqual(response.json()['participant'], {
  81. 'id': self.other_user.id,
  82. 'username': self.other_user.username,
  83. 'avatar_hash': self.other_user.avatar_hash,
  84. 'url': self.other_user.get_absolute_url(),
  85. 'is_owner': False,
  86. })
  87. # event was set on thread
  88. event = self.thread.post_set.order_by('id').last()
  89. self.assertTrue(event.is_event)
  90. self.assertTrue(event.event_type, 'added_participant')
  91. # notification about new private thread was sent to other user
  92. self.assertEqual(len(mail.outbox), 1)
  93. email = mail.outbox[-1]
  94. self.assertIn(self.user.username, email.subject)
  95. self.assertIn(self.thread.title, email.subject)
  96. class PrivateThreadRemoveParticipantApiTests(PrivateThreadPatchApiTestCase):
  97. def setUp(self):
  98. super(PrivateThreadRemoveParticipantApiTests, self).setUp()
  99. User = get_user_model()
  100. self.other_user = get_user_model().objects.create_user(
  101. 'BobBoberson', 'bob@boberson.com', 'pass123')
  102. def test_remove_invalid(self):
  103. """removed user has to be participant"""
  104. ThreadParticipant.objects.set_owner(self.thread, self.user)
  105. response = self.patch(self.api_link, [
  106. {'op': 'remove', 'path': 'participants', 'value': 'string'}
  107. ])
  108. self.assertContains(
  109. response, "Participant to remove is invalid.", status_code=400)
  110. def test_remove_nonexistant(self):
  111. """removed user has to be participant"""
  112. ThreadParticipant.objects.set_owner(self.thread, self.user)
  113. response = self.patch(self.api_link, [
  114. {'op': 'remove', 'path': 'participants', 'value': self.other_user.pk}
  115. ])
  116. self.assertContains(
  117. response, "Participant doesn't exist.", status_code=400)
  118. def test_remove_not_owner(self):
  119. """api validates if user trying to remove other user is an owner"""
  120. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  121. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  122. response = self.patch(self.api_link, [
  123. {'op': 'remove', 'path': 'participants', 'value': self.other_user.pk}
  124. ])
  125. self.assertContains(
  126. response, "be thread owner to remove participants from it", status_code=400)
  127. def test_user_leave_thread(self):
  128. """api allows user to remove himself from thread"""
  129. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  130. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  131. response = self.patch(self.api_link, [
  132. {'op': 'remove', 'path': 'participants', 'value': self.user.pk}
  133. ])
  134. raise NotImplementedError('this test scenario is incomplete!')
  135. def test_owner_remove_user(self):
  136. """api allows owner to remove other user"""
  137. ThreadParticipant.objects.set_owner(self.thread, self.user)
  138. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  139. response = self.patch(self.api_link, [
  140. {'op': 'remove', 'path': 'participants', 'value': self.other_user.pk}
  141. ])
  142. raise NotImplementedError('this test scenario is incomplete!')
  143. def test_owner_leave_thread(self):
  144. """api allows owner to remove hisemf from thread, causing thread to close"""
  145. ThreadParticipant.objects.set_owner(self.thread, self.user)
  146. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  147. response = self.patch(self.api_link, [
  148. {'op': 'remove', 'path': 'participants', 'value': self.user.pk}
  149. ])
  150. raise NotImplementedError('this test scenario is incomplete!')
  151. def test_last_user_leave_thread(self):
  152. """api allows last user leave thread, causing thread to delete"""
  153. ThreadParticipant.objects.set_owner(self.thread, self.user)
  154. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  155. response = self.patch(self.api_link, [
  156. {'op': 'remove', 'path': 'participants', 'value': self.user.pk}
  157. ])
  158. raise NotImplementedError('this test scenario is incomplete!')
  159. class PrivateThreadTakeOverApiTests(PrivateThreadPatchApiTestCase):
  160. pass