test_privatethread_patch_api.py 24 KB

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