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