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