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