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