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