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