test_privatethread_patch_api.py 29 KB

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