test_privatethread_patch_api.py 22 KB

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