test_privatethread_patch_api.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. import json
  2. from django.contrib.auth import get_user_model
  3. from django.core import mail
  4. from misago.acl.test import patch_user_acl
  5. from misago.threads import testutils
  6. from misago.threads.test import other_user_cant_use_private_threads
  7. from misago.threads.models import Thread, ThreadParticipant
  8. from .test_privatethreads import PrivateThreadsTestCase
  9. UserModel = get_user_model()
  10. class PrivateThreadPatchApiTestCase(PrivateThreadsTestCase):
  11. def setUp(self):
  12. super().setUp()
  13. self.thread = testutils.post_thread(self.category, poster=self.user)
  14. self.api_link = self.thread.get_api_url()
  15. self.other_user = UserModel.objects.create_user(
  16. 'BobBoberson', 'bob@boberson.com', 'pass123'
  17. )
  18. def patch(self, api_link, ops):
  19. return self.client.patch(api_link, json.dumps(ops), content_type="application/json")
  20. class PrivateThreadAddParticipantApiTests(PrivateThreadPatchApiTestCase):
  21. def test_add_participant_not_owner(self):
  22. """non-owner can't add participant"""
  23. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  24. response = self.patch(
  25. self.api_link, [
  26. {
  27. 'op': 'add',
  28. 'path': 'participants',
  29. 'value': self.user.username,
  30. },
  31. ]
  32. )
  33. self.assertEqual(response.status_code, 400)
  34. self.assertEqual(response.json(), {
  35. 'id': self.thread.pk,
  36. 'detail': ["You have to be thread owner to add new participants to it."],
  37. })
  38. def test_add_empty_username(self):
  39. """path validates username"""
  40. ThreadParticipant.objects.set_owner(self.thread, self.user)
  41. response = self.patch(
  42. self.api_link, [
  43. {
  44. 'op': 'add',
  45. 'path': 'participants',
  46. 'value': '',
  47. },
  48. ]
  49. )
  50. self.assertEqual(response.status_code, 400)
  51. self.assertEqual(response.json(), {
  52. 'id': self.thread.pk,
  53. 'detail': ["You have to enter new participant's username."],
  54. })
  55. def test_add_nonexistant_user(self):
  56. """can't user two times"""
  57. ThreadParticipant.objects.set_owner(self.thread, self.user)
  58. response = self.patch(
  59. self.api_link, [
  60. {
  61. 'op': 'add',
  62. 'path': 'participants',
  63. 'value': 'InvalidUser',
  64. },
  65. ]
  66. )
  67. self.assertEqual(response.status_code, 400)
  68. self.assertEqual(response.json(), {
  69. 'id': self.thread.pk,
  70. 'detail': ["No user with such name exists."],
  71. })
  72. def test_add_already_participant(self):
  73. """can't add user that is already participant"""
  74. ThreadParticipant.objects.set_owner(self.thread, self.user)
  75. response = self.patch(
  76. self.api_link, [
  77. {
  78. 'op': 'add',
  79. 'path': 'participants',
  80. 'value': self.user.username,
  81. },
  82. ]
  83. )
  84. self.assertEqual(response.status_code, 400)
  85. self.assertEqual(response.json(), {
  86. 'id': self.thread.pk,
  87. 'detail': ["This user is already thread participant."],
  88. })
  89. def test_add_blocking_user(self):
  90. """can't add user that is already participant"""
  91. ThreadParticipant.objects.set_owner(self.thread, self.user)
  92. self.other_user.blocks.add(self.user)
  93. response = self.patch(
  94. self.api_link, [
  95. {
  96. 'op': 'add',
  97. 'path': 'participants',
  98. 'value': self.other_user.username,
  99. },
  100. ]
  101. )
  102. self.assertEqual(response.status_code, 400)
  103. self.assertEqual(response.json(), {
  104. 'id': self.thread.pk,
  105. 'detail': ["BobBoberson is blocking you."],
  106. })
  107. @patch_user_acl(other_user_cant_use_private_threads)
  108. def test_add_no_perm_user(self):
  109. """can't add user that has no permission to use private threads"""
  110. ThreadParticipant.objects.set_owner(self.thread, self.user)
  111. response = self.patch(
  112. self.api_link, [
  113. {
  114. 'op': 'add',
  115. 'path': 'participants',
  116. 'value': self.other_user.username,
  117. },
  118. ]
  119. )
  120. self.assertEqual(response.status_code, 400)
  121. self.assertEqual(response.json(), {
  122. 'id': self.thread.pk,
  123. 'detail': ["BobBoberson can't participate in private threads."],
  124. })
  125. def test_add_too_many_users(self):
  126. """can't add user that is already participant"""
  127. ThreadParticipant.objects.set_owner(self.thread, self.user)
  128. for i in range(self.user.acl_cache['max_private_thread_participants']):
  129. user = UserModel.objects.create_user(
  130. 'User%s' % i, 'user%s@example.com' % i, 'Pass.123'
  131. )
  132. ThreadParticipant.objects.add_participants(self.thread, [user])
  133. response = self.patch(
  134. self.api_link, [
  135. {
  136. 'op': 'add',
  137. 'path': 'participants',
  138. 'value': self.other_user.username,
  139. },
  140. ]
  141. )
  142. self.assertEqual(response.status_code, 400)
  143. self.assertEqual(response.json(), {
  144. 'id': self.thread.pk,
  145. 'detail': ["You can't add any more new users to this thread."],
  146. })
  147. def test_add_user_closed_thread(self):
  148. """adding user to closed thread fails for non-moderator"""
  149. ThreadParticipant.objects.set_owner(self.thread, self.user)
  150. self.thread.is_closed = True
  151. self.thread.save()
  152. response = self.patch(
  153. self.api_link, [
  154. {
  155. 'op': 'add',
  156. 'path': 'participants',
  157. 'value': self.other_user.username,
  158. },
  159. ]
  160. )
  161. self.assertEqual(response.status_code, 400)
  162. self.assertEqual(response.json(), {
  163. 'id': self.thread.pk,
  164. 'detail': ["Only moderators can add participants to closed threads."],
  165. })
  166. def test_add_user(self):
  167. """adding user to thread add user to thread as participant, sets event and emails him"""
  168. ThreadParticipant.objects.set_owner(self.thread, self.user)
  169. self.patch(
  170. self.api_link, [
  171. {
  172. 'op': 'add',
  173. 'path': 'participants',
  174. 'value': self.other_user.username,
  175. },
  176. ]
  177. )
  178. # event was set on thread
  179. event = self.thread.post_set.order_by('id').last()
  180. self.assertTrue(event.is_event)
  181. self.assertTrue(event.event_type, 'added_participant')
  182. # notification about new private thread was sent to other user
  183. self.assertEqual(len(mail.outbox), 1)
  184. email = mail.outbox[-1]
  185. self.assertIn(self.user.username, email.subject)
  186. self.assertIn(self.thread.title, email.subject)
  187. @patch_user_acl({'can_moderate_private_threads': True})
  188. def test_add_user_to_other_user_thread_moderator(self):
  189. """moderators can add users to other users threads"""
  190. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  191. self.thread.has_reported_posts = True
  192. self.thread.save()
  193. self.patch(
  194. self.api_link, [
  195. {
  196. 'op': 'add',
  197. 'path': 'participants',
  198. 'value': self.user.username,
  199. },
  200. ]
  201. )
  202. # event was set on thread
  203. event = self.thread.post_set.order_by('id').last()
  204. self.assertTrue(event.is_event)
  205. self.assertTrue(event.event_type, 'entered_thread')
  206. # notification about new private thread wasn't send because we invited ourselves
  207. self.assertEqual(len(mail.outbox), 0)
  208. @patch_user_acl({'can_moderate_private_threads': True})
  209. def test_add_user_to_closed_moderator(self):
  210. """moderators can add users to closed threads"""
  211. ThreadParticipant.objects.set_owner(self.thread, self.user)
  212. self.thread.is_closed = True
  213. self.thread.save()
  214. self.patch(
  215. self.api_link, [
  216. {
  217. 'op': 'add',
  218. 'path': 'participants',
  219. 'value': self.other_user.username,
  220. },
  221. ]
  222. )
  223. # event was set on thread
  224. event = self.thread.post_set.order_by('id').last()
  225. self.assertTrue(event.is_event)
  226. self.assertTrue(event.event_type, 'added_participant')
  227. # notification about new private thread was sent to other user
  228. self.assertEqual(len(mail.outbox), 1)
  229. email = mail.outbox[-1]
  230. self.assertIn(self.user.username, email.subject)
  231. self.assertIn(self.thread.title, email.subject)
  232. class PrivateThreadRemoveParticipantApiTests(PrivateThreadPatchApiTestCase):
  233. def test_remove_empty(self):
  234. """api handles empty user id"""
  235. ThreadParticipant.objects.set_owner(self.thread, self.user)
  236. response = self.patch(
  237. self.api_link, [
  238. {
  239. 'op': 'remove',
  240. 'path': 'participants',
  241. 'value': '',
  242. },
  243. ]
  244. )
  245. self.assertEqual(response.status_code, 400)
  246. self.assertEqual(response.json(), {
  247. 'id': self.thread.pk,
  248. 'detail': ["A valid integer is required."],
  249. })
  250. def test_remove_invalid(self):
  251. """api validates user id type"""
  252. ThreadParticipant.objects.set_owner(self.thread, self.user)
  253. response = self.patch(
  254. self.api_link, [
  255. {
  256. 'op': 'remove',
  257. 'path': 'participants',
  258. 'value': 'string',
  259. },
  260. ]
  261. )
  262. self.assertEqual(response.status_code, 400)
  263. self.assertEqual(response.json(), {
  264. 'id': self.thread.pk,
  265. 'detail': ["A valid integer is required."],
  266. })
  267. def test_remove_nonexistant(self):
  268. """removed user has to be participant"""
  269. ThreadParticipant.objects.set_owner(self.thread, self.user)
  270. response = self.patch(
  271. self.api_link, [
  272. {
  273. 'op': 'remove',
  274. 'path': 'participants',
  275. 'value': self.other_user.pk,
  276. },
  277. ]
  278. )
  279. self.assertEqual(response.status_code, 400)
  280. self.assertEqual(response.json(), {
  281. 'id': self.thread.pk,
  282. 'detail': ["Participant doesn't exist."],
  283. })
  284. def test_remove_not_owner(self):
  285. """api validates if user trying to remove other user is an owner"""
  286. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  287. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  288. response = self.patch(
  289. self.api_link, [
  290. {
  291. 'op': 'remove',
  292. 'path': 'participants',
  293. 'value': self.other_user.pk,
  294. },
  295. ]
  296. )
  297. self.assertEqual(response.status_code, 400)
  298. self.assertEqual(response.json(), {
  299. 'id': self.thread.pk,
  300. 'detail': ["You have to be thread owner to remove participants from it."],
  301. })
  302. def test_owner_remove_user_closed_thread(self):
  303. """api disallows owner to remove other user from closed thread"""
  304. ThreadParticipant.objects.set_owner(self.thread, self.user)
  305. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  306. self.thread.is_closed = True
  307. self.thread.save()
  308. response = self.patch(
  309. self.api_link, [
  310. {
  311. 'op': 'remove',
  312. 'path': 'participants',
  313. 'value': self.other_user.pk,
  314. },
  315. ]
  316. )
  317. self.assertEqual(response.status_code, 400)
  318. self.assertEqual(response.json(), {
  319. 'id': self.thread.pk,
  320. 'detail': ["Only moderators can remove participants from closed threads."],
  321. })
  322. def test_user_leave_thread(self):
  323. """api allows user to remove himself from thread"""
  324. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  325. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  326. self.user.subscription_set.create(
  327. category=self.category,
  328. thread=self.thread,
  329. )
  330. response = self.patch(
  331. self.api_link, [
  332. {
  333. 'op': 'remove',
  334. 'path': 'participants',
  335. 'value': self.user.pk,
  336. },
  337. ]
  338. )
  339. self.assertEqual(response.status_code, 200)
  340. self.assertFalse(response.json()['deleted'])
  341. # thread still exists
  342. self.assertTrue(Thread.objects.get(pk=self.thread.pk))
  343. # leave event has valid type
  344. event = self.thread.post_set.order_by('id').last()
  345. self.assertTrue(event.is_event)
  346. self.assertTrue(event.event_type, 'participant_left')
  347. # valid users were flagged for sync
  348. self.assertTrue(UserModel.objects.get(pk=self.user.pk).sync_unread_private_threads)
  349. self.assertTrue(UserModel.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  350. # user was removed from participation
  351. self.assertEqual(self.thread.participants.count(), 1)
  352. self.assertEqual(self.thread.participants.filter(pk=self.user.pk).count(), 0)
  353. # thread was removed from user subscriptions
  354. self.assertEqual(self.user.subscription_set.count(), 0)
  355. def test_user_leave_closed_thread(self):
  356. """api allows user to remove himself from closed thread"""
  357. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  358. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  359. self.thread.is_closed = True
  360. self.thread.save()
  361. response = self.patch(
  362. self.api_link, [
  363. {
  364. 'op': 'remove',
  365. 'path': 'participants',
  366. 'value': self.user.pk,
  367. },
  368. ]
  369. )
  370. self.assertEqual(response.status_code, 200)
  371. self.assertFalse(response.json()['deleted'])
  372. # thread still exists
  373. self.assertTrue(Thread.objects.get(pk=self.thread.pk))
  374. # leave event has valid type
  375. event = self.thread.post_set.order_by('id').last()
  376. self.assertTrue(event.is_event)
  377. self.assertTrue(event.event_type, 'participant_left')
  378. # valid users were flagged for sync
  379. self.assertTrue(UserModel.objects.get(pk=self.user.pk).sync_unread_private_threads)
  380. self.assertTrue(UserModel.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  381. # user was removed from participation
  382. self.assertEqual(self.thread.participants.count(), 1)
  383. self.assertEqual(self.thread.participants.filter(pk=self.user.pk).count(), 0)
  384. @patch_user_acl({'can_moderate_private_threads': True})
  385. def test_moderator_remove_user(self):
  386. """api allows moderator to remove other user"""
  387. removed_user = UserModel.objects.create_user('Vigilante', 'test@test.com', 'pass123')
  388. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  389. ThreadParticipant.objects.add_participants(self.thread, [self.user, removed_user])
  390. response = self.patch(
  391. self.api_link, [
  392. {
  393. 'op': 'remove',
  394. 'path': 'participants',
  395. 'value': removed_user.pk,
  396. },
  397. ]
  398. )
  399. self.assertEqual(response.status_code, 200)
  400. self.assertFalse(response.json()['deleted'])
  401. # thread still exists
  402. self.assertTrue(Thread.objects.get(pk=self.thread.pk))
  403. # leave event has valid type
  404. event = self.thread.post_set.order_by('id').last()
  405. self.assertTrue(event.is_event)
  406. self.assertTrue(event.event_type, 'participant_removed')
  407. # valid users were flagged for sync
  408. self.assertTrue(UserModel.objects.get(pk=self.user.pk).sync_unread_private_threads)
  409. self.assertTrue(UserModel.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  410. self.assertTrue(UserModel.objects.get(pk=removed_user.pk).sync_unread_private_threads)
  411. # user was removed from participation
  412. self.assertEqual(self.thread.participants.count(), 2)
  413. self.assertEqual(self.thread.participants.filter(pk=removed_user.pk).count(), 0)
  414. def test_owner_remove_user(self):
  415. """api allows owner to remove other user"""
  416. ThreadParticipant.objects.set_owner(self.thread, self.user)
  417. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  418. response = self.patch(
  419. self.api_link, [
  420. {
  421. 'op': 'remove',
  422. 'path': 'participants',
  423. 'value': self.other_user.pk,
  424. },
  425. ]
  426. )
  427. self.assertEqual(response.status_code, 200)
  428. self.assertFalse(response.json()['deleted'])
  429. # thread still exists
  430. self.assertTrue(Thread.objects.get(pk=self.thread.pk))
  431. # leave event has valid type
  432. event = self.thread.post_set.order_by('id').last()
  433. self.assertTrue(event.is_event)
  434. self.assertTrue(event.event_type, 'participant_removed')
  435. # valid users were flagged for sync
  436. self.assertTrue(UserModel.objects.get(pk=self.user.pk).sync_unread_private_threads)
  437. self.assertTrue(UserModel.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  438. # user was removed from participation
  439. self.assertEqual(self.thread.participants.count(), 1)
  440. self.assertEqual(self.thread.participants.filter(pk=self.other_user.pk).count(), 0)
  441. def test_owner_leave_thread(self):
  442. """api allows owner to remove hisemf from thread, causing thread to close"""
  443. ThreadParticipant.objects.set_owner(self.thread, self.user)
  444. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  445. response = self.patch(
  446. self.api_link, [
  447. {
  448. 'op': 'remove',
  449. 'path': 'participants',
  450. 'value': self.user.pk,
  451. },
  452. ]
  453. )
  454. self.assertEqual(response.status_code, 200)
  455. self.assertFalse(response.json()['deleted'])
  456. # thread still exists and is closed
  457. self.assertTrue(Thread.objects.get(pk=self.thread.pk).is_closed)
  458. # leave event has valid type
  459. event = self.thread.post_set.order_by('id').last()
  460. self.assertTrue(event.is_event)
  461. self.assertTrue(event.event_type, 'owner_left')
  462. # valid users were flagged for sync
  463. self.assertTrue(UserModel.objects.get(pk=self.user.pk).sync_unread_private_threads)
  464. self.assertTrue(UserModel.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  465. # user was removed from participation
  466. self.assertEqual(self.thread.participants.count(), 1)
  467. self.assertEqual(self.thread.participants.filter(pk=self.user.pk).count(), 0)
  468. def test_last_user_leave_thread(self):
  469. """api allows last user leave thread, causing thread to delete"""
  470. ThreadParticipant.objects.set_owner(self.thread, self.user)
  471. response = self.patch(
  472. self.api_link, [
  473. {
  474. 'op': 'remove',
  475. 'path': 'participants',
  476. 'value': self.user.pk,
  477. },
  478. ]
  479. )
  480. self.assertEqual(response.status_code, 200)
  481. self.assertTrue(response.json()['deleted'])
  482. # thread is gone
  483. with self.assertRaises(Thread.DoesNotExist):
  484. Thread.objects.get(pk=self.thread.pk)
  485. # valid users were flagged for sync
  486. self.assertTrue(UserModel.objects.get(pk=self.user.pk).sync_unread_private_threads)
  487. class PrivateThreadTakeOverApiTests(PrivateThreadPatchApiTestCase):
  488. def test_empty_user_id(self):
  489. """api handles empty user id"""
  490. ThreadParticipant.objects.set_owner(self.thread, self.user)
  491. response = self.patch(
  492. self.api_link, [
  493. {
  494. 'op': 'replace',
  495. 'path': 'owner',
  496. 'value': '',
  497. },
  498. ]
  499. )
  500. self.assertEqual(response.status_code, 400)
  501. self.assertEqual(response.json(), {
  502. 'id': self.thread.pk,
  503. 'detail': ["A valid integer is required."],
  504. })
  505. def test_invalid_user_id(self):
  506. """api handles invalid user id"""
  507. ThreadParticipant.objects.set_owner(self.thread, self.user)
  508. response = self.patch(
  509. self.api_link, [
  510. {
  511. 'op': 'replace',
  512. 'path': 'owner',
  513. 'value': 'dsadsa',
  514. },
  515. ]
  516. )
  517. self.assertEqual(response.status_code, 400)
  518. self.assertEqual(response.json(), {
  519. 'id': self.thread.pk,
  520. 'detail': ["A valid integer is required."],
  521. })
  522. def test_nonexistant_user_id(self):
  523. """api handles nonexistant user id"""
  524. ThreadParticipant.objects.set_owner(self.thread, self.user)
  525. response = self.patch(
  526. self.api_link, [
  527. {
  528. 'op': 'replace',
  529. 'path': 'owner',
  530. 'value': self.other_user.pk,
  531. },
  532. ]
  533. )
  534. self.assertEqual(response.status_code, 400)
  535. self.assertEqual(response.json(), {
  536. 'id': self.thread.pk,
  537. 'detail': ["Participant doesn't exist."],
  538. })
  539. def test_no_permission(self):
  540. """non-moderator/owner can't change owner"""
  541. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  542. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  543. response = self.patch(
  544. self.api_link, [
  545. {
  546. 'op': 'replace',
  547. 'path': 'owner',
  548. 'value': self.user.pk,
  549. },
  550. ]
  551. )
  552. self.assertEqual(response.status_code, 400)
  553. self.assertEqual(response.json(), {
  554. 'id': self.thread.pk,
  555. 'detail': ["Only thread owner and moderators can change threads owners."],
  556. })
  557. def test_no_change(self):
  558. """api validates that new owner id is same as current owner"""
  559. ThreadParticipant.objects.set_owner(self.thread, self.user)
  560. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  561. response = self.patch(
  562. self.api_link, [
  563. {
  564. 'op': 'replace',
  565. 'path': 'owner',
  566. 'value': self.user.pk,
  567. },
  568. ]
  569. )
  570. self.assertEqual(response.status_code, 400)
  571. self.assertEqual(response.json(), {
  572. 'id': self.thread.pk,
  573. 'detail': ["This user already is thread owner."],
  574. })
  575. def test_change_closed_thread_owner(self):
  576. """non-moderator can't change owner in closed thread"""
  577. ThreadParticipant.objects.set_owner(self.thread, self.user)
  578. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  579. self.thread.is_closed = True
  580. self.thread.save()
  581. response = self.patch(
  582. self.api_link, [
  583. {
  584. 'op': 'replace',
  585. 'path': 'owner',
  586. 'value': self.other_user.pk,
  587. },
  588. ]
  589. )
  590. self.assertEqual(response.status_code, 400)
  591. self.assertEqual(response.json(), {
  592. 'id': self.thread.pk,
  593. 'detail': ["Only moderators can change closed threads owners."],
  594. })
  595. def test_owner_change_thread_owner(self):
  596. """owner can pass thread ownership to other participant"""
  597. ThreadParticipant.objects.set_owner(self.thread, self.user)
  598. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  599. response = self.patch(
  600. self.api_link, [
  601. {
  602. 'op': 'replace',
  603. 'path': 'owner',
  604. 'value': self.other_user.pk,
  605. },
  606. ]
  607. )
  608. self.assertEqual(response.status_code, 200)
  609. # valid users were flagged for sync
  610. self.assertFalse(UserModel.objects.get(pk=self.user.pk).sync_unread_private_threads)
  611. self.assertTrue(UserModel.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  612. # ownership was transfered
  613. self.assertEqual(self.thread.participants.count(), 2)
  614. self.assertTrue(ThreadParticipant.objects.get(user=self.other_user).is_owner)
  615. self.assertFalse(ThreadParticipant.objects.get(user=self.user).is_owner)
  616. # change was recorded in event
  617. event = self.thread.post_set.order_by('id').last()
  618. self.assertTrue(event.is_event)
  619. self.assertTrue(event.event_type, 'changed_owner')
  620. @patch_user_acl({'can_moderate_private_threads': True})
  621. def test_moderator_change_owner(self):
  622. """moderator can change thread owner to other user"""
  623. new_owner = UserModel.objects.create_user('NewOwner', 'new@owner.com', 'pass123')
  624. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  625. ThreadParticipant.objects.add_participants(self.thread, [self.user, new_owner])
  626. response = self.patch(
  627. self.api_link, [
  628. {
  629. 'op': 'replace',
  630. 'path': 'owner',
  631. 'value': new_owner.pk,
  632. },
  633. ]
  634. )
  635. self.assertEqual(response.status_code, 200)
  636. # valid users were flagged for sync
  637. self.assertTrue(UserModel.objects.get(pk=new_owner.pk).sync_unread_private_threads)
  638. self.assertFalse(UserModel.objects.get(pk=self.user.pk).sync_unread_private_threads)
  639. self.assertTrue(UserModel.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  640. # ownership was transferred
  641. self.assertEqual(self.thread.participants.count(), 3)
  642. self.assertTrue(ThreadParticipant.objects.get(user=new_owner).is_owner)
  643. self.assertFalse(ThreadParticipant.objects.get(user=self.user).is_owner)
  644. self.assertFalse(ThreadParticipant.objects.get(user=self.other_user).is_owner)
  645. # change was recorded in event
  646. event = self.thread.post_set.order_by('id').last()
  647. self.assertTrue(event.is_event)
  648. self.assertTrue(event.event_type, 'changed_owner')
  649. @patch_user_acl({'can_moderate_private_threads': True})
  650. def test_moderator_takeover(self):
  651. """moderator can takeover the thread"""
  652. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  653. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  654. response = self.patch(
  655. self.api_link, [
  656. {
  657. 'op': 'replace',
  658. 'path': 'owner',
  659. 'value': self.user.pk,
  660. },
  661. ]
  662. )
  663. self.assertEqual(response.status_code, 200)
  664. # valid users were flagged for sync
  665. self.assertFalse(UserModel.objects.get(pk=self.user.pk).sync_unread_private_threads)
  666. self.assertTrue(UserModel.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  667. # ownership was transfered
  668. self.assertEqual(self.thread.participants.count(), 2)
  669. self.assertTrue(ThreadParticipant.objects.get(user=self.user).is_owner)
  670. self.assertFalse(ThreadParticipant.objects.get(user=self.other_user).is_owner)
  671. # change was recorded in event
  672. event = self.thread.post_set.order_by('id').last()
  673. self.assertTrue(event.is_event)
  674. self.assertTrue(event.event_type, 'tookover')
  675. @patch_user_acl({'can_moderate_private_threads': True})
  676. def test_moderator_closed_thread_takeover(self):
  677. """moderator can takeover closed thread thread"""
  678. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  679. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  680. self.thread.is_closed = True
  681. self.thread.save()
  682. response = self.patch(
  683. self.api_link, [
  684. {
  685. 'op': 'replace',
  686. 'path': 'owner',
  687. 'value': self.user.pk,
  688. },
  689. ]
  690. )
  691. self.assertEqual(response.status_code, 200)
  692. # valid users were flagged for sync
  693. self.assertFalse(UserModel.objects.get(pk=self.user.pk).sync_unread_private_threads)
  694. self.assertTrue(UserModel.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  695. # ownership was transferred
  696. self.assertEqual(self.thread.participants.count(), 2)
  697. self.assertTrue(ThreadParticipant.objects.get(user=self.user).is_owner)
  698. self.assertFalse(ThreadParticipant.objects.get(user=self.other_user).is_owner)
  699. # change was recorded in event
  700. event = self.thread.post_set.order_by('id').last()
  701. self.assertTrue(event.is_event)
  702. self.assertTrue(event.event_type, 'tookover')