abuses.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  3. import { AbuseCreate, AbuseState } from '@shared/models'
  4. import {
  5. addAbuseMessage,
  6. cleanupTests,
  7. createUser,
  8. deleteAbuse,
  9. deleteAbuseMessage,
  10. doubleFollow,
  11. flushAndRunServer,
  12. generateUserAccessToken,
  13. getAdminAbusesList,
  14. getVideoIdFromUUID,
  15. listAbuseMessages,
  16. makeGetRequest,
  17. makePostBodyRequest,
  18. reportAbuse,
  19. ServerInfo,
  20. setAccessTokensToServers,
  21. updateAbuse,
  22. uploadVideo,
  23. userLogin,
  24. waitJobs
  25. } from '../../../../shared/extra-utils'
  26. import {
  27. checkBadCountPagination,
  28. checkBadSortPagination,
  29. checkBadStartPagination
  30. } from '../../../../shared/extra-utils/requests/check-api-params'
  31. import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
  32. describe('Test abuses API validators', function () {
  33. const basePath = '/api/v1/abuses/'
  34. let server: ServerInfo
  35. let userAccessToken = ''
  36. let userAccessToken2 = ''
  37. let abuseId: number
  38. let messageId: number
  39. // ---------------------------------------------------------------
  40. before(async function () {
  41. this.timeout(30000)
  42. server = await flushAndRunServer(1)
  43. await setAccessTokensToServers([ server ])
  44. const username = 'user1'
  45. const password = 'my super password'
  46. await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
  47. userAccessToken = await userLogin(server, { username, password })
  48. {
  49. userAccessToken2 = await generateUserAccessToken(server, 'user_2')
  50. }
  51. const res = await uploadVideo(server.url, server.accessToken, {})
  52. server.video = res.body.video
  53. })
  54. describe('When listing abuses for admins', function () {
  55. const path = basePath
  56. it('Should fail with a bad start pagination', async function () {
  57. await checkBadStartPagination(server.url, path, server.accessToken)
  58. })
  59. it('Should fail with a bad count pagination', async function () {
  60. await checkBadCountPagination(server.url, path, server.accessToken)
  61. })
  62. it('Should fail with an incorrect sort', async function () {
  63. await checkBadSortPagination(server.url, path, server.accessToken)
  64. })
  65. it('Should fail with a non authenticated user', async function () {
  66. await makeGetRequest({
  67. url: server.url,
  68. path,
  69. statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
  70. })
  71. })
  72. it('Should fail with a non admin user', async function () {
  73. await makeGetRequest({
  74. url: server.url,
  75. path,
  76. token: userAccessToken,
  77. statusCodeExpected: HttpStatusCode.FORBIDDEN_403
  78. })
  79. })
  80. it('Should fail with a bad id filter', async function () {
  81. await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { id: 'toto' } })
  82. })
  83. it('Should fail with a bad filter', async function () {
  84. await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { filter: 'toto' } })
  85. await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { filter: 'videos' } })
  86. })
  87. it('Should fail with bad predefined reason', async function () {
  88. await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { predefinedReason: 'violentOrRepulsives' } })
  89. })
  90. it('Should fail with a bad state filter', async function () {
  91. await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { state: 'toto' } })
  92. await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { state: 0 } })
  93. })
  94. it('Should fail with a bad videoIs filter', async function () {
  95. await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { videoIs: 'toto' } })
  96. })
  97. it('Should succeed with the correct params', async function () {
  98. const query = {
  99. id: 13,
  100. predefinedReason: 'violentOrRepulsive',
  101. filter: 'comment',
  102. state: 2,
  103. videoIs: 'deleted'
  104. }
  105. await makeGetRequest({ url: server.url, path, token: server.accessToken, query, statusCodeExpected: HttpStatusCode.OK_200 })
  106. })
  107. })
  108. describe('When listing abuses for users', function () {
  109. const path = '/api/v1/users/me/abuses'
  110. it('Should fail with a bad start pagination', async function () {
  111. await checkBadStartPagination(server.url, path, userAccessToken)
  112. })
  113. it('Should fail with a bad count pagination', async function () {
  114. await checkBadCountPagination(server.url, path, userAccessToken)
  115. })
  116. it('Should fail with an incorrect sort', async function () {
  117. await checkBadSortPagination(server.url, path, userAccessToken)
  118. })
  119. it('Should fail with a non authenticated user', async function () {
  120. await makeGetRequest({
  121. url: server.url,
  122. path,
  123. statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
  124. })
  125. })
  126. it('Should fail with a bad id filter', async function () {
  127. await makeGetRequest({ url: server.url, path, token: userAccessToken, query: { id: 'toto' } })
  128. })
  129. it('Should fail with a bad state filter', async function () {
  130. await makeGetRequest({ url: server.url, path, token: userAccessToken, query: { state: 'toto' } })
  131. await makeGetRequest({ url: server.url, path, token: userAccessToken, query: { state: 0 } })
  132. })
  133. it('Should succeed with the correct params', async function () {
  134. const query = {
  135. id: 13,
  136. state: 2
  137. }
  138. await makeGetRequest({ url: server.url, path, token: userAccessToken, query, statusCodeExpected: HttpStatusCode.OK_200 })
  139. })
  140. })
  141. describe('When reporting an abuse', function () {
  142. const path = basePath
  143. it('Should fail with nothing', async function () {
  144. const fields = {}
  145. await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
  146. })
  147. it('Should fail with a wrong video', async function () {
  148. const fields = { video: { id: 'blabla' }, reason: 'my super reason' }
  149. await makePostBodyRequest({ url: server.url, path: path, token: userAccessToken, fields })
  150. })
  151. it('Should fail with an unknown video', async function () {
  152. const fields = { video: { id: 42 }, reason: 'my super reason' }
  153. await makePostBodyRequest({
  154. url: server.url,
  155. path,
  156. token: userAccessToken,
  157. fields,
  158. statusCodeExpected: HttpStatusCode.NOT_FOUND_404
  159. })
  160. })
  161. it('Should fail with a wrong comment', async function () {
  162. const fields = { comment: { id: 'blabla' }, reason: 'my super reason' }
  163. await makePostBodyRequest({ url: server.url, path: path, token: userAccessToken, fields })
  164. })
  165. it('Should fail with an unknown comment', async function () {
  166. const fields = { comment: { id: 42 }, reason: 'my super reason' }
  167. await makePostBodyRequest({
  168. url: server.url,
  169. path,
  170. token: userAccessToken,
  171. fields,
  172. statusCodeExpected: HttpStatusCode.NOT_FOUND_404
  173. })
  174. })
  175. it('Should fail with a wrong account', async function () {
  176. const fields = { account: { id: 'blabla' }, reason: 'my super reason' }
  177. await makePostBodyRequest({ url: server.url, path: path, token: userAccessToken, fields })
  178. })
  179. it('Should fail with an unknown account', async function () {
  180. const fields = { account: { id: 42 }, reason: 'my super reason' }
  181. await makePostBodyRequest({
  182. url: server.url,
  183. path,
  184. token: userAccessToken,
  185. fields,
  186. statusCodeExpected: HttpStatusCode.NOT_FOUND_404
  187. })
  188. })
  189. it('Should fail with not account, comment or video', async function () {
  190. const fields = { reason: 'my super reason' }
  191. await makePostBodyRequest({
  192. url: server.url,
  193. path,
  194. token: userAccessToken,
  195. fields,
  196. statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
  197. })
  198. })
  199. it('Should fail with a non authenticated user', async function () {
  200. const fields = { video: { id: server.video.id }, reason: 'my super reason' }
  201. await makePostBodyRequest({ url: server.url, path, token: 'hello', fields, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
  202. })
  203. it('Should fail with a reason too short', async function () {
  204. const fields = { video: { id: server.video.id }, reason: 'h' }
  205. await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
  206. })
  207. it('Should fail with a too big reason', async function () {
  208. const fields = { video: { id: server.video.id }, reason: 'super'.repeat(605) }
  209. await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
  210. })
  211. it('Should succeed with the correct parameters (basic)', async function () {
  212. const fields: AbuseCreate = { video: { id: server.video.id }, reason: 'my super reason' }
  213. const res = await makePostBodyRequest({
  214. url: server.url,
  215. path,
  216. token: userAccessToken,
  217. fields,
  218. statusCodeExpected: HttpStatusCode.OK_200
  219. })
  220. abuseId = res.body.abuse.id
  221. })
  222. it('Should fail with a wrong predefined reason', async function () {
  223. const fields = { video: { id: server.video.id }, reason: 'my super reason', predefinedReasons: [ 'wrongPredefinedReason' ] }
  224. await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
  225. })
  226. it('Should fail with negative timestamps', async function () {
  227. const fields = { video: { id: server.video.id, startAt: -1 }, reason: 'my super reason' }
  228. await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
  229. })
  230. it('Should fail mith misordered startAt/endAt', async function () {
  231. const fields = { video: { id: server.video.id, startAt: 5, endAt: 1 }, reason: 'my super reason' }
  232. await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
  233. })
  234. it('Should succeed with the corret parameters (advanced)', async function () {
  235. const fields: AbuseCreate = {
  236. video: {
  237. id: server.video.id,
  238. startAt: 1,
  239. endAt: 5
  240. },
  241. reason: 'my super reason',
  242. predefinedReasons: [ 'serverRules' ]
  243. }
  244. await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields, statusCodeExpected: HttpStatusCode.OK_200 })
  245. })
  246. })
  247. describe('When updating an abuse', function () {
  248. it('Should fail with a non authenticated user', async function () {
  249. await updateAbuse(server.url, 'blabla', abuseId, {}, HttpStatusCode.UNAUTHORIZED_401)
  250. })
  251. it('Should fail with a non admin user', async function () {
  252. await updateAbuse(server.url, userAccessToken, abuseId, {}, HttpStatusCode.FORBIDDEN_403)
  253. })
  254. it('Should fail with a bad abuse id', async function () {
  255. await updateAbuse(server.url, server.accessToken, 45, {}, HttpStatusCode.NOT_FOUND_404)
  256. })
  257. it('Should fail with a bad state', async function () {
  258. const body = { state: 5 }
  259. await updateAbuse(server.url, server.accessToken, abuseId, body, HttpStatusCode.BAD_REQUEST_400)
  260. })
  261. it('Should fail with a bad moderation comment', async function () {
  262. const body = { moderationComment: 'b'.repeat(3001) }
  263. await updateAbuse(server.url, server.accessToken, abuseId, body, HttpStatusCode.BAD_REQUEST_400)
  264. })
  265. it('Should succeed with the correct params', async function () {
  266. const body = { state: AbuseState.ACCEPTED }
  267. await updateAbuse(server.url, server.accessToken, abuseId, body)
  268. })
  269. })
  270. describe('When creating an abuse message', function () {
  271. const message = 'my super message'
  272. it('Should fail with an invalid abuse id', async function () {
  273. await addAbuseMessage(server.url, userAccessToken2, 888, message, HttpStatusCode.NOT_FOUND_404)
  274. })
  275. it('Should fail with a non authenticated user', async function () {
  276. await addAbuseMessage(server.url, 'fake_token', abuseId, message, HttpStatusCode.UNAUTHORIZED_401)
  277. })
  278. it('Should fail with an invalid logged in user', async function () {
  279. await addAbuseMessage(server.url, userAccessToken2, abuseId, message, HttpStatusCode.FORBIDDEN_403)
  280. })
  281. it('Should fail with an invalid message', async function () {
  282. await addAbuseMessage(server.url, userAccessToken, abuseId, 'a'.repeat(5000), HttpStatusCode.BAD_REQUEST_400)
  283. })
  284. it('Should suceed with the correct params', async function () {
  285. const res = await addAbuseMessage(server.url, userAccessToken, abuseId, message)
  286. messageId = res.body.abuseMessage.id
  287. })
  288. })
  289. describe('When listing abuse messages', function () {
  290. it('Should fail with an invalid abuse id', async function () {
  291. await listAbuseMessages(server.url, userAccessToken, 888, HttpStatusCode.NOT_FOUND_404)
  292. })
  293. it('Should fail with a non authenticated user', async function () {
  294. await listAbuseMessages(server.url, 'fake_token', abuseId, HttpStatusCode.UNAUTHORIZED_401)
  295. })
  296. it('Should fail with an invalid logged in user', async function () {
  297. await listAbuseMessages(server.url, userAccessToken2, abuseId, HttpStatusCode.FORBIDDEN_403)
  298. })
  299. it('Should succeed with the correct params', async function () {
  300. await listAbuseMessages(server.url, userAccessToken, abuseId)
  301. })
  302. })
  303. describe('When deleting an abuse message', function () {
  304. it('Should fail with an invalid abuse id', async function () {
  305. await deleteAbuseMessage(server.url, userAccessToken, 888, messageId, HttpStatusCode.NOT_FOUND_404)
  306. })
  307. it('Should fail with an invalid message id', async function () {
  308. await deleteAbuseMessage(server.url, userAccessToken, abuseId, 888, HttpStatusCode.NOT_FOUND_404)
  309. })
  310. it('Should fail with a non authenticated user', async function () {
  311. await deleteAbuseMessage(server.url, 'fake_token', abuseId, messageId, HttpStatusCode.UNAUTHORIZED_401)
  312. })
  313. it('Should fail with an invalid logged in user', async function () {
  314. await deleteAbuseMessage(server.url, userAccessToken2, abuseId, messageId, HttpStatusCode.FORBIDDEN_403)
  315. })
  316. it('Should succeed with the correct params', async function () {
  317. await deleteAbuseMessage(server.url, userAccessToken, abuseId, messageId)
  318. })
  319. })
  320. describe('When deleting a video abuse', function () {
  321. it('Should fail with a non authenticated user', async function () {
  322. await deleteAbuse(server.url, 'blabla', abuseId, HttpStatusCode.UNAUTHORIZED_401)
  323. })
  324. it('Should fail with a non admin user', async function () {
  325. await deleteAbuse(server.url, userAccessToken, abuseId, HttpStatusCode.FORBIDDEN_403)
  326. })
  327. it('Should fail with a bad abuse id', async function () {
  328. await deleteAbuse(server.url, server.accessToken, 45, HttpStatusCode.NOT_FOUND_404)
  329. })
  330. it('Should succeed with the correct params', async function () {
  331. await deleteAbuse(server.url, server.accessToken, abuseId)
  332. })
  333. })
  334. describe('When trying to manage messages of a remote abuse', function () {
  335. let remoteAbuseId: number
  336. let anotherServer: ServerInfo
  337. before(async function () {
  338. this.timeout(50000)
  339. anotherServer = await flushAndRunServer(2)
  340. await setAccessTokensToServers([ anotherServer ])
  341. await doubleFollow(anotherServer, server)
  342. const server2VideoId = await getVideoIdFromUUID(anotherServer.url, server.video.uuid)
  343. await reportAbuse({
  344. url: anotherServer.url,
  345. token: anotherServer.accessToken,
  346. reason: 'remote server',
  347. videoId: server2VideoId
  348. })
  349. await waitJobs([ server, anotherServer ])
  350. const res = await getAdminAbusesList({ url: server.url, token: server.accessToken, sort: '-createdAt' })
  351. remoteAbuseId = res.body.data[0].id
  352. })
  353. it('Should fail when listing abuse messages of a remote abuse', async function () {
  354. await listAbuseMessages(server.url, server.accessToken, remoteAbuseId, HttpStatusCode.BAD_REQUEST_400)
  355. })
  356. it('Should fail when creating abuse message of a remote abuse', async function () {
  357. await addAbuseMessage(server.url, server.accessToken, remoteAbuseId, 'message', HttpStatusCode.BAD_REQUEST_400)
  358. })
  359. after(async function () {
  360. await cleanupTests([ anotherServer ])
  361. })
  362. })
  363. after(async function () {
  364. await cleanupTests([ server ])
  365. })
  366. })