test_privatethread_patch_api.py 27 KB

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