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