1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
4 import { AbuseCreate, AbuseState, HttpStatusCode } from '@shared/models'
13 setAccessTokensToServers,
15 } from '@shared/server-commands'
17 describe('Test abuses API validators', function () {
18 const basePath = '/api/v1/abuses/'
20 let server: PeerTubeServer
27 let command: AbusesCommand
29 // ---------------------------------------------------------------
31 before(async function () {
34 server = await createSingleServer(1)
36 await setAccessTokensToServers([ server ])
38 userToken = await server.users.generateUserAndToken('user_1')
39 userToken2 = await server.users.generateUserAndToken('user_2')
41 server.store.videoCreated = await server.videos.upload()
43 command = server.abuses
46 describe('When listing abuses for admins', function () {
49 it('Should fail with a bad start pagination', async function () {
50 await checkBadStartPagination(server.url, path, server.accessToken)
53 it('Should fail with a bad count pagination', async function () {
54 await checkBadCountPagination(server.url, path, server.accessToken)
57 it('Should fail with an incorrect sort', async function () {
58 await checkBadSortPagination(server.url, path, server.accessToken)
61 it('Should fail with a non authenticated user', async function () {
62 await makeGetRequest({
65 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
69 it('Should fail with a non admin user', async function () {
70 await makeGetRequest({
74 expectedStatus: HttpStatusCode.FORBIDDEN_403
78 it('Should fail with a bad id filter', async function () {
79 await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { id: 'toto' } })
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' } })
87 it('Should fail with bad predefined reason', async function () {
88 await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { predefinedReason: 'violentOrRepulsives' } })
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 } })
96 it('Should fail with a bad videoIs filter', async function () {
97 await makeGetRequest({ url: server.url, path, token: server.accessToken, query: { videoIs: 'toto' } })
100 it('Should succeed with the correct params', async function () {
103 predefinedReason: 'violentOrRepulsive',
109 await makeGetRequest({ url: server.url, path, token: server.accessToken, query, expectedStatus: HttpStatusCode.OK_200 })
113 describe('When listing abuses for users', function () {
114 const path = '/api/v1/users/me/abuses'
116 it('Should fail with a bad start pagination', async function () {
117 await checkBadStartPagination(server.url, path, userToken)
120 it('Should fail with a bad count pagination', async function () {
121 await checkBadCountPagination(server.url, path, userToken)
124 it('Should fail with an incorrect sort', async function () {
125 await checkBadSortPagination(server.url, path, userToken)
128 it('Should fail with a non authenticated user', async function () {
129 await makeGetRequest({
132 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
136 it('Should fail with a bad id filter', async function () {
137 await makeGetRequest({ url: server.url, path, token: userToken, query: { id: 'toto' } })
140 it('Should fail with a bad state filter', async function () {
141 await makeGetRequest({ url: server.url, path, token: userToken, query: { state: 'toto' } })
142 await makeGetRequest({ url: server.url, path, token: userToken, query: { state: 0 } })
145 it('Should succeed with the correct params', async function () {
151 await makeGetRequest({ url: server.url, path, token: userToken, query, expectedStatus: HttpStatusCode.OK_200 })
155 describe('When reporting an abuse', function () {
156 const path = basePath
158 it('Should fail with nothing', async function () {
160 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
163 it('Should fail with a wrong video', async function () {
164 const fields = { video: { id: 'blabla' }, reason: 'my super reason' }
165 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
168 it('Should fail with an unknown video', async function () {
169 const fields = { video: { id: 42 }, reason: 'my super reason' }
170 await makePostBodyRequest({
175 expectedStatus: HttpStatusCode.NOT_FOUND_404
179 it('Should fail with a wrong comment', async function () {
180 const fields = { comment: { id: 'blabla' }, reason: 'my super reason' }
181 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
184 it('Should fail with an unknown comment', async function () {
185 const fields = { comment: { id: 42 }, reason: 'my super reason' }
186 await makePostBodyRequest({
191 expectedStatus: HttpStatusCode.NOT_FOUND_404
195 it('Should fail with a wrong account', async function () {
196 const fields = { account: { id: 'blabla' }, reason: 'my super reason' }
197 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
200 it('Should fail with an unknown account', async function () {
201 const fields = { account: { id: 42 }, reason: 'my super reason' }
202 await makePostBodyRequest({
207 expectedStatus: HttpStatusCode.NOT_FOUND_404
211 it('Should fail with not account, comment or video', async function () {
212 const fields = { reason: 'my super reason' }
213 await makePostBodyRequest({
218 expectedStatus: HttpStatusCode.BAD_REQUEST_400
222 it('Should fail with a non authenticated user', async function () {
223 const fields = { video: { id: server.store.videoCreated.id }, reason: 'my super reason' }
225 await makePostBodyRequest({ url: server.url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
228 it('Should fail with a reason too short', async function () {
229 const fields = { video: { id: server.store.videoCreated.id }, reason: 'h' }
231 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
234 it('Should fail with a too big reason', async function () {
235 const fields = { video: { id: server.store.videoCreated.id }, reason: 'super'.repeat(605) }
237 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
240 it('Should succeed with the correct parameters (basic)', async function () {
241 const fields: AbuseCreate = { video: { id: server.store.videoCreated.shortUUID }, reason: 'my super reason' }
243 const res = await makePostBodyRequest({
248 expectedStatus: HttpStatusCode.OK_200
250 abuseId = res.body.abuse.id
253 it('Should fail with a wrong predefined reason', async function () {
254 const fields = { video: server.store.videoCreated, reason: 'my super reason', predefinedReasons: [ 'wrongPredefinedReason' ] }
256 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
259 it('Should fail with negative timestamps', async function () {
260 const fields = { video: { id: server.store.videoCreated.id, startAt: -1 }, reason: 'my super reason' }
262 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
265 it('Should fail mith misordered startAt/endAt', async function () {
266 const fields = { video: { id: server.store.videoCreated.id, startAt: 5, endAt: 1 }, reason: 'my super reason' }
268 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
271 it('Should succeed with the correct parameters (advanced)', async function () {
272 const fields: AbuseCreate = {
274 id: server.store.videoCreated.id,
278 reason: 'my super reason',
279 predefinedReasons: [ 'serverRules' ]
282 await makePostBodyRequest({ url: server.url, path, token: userToken, fields, expectedStatus: HttpStatusCode.OK_200 })
286 describe('When updating an abuse', function () {
288 it('Should fail with a non authenticated user', async function () {
289 await command.update({ token: 'blabla', abuseId, body: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
292 it('Should fail with a non admin user', async function () {
293 await command.update({ token: userToken, abuseId, body: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
296 it('Should fail with a bad abuse id', async function () {
297 await command.update({ abuseId: 45, body: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
300 it('Should fail with a bad state', async function () {
301 const body = { state: 5 }
302 await command.update({ abuseId, body, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
305 it('Should fail with a bad moderation comment', async function () {
306 const body = { moderationComment: 'b'.repeat(3001) }
307 await command.update({ abuseId, body, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
310 it('Should succeed with the correct params', async function () {
311 const body = { state: AbuseState.ACCEPTED }
312 await command.update({ abuseId, body })
316 describe('When creating an abuse message', function () {
317 const message = 'my super message'
319 it('Should fail with an invalid abuse id', async function () {
320 await command.addMessage({ token: userToken2, abuseId: 888, message, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
323 it('Should fail with a non authenticated user', async function () {
324 await command.addMessage({ token: 'fake_token', abuseId, message, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
327 it('Should fail with an invalid logged in user', async function () {
328 await command.addMessage({ token: userToken2, abuseId, message, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
331 it('Should fail with an invalid message', async function () {
332 await command.addMessage({ token: userToken, abuseId, message: 'a'.repeat(5000), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
335 it('Should succeed with the correct params', async function () {
336 const res = await command.addMessage({ token: userToken, abuseId, message })
337 messageId = res.body.abuseMessage.id
341 describe('When listing abuse messages', function () {
343 it('Should fail with an invalid abuse id', async function () {
344 await command.listMessages({ token: userToken, abuseId: 888, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
347 it('Should fail with a non authenticated user', async function () {
348 await command.listMessages({ token: 'fake_token', abuseId, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
351 it('Should fail with an invalid logged in user', async function () {
352 await command.listMessages({ token: userToken2, abuseId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
355 it('Should succeed with the correct params', async function () {
356 await command.listMessages({ token: userToken, abuseId })
360 describe('When deleting an abuse message', function () {
361 it('Should fail with an invalid abuse id', async function () {
362 await command.deleteMessage({ token: userToken, abuseId: 888, messageId, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
365 it('Should fail with an invalid message id', async function () {
366 await command.deleteMessage({ token: userToken, abuseId, messageId: 888, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
369 it('Should fail with a non authenticated user', async function () {
370 await command.deleteMessage({ token: 'fake_token', abuseId, messageId, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
373 it('Should fail with an invalid logged in user', async function () {
374 await command.deleteMessage({ token: userToken2, abuseId, messageId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
377 it('Should succeed with the correct params', async function () {
378 await command.deleteMessage({ token: userToken, abuseId, messageId })
382 describe('When deleting a video abuse', function () {
384 it('Should fail with a non authenticated user', async function () {
385 await command.delete({ token: 'blabla', abuseId, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
388 it('Should fail with a non admin user', async function () {
389 await command.delete({ token: userToken, abuseId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
392 it('Should fail with a bad abuse id', async function () {
393 await command.delete({ abuseId: 45, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
396 it('Should succeed with the correct params', async function () {
397 await command.delete({ abuseId })
401 describe('When trying to manage messages of a remote abuse', function () {
402 let remoteAbuseId: number
403 let anotherServer: PeerTubeServer
405 before(async function () {
408 anotherServer = await createSingleServer(2)
409 await setAccessTokensToServers([ anotherServer ])
411 await doubleFollow(anotherServer, server)
413 const server2VideoId = await anotherServer.videos.getId({ uuid: server.store.videoCreated.uuid })
414 await anotherServer.abuses.report({ reason: 'remote server', videoId: server2VideoId })
416 await waitJobs([ server, anotherServer ])
418 const body = await command.getAdminList({ sort: '-createdAt' })
419 remoteAbuseId = body.data[0].id
422 it('Should fail when listing abuse messages of a remote abuse', async function () {
423 await command.listMessages({ abuseId: remoteAbuseId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
426 it('Should fail when creating abuse message of a remote abuse', async function () {
427 await command.addMessage({ abuseId: remoteAbuseId, message: 'message', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
430 after(async function () {
431 await cleanupTests([ anotherServer ])
435 after(async function () {
436 await cleanupTests([ server ])