test_privatethread_patch_api.py 29 KB

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