test_privatethread_patch_api.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. response = self.patch(self.api_link, [
  191. {'op': 'remove', 'path': 'participants', 'value': self.user.pk}
  192. ])
  193. self.assertEqual(response.status_code, 200)
  194. self.assertFalse(response.json()['deleted'])
  195. # thread still exists
  196. self.assertTrue(Thread.objects.get(pk=self.thread.pk))
  197. # leave event has valid type
  198. event = self.thread.post_set.order_by('id').last()
  199. self.assertTrue(event.is_event)
  200. self.assertTrue(event.event_type, 'participant_left')
  201. # valid users were flagged for sync
  202. User = get_user_model()
  203. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  204. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  205. # user was removed from participation
  206. self.assertEqual(self.thread.participants.count(), 1)
  207. self.assertEqual(self.thread.participants.filter(pk=self.user.pk).count(), 0)
  208. def test_user_leave_closed_thread(self):
  209. """api allows user to remove himself from closed thread"""
  210. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  211. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  212. self.thread.is_closed = True
  213. self.thread.save()
  214. response = self.patch(self.api_link, [
  215. {'op': 'remove', 'path': 'participants', 'value': self.user.pk}
  216. ])
  217. self.assertEqual(response.status_code, 200)
  218. self.assertFalse(response.json()['deleted'])
  219. # thread still exists
  220. self.assertTrue(Thread.objects.get(pk=self.thread.pk))
  221. # leave event has valid type
  222. event = self.thread.post_set.order_by('id').last()
  223. self.assertTrue(event.is_event)
  224. self.assertTrue(event.event_type, 'participant_left')
  225. # valid users were flagged for sync
  226. User = get_user_model()
  227. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  228. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  229. # user was removed from participation
  230. self.assertEqual(self.thread.participants.count(), 1)
  231. self.assertEqual(self.thread.participants.filter(pk=self.user.pk).count(), 0)
  232. def test_moderator_remove_user(self):
  233. """api allows moderator to remove other user"""
  234. User = get_user_model()
  235. removed_user = User.objects.create_user(
  236. 'Vigilante', 'test@test.com', 'pass123')
  237. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  238. ThreadParticipant.objects.add_participants(self.thread, [self.user, removed_user])
  239. override_acl(self.user, {
  240. 'can_moderate_private_threads': True
  241. })
  242. response = self.patch(self.api_link, [
  243. {'op': 'remove', 'path': 'participants', 'value': removed_user.pk}
  244. ])
  245. self.assertEqual(response.status_code, 200)
  246. self.assertFalse(response.json()['deleted'])
  247. # thread still exists
  248. self.assertTrue(Thread.objects.get(pk=self.thread.pk))
  249. # leave event has valid type
  250. event = self.thread.post_set.order_by('id').last()
  251. self.assertTrue(event.is_event)
  252. self.assertTrue(event.event_type, 'participant_removed')
  253. # valid users were flagged for sync
  254. User = get_user_model()
  255. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  256. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  257. self.assertTrue(User.objects.get(pk=removed_user.pk).sync_unread_private_threads)
  258. # user was removed from participation
  259. self.assertEqual(self.thread.participants.count(), 2)
  260. self.assertEqual(self.thread.participants.filter(pk=removed_user.pk).count(), 0)
  261. def test_owner_remove_user(self):
  262. """api allows owner to remove other user"""
  263. ThreadParticipant.objects.set_owner(self.thread, self.user)
  264. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  265. response = self.patch(self.api_link, [
  266. {'op': 'remove', 'path': 'participants', 'value': self.other_user.pk}
  267. ])
  268. self.assertEqual(response.status_code, 200)
  269. self.assertFalse(response.json()['deleted'])
  270. # thread still exists
  271. self.assertTrue(Thread.objects.get(pk=self.thread.pk))
  272. # leave event has valid type
  273. event = self.thread.post_set.order_by('id').last()
  274. self.assertTrue(event.is_event)
  275. self.assertTrue(event.event_type, 'participant_removed')
  276. # valid users were flagged for sync
  277. User = get_user_model()
  278. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  279. self.assertTrue(User.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. User = get_user_model()
  300. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  301. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  302. # user was removed from participation
  303. self.assertEqual(self.thread.participants.count(), 1)
  304. self.assertEqual(self.thread.participants.filter(pk=self.user.pk).count(), 0)
  305. def test_last_user_leave_thread(self):
  306. """api allows last user leave thread, causing thread to delete"""
  307. ThreadParticipant.objects.set_owner(self.thread, self.user)
  308. response = self.patch(self.api_link, [
  309. {'op': 'remove', 'path': 'participants', 'value': self.user.pk}
  310. ])
  311. self.assertEqual(response.status_code, 200)
  312. self.assertTrue(response.json()['deleted'])
  313. # thread is gone
  314. with self.assertRaises(Thread.DoesNotExist):
  315. Thread.objects.get(pk=self.thread.pk)
  316. # valid users were flagged for sync
  317. User = get_user_model()
  318. self.assertTrue(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  319. class PrivateThreadTakeOverApiTests(PrivateThreadPatchApiTestCase):
  320. def test_empty_user_id(self):
  321. """api handles empty user id"""
  322. ThreadParticipant.objects.set_owner(self.thread, self.user)
  323. response = self.patch(self.api_link, [
  324. {'op': 'replace', 'path': 'owner', 'value': ''}
  325. ])
  326. self.assertContains(response, "Participant doesn't exist.", status_code=400)
  327. def test_invalid_user_id(self):
  328. """api handles invalid user id"""
  329. ThreadParticipant.objects.set_owner(self.thread, self.user)
  330. response = self.patch(self.api_link, [
  331. {'op': 'replace', 'path': 'owner', 'value': 'dsadsa'}
  332. ])
  333. self.assertContains(response, "Participant doesn't exist.", status_code=400)
  334. def test_nonexistant_user_id(self):
  335. """api handles nonexistant user id"""
  336. ThreadParticipant.objects.set_owner(self.thread, self.user)
  337. response = self.patch(self.api_link, [
  338. {'op': 'replace', 'path': 'owner', 'value': self.other_user.pk}
  339. ])
  340. self.assertContains(response, "Participant doesn't exist.", status_code=400)
  341. def test_no_permission(self):
  342. """non-moderator/owner can't change owner"""
  343. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  344. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  345. response = self.patch(self.api_link, [
  346. {'op': 'replace', 'path': 'owner', 'value': self.user.pk}
  347. ])
  348. self.assertContains(
  349. response, "thread owner and moderators can change threads owners", status_code=400)
  350. def test_no_change(self):
  351. """api validates that new owner id is same as current owner"""
  352. ThreadParticipant.objects.set_owner(self.thread, self.user)
  353. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  354. response = self.patch(self.api_link, [
  355. {'op': 'replace', 'path': 'owner', 'value': self.user.pk}
  356. ])
  357. self.assertContains(response, "This user already is thread owner.", status_code=400)
  358. def test_change_closed_thread_owner(self):
  359. """non-moderator can't change owner in closed thread"""
  360. ThreadParticipant.objects.set_owner(self.thread, self.user)
  361. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  362. self.thread.is_closed = True
  363. self.thread.save()
  364. response = self.patch(self.api_link, [
  365. {'op': 'replace', 'path': 'owner', 'value': self.other_user.pk}
  366. ])
  367. self.assertContains(
  368. response, "Only moderators can change closed threads owners.", status_code=400)
  369. def test_owner_change_thread_owner(self):
  370. """owner can pass thread ownership to other participant"""
  371. ThreadParticipant.objects.set_owner(self.thread, self.user)
  372. ThreadParticipant.objects.add_participants(self.thread, [self.other_user])
  373. response = self.patch(self.api_link, [
  374. {'op': 'replace', 'path': 'owner', 'value': self.other_user.pk}
  375. ])
  376. self.assertEqual(response.status_code, 200)
  377. # valid users were flagged for sync
  378. User = get_user_model()
  379. self.assertFalse(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  380. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  381. # ownership was transfered
  382. self.assertEqual(self.thread.participants.count(), 2)
  383. self.assertTrue(ThreadParticipant.objects.get(user=self.other_user).is_owner)
  384. self.assertFalse(ThreadParticipant.objects.get(user=self.user).is_owner)
  385. # change was recorded in event
  386. event = self.thread.post_set.order_by('id').last()
  387. self.assertTrue(event.is_event)
  388. self.assertTrue(event.event_type, 'changed_owner')
  389. def test_moderator_change_owner(self):
  390. """moderator can change thread owner to other user"""
  391. User = get_user_model()
  392. new_owner = User.objects.create_user(
  393. 'NewOwner', 'new@owner.com', 'pass123')
  394. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  395. ThreadParticipant.objects.add_participants(self.thread, [self.user, new_owner])
  396. override_acl(self.user, {
  397. 'can_moderate_private_threads': 1
  398. })
  399. response = self.patch(self.api_link, [
  400. {'op': 'replace', 'path': 'owner', 'value': new_owner.pk}
  401. ])
  402. self.assertEqual(response.status_code, 200)
  403. # valid users were flagged for sync
  404. self.assertTrue(User.objects.get(pk=new_owner.pk).sync_unread_private_threads)
  405. self.assertFalse(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  406. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  407. # ownership was transfered
  408. self.assertEqual(self.thread.participants.count(), 3)
  409. self.assertTrue(ThreadParticipant.objects.get(user=new_owner).is_owner)
  410. self.assertFalse(ThreadParticipant.objects.get(user=self.user).is_owner)
  411. self.assertFalse(ThreadParticipant.objects.get(user=self.other_user).is_owner)
  412. # change was recorded in event
  413. event = self.thread.post_set.order_by('id').last()
  414. self.assertTrue(event.is_event)
  415. self.assertTrue(event.event_type, 'changed_owner')
  416. def test_moderator_takeover(self):
  417. """moderator can takeover the thread"""
  418. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  419. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  420. override_acl(self.user, {
  421. 'can_moderate_private_threads': 1
  422. })
  423. response = self.patch(self.api_link, [
  424. {'op': 'replace', 'path': 'owner', 'value': self.user.pk}
  425. ])
  426. self.assertEqual(response.status_code, 200)
  427. # valid users were flagged for sync
  428. User = get_user_model()
  429. self.assertFalse(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  430. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  431. # ownership was transfered
  432. self.assertEqual(self.thread.participants.count(), 2)
  433. self.assertTrue(ThreadParticipant.objects.get(user=self.user).is_owner)
  434. self.assertFalse(ThreadParticipant.objects.get(user=self.other_user).is_owner)
  435. # change was recorded in event
  436. event = self.thread.post_set.order_by('id').last()
  437. self.assertTrue(event.is_event)
  438. self.assertTrue(event.event_type, 'tookover')
  439. def test_moderator_closed_thread_takeover(self):
  440. """moderator can takeover closed thread thread"""
  441. ThreadParticipant.objects.set_owner(self.thread, self.other_user)
  442. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  443. self.thread.is_closed = True
  444. self.thread.save()
  445. override_acl(self.user, {
  446. 'can_moderate_private_threads': 1
  447. })
  448. response = self.patch(self.api_link, [
  449. {'op': 'replace', 'path': 'owner', 'value': self.user.pk}
  450. ])
  451. self.assertEqual(response.status_code, 200)
  452. # valid users were flagged for sync
  453. User = get_user_model()
  454. self.assertFalse(User.objects.get(pk=self.user.pk).sync_unread_private_threads)
  455. self.assertTrue(User.objects.get(pk=self.other_user.pk).sync_unread_private_threads)
  456. # ownership was transfered
  457. self.assertEqual(self.thread.participants.count(), 2)
  458. self.assertTrue(ThreadParticipant.objects.get(user=self.user).is_owner)
  459. self.assertFalse(ThreadParticipant.objects.get(user=self.other_user).is_owner)
  460. # change was recorded in event
  461. event = self.thread.post_set.order_by('id').last()
  462. self.assertTrue(event.is_event)
  463. self.assertTrue(event.event_type, 'tookover')