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