test_privatethread_patch_api.py 30 KB

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