test_privatethread_patch_api.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 Thread, 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. self.other_user.email = 'rafio.xudb@gmail.com'
  78. self.other_user.save()
  79. response = self.patch(self.api_link, [
  80. {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
  81. ])
  82. self.assertEqual(response.json()['participant'], {
  83. 'id': self.other_user.id,
  84. 'username': self.other_user.username,
  85. 'avatar_hash': self.other_user.avatar_hash,
  86. 'url': self.other_user.get_absolute_url(),
  87. 'is_owner': False,
  88. })
  89. # event was set on thread
  90. event = self.thread.post_set.order_by('id').last()
  91. self.assertTrue(event.is_event)
  92. self.assertTrue(event.event_type, 'added_participant')
  93. # notification about new private thread was sent to other user
  94. self.assertEqual(len(mail.outbox), 1)
  95. email = mail.outbox[-1]
  96. self.assertIn(self.user.username, email.subject)
  97. self.assertIn(self.thread.title, email.subject)
  98. class PrivateThreadRemoveParticipantApiTests(PrivateThreadPatchApiTestCase):
  99. def setUp(self):
  100. super(PrivateThreadRemoveParticipantApiTests, self).setUp()
  101. User = get_user_model()
  102. self.other_user = get_user_model().objects.create_user(
  103. 'BobBoberson', 'bob@boberson.com', 'pass123')
  104. def test_remove_invalid(self):
  105. """removed user has to be participant"""
  106. ThreadParticipant.objects.set_owner(self.thread, self.user)
  107. response = self.patch(self.api_link, [
  108. {'op': 'remove', 'path': 'participants', 'value': 'string'}
  109. ])
  110. self.assertContains(
  111. response, "Participant to remove is invalid.", status_code=400)
  112. def test_remove_nonexistant(self):
  113. """removed user has to be participant"""
  114. ThreadParticipant.objects.set_owner(self.thread, self.user)
  115. response = self.patch(self.api_link, [
  116. {'op': 'remove', 'path': 'participants', 'value': self.other_user.pk}
  117. ])
  118. self.assertContains(
  119. response, "Participant doesn't exist.", status_code=400)
  120. def test_remove_not_owner(self):
  121. """api validates if user trying to remove other user is an owner"""
  122. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  123. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  124. response = self.patch(self.api_link, [
  125. {'op': 'remove', 'path': 'participants', 'value': self.other_user.pk}
  126. ])
  127. self.assertContains(
  128. response, "be thread owner to remove participants from it", status_code=400)
  129. def test_user_leave_thread(self):
  130. """api allows user to remove himself from thread"""
  131. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  132. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  133. response = self.patch(self.api_link, [
  134. {'op': 'remove', 'path': 'participants', 'value': self.user.pk}
  135. ])
  136. self.assertEqual(response.status_code, 200)
  137. self.assertFalse(response.json()['deleted'])
  138. # thread still exists
  139. self.assertTrue(Thread.objects.get(pk=self.thread.pk))
  140. # leave event has valid type
  141. event = self.thread.post_set.order_by('id').last()
  142. self.assertTrue(event.is_event)
  143. self.assertTrue(event.event_type, 'participant_left')
  144. # users were flagged for sync
  145. User = get_user_model()
  146. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  147. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  148. # user was removed from participation
  149. self.assertEqual(self.thread.participants.count(), 1)
  150. self.assertEqual(self.thread.participants.filter(pk=self.user.pk).count(), 0)
  151. def test_owner_remove_user(self):
  152. """api allows owner to remove other user"""
  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.other_user.pk}
  157. ])
  158. self.assertEqual(response.status_code, 200)
  159. self.assertFalse(response.json()['deleted'])
  160. # thread still exists
  161. self.assertTrue(Thread.objects.get(pk=self.thread.pk))
  162. # leave event has valid type
  163. event = self.thread.post_set.order_by('id').last()
  164. self.assertTrue(event.is_event)
  165. self.assertTrue(event.event_type, 'participant_removed')
  166. # users were flagged for sync
  167. User = get_user_model()
  168. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  169. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  170. # user was removed from participation
  171. self.assertEqual(self.thread.participants.count(), 1)
  172. self.assertEqual(self.thread.participants.filter(pk=self.other_user.pk).count(), 0)
  173. def test_owner_leave_thread(self):
  174. """api allows owner to remove hisemf from thread, causing thread to close"""
  175. ThreadParticipant.objects.set_owner(self.thread, self.user)
  176. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  177. response = self.patch(self.api_link, [
  178. {'op': 'remove', 'path': 'participants', 'value': self.user.pk}
  179. ])
  180. self.assertEqual(response.status_code, 200)
  181. self.assertFalse(response.json()['deleted'])
  182. # thread still exists and is closed
  183. self.assertTrue(Thread.objects.get(pk=self.thread.pk).is_closed)
  184. # leave event has valid type
  185. event = self.thread.post_set.order_by('id').last()
  186. self.assertTrue(event.is_event)
  187. self.assertTrue(event.event_type, 'owner_left')
  188. # users were flagged for sync
  189. User = get_user_model()
  190. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  191. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  192. # user was removed from participation
  193. self.assertEqual(self.thread.participants.count(), 1)
  194. self.assertEqual(self.thread.participants.filter(pk=self.user.pk).count(), 0)
  195. def test_last_user_leave_thread(self):
  196. """api allows last user leave thread, causing thread to delete"""
  197. ThreadParticipant.objects.set_owner(self.thread, self.user)
  198. response = self.patch(self.api_link, [
  199. {'op': 'remove', 'path': 'participants', 'value': self.user.pk}
  200. ])
  201. self.assertEqual(response.status_code, 200)
  202. self.assertTrue(response.json()['deleted'])
  203. # thread is gone
  204. with self.assertRaises(Thread.DoesNotExist):
  205. Thread.objects.get(pk=self.thread.pk)
  206. # users were flagged for sync
  207. User = get_user_model()
  208. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  209. class PrivateThreadTakeOverApiTests(PrivateThreadPatchApiTestCase):
  210. pass