test_privatethread_patch_api.py 25 KB

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