test_privatethread_patch_api.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. User = get_user_model()
  14. self.other_user = User.objects.create_user(
  15. 'BobBoberson', 'bob@boberson.com', 'pass123')
  16. def patch(self, api_link, ops):
  17. return self.client.patch(
  18. api_link, json.dumps(ops), content_type="application/json")
  19. class PrivateThreadAddParticipantApiTests(PrivateThreadPatchApiTestCase):
  20. def test_add_participant_not_owner(self):
  21. """non-owner can't add participant"""
  22. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  23. response = self.patch(self.api_link, [
  24. {'op': 'add', 'path': 'participants', 'value': self.user.username}
  25. ])
  26. self.assertContains(
  27. response, "be thread owner to add new participants to it", status_code=400)
  28. def test_add_empty_username(self):
  29. """path validates username"""
  30. ThreadParticipant.objects.set_owner(self.thread, self.user)
  31. response = self.patch(self.api_link, [
  32. {'op': 'add', 'path': 'participants', 'value': ''}
  33. ])
  34. self.assertContains(
  35. response, "You have to enter new participant's username.", status_code=400)
  36. def test_add_nonexistant_user(self):
  37. """can't user two times"""
  38. ThreadParticipant.objects.set_owner(self.thread, self.user)
  39. response = self.patch(self.api_link, [
  40. {'op': 'add', 'path': 'participants', 'value': 'InvalidUser'}
  41. ])
  42. self.assertContains(response, "No user with such name exists.", status_code=400)
  43. def test_add_already_participant(self):
  44. """can't add user that is already participant"""
  45. ThreadParticipant.objects.set_owner(self.thread, self.user)
  46. response = self.patch(self.api_link, [
  47. {'op': 'add', 'path': 'participants', 'value': self.user.username}
  48. ])
  49. self.assertContains(
  50. response, "This user is already thread participant", status_code=400)
  51. def test_add_blocking_user(self):
  52. """can't add user that is already participant"""
  53. ThreadParticipant.objects.set_owner(self.thread, self.user)
  54. self.other_user.blocks.add(self.user)
  55. response = self.patch(self.api_link, [
  56. {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
  57. ])
  58. self.assertContains(response, "BobBoberson is blocking you.", status_code=400)
  59. def test_add_too_many_users(self):
  60. """can't add user that is already participant"""
  61. ThreadParticipant.objects.set_owner(self.thread, self.user)
  62. User = get_user_model()
  63. for i in range(self.user.acl['max_private_thread_participants']):
  64. user = User.objects.create_user(
  65. 'User{}'.format(i), 'user{}@example.com'.format(i), 'Pass.123')
  66. ThreadParticipant.objects.add_participants(self.thread, [user])
  67. response = self.patch(self.api_link, [
  68. {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
  69. ])
  70. self.assertContains(
  71. response, "You can't add any more new users to this thread.", status_code=400)
  72. def test_add_user_closed_thread(self):
  73. """adding user to closed thread fails for non-moderator"""
  74. ThreadParticipant.objects.set_owner(self.thread, self.user)
  75. self.thread.is_closed = True
  76. self.thread.save()
  77. response = self.patch(self.api_link, [
  78. {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
  79. ])
  80. self.assertContains(
  81. response, "Only moderators can add participants to closed threads.", status_code=400)
  82. def test_add_user(self):
  83. """adding user to thread add user to thread as participant, sets event and emails him"""
  84. ThreadParticipant.objects.set_owner(self.thread, self.user)
  85. response = self.patch(self.api_link, [
  86. {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
  87. ])
  88. # event was set on thread
  89. event = self.thread.post_set.order_by('id').last()
  90. self.assertTrue(event.is_event)
  91. self.assertTrue(event.event_type, 'added_participant')
  92. # notification about new private thread was sent to other user
  93. self.assertEqual(len(mail.outbox), 1)
  94. email = mail.outbox[-1]
  95. self.assertIn(self.user.username, email.subject)
  96. self.assertIn(self.thread.title, email.subject)
  97. def test_add_user_too_many_users_moderator(self):
  98. """moderators bypass users limit"""
  99. ThreadParticipant.objects.set_owner(self.thread, self.user)
  100. override_acl(self.user, {
  101. 'can_moderate_private_threads': 1
  102. })
  103. User = get_user_model()
  104. for i in range(self.user.acl['max_private_thread_participants']):
  105. user = User.objects.create_user(
  106. 'User{}'.format(i), 'user{}@example.com'.format(i), 'Pass.123')
  107. ThreadParticipant.objects.add_participants(self.thread, [user])
  108. response = self.patch(self.api_link, [
  109. {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
  110. ])
  111. # event was set on thread
  112. event = self.thread.post_set.order_by('id').last()
  113. self.assertTrue(event.is_event)
  114. self.assertTrue(event.event_type, 'added_participant')
  115. # notification about new private thread was sent to other user
  116. self.assertEqual(len(mail.outbox), 1)
  117. email = mail.outbox[-1]
  118. self.assertIn(self.user.username, email.subject)
  119. self.assertIn(self.thread.title, email.subject)
  120. def test_add_user_to_closed_moderator(self):
  121. """moderators can add users to closed threads"""
  122. ThreadParticipant.objects.set_owner(self.thread, self.user)
  123. self.thread.is_closed = True
  124. self.thread.save()
  125. override_acl(self.user, {
  126. 'can_moderate_private_threads': 1
  127. })
  128. response = self.patch(self.api_link, [
  129. {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
  130. ])
  131. # event was set on thread
  132. event = self.thread.post_set.order_by('id').last()
  133. self.assertTrue(event.is_event)
  134. self.assertTrue(event.event_type, 'added_participant')
  135. # notification about new private thread was sent to other user
  136. self.assertEqual(len(mail.outbox), 1)
  137. email = mail.outbox[-1]
  138. self.assertIn(self.user.username, email.subject)
  139. self.assertIn(self.thread.title, email.subject)
  140. class PrivateThreadRemoveParticipantApiTests(PrivateThreadPatchApiTestCase):
  141. def test_remove_empty(self):
  142. """api handles empty user id"""
  143. ThreadParticipant.objects.set_owner(self.thread, self.user)
  144. response = self.patch(self.api_link, [
  145. {'op': 'remove', 'path': 'participants', 'value': 'string'}
  146. ])
  147. self.assertContains(response, "Participant doesn't exist.", status_code=400)
  148. def test_remove_invalid(self):
  149. """api validates user id type"""
  150. ThreadParticipant.objects.set_owner(self.thread, self.user)
  151. response = self.patch(self.api_link, [
  152. {'op': 'remove', 'path': 'participants', 'value': 'string'}
  153. ])
  154. self.assertContains(response, "Participant doesn't exist.", status_code=400)
  155. def test_remove_nonexistant(self):
  156. """removed user has to be participant"""
  157. ThreadParticipant.objects.set_owner(self.thread, self.user)
  158. response = self.patch(self.api_link, [
  159. {'op': 'remove', 'path': 'participants', 'value': self.other_user.pk}
  160. ])
  161. self.assertContains(response, "Participant doesn't exist.", status_code=400)
  162. def test_remove_not_owner(self):
  163. """api validates if user trying to remove other user is an owner"""
  164. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  165. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  166. response = self.patch(self.api_link, [
  167. {'op': 'remove', 'path': 'participants', 'value': self.other_user.pk}
  168. ])
  169. self.assertContains(
  170. response, "be thread owner to remove participants from it", status_code=400)
  171. def test_owner_remove_user_closed_thread(self):
  172. """api disallows owner to remove other user from closed thread"""
  173. ThreadParticipant.objects.set_owner(self.thread, self.user)
  174. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  175. self.thread.is_closed = True
  176. self.thread.save()
  177. response = self.patch(self.api_link, [
  178. {'op': 'remove', 'path': 'participants', 'value': self.other_user.pk}
  179. ])
  180. self.assertContains(
  181. response, "moderators can remove participants from closed threads", status_code=400)
  182. def test_user_leave_thread(self):
  183. """api allows user to remove himself from thread"""
  184. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  185. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  186. response = self.patch(self.api_link, [
  187. {'op': 'remove', 'path': 'participants', 'value': self.user.pk}
  188. ])
  189. self.assertEqual(response.status_code, 200)
  190. self.assertFalse(response.json()['deleted'])
  191. # thread still exists
  192. self.assertTrue(Thread.objects.get(pk=self.thread.pk))
  193. # leave event has valid type
  194. event = self.thread.post_set.order_by('id').last()
  195. self.assertTrue(event.is_event)
  196. self.assertTrue(event.event_type, 'participant_left')
  197. # users were flagged for sync
  198. User = get_user_model()
  199. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  200. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  201. # user was removed from participation
  202. self.assertEqual(self.thread.participants.count(), 1)
  203. self.assertEqual(self.thread.participants.filter(pk=self.user.pk).count(), 0)
  204. def test_user_leave_closed_thread(self):
  205. """api allows user to remove himself from closed thread"""
  206. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  207. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  208. self.thread.is_closed = True
  209. self.thread.save()
  210. response = self.patch(self.api_link, [
  211. {'op': 'remove', 'path': 'participants', 'value': self.user.pk}
  212. ])
  213. self.assertEqual(response.status_code, 200)
  214. self.assertFalse(response.json()['deleted'])
  215. # thread still exists
  216. self.assertTrue(Thread.objects.get(pk=self.thread.pk))
  217. # leave event has valid type
  218. event = self.thread.post_set.order_by('id').last()
  219. self.assertTrue(event.is_event)
  220. self.assertTrue(event.event_type, 'participant_left')
  221. # users were flagged for sync
  222. User = get_user_model()
  223. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  224. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  225. # user was removed from participation
  226. self.assertEqual(self.thread.participants.count(), 1)
  227. self.assertEqual(self.thread.participants.filter(pk=self.user.pk).count(), 0)
  228. def test_owner_remove_user(self):
  229. """api allows owner to remove other user"""
  230. ThreadParticipant.objects.set_owner(self.thread, self.user)
  231. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  232. response = self.patch(self.api_link, [
  233. {'op': 'remove', 'path': 'participants', 'value': self.other_user.pk}
  234. ])
  235. self.assertEqual(response.status_code, 200)
  236. self.assertFalse(response.json()['deleted'])
  237. # thread still exists
  238. self.assertTrue(Thread.objects.get(pk=self.thread.pk))
  239. # leave event has valid type
  240. event = self.thread.post_set.order_by('id').last()
  241. self.assertTrue(event.is_event)
  242. self.assertTrue(event.event_type, 'participant_removed')
  243. # users were flagged for sync
  244. User = get_user_model()
  245. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  246. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  247. # user was removed from participation
  248. self.assertEqual(self.thread.participants.count(), 1)
  249. self.assertEqual(self.thread.participants.filter(pk=self.other_user.pk).count(), 0)
  250. def test_owner_leave_thread(self):
  251. """api allows owner to remove hisemf from thread, causing thread to close"""
  252. ThreadParticipant.objects.set_owner(self.thread, self.user)
  253. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  254. response = self.patch(self.api_link, [
  255. {'op': 'remove', 'path': 'participants', 'value': self.user.pk}
  256. ])
  257. self.assertEqual(response.status_code, 200)
  258. self.assertFalse(response.json()['deleted'])
  259. # thread still exists and is closed
  260. self.assertTrue(Thread.objects.get(pk=self.thread.pk).is_closed)
  261. # leave event has valid type
  262. event = self.thread.post_set.order_by('id').last()
  263. self.assertTrue(event.is_event)
  264. self.assertTrue(event.event_type, 'owner_left')
  265. # users were flagged for sync
  266. User = get_user_model()
  267. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  268. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  269. # user was removed from participation
  270. self.assertEqual(self.thread.participants.count(), 1)
  271. self.assertEqual(self.thread.participants.filter(pk=self.user.pk).count(), 0)
  272. def test_last_user_leave_thread(self):
  273. """api allows last user leave thread, causing thread to delete"""
  274. ThreadParticipant.objects.set_owner(self.thread, self.user)
  275. response = self.patch(self.api_link, [
  276. {'op': 'remove', 'path': 'participants', 'value': self.user.pk}
  277. ])
  278. self.assertEqual(response.status_code, 200)
  279. self.assertTrue(response.json()['deleted'])
  280. # thread is gone
  281. with self.assertRaises(Thread.DoesNotExist):
  282. Thread.objects.get(pk=self.thread.pk)
  283. # users were flagged for sync
  284. User = get_user_model()
  285. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  286. class PrivateThreadTakeOverApiTests(PrivateThreadPatchApiTestCase):
  287. def test_empty_user_id(self):
  288. """api handles empty user id"""
  289. ThreadParticipant.objects.set_owner(self.thread, self.user)
  290. response = self.patch(self.api_link, [
  291. {'op': 'replace', 'path': 'owner', 'value': ''}
  292. ])
  293. self.assertContains(response, "Participant doesn't exist.", status_code=400)
  294. def test_invalid_user_id(self):
  295. """api handles invalid user id"""
  296. ThreadParticipant.objects.set_owner(self.thread, self.user)
  297. response = self.patch(self.api_link, [
  298. {'op': 'replace', 'path': 'owner', 'value': 'dsadsa'}
  299. ])
  300. self.assertContains(response, "Participant doesn't exist.", status_code=400)
  301. def test_nonexistant_user_id(self):
  302. """api handles nonexistant user id"""
  303. ThreadParticipant.objects.set_owner(self.thread, self.user)
  304. response = self.patch(self.api_link, [
  305. {'op': 'replace', 'path': 'owner', 'value': self.other_user.pk}
  306. ])
  307. self.assertContains(response, "Participant doesn't exist.", status_code=400)
  308. def test_no_permission(self):
  309. """non-moderator/owner can't change owner"""
  310. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  311. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  312. response = self.patch(self.api_link, [
  313. {'op': 'replace', 'path': 'owner', 'value': self.user.pk}
  314. ])
  315. self.assertContains(
  316. response, "thread owner and moderators can change threads owners", status_code=400)
  317. def test_no_change(self):
  318. """api validates that new owner id is same as current owner"""
  319. ThreadParticipant.objects.set_owner(self.thread, self.user)
  320. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  321. response = self.patch(self.api_link, [
  322. {'op': 'replace', 'path': 'owner', 'value': self.user.pk}
  323. ])
  324. self.assertContains(response, "This user already is thread owner.", status_code=400)
  325. def test_change_closed_thread_owner(self):
  326. """non-moderator can't change owner in closed thread"""
  327. ThreadParticipant.objects.set_owner(self.thread, self.user)
  328. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  329. self.thread.is_closed = True
  330. self.thread.save()
  331. response = self.patch(self.api_link, [
  332. {'op': 'replace', 'path': 'owner', 'value': self.other_user.pk}
  333. ])
  334. self.assertContains(
  335. response, "Only moderators can change closed threads owners.", status_code=400)
  336. def test_owner_change_thread_owner(self):
  337. """owner can pass thread ownership to other participant"""
  338. ThreadParticipant.objects.set_owner(self.thread, self.user)
  339. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  340. response = self.patch(self.api_link, [
  341. {'op': 'replace', 'path': 'owner', 'value': self.other_user.pk}
  342. ])
  343. self.assertEqual(response.status_code, 200)
  344. # users were flagged for sync
  345. User = get_user_model()
  346. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  347. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  348. # ownership was transfered
  349. self.assertEqual(self.thread.participants.count(), 2)
  350. self.assertTrue(ThreadParticipant.objects.get(user=self.other_user).is_owner)
  351. self.assertFalse(ThreadParticipant.objects.get(user=self.user).is_owner)
  352. # change was recorded in event
  353. event = self.thread.post_set.order_by('id').last()
  354. self.assertTrue(event.is_event)
  355. self.assertTrue(event.event_type, 'changed_owner')
  356. def test_moderator_change_owner(self):
  357. """moderator can change thread owner to other user"""
  358. User = get_user_model()
  359. new_owner = User.objects.create_user(
  360. 'NewOwner', 'new@owner.com', 'pass123')
  361. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  362. ThreadParticipant.objects.add_participants(self.thread, [self.user, new_owner])
  363. override_acl(self.user, {
  364. 'can_moderate_private_threads': 1
  365. })
  366. response = self.patch(self.api_link, [
  367. {'op': 'replace', 'path': 'owner', 'value': new_owner.pk}
  368. ])
  369. self.assertEqual(response.status_code, 200)
  370. # users were flagged for sync
  371. self.assertTrue(User.objects.get(pk=new_owner.pk).sync_unread_private_threads)
  372. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  373. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  374. # ownership was transfered
  375. self.assertEqual(self.thread.participants.count(), 3)
  376. self.assertTrue(ThreadParticipant.objects.get(user=new_owner).is_owner)
  377. self.assertFalse(ThreadParticipant.objects.get(user=self.user).is_owner)
  378. self.assertFalse(ThreadParticipant.objects.get(user=self.other_user).is_owner)
  379. # change was recorded in event
  380. event = self.thread.post_set.order_by('id').last()
  381. self.assertTrue(event.is_event)
  382. self.assertTrue(event.event_type, 'changed_owner')
  383. def test_moderator_takeover(self):
  384. """moderator can takeover the thread"""
  385. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  386. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  387. override_acl(self.user, {
  388. 'can_moderate_private_threads': 1
  389. })
  390. response = self.patch(self.api_link, [
  391. {'op': 'replace', 'path': 'owner', 'value': self.user.pk}
  392. ])
  393. self.assertEqual(response.status_code, 200)
  394. # users were flagged for sync
  395. User = get_user_model()
  396. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  397. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  398. # ownership was transfered
  399. self.assertEqual(self.thread.participants.count(), 2)
  400. self.assertTrue(ThreadParticipant.objects.get(user=self.user).is_owner)
  401. self.assertFalse(ThreadParticipant.objects.get(user=self.other_user).is_owner)
  402. # change was recorded in event
  403. event = self.thread.post_set.order_by('id').last()
  404. self.assertTrue(event.is_event)
  405. self.assertTrue(event.event_type, 'tookover')
  406. def test_moderator_closed_thread_takeover(self):
  407. """moderator can takeover closed thread thread"""
  408. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  409. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  410. self.thread.is_closed = True
  411. self.thread.save()
  412. override_acl(self.user, {
  413. 'can_moderate_private_threads': 1
  414. })
  415. response = self.patch(self.api_link, [
  416. {'op': 'replace', 'path': 'owner', 'value': self.user.pk}
  417. ])
  418. self.assertEqual(response.status_code, 200)
  419. # users were flagged for sync
  420. User = get_user_model()
  421. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  422. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  423. # ownership was transfered
  424. self.assertEqual(self.thread.participants.count(), 2)
  425. self.assertTrue(ThreadParticipant.objects.get(user=self.user).is_owner)
  426. self.assertFalse(ThreadParticipant.objects.get(user=self.other_user).is_owner)
  427. # change was recorded in event
  428. event = self.thread.post_set.order_by('id').last()
  429. self.assertTrue(event.is_event)
  430. self.assertTrue(event.event_type, 'tookover')