1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { AbuseCreate, AbuseState } from '@shared/models'
13 setAccessTokensToServers,
17 generateUserAccessToken,
21 } from '../../../../shared/extra-utils'
23 checkBadCountPagination,
24 checkBadSortPagination,
25 checkBadStartPagination
26 } from '../../../../shared/extra-utils/requests/check-api-params'
28 describe('Test abuses API validators', function () {
29 const basePath = '/api/v1/abuses/'
31 let server: ServerInfo
32 let userAccessToken = ''
33 let userAccessToken2 = ''
37 // ---------------------------------------------------------------
39 before(async function () {
42 server = await flushAndRunServer(1)
44 await setAccessTokensToServers([ server ])
46 const username = 'user1'
47 const password = 'my super password'
48 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
49 userAccessToken = await userLogin(server, { username, password })
52 userAccessToken2 = await generateUserAccessToken(server, 'user_2')
55 const res = await uploadVideo(server.url, server.accessToken, {})
56 server.video = res.body.video
59 describe('When listing abuses for admins', function () {
62 it('Should fail with a bad start pagination', async function () {
63 await checkBadStartPagination(server.url, path, server.accessToken)
66 it('Should fail with a bad count pagination', async function () {
67 await checkBadCountPagination(server.url, path, server.accessToken)
70 it('Should fail with an incorrect sort', async function () {
71 await checkBadSortPagination(server.url, path, server.accessToken)
74 it('Should fail with a non authenticated user', async function () {
75 await makeGetRequest({
78 statusCodeExpected: 401
82 it('Should fail with a non admin user', async function () {
83 await makeGetRequest({
86 token: userAccessToken,
87 statusCodeExpected: 403
91 it('Should fail with a bad id filter', async function () {
92 await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { id: 'toto' } })
95 it('Should fail with a bad filter', async function () {
96 await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { filter: 'toto' } })
97 await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { filter: 'videos' } })
100 it('Should fail with bad predefined reason', async function () {
101 await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { predefinedReason: 'violentOrRepulsives' } })
104 it('Should fail with a bad state filter', async function () {
105 await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { state: 'toto' } })
106 await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { state: 0 } })
109 it('Should fail with a bad videoIs filter', async function () {
110 await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { videoIs: 'toto' } })
113 it('Should succeed with the correct params', async function () {
116 predefinedReason: 'violentOrRepulsive',
122 await makeGetRequest({ url: server.url, path, token: server.accessToken, query, statusCodeExpected: 200 })
126 describe('When listing abuses for users', function () {
127 const path = '/api/v1/users/me/abuses'
129 it('Should fail with a bad start pagination', async function () {
130 await checkBadStartPagination(server.url, path, userAccessToken)
133 it('Should fail with a bad count pagination', async function () {
134 await checkBadCountPagination(server.url, path, userAccessToken)
137 it('Should fail with an incorrect sort', async function () {
138 await checkBadSortPagination(server.url, path, userAccessToken)
141 it('Should fail with a non authenticated user', async function () {
142 await makeGetRequest({
145 statusCodeExpected: 401
149 it('Should fail with a bad id filter', async function () {
150 await makeGetRequest({ url: server.url, path, token: userAccessToken, query: { id: 'toto' } })
153 it('Should fail with a bad state filter', async function () {
154 await makeGetRequest({ url: server.url, path, token: userAccessToken, query: { state: 'toto' } })
155 await makeGetRequest({ url: server.url, path, token: userAccessToken, query: { state: 0 } })
158 it('Should succeed with the correct params', async function () {
164 await makeGetRequest({ url: server.url, path, token: userAccessToken, query, statusCodeExpected: 200 })
168 describe('When reporting an abuse', function () {
169 const path = basePath
171 it('Should fail with nothing', async function () {
173 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
176 it('Should fail with a wrong video', async function () {
177 const fields = { video: { id: 'blabla' }, reason: 'my super reason' }
178 await makePostBodyRequest({ url: server.url, path: path, token: userAccessToken, fields })
181 it('Should fail with an unknown video', async function () {
182 const fields = { video: { id: 42 }, reason: 'my super reason' }
183 await makePostBodyRequest({ url: server.url, path: path, token: userAccessToken, fields, statusCodeExpected: 404 })
186 it('Should fail with a wrong comment', async function () {
187 const fields = { comment: { id: 'blabla' }, reason: 'my super reason' }
188 await makePostBodyRequest({ url: server.url, path: path, token: userAccessToken, fields })
191 it('Should fail with an unknown comment', async function () {
192 const fields = { comment: { id: 42 }, reason: 'my super reason' }
193 await makePostBodyRequest({ url: server.url, path: path, token: userAccessToken, fields, statusCodeExpected: 404 })
196 it('Should fail with a wrong account', async function () {
197 const fields = { account: { id: 'blabla' }, reason: 'my super reason' }
198 await makePostBodyRequest({ url: server.url, path: path, token: userAccessToken, fields })
201 it('Should fail with an unknown account', async function () {
202 const fields = { account: { id: 42 }, reason: 'my super reason' }
203 await makePostBodyRequest({ url: server.url, path: path, token: userAccessToken, fields, statusCodeExpected: 404 })
206 it('Should fail with not account, comment or video', async function () {
207 const fields = { reason: 'my super reason' }
208 await makePostBodyRequest({ url: server.url, path: path, token: userAccessToken, fields, statusCodeExpected: 400 })
211 it('Should fail with a non authenticated user', async function () {
212 const fields = { video: { id: server.video.id }, reason: 'my super reason' }
214 await makePostBodyRequest({ url: server.url, path, token: 'hello', fields, statusCodeExpected: 401 })
217 it('Should fail with a reason too short', async function () {
218 const fields = { video: { id: server.video.id }, reason: 'h' }
220 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
223 it('Should fail with a too big reason', async function () {
224 const fields = { video: { id: server.video.id }, reason: 'super'.repeat(605) }
226 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
229 it('Should succeed with the correct parameters (basic)', async function () {
230 const fields: AbuseCreate = { video: { id: server.video.id }, reason: 'my super reason' }
232 const res = await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields, statusCodeExpected: 200 })
233 abuseId = res.body.abuse.id
236 it('Should fail with a wrong predefined reason', async function () {
237 const fields = { video: { id: server.video.id }, reason: 'my super reason', predefinedReasons: [ 'wrongPredefinedReason' ] }
239 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
242 it('Should fail with negative timestamps', async function () {
243 const fields = { video: { id: server.video.id, startAt: -1 }, reason: 'my super reason' }
245 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
248 it('Should fail mith misordered startAt/endAt', async function () {
249 const fields = { video: { id: server.video.id, startAt: 5, endAt: 1 }, reason: 'my super reason' }
251 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
254 it('Should succeed with the corret parameters (advanced)', async function () {
255 const fields: AbuseCreate = {
261 reason: 'my super reason',
262 predefinedReasons: [ 'serverRules' ]
265 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields, statusCodeExpected: 200 })
269 describe('When updating an abuse', function () {
271 it('Should fail with a non authenticated user', async function () {
272 await updateAbuse(server.url, 'blabla', abuseId, {}, 401)
275 it('Should fail with a non admin user', async function () {
276 await updateAbuse(server.url, userAccessToken, abuseId, {}, 403)
279 it('Should fail with a bad abuse id', async function () {
280 await updateAbuse(server.url, server.accessToken, 45, {}, 404)
283 it('Should fail with a bad state', async function () {
284 const body = { state: 5 }
285 await updateAbuse(server.url, server.accessToken, abuseId, body, 400)
288 it('Should fail with a bad moderation comment', async function () {
289 const body = { moderationComment: 'b'.repeat(3001) }
290 await updateAbuse(server.url, server.accessToken, abuseId, body, 400)
293 it('Should succeed with the correct params', async function () {
294 const body = { state: AbuseState.ACCEPTED }
295 await updateAbuse(server.url, server.accessToken, abuseId, body)
299 describe('When creating an abuse message', function () {
300 const message = 'my super message'
302 it('Should fail with an invalid abuse id', async function () {
303 await addAbuseMessage(server.url, userAccessToken2, 888, message, 404)
306 it('Should fail with a non authenticated user', async function () {
307 await addAbuseMessage(server.url, 'fake_token', abuseId, message, 401)
310 it('Should fail with an invalid logged in user', async function () {
311 await addAbuseMessage(server.url, userAccessToken2, abuseId, message, 403)
314 it('Should fail with an invalid message', async function () {
315 await addAbuseMessage(server.url, userAccessToken, abuseId, 'a'.repeat(5000), 400)
318 it('Should suceed with the correct params', async function () {
319 const res = await addAbuseMessage(server.url, userAccessToken, abuseId, message)
320 messageId = res.body.abuseMessage.id
324 describe('When listing abuse message', function () {
326 it('Should fail with an invalid abuse id', async function () {
327 await listAbuseMessages(server.url, userAccessToken, 888, 404)
330 it('Should fail with a non authenticated user', async function () {
331 await listAbuseMessages(server.url, 'fake_token', abuseId, 401)
334 it('Should fail with an invalid logged in user', async function () {
335 await listAbuseMessages(server.url, userAccessToken2, abuseId, 403)
338 it('Should succeed with the correct params', async function () {
339 await listAbuseMessages(server.url, userAccessToken, abuseId)
343 describe('When deleting an abuse message', function () {
345 it('Should fail with an invalid abuse id', async function () {
346 await deleteAbuseMessage(server.url, userAccessToken, 888, messageId, 404)
349 it('Should fail with an invalid message id', async function () {
350 await deleteAbuseMessage(server.url, userAccessToken, abuseId, 888, 404)
353 it('Should fail with a non authenticated user', async function () {
354 await deleteAbuseMessage(server.url, 'fake_token', abuseId, messageId, 401)
357 it('Should fail with an invalid logged in user', async function () {
358 await deleteAbuseMessage(server.url, userAccessToken2, abuseId, messageId, 403)
361 it('Should succeed with the correct params', async function () {
362 await deleteAbuseMessage(server.url, userAccessToken, abuseId, messageId)
366 describe('When deleting a video abuse', function () {
368 it('Should fail with a non authenticated user', async function () {
369 await deleteAbuse(server.url, 'blabla', abuseId, 401)
372 it('Should fail with a non admin user', async function () {
373 await deleteAbuse(server.url, userAccessToken, abuseId, 403)
376 it('Should fail with a bad abuse id', async function () {
377 await deleteAbuse(server.url, server.accessToken, 45, 404)
380 it('Should succeed with the correct params', async function () {
381 await deleteAbuse(server.url, server.accessToken, abuseId)
385 after(async function () {
386 await cleanupTests([ server ])