test_privatethread_patch_api.py 26 KB

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