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