]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/moderation/abuses.ts
Introduce server commands
[github/Chocobozzz/PeerTube.git] / server / tests / api / moderation / abuses.ts
CommitLineData
4f32032f
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import * as chai from 'chai'
4f32032f 5import {
0c1a77e9 6 AbusesCommand,
4f32032f
C
7 cleanupTests,
8 createUser,
0c1a77e9 9 doubleFollow,
4f32032f 10 flushAndRunMultipleServers,
edbc9325 11 generateUserAccessToken,
310b5219 12 getVideoIdFromUUID,
4f32032f 13 getVideosList,
edbc9325 14 removeUser,
4f32032f 15 removeVideo,
4f32032f
C
16 ServerInfo,
17 setAccessTokensToServers,
4f32032f 18 uploadVideo,
310b5219 19 uploadVideoAndGetId,
0c1a77e9
C
20 userLogin,
21 waitJobs
22} from '@shared/extra-utils'
12edc149 23import { AbuseMessage, AbusePredefinedReasonsString, AbuseState, AdminAbuse, UserAbuse } from '@shared/models'
4f32032f
C
24
25const expect = chai.expect
26
27describe('Test abuses', function () {
28 let servers: ServerInfo[] = []
edbc9325
C
29 let abuseServer1: AdminAbuse
30 let abuseServer2: AdminAbuse
0c1a77e9 31 let commands: AbusesCommand[]
4f32032f
C
32
33 before(async function () {
34 this.timeout(50000)
35
36 // Run servers
37 servers = await flushAndRunMultipleServers(2)
38
39 // Get the access tokens
40 await setAccessTokensToServers(servers)
41
42 // Server 1 and server 2 follow each other
43 await doubleFollow(servers[0], servers[1])
0c1a77e9
C
44
45 commands = servers.map(s => s.abusesCommand)
310b5219 46 })
4f32032f 47
310b5219 48 describe('Video abuses', function () {
4f32032f 49
310b5219
C
50 before(async function () {
51 this.timeout(50000)
4f32032f 52
310b5219
C
53 // Upload some videos on each servers
54 const video1Attributes = {
55 name: 'my super name for server 1',
56 description: 'my super description for server 1'
57 }
58 await uploadVideo(servers[0].url, servers[0].accessToken, video1Attributes)
4f32032f 59
310b5219
C
60 const video2Attributes = {
61 name: 'my super name for server 2',
62 description: 'my super description for server 2'
63 }
64 await uploadVideo(servers[1].url, servers[1].accessToken, video2Attributes)
4f32032f 65
310b5219
C
66 // Wait videos propagation, server 2 has transcoding enabled
67 await waitJobs(servers)
4f32032f 68
310b5219
C
69 const res = await getVideosList(servers[0].url)
70 const videos = res.body.data
4f32032f 71
310b5219 72 expect(videos.length).to.equal(2)
4f32032f 73
310b5219
C
74 servers[0].video = videos.find(video => video.name === 'my super name for server 1')
75 servers[1].video = videos.find(video => video.name === 'my super name for server 2')
76 })
4f32032f 77
310b5219 78 it('Should not have abuses', async function () {
0c1a77e9 79 const body = await commands[0].getAdminList()
4f32032f 80
0c1a77e9
C
81 expect(body.total).to.equal(0)
82 expect(body.data).to.be.an('array')
83 expect(body.data.length).to.equal(0)
310b5219 84 })
4f32032f 85
310b5219
C
86 it('Should report abuse on a local video', async function () {
87 this.timeout(15000)
4f32032f 88
310b5219 89 const reason = 'my super bad reason'
0c1a77e9 90 await commands[0].report({ videoId: servers[0].video.id, reason })
4f32032f 91
310b5219
C
92 // We wait requests propagation, even if the server 1 is not supposed to make a request to server 2
93 await waitJobs(servers)
94 })
4f32032f 95
310b5219 96 it('Should have 1 video abuses on server 1 and 0 on server 2', async function () {
0c1a77e9
C
97 {
98 const body = await commands[0].getAdminList()
4f32032f 99
0c1a77e9
C
100 expect(body.total).to.equal(1)
101 expect(body.data).to.be.an('array')
102 expect(body.data.length).to.equal(1)
4f32032f 103
0c1a77e9
C
104 const abuse = body.data[0]
105 expect(abuse.reason).to.equal('my super bad reason')
4f32032f 106
0c1a77e9
C
107 expect(abuse.reporterAccount.name).to.equal('root')
108 expect(abuse.reporterAccount.host).to.equal(servers[0].host)
4f32032f 109
0c1a77e9
C
110 expect(abuse.video.id).to.equal(servers[0].video.id)
111 expect(abuse.video.channel).to.exist
4f32032f 112
0c1a77e9 113 expect(abuse.comment).to.be.null
4f32032f 114
0c1a77e9
C
115 expect(abuse.flaggedAccount.name).to.equal('root')
116 expect(abuse.flaggedAccount.host).to.equal(servers[0].host)
310b5219 117
0c1a77e9
C
118 expect(abuse.video.countReports).to.equal(1)
119 expect(abuse.video.nthReport).to.equal(1)
310b5219 120
0c1a77e9
C
121 expect(abuse.countReportsForReporter).to.equal(1)
122 expect(abuse.countReportsForReportee).to.equal(1)
123 }
4f32032f 124
0c1a77e9
C
125 {
126 const body = await commands[1].getAdminList()
127 expect(body.total).to.equal(0)
128 expect(body.data).to.be.an('array')
129 expect(body.data.length).to.equal(0)
130 }
310b5219 131 })
4f32032f 132
310b5219
C
133 it('Should report abuse on a remote video', async function () {
134 this.timeout(10000)
135
136 const reason = 'my super bad reason 2'
edbc9325 137 const videoId = await getVideoIdFromUUID(servers[0].url, servers[1].video.uuid)
0c1a77e9 138 await commands[0].report({ videoId, reason })
310b5219
C
139
140 // We wait requests propagation
4f32032f 141 await waitJobs(servers)
310b5219 142 })
4f32032f 143
310b5219 144 it('Should have 2 video abuses on server 1 and 1 on server 2', async function () {
0c1a77e9
C
145 {
146 const body = await commands[0].getAdminList()
4f32032f 147
0c1a77e9
C
148 expect(body.total).to.equal(2)
149 expect(body.data.length).to.equal(2)
4f32032f 150
0c1a77e9
C
151 const abuse1 = body.data[0]
152 expect(abuse1.reason).to.equal('my super bad reason')
153 expect(abuse1.reporterAccount.name).to.equal('root')
154 expect(abuse1.reporterAccount.host).to.equal(servers[0].host)
4f32032f 155
0c1a77e9
C
156 expect(abuse1.video.id).to.equal(servers[0].video.id)
157 expect(abuse1.video.countReports).to.equal(1)
158 expect(abuse1.video.nthReport).to.equal(1)
4f32032f 159
0c1a77e9 160 expect(abuse1.comment).to.be.null
4f32032f 161
0c1a77e9
C
162 expect(abuse1.flaggedAccount.name).to.equal('root')
163 expect(abuse1.flaggedAccount.host).to.equal(servers[0].host)
4f32032f 164
0c1a77e9
C
165 expect(abuse1.state.id).to.equal(AbuseState.PENDING)
166 expect(abuse1.state.label).to.equal('Pending')
167 expect(abuse1.moderationComment).to.be.null
4f32032f 168
0c1a77e9
C
169 const abuse2 = body.data[1]
170 expect(abuse2.reason).to.equal('my super bad reason 2')
4f32032f 171
0c1a77e9
C
172 expect(abuse2.reporterAccount.name).to.equal('root')
173 expect(abuse2.reporterAccount.host).to.equal(servers[0].host)
4f32032f 174
0c1a77e9 175 expect(abuse2.video.id).to.equal(servers[1].video.id)
4f32032f 176
0c1a77e9 177 expect(abuse2.comment).to.be.null
4f32032f 178
0c1a77e9
C
179 expect(abuse2.flaggedAccount.name).to.equal('root')
180 expect(abuse2.flaggedAccount.host).to.equal(servers[1].host)
4f32032f 181
0c1a77e9
C
182 expect(abuse2.state.id).to.equal(AbuseState.PENDING)
183 expect(abuse2.state.label).to.equal('Pending')
184 expect(abuse2.moderationComment).to.be.null
185 }
4f32032f 186
0c1a77e9
C
187 {
188 const body = await commands[1].getAdminList()
189 expect(body.total).to.equal(1)
190 expect(body.data.length).to.equal(1)
4f32032f 191
0c1a77e9
C
192 abuseServer2 = body.data[0]
193 expect(abuseServer2.reason).to.equal('my super bad reason 2')
194 expect(abuseServer2.reporterAccount.name).to.equal('root')
195 expect(abuseServer2.reporterAccount.host).to.equal(servers[0].host)
4f32032f 196
0c1a77e9
C
197 expect(abuseServer2.flaggedAccount.name).to.equal('root')
198 expect(abuseServer2.flaggedAccount.host).to.equal(servers[1].host)
4f32032f 199
0c1a77e9
C
200 expect(abuseServer2.state.id).to.equal(AbuseState.PENDING)
201 expect(abuseServer2.state.label).to.equal('Pending')
202 expect(abuseServer2.moderationComment).to.be.null
203 }
310b5219 204 })
4f32032f 205
310b5219
C
206 it('Should hide video abuses from blocked accounts', async function () {
207 this.timeout(10000)
4f32032f 208
310b5219
C
209 {
210 const videoId = await getVideoIdFromUUID(servers[1].url, servers[0].video.uuid)
0c1a77e9 211 await commands[1].report({ videoId, reason: 'will mute this' })
310b5219 212 await waitJobs(servers)
4f32032f 213
0c1a77e9
C
214 const body = await commands[0].getAdminList()
215 expect(body.total).to.equal(3)
310b5219 216 }
4f32032f 217
310b5219
C
218 const accountToBlock = 'root@' + servers[1].host
219
220 {
5f8bd4cb 221 await servers[0].blocklistCommand.addToServerBlocklist({ account: accountToBlock })
310b5219 222
0c1a77e9
C
223 const body = await commands[0].getAdminList()
224 expect(body.total).to.equal(2)
310b5219 225
0c1a77e9 226 const abuse = body.data.find(a => a.reason === 'will mute this')
310b5219
C
227 expect(abuse).to.be.undefined
228 }
229
230 {
5f8bd4cb 231 await servers[0].blocklistCommand.removeFromServerBlocklist({ account: accountToBlock })
310b5219 232
0c1a77e9
C
233 const body = await commands[0].getAdminList()
234 expect(body.total).to.equal(3)
310b5219
C
235 }
236 })
237
238 it('Should hide video abuses from blocked servers', async function () {
239 const serverToBlock = servers[1].host
240
241 {
5f8bd4cb 242 await servers[0].blocklistCommand.addToServerBlocklist({ server: serverToBlock })
310b5219 243
0c1a77e9
C
244 const body = await commands[0].getAdminList()
245 expect(body.total).to.equal(2)
310b5219 246
0c1a77e9 247 const abuse = body.data.find(a => a.reason === 'will mute this')
310b5219
C
248 expect(abuse).to.be.undefined
249 }
250
251 {
5f8bd4cb 252 await servers[0].blocklistCommand.removeFromServerBlocklist({ server: serverToBlock })
310b5219 253
0c1a77e9
C
254 const body = await commands[0].getAdminList()
255 expect(body.total).to.equal(3)
310b5219
C
256 }
257 })
258
259 it('Should keep the video abuse when deleting the video', async function () {
260 this.timeout(10000)
261
262 await removeVideo(servers[1].url, servers[1].accessToken, abuseServer2.video.uuid)
263
264 await waitJobs(servers)
265
0c1a77e9
C
266 const body = await commands[1].getAdminList()
267 expect(body.total).to.equal(2, "wrong number of videos returned")
268 expect(body.data).to.have.lengthOf(2, "wrong number of videos returned")
310b5219 269
0c1a77e9 270 const abuse = body.data[0]
310b5219
C
271 expect(abuse.id).to.equal(abuseServer2.id, "wrong origin server id for first video")
272 expect(abuse.video.id).to.equal(abuseServer2.video.id, "wrong video id")
273 expect(abuse.video.channel).to.exist
274 expect(abuse.video.deleted).to.be.true
275 })
276
277 it('Should include counts of reports from reporter and reportee', async function () {
278 this.timeout(10000)
279
280 // register a second user to have two reporters/reportees
281 const user = { username: 'user2', password: 'password' }
282 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, ...user })
283 const userAccessToken = await userLogin(servers[0], user)
284
285 // upload a third video via this user
286 const video3Attributes = {
287 name: 'my second super name for server 1',
288 description: 'my second super description for server 1'
289 }
0c1a77e9
C
290 const resUpload = await uploadVideo(servers[0].url, userAccessToken, video3Attributes)
291 const video3Id = resUpload.body.video.id
310b5219
C
292
293 // resume with the test
294 const reason3 = 'my super bad reason 3'
0c1a77e9 295 await commands[0].report({ videoId: video3Id, reason: reason3 })
310b5219
C
296
297 const reason4 = 'my super bad reason 4'
0c1a77e9 298 await commands[0].report({ token: userAccessToken, videoId: servers[0].video.id, reason: reason4 })
310b5219
C
299
300 {
0c1a77e9
C
301 const body = await commands[0].getAdminList()
302 const abuses = body.data
310b5219 303
0c1a77e9 304 const abuseVideo3 = body.data.find(a => a.video.id === video3Id)
310b5219
C
305 expect(abuseVideo3).to.not.be.undefined
306 expect(abuseVideo3.video.countReports).to.equal(1, "wrong reports count for video 3")
307 expect(abuseVideo3.video.nthReport).to.equal(1, "wrong report position in report list for video 3")
308 expect(abuseVideo3.countReportsForReportee).to.equal(1, "wrong reports count for reporter on video 3 abuse")
309 expect(abuseVideo3.countReportsForReporter).to.equal(3, "wrong reports count for reportee on video 3 abuse")
310
311 const abuseServer1 = abuses.find(a => a.video.id === servers[0].video.id)
312 expect(abuseServer1.countReportsForReportee).to.equal(3, "wrong reports count for reporter on video 1 abuse")
313 }
314 })
315
316 it('Should list predefined reasons as well as timestamps for the reported video', async function () {
317 this.timeout(10000)
318
319 const reason5 = 'my super bad reason 5'
320 const predefinedReasons5: AbusePredefinedReasonsString[] = [ 'violentOrRepulsive', 'captions' ]
0c1a77e9 321 const createRes = await commands[0].report({
310b5219
C
322 videoId: servers[0].video.id,
323 reason: reason5,
324 predefinedReasons: predefinedReasons5,
325 startAt: 1,
326 endAt: 5
0c1a77e9 327 })
310b5219 328
0c1a77e9 329 const body = await commands[0].getAdminList()
310b5219
C
330
331 {
0c1a77e9 332 const abuse = body.data.find(a => a.id === createRes.abuse.id)
310b5219
C
333 expect(abuse.reason).to.equals(reason5)
334 expect(abuse.predefinedReasons).to.deep.equals(predefinedReasons5, "predefined reasons do not match the one reported")
335 expect(abuse.video.startAt).to.equal(1, "starting timestamp doesn't match the one reported")
336 expect(abuse.video.endAt).to.equal(5, "ending timestamp doesn't match the one reported")
337 }
338 })
339
340 it('Should delete the video abuse', async function () {
341 this.timeout(10000)
342
0c1a77e9 343 await commands[1].delete({ abuseId: abuseServer2.id })
310b5219
C
344
345 await waitJobs(servers)
346
347 {
0c1a77e9
C
348 const body = await commands[1].getAdminList()
349 expect(body.total).to.equal(1)
350 expect(body.data.length).to.equal(1)
351 expect(body.data[0].id).to.not.equal(abuseServer2.id)
310b5219
C
352 }
353
354 {
0c1a77e9
C
355 const body = await commands[0].getAdminList()
356 expect(body.total).to.equal(6)
310b5219
C
357 }
358 })
359
360 it('Should list and filter video abuses', async function () {
361 this.timeout(10000)
362
0c1a77e9
C
363 async function list (query: Parameters<AbusesCommand['getAdminList']>[0]) {
364 const body = await commands[0].getAdminList(query)
310b5219 365
0c1a77e9 366 return body.data
4f32032f 367 }
4f32032f 368
310b5219
C
369 expect(await list({ id: 56 })).to.have.lengthOf(0)
370 expect(await list({ id: 1 })).to.have.lengthOf(1)
371
372 expect(await list({ search: 'my super name for server 1' })).to.have.lengthOf(4)
373 expect(await list({ search: 'aaaaaaaaaaaaaaaaaaaaaaaaaa' })).to.have.lengthOf(0)
374
375 expect(await list({ searchVideo: 'my second super name for server 1' })).to.have.lengthOf(1)
376
377 expect(await list({ searchVideoChannel: 'root' })).to.have.lengthOf(4)
378 expect(await list({ searchVideoChannel: 'aaaa' })).to.have.lengthOf(0)
379
380 expect(await list({ searchReporter: 'user2' })).to.have.lengthOf(1)
381 expect(await list({ searchReporter: 'root' })).to.have.lengthOf(5)
382
383 expect(await list({ searchReportee: 'root' })).to.have.lengthOf(5)
384 expect(await list({ searchReportee: 'aaaa' })).to.have.lengthOf(0)
385
386 expect(await list({ videoIs: 'deleted' })).to.have.lengthOf(1)
387 expect(await list({ videoIs: 'blacklisted' })).to.have.lengthOf(0)
388
389 expect(await list({ state: AbuseState.ACCEPTED })).to.have.lengthOf(0)
390 expect(await list({ state: AbuseState.PENDING })).to.have.lengthOf(6)
391
392 expect(await list({ predefinedReason: 'violentOrRepulsive' })).to.have.lengthOf(1)
393 expect(await list({ predefinedReason: 'serverRules' })).to.have.lengthOf(0)
394 })
4f32032f
C
395 })
396
310b5219 397 describe('Comment abuses', function () {
4f32032f 398
12edc149 399 async function getComment (server: ServerInfo, videoIdArg: number | string) {
310b5219 400 const videoId = typeof videoIdArg === 'string'
12edc149 401 ? await getVideoIdFromUUID(server.url, videoIdArg)
310b5219 402 : videoIdArg
4f32032f 403
12edc149 404 const { data } = await server.commentsCommand.listThreads({ videoId })
4f32032f 405
12edc149 406 return data[0]
4f32032f
C
407 }
408
310b5219
C
409 before(async function () {
410 this.timeout(50000)
4f32032f 411
310b5219
C
412 servers[0].video = await uploadVideoAndGetId({ server: servers[0], videoName: 'server 1' })
413 servers[1].video = await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' })
414
12edc149
C
415 await servers[0].commentsCommand.createThread({ videoId: servers[0].video.id, text: 'comment server 1' })
416 await servers[1].commentsCommand.createThread({ videoId: servers[1].video.id, text: 'comment server 2' })
310b5219
C
417
418 await waitJobs(servers)
419 })
420
421 it('Should report abuse on a comment', async function () {
422 this.timeout(15000)
423
12edc149 424 const comment = await getComment(servers[0], servers[0].video.id)
310b5219
C
425
426 const reason = 'it is a bad comment'
0c1a77e9 427 await commands[0].report({ commentId: comment.id, reason })
310b5219
C
428
429 await waitJobs(servers)
430 })
431
432 it('Should have 1 comment abuse on server 1 and 0 on server 2', async function () {
433 {
12edc149 434 const comment = await getComment(servers[0], servers[0].video.id)
0c1a77e9 435 const body = await commands[0].getAdminList({ filter: 'comment' })
310b5219 436
0c1a77e9
C
437 expect(body.total).to.equal(1)
438 expect(body.data).to.have.lengthOf(1)
310b5219 439
0c1a77e9 440 const abuse = body.data[0]
310b5219
C
441 expect(abuse.reason).to.equal('it is a bad comment')
442
443 expect(abuse.reporterAccount.name).to.equal('root')
444 expect(abuse.reporterAccount.host).to.equal(servers[0].host)
445
446 expect(abuse.video).to.be.null
447
448 expect(abuse.comment.deleted).to.be.false
449 expect(abuse.comment.id).to.equal(comment.id)
450 expect(abuse.comment.text).to.equal(comment.text)
451 expect(abuse.comment.video.name).to.equal('server 1')
452 expect(abuse.comment.video.id).to.equal(servers[0].video.id)
453 expect(abuse.comment.video.uuid).to.equal(servers[0].video.uuid)
454
455 expect(abuse.countReportsForReporter).to.equal(5)
456 expect(abuse.countReportsForReportee).to.equal(5)
457 }
458
459 {
0c1a77e9
C
460 const body = await commands[1].getAdminList({ filter: 'comment' })
461 expect(body.total).to.equal(0)
462 expect(body.data.length).to.equal(0)
310b5219
C
463 }
464 })
465
466 it('Should report abuse on a remote comment', async function () {
467 this.timeout(10000)
468
12edc149 469 const comment = await getComment(servers[0], servers[1].video.uuid)
310b5219
C
470
471 const reason = 'it is a really bad comment'
0c1a77e9 472 await commands[0].report({ commentId: comment.id, reason })
310b5219
C
473
474 await waitJobs(servers)
475 })
476
477 it('Should have 2 comment abuses on server 1 and 1 on server 2', async function () {
12edc149 478 const commentServer2 = await getComment(servers[0], servers[1].video.id)
310b5219 479
0c1a77e9
C
480 {
481 const body = await commands[0].getAdminList({ filter: 'comment' })
482 expect(body.total).to.equal(2)
483 expect(body.data.length).to.equal(2)
310b5219 484
0c1a77e9
C
485 const abuse = body.data[0]
486 expect(abuse.reason).to.equal('it is a bad comment')
487 expect(abuse.countReportsForReporter).to.equal(6)
488 expect(abuse.countReportsForReportee).to.equal(5)
310b5219 489
0c1a77e9 490 const abuse2 = body.data[1]
310b5219 491
0c1a77e9 492 expect(abuse2.reason).to.equal('it is a really bad comment')
310b5219 493
0c1a77e9
C
494 expect(abuse2.reporterAccount.name).to.equal('root')
495 expect(abuse2.reporterAccount.host).to.equal(servers[0].host)
310b5219 496
0c1a77e9 497 expect(abuse2.video).to.be.null
310b5219 498
0c1a77e9
C
499 expect(abuse2.comment.deleted).to.be.false
500 expect(abuse2.comment.id).to.equal(commentServer2.id)
501 expect(abuse2.comment.text).to.equal(commentServer2.text)
502 expect(abuse2.comment.video.name).to.equal('server 2')
503 expect(abuse2.comment.video.uuid).to.equal(servers[1].video.uuid)
310b5219 504
0c1a77e9
C
505 expect(abuse2.state.id).to.equal(AbuseState.PENDING)
506 expect(abuse2.state.label).to.equal('Pending')
310b5219 507
0c1a77e9 508 expect(abuse2.moderationComment).to.be.null
310b5219 509
0c1a77e9
C
510 expect(abuse2.countReportsForReporter).to.equal(6)
511 expect(abuse2.countReportsForReportee).to.equal(2)
512 }
310b5219 513
0c1a77e9
C
514 {
515 const body = await commands[1].getAdminList({ filter: 'comment' })
516 expect(body.total).to.equal(1)
517 expect(body.data.length).to.equal(1)
310b5219 518
0c1a77e9
C
519 abuseServer2 = body.data[0]
520 expect(abuseServer2.reason).to.equal('it is a really bad comment')
521 expect(abuseServer2.reporterAccount.name).to.equal('root')
522 expect(abuseServer2.reporterAccount.host).to.equal(servers[0].host)
310b5219 523
0c1a77e9
C
524 expect(abuseServer2.state.id).to.equal(AbuseState.PENDING)
525 expect(abuseServer2.state.label).to.equal('Pending')
310b5219 526
0c1a77e9 527 expect(abuseServer2.moderationComment).to.be.null
310b5219 528
0c1a77e9
C
529 expect(abuseServer2.countReportsForReporter).to.equal(1)
530 expect(abuseServer2.countReportsForReportee).to.equal(1)
531 }
310b5219
C
532 })
533
534 it('Should keep the comment abuse when deleting the comment', async function () {
535 this.timeout(10000)
536
12edc149 537 const commentServer2 = await getComment(servers[0], servers[1].video.id)
310b5219 538
12edc149 539 await servers[0].commentsCommand.delete({ videoId: servers[1].video.uuid, commentId: commentServer2.id })
310b5219
C
540
541 await waitJobs(servers)
542
0c1a77e9
C
543 const body = await commands[0].getAdminList({ filter: 'comment' })
544 expect(body.total).to.equal(2)
545 expect(body.data).to.have.lengthOf(2)
310b5219 546
0c1a77e9 547 const abuse = body.data.find(a => a.comment?.id === commentServer2.id)
310b5219
C
548 expect(abuse).to.not.be.undefined
549
550 expect(abuse.comment.text).to.be.empty
551 expect(abuse.comment.video.name).to.equal('server 2')
552 expect(abuse.comment.deleted).to.be.true
553 })
554
555 it('Should delete the comment abuse', async function () {
556 this.timeout(10000)
557
0c1a77e9 558 await commands[1].delete({ abuseId: abuseServer2.id })
310b5219
C
559
560 await waitJobs(servers)
561
562 {
0c1a77e9
C
563 const body = await commands[1].getAdminList({ filter: 'comment' })
564 expect(body.total).to.equal(0)
565 expect(body.data.length).to.equal(0)
310b5219
C
566 }
567
568 {
0c1a77e9
C
569 const body = await commands[0].getAdminList({ filter: 'comment' })
570 expect(body.total).to.equal(2)
310b5219
C
571 }
572 })
573
574 it('Should list and filter video abuses', async function () {
575 {
0c1a77e9
C
576 const body = await commands[0].getAdminList({ filter: 'comment', searchReportee: 'foo' })
577 expect(body.total).to.equal(0)
310b5219
C
578 }
579
580 {
0c1a77e9
C
581 const body = await commands[0].getAdminList({ filter: 'comment', searchReportee: 'ot' })
582 expect(body.total).to.equal(2)
310b5219
C
583 }
584
585 {
0c1a77e9
C
586 const body = await commands[0].getAdminList({ filter: 'comment', start: 1, count: 1, sort: 'createdAt' })
587 expect(body.data).to.have.lengthOf(1)
588 expect(body.data[0].comment.text).to.be.empty
589 }
310b5219 590
0c1a77e9
C
591 {
592 const body = await commands[0].getAdminList({ filter: 'comment', start: 1, count: 1, sort: '-createdAt' })
593 expect(body.data).to.have.lengthOf(1)
594 expect(body.data[0].comment.text).to.equal('comment server 1')
4f32032f 595 }
310b5219
C
596 })
597 })
4f32032f 598
310b5219 599 describe('Account abuses', function () {
4f32032f 600
9fff08cf
C
601 function getAccountFromServer (server: ServerInfo, targetName: string, targetServer: ServerInfo) {
602 return server.accountsCommand.get({ accountName: targetName + '@' + targetServer.host })
4f32032f
C
603 }
604
310b5219
C
605 before(async function () {
606 this.timeout(50000)
607
608 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: 'user_1', password: 'donald' })
609
610 const token = await generateUserAccessToken(servers[1], 'user_2')
611 await uploadVideo(servers[1].url, token, { name: 'super video' })
612
613 await waitJobs(servers)
614 })
615
616 it('Should report abuse on an account', async function () {
617 this.timeout(15000)
618
9fff08cf 619 const account = await getAccountFromServer(servers[0], 'user_1', servers[0])
310b5219
C
620
621 const reason = 'it is a bad account'
0c1a77e9 622 await commands[0].report({ accountId: account.id, reason })
310b5219
C
623
624 await waitJobs(servers)
625 })
626
627 it('Should have 1 account abuse on server 1 and 0 on server 2', async function () {
628 {
0c1a77e9 629 const body = await commands[0].getAdminList({ filter: 'account' })
310b5219 630
0c1a77e9
C
631 expect(body.total).to.equal(1)
632 expect(body.data).to.have.lengthOf(1)
310b5219 633
0c1a77e9 634 const abuse = body.data[0]
310b5219
C
635 expect(abuse.reason).to.equal('it is a bad account')
636
637 expect(abuse.reporterAccount.name).to.equal('root')
638 expect(abuse.reporterAccount.host).to.equal(servers[0].host)
639
640 expect(abuse.video).to.be.null
641 expect(abuse.comment).to.be.null
642
643 expect(abuse.flaggedAccount.name).to.equal('user_1')
644 expect(abuse.flaggedAccount.host).to.equal(servers[0].host)
645 }
646
647 {
0c1a77e9
C
648 const body = await commands[1].getAdminList({ filter: 'comment' })
649 expect(body.total).to.equal(0)
650 expect(body.data.length).to.equal(0)
310b5219
C
651 }
652 })
653
654 it('Should report abuse on a remote account', async function () {
655 this.timeout(10000)
656
9fff08cf 657 const account = await getAccountFromServer(servers[0], 'user_2', servers[1])
310b5219
C
658
659 const reason = 'it is a really bad account'
0c1a77e9 660 await commands[0].report({ accountId: account.id, reason })
310b5219
C
661
662 await waitJobs(servers)
663 })
664
665 it('Should have 2 comment abuses on server 1 and 1 on server 2', async function () {
0c1a77e9
C
666 {
667 const body = await commands[0].getAdminList({ filter: 'account' })
668 expect(body.total).to.equal(2)
669 expect(body.data.length).to.equal(2)
310b5219 670
0c1a77e9
C
671 const abuse: AdminAbuse = body.data[0]
672 expect(abuse.reason).to.equal('it is a bad account')
4f32032f 673
0c1a77e9
C
674 const abuse2: AdminAbuse = body.data[1]
675 expect(abuse2.reason).to.equal('it is a really bad account')
4f32032f 676
0c1a77e9
C
677 expect(abuse2.reporterAccount.name).to.equal('root')
678 expect(abuse2.reporterAccount.host).to.equal(servers[0].host)
4f32032f 679
0c1a77e9
C
680 expect(abuse2.video).to.be.null
681 expect(abuse2.comment).to.be.null
310b5219 682
0c1a77e9
C
683 expect(abuse2.state.id).to.equal(AbuseState.PENDING)
684 expect(abuse2.state.label).to.equal('Pending')
310b5219 685
0c1a77e9
C
686 expect(abuse2.moderationComment).to.be.null
687 }
310b5219 688
0c1a77e9
C
689 {
690 const body = await commands[1].getAdminList({ filter: 'account' })
691 expect(body.total).to.equal(1)
692 expect(body.data.length).to.equal(1)
310b5219 693
0c1a77e9 694 abuseServer2 = body.data[0]
310b5219 695
0c1a77e9 696 expect(abuseServer2.reason).to.equal('it is a really bad account')
310b5219 697
0c1a77e9
C
698 expect(abuseServer2.reporterAccount.name).to.equal('root')
699 expect(abuseServer2.reporterAccount.host).to.equal(servers[0].host)
310b5219 700
0c1a77e9
C
701 expect(abuseServer2.state.id).to.equal(AbuseState.PENDING)
702 expect(abuseServer2.state.label).to.equal('Pending')
310b5219 703
0c1a77e9
C
704 expect(abuseServer2.moderationComment).to.be.null
705 }
310b5219
C
706 })
707
708 it('Should keep the account abuse when deleting the account', async function () {
709 this.timeout(10000)
710
9fff08cf 711 const account = await getAccountFromServer(servers[1], 'user_2', servers[1])
310b5219
C
712 await removeUser(servers[1].url, account.userId, servers[1].accessToken)
713
714 await waitJobs(servers)
715
0c1a77e9
C
716 const body = await commands[0].getAdminList({ filter: 'account' })
717 expect(body.total).to.equal(2)
718 expect(body.data).to.have.lengthOf(2)
310b5219 719
0c1a77e9 720 const abuse = body.data.find(a => a.reason === 'it is a really bad account')
310b5219
C
721 expect(abuse).to.not.be.undefined
722 })
723
724 it('Should delete the account abuse', async function () {
725 this.timeout(10000)
726
0c1a77e9 727 await commands[1].delete({ abuseId: abuseServer2.id })
310b5219
C
728
729 await waitJobs(servers)
730
731 {
0c1a77e9
C
732 const body = await commands[1].getAdminList({ filter: 'account' })
733 expect(body.total).to.equal(0)
734 expect(body.data.length).to.equal(0)
310b5219
C
735 }
736
737 {
0c1a77e9
C
738 const body = await commands[0].getAdminList({ filter: 'account' })
739 expect(body.total).to.equal(2)
310b5219 740
0c1a77e9 741 abuseServer1 = body.data[0]
310b5219
C
742 }
743 })
744 })
4f32032f 745
310b5219 746 describe('Common actions on abuses', function () {
4f32032f 747
310b5219 748 it('Should update the state of an abuse', async function () {
0c1a77e9 749 await commands[0].update({ abuseId: abuseServer1.id, body: { state: AbuseState.REJECTED } })
4f32032f 750
0c1a77e9
C
751 const body = await commands[0].getAdminList({ id: abuseServer1.id })
752 expect(body.data[0].state.id).to.equal(AbuseState.REJECTED)
310b5219 753 })
4f32032f 754
310b5219 755 it('Should add a moderation comment', async function () {
0c1a77e9 756 await commands[0].update({ abuseId: abuseServer1.id, body: { state: AbuseState.ACCEPTED, moderationComment: 'Valid' } })
4f32032f 757
0c1a77e9
C
758 const body = await commands[0].getAdminList({ id: abuseServer1.id })
759 expect(body.data[0].state.id).to.equal(AbuseState.ACCEPTED)
760 expect(body.data[0].moderationComment).to.equal('Valid')
310b5219 761 })
4f32032f
C
762 })
763
edbc9325
C
764 describe('My abuses', async function () {
765 let abuseId1: number
766 let userAccessToken: string
767
768 before(async function () {
769 userAccessToken = await generateUserAccessToken(servers[0], 'user_42')
770
0c1a77e9 771 await commands[0].report({ token: userAccessToken, videoId: servers[0].video.id, reason: 'user reason 1' })
edbc9325
C
772
773 const videoId = await getVideoIdFromUUID(servers[0].url, servers[1].video.uuid)
0c1a77e9 774 await commands[0].report({ token: userAccessToken, videoId, reason: 'user reason 2' })
edbc9325
C
775 })
776
777 it('Should correctly list my abuses', async function () {
778 {
0c1a77e9
C
779 const body = await commands[0].getUserList({ token: userAccessToken, start: 0, count: 5, sort: 'createdAt' })
780 expect(body.total).to.equal(2)
edbc9325 781
0c1a77e9 782 const abuses = body.data
edbc9325
C
783 expect(abuses[0].reason).to.equal('user reason 1')
784 expect(abuses[1].reason).to.equal('user reason 2')
785
786 abuseId1 = abuses[0].id
787 }
788
789 {
0c1a77e9
C
790 const body = await commands[0].getUserList({ token: userAccessToken, start: 1, count: 1, sort: 'createdAt' })
791 expect(body.total).to.equal(2)
edbc9325 792
0c1a77e9 793 const abuses: UserAbuse[] = body.data
edbc9325
C
794 expect(abuses[0].reason).to.equal('user reason 2')
795 }
796
797 {
0c1a77e9
C
798 const body = await commands[0].getUserList({ token: userAccessToken, start: 1, count: 1, sort: '-createdAt' })
799 expect(body.total).to.equal(2)
edbc9325 800
0c1a77e9 801 const abuses: UserAbuse[] = body.data
edbc9325
C
802 expect(abuses[0].reason).to.equal('user reason 1')
803 }
804 })
805
806 it('Should correctly filter my abuses by id', async function () {
0c1a77e9
C
807 const body = await commands[0].getUserList({ token: userAccessToken, id: abuseId1 })
808 expect(body.total).to.equal(1)
edbc9325 809
0c1a77e9 810 const abuses: UserAbuse[] = body.data
edbc9325
C
811 expect(abuses[0].reason).to.equal('user reason 1')
812 })
813
814 it('Should correctly filter my abuses by search', async function () {
0c1a77e9
C
815 const body = await commands[0].getUserList({ token: userAccessToken, search: 'server 2' })
816 expect(body.total).to.equal(1)
edbc9325 817
0c1a77e9 818 const abuses: UserAbuse[] = body.data
edbc9325
C
819 expect(abuses[0].reason).to.equal('user reason 2')
820 })
821
822 it('Should correctly filter my abuses by state', async function () {
0c1a77e9 823 await commands[0].update({ abuseId: abuseId1, body: { state: AbuseState.REJECTED } })
edbc9325 824
0c1a77e9
C
825 const body = await commands[0].getUserList({ token: userAccessToken, state: AbuseState.REJECTED })
826 expect(body.total).to.equal(1)
edbc9325 827
0c1a77e9 828 const abuses: UserAbuse[] = body.data
edbc9325
C
829 expect(abuses[0].reason).to.equal('user reason 1')
830 })
831 })
832
833 describe('Abuse messages', async function () {
834 let abuseId: number
0c1a77e9 835 let userToken: string
edbc9325
C
836 let abuseMessageUserId: number
837 let abuseMessageModerationId: number
838
839 before(async function () {
0c1a77e9 840 userToken = await generateUserAccessToken(servers[0], 'user_43')
edbc9325 841
0c1a77e9
C
842 const body = await commands[0].report({ token: userToken, videoId: servers[0].video.id, reason: 'user 43 reason 1' })
843 abuseId = body.abuse.id
edbc9325
C
844 })
845
846 it('Should create some messages on the abuse', async function () {
0c1a77e9
C
847 await commands[0].addMessage({ token: userToken, abuseId, message: 'message 1' })
848 await commands[0].addMessage({ abuseId, message: 'message 2' })
849 await commands[0].addMessage({ abuseId, message: 'message 3' })
850 await commands[0].addMessage({ token: userToken, abuseId, message: 'message 4' })
edbc9325
C
851 })
852
853 it('Should have the correct messages count when listing abuses', async function () {
854 const results = await Promise.all([
0c1a77e9
C
855 commands[0].getAdminList({ start: 0, count: 50 }),
856 commands[0].getUserList({ token: userToken, start: 0, count: 50 })
edbc9325
C
857 ])
858
0c1a77e9
C
859 for (const body of results) {
860 const abuses = body.data
edbc9325
C
861 const abuse = abuses.find(a => a.id === abuseId)
862 expect(abuse.countMessages).to.equal(4)
863 }
864 })
865
866 it('Should correctly list messages of this abuse', async function () {
867 const results = await Promise.all([
0c1a77e9
C
868 commands[0].listMessages({ abuseId }),
869 commands[0].listMessages({ token: userToken, abuseId })
edbc9325
C
870 ])
871
0c1a77e9
C
872 for (const body of results) {
873 expect(body.total).to.equal(4)
edbc9325 874
0c1a77e9 875 const abuseMessages: AbuseMessage[] = body.data
edbc9325
C
876
877 expect(abuseMessages[0].message).to.equal('message 1')
878 expect(abuseMessages[0].byModerator).to.be.false
879 expect(abuseMessages[0].account.name).to.equal('user_43')
880
881 abuseMessageUserId = abuseMessages[0].id
882
883 expect(abuseMessages[1].message).to.equal('message 2')
884 expect(abuseMessages[1].byModerator).to.be.true
885 expect(abuseMessages[1].account.name).to.equal('root')
886
887 expect(abuseMessages[2].message).to.equal('message 3')
888 expect(abuseMessages[2].byModerator).to.be.true
889 expect(abuseMessages[2].account.name).to.equal('root')
890 abuseMessageModerationId = abuseMessages[2].id
891
892 expect(abuseMessages[3].message).to.equal('message 4')
893 expect(abuseMessages[3].byModerator).to.be.false
894 expect(abuseMessages[3].account.name).to.equal('user_43')
895 }
896 })
897
898 it('Should delete messages', async function () {
0c1a77e9
C
899 await commands[0].deleteMessage({ abuseId, messageId: abuseMessageModerationId })
900 await commands[0].deleteMessage({ token: userToken, abuseId, messageId: abuseMessageUserId })
edbc9325
C
901
902 const results = await Promise.all([
0c1a77e9
C
903 commands[0].listMessages({ abuseId }),
904 commands[0].listMessages({ token: userToken, abuseId })
edbc9325
C
905 ])
906
0c1a77e9
C
907 for (const body of results) {
908 expect(body.total).to.equal(2)
edbc9325 909
0c1a77e9 910 const abuseMessages: AbuseMessage[] = body.data
edbc9325
C
911 expect(abuseMessages[0].message).to.equal('message 2')
912 expect(abuseMessages[1].message).to.equal('message 4')
913 }
914 })
915 })
916
4f32032f
C
917 after(async function () {
918 await cleanupTests(servers)
919 })
920})