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