]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/moderation/video-blacklist.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / api / moderation / video-blacklist.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
0e1dc3e7 2
86347717 3import { expect } from 'chai'
c55e3d72 4import { FIXTURE_URLS } from '@server/tests/shared'
bbd5aa7e 5import { sortObjectComparator } from '@shared/core-utils'
c55e3d72 6import { UserAdminFlag, UserRole, VideoBlacklist, VideoBlacklistType } from '@shared/models'
0e1dc3e7 7import {
e3d15a6a 8 BlacklistCommand,
03371ad9 9 cleanupTests,
254d3579 10 createMultipleServers,
59bbcced 11 doubleFollow,
975e6e0e 12 killallServers,
254d3579 13 PeerTubeServer,
975e6e0e 14 setAccessTokensToServers,
d0800f76 15 setDefaultChannelAvatar,
af971e06 16 waitJobs
bf54587a 17} from '@shared/server-commands'
975e6e0e 18
1eddc9a7 19describe('Test video blacklist', function () {
254d3579 20 let servers: PeerTubeServer[] = []
5abb9fbb 21 let videoId: number
e3d15a6a 22 let command: BlacklistCommand
5abb9fbb 23
254d3579 24 async function blacklistVideosOnServer (server: PeerTubeServer) {
89d241a7 25 const { data } = await server.videos.list()
5abb9fbb 26
d23dd9fb 27 for (const video of data) {
89d241a7 28 await server.blacklist.add({ videoId: video.id, reason: 'super reason' })
5abb9fbb
C
29 }
30 }
0e1dc3e7
C
31
32 before(async function () {
ebee0c04 33 this.timeout(120000)
0e1dc3e7
C
34
35 // Run servers
254d3579 36 servers = await createMultipleServers(2)
0e1dc3e7
C
37
38 // Get the access tokens
39 await setAccessTokensToServers(servers)
40
975e6e0e
C
41 // Server 1 and server 2 follow each other
42 await doubleFollow(servers[0], servers[1])
d0800f76 43 await setDefaultChannelAvatar(servers[0])
0e1dc3e7 44
5abb9fbb 45 // Upload 2 videos on server 2
89d241a7
C
46 await servers[1].videos.upload({ attributes: { name: 'My 1st video', description: 'A video on server 2' } })
47 await servers[1].videos.upload({ attributes: { name: 'My 2nd video', description: 'A video on server 2' } })
0e1dc3e7 48
572f8d3d 49 // Wait videos propagation, server 2 has transcoding enabled
3cd0734f 50 await waitJobs(servers)
0e1dc3e7 51
89d241a7 52 command = servers[0].blacklist
e3d15a6a 53
5abb9fbb
C
54 // Blacklist the two videos on server 1
55 await blacklistVideosOnServer(servers[0])
56 })
57
58 describe('When listing/searching videos', function () {
0e1dc3e7 59
5abb9fbb
C
60 it('Should not have the video blacklisted in videos list/search on server 1', async function () {
61 {
89d241a7 62 const { total, data } = await servers[0].videos.list()
0e1dc3e7 63
d23dd9fb
C
64 expect(total).to.equal(0)
65 expect(data).to.be.an('array')
66 expect(data.length).to.equal(0)
5abb9fbb
C
67 }
68
69 {
89d241a7 70 const body = await servers[0].search.searchVideos({ search: 'video' })
5abb9fbb 71
af971e06
C
72 expect(body.total).to.equal(0)
73 expect(body.data).to.be.an('array')
74 expect(body.data.length).to.equal(0)
5abb9fbb
C
75 }
76 })
77
78 it('Should have the blacklisted video in videos list/search on server 2', async function () {
79 {
89d241a7 80 const { total, data } = await servers[1].videos.list()
5abb9fbb 81
d23dd9fb
C
82 expect(total).to.equal(2)
83 expect(data).to.be.an('array')
84 expect(data.length).to.equal(2)
5abb9fbb
C
85 }
86
87 {
89d241a7 88 const body = await servers[1].search.searchVideos({ search: 'video' })
5abb9fbb 89
af971e06
C
90 expect(body.total).to.equal(2)
91 expect(body.data).to.be.an('array')
92 expect(body.data.length).to.equal(2)
5abb9fbb
C
93 }
94 })
0e1dc3e7
C
95 })
96
7ccddd7b 97 describe('When listing manually blacklisted videos', function () {
5abb9fbb 98 it('Should display all the blacklisted videos', async function () {
e3d15a6a
C
99 const body = await command.list()
100 expect(body.total).to.equal(2)
5abb9fbb 101
e3d15a6a 102 const blacklistedVideos = body.data
5abb9fbb
C
103 expect(blacklistedVideos).to.be.an('array')
104 expect(blacklistedVideos.length).to.equal(2)
105
106 for (const blacklistedVideo of blacklistedVideos) {
107 expect(blacklistedVideo.reason).to.equal('super reason')
108 videoId = blacklistedVideo.video.id
109 }
110 })
111
7ccddd7b 112 it('Should display all the blacklisted videos when applying manual type filter', async function () {
e3d15a6a
C
113 const body = await command.list({ type: VideoBlacklistType.MANUAL })
114 expect(body.total).to.equal(2)
7ccddd7b 115
e3d15a6a 116 const blacklistedVideos = body.data
7ccddd7b
JM
117 expect(blacklistedVideos).to.be.an('array')
118 expect(blacklistedVideos.length).to.equal(2)
119 })
120
121 it('Should display nothing when applying automatic type filter', async function () {
e3d15a6a
C
122 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
123 expect(body.total).to.equal(0)
7ccddd7b 124
e3d15a6a 125 const blacklistedVideos = body.data
7ccddd7b
JM
126 expect(blacklistedVideos).to.be.an('array')
127 expect(blacklistedVideos.length).to.equal(0)
128 })
129
5abb9fbb 130 it('Should get the correct sort when sorting by descending id', async function () {
e3d15a6a
C
131 const body = await command.list({ sort: '-id' })
132 expect(body.total).to.equal(2)
5abb9fbb 133
e3d15a6a 134 const blacklistedVideos = body.data
5abb9fbb
C
135 expect(blacklistedVideos).to.be.an('array')
136 expect(blacklistedVideos.length).to.equal(2)
137
bbd5aa7e 138 const result = [ ...body.data ].sort(sortObjectComparator('id', 'desc'))
5abb9fbb
C
139 expect(blacklistedVideos).to.deep.equal(result)
140 })
141
142 it('Should get the correct sort when sorting by descending video name', async function () {
e3d15a6a
C
143 const body = await command.list({ sort: '-name' })
144 expect(body.total).to.equal(2)
5abb9fbb 145
e3d15a6a 146 const blacklistedVideos = body.data
5abb9fbb
C
147 expect(blacklistedVideos).to.be.an('array')
148 expect(blacklistedVideos.length).to.equal(2)
149
bbd5aa7e 150 const result = [ ...body.data ].sort(sortObjectComparator('name', 'desc'))
5abb9fbb
C
151 expect(blacklistedVideos).to.deep.equal(result)
152 })
153
154 it('Should get the correct sort when sorting by ascending creation date', async function () {
e3d15a6a
C
155 const body = await command.list({ sort: 'createdAt' })
156 expect(body.total).to.equal(2)
5abb9fbb 157
e3d15a6a 158 const blacklistedVideos = body.data
5abb9fbb
C
159 expect(blacklistedVideos).to.be.an('array')
160 expect(blacklistedVideos.length).to.equal(2)
161
bbd5aa7e 162 const result = [ ...body.data ].sort(sortObjectComparator('createdAt', 'asc'))
5abb9fbb
C
163 expect(blacklistedVideos).to.deep.equal(result)
164 })
0e1dc3e7
C
165 })
166
5abb9fbb
C
167 describe('When updating blacklisted videos', function () {
168 it('Should change the reason', async function () {
e3d15a6a 169 await command.update({ videoId, reason: 'my super reason updated' })
5abb9fbb 170
e3d15a6a
C
171 const body = await command.list({ sort: '-name' })
172 const video = body.data.find(b => b.video.id === videoId)
0e1dc3e7 173
5abb9fbb
C
174 expect(video.reason).to.equal('my super reason updated')
175 })
0e1dc3e7
C
176 })
177
5abb9fbb
C
178 describe('When listing my videos', function () {
179 it('Should display blacklisted videos', async function () {
180 await blacklistVideosOnServer(servers[1])
181
89d241a7 182 const { total, data } = await servers[1].videos.listMyVideos()
0e1dc3e7 183
d23dd9fb
C
184 expect(total).to.equal(2)
185 expect(data).to.have.lengthOf(2)
5abb9fbb 186
d23dd9fb 187 for (const video of data) {
5abb9fbb
C
188 expect(video.blacklisted).to.be.true
189 expect(video.blacklistedReason).to.equal('super reason')
190 }
191 })
0e1dc3e7
C
192 })
193
5abb9fbb 194 describe('When removing a blacklisted video', function () {
3487330d 195 let videoToRemove: VideoBlacklist
5abb9fbb
C
196 let blacklist = []
197
198 it('Should not have any video in videos list on server 1', async function () {
89d241a7 199 const { total, data } = await servers[0].videos.list()
d23dd9fb
C
200 expect(total).to.equal(0)
201 expect(data).to.be.an('array')
202 expect(data.length).to.equal(0)
5abb9fbb
C
203 })
204
205 it('Should remove a video from the blacklist on server 1', async function () {
206 // Get one video in the blacklist
e3d15a6a
C
207 const body = await command.list({ sort: '-name' })
208 videoToRemove = body.data[0]
209 blacklist = body.data.slice(1)
5abb9fbb
C
210
211 // Remove it
e3d15a6a 212 await command.remove({ videoId: videoToRemove.video.id })
5abb9fbb
C
213 })
214
215 it('Should have the ex-blacklisted video in videos list on server 1', async function () {
89d241a7 216 const { total, data } = await servers[0].videos.list()
d23dd9fb 217 expect(total).to.equal(1)
5abb9fbb 218
d23dd9fb
C
219 expect(data).to.be.an('array')
220 expect(data.length).to.equal(1)
5abb9fbb 221
d23dd9fb
C
222 expect(data[0].name).to.equal(videoToRemove.video.name)
223 expect(data[0].id).to.equal(videoToRemove.video.id)
5abb9fbb
C
224 })
225
226 it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () {
e3d15a6a
C
227 const body = await command.list({ sort: '-name' })
228 expect(body.total).to.equal(1)
0e1dc3e7 229
e3d15a6a 230 const videos = body.data
5abb9fbb
C
231 expect(videos).to.be.an('array')
232 expect(videos.length).to.equal(1)
233 expect(videos).to.deep.equal(blacklist)
234 })
0e1dc3e7
C
235 })
236
5abb9fbb
C
237 describe('When blacklisting local videos', function () {
238 let video3UUID: string
239 let video4UUID: string
240
241 before(async function () {
242 this.timeout(10000)
243
244 {
89d241a7 245 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 3' } })
d23dd9fb 246 video3UUID = uuid
5abb9fbb
C
247 }
248 {
89d241a7 249 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 4' } })
d23dd9fb 250 video4UUID = uuid
5abb9fbb
C
251 }
252
253 await waitJobs(servers)
254 })
255
256 it('Should blacklist video 3 and keep it federated', async function () {
257 this.timeout(10000)
258
e3d15a6a 259 await command.add({ videoId: video3UUID, reason: 'super reason', unfederate: false })
5abb9fbb
C
260
261 await waitJobs(servers)
262
263 {
89d241a7 264 const { data } = await servers[0].videos.list()
d23dd9fb 265 expect(data.find(v => v.uuid === video3UUID)).to.be.undefined
5abb9fbb
C
266 }
267
268 {
89d241a7 269 const { data } = await servers[1].videos.list()
d23dd9fb 270 expect(data.find(v => v.uuid === video3UUID)).to.not.be.undefined
5abb9fbb
C
271 }
272 })
273
274 it('Should unfederate the video', async function () {
275 this.timeout(10000)
276
e3d15a6a 277 await command.add({ videoId: video4UUID, reason: 'super reason', unfederate: true })
5abb9fbb
C
278
279 await waitJobs(servers)
280
281 for (const server of servers) {
89d241a7 282 const { data } = await server.videos.list()
d23dd9fb 283 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
5abb9fbb
C
284 }
285 })
286
287 it('Should have the video unfederated even after an Update AP message', async function () {
288 this.timeout(10000)
289
89d241a7 290 await servers[0].videos.update({ id: video4UUID, attributes: { description: 'super description' } })
5abb9fbb
C
291
292 await waitJobs(servers)
293
294 for (const server of servers) {
89d241a7 295 const { data } = await server.videos.list()
d23dd9fb 296 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
5abb9fbb
C
297 }
298 })
299
300 it('Should have the correct video blacklist unfederate attribute', async function () {
e3d15a6a 301 const body = await command.list({ sort: 'createdAt' })
5abb9fbb 302
e3d15a6a 303 const blacklistedVideos = body.data
5abb9fbb
C
304 const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID)
305 const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID)
306
307 expect(video3Blacklisted.unfederated).to.be.false
308 expect(video4Blacklisted.unfederated).to.be.true
309 })
310
311 it('Should remove the video from blacklist and refederate the video', async function () {
312 this.timeout(10000)
313
e3d15a6a 314 await command.remove({ videoId: video4UUID })
5abb9fbb
C
315
316 await waitJobs(servers)
317
318 for (const server of servers) {
89d241a7 319 const { data } = await server.videos.list()
d23dd9fb 320 expect(data.find(v => v.uuid === video4UUID)).to.not.be.undefined
5abb9fbb
C
321 }
322 })
0e1dc3e7 323
0e1dc3e7
C
324 })
325
1eddc9a7
C
326 describe('When auto blacklist videos', function () {
327 let userWithoutFlag: string
328 let userWithFlag: string
03371ad9 329 let channelOfUserWithoutFlag: number
1eddc9a7
C
330
331 before(async function () {
332 this.timeout(20000)
333
9293139f 334 await killallServers([ servers[0] ])
1eddc9a7
C
335
336 const config = {
a1587156 337 auto_blacklist: {
1eddc9a7 338 videos: {
a1587156 339 of_users: {
1eddc9a7
C
340 enabled: true
341 }
342 }
343 }
344 }
254d3579 345 await servers[0].run(config)
1eddc9a7
C
346
347 {
348 const user = { username: 'user_without_flag', password: 'password' }
89d241a7 349 await servers[0].users.create({
1eddc9a7
C
350 username: user.username,
351 adminFlags: UserAdminFlag.NONE,
352 password: user.password,
353 role: UserRole.USER
354 })
355
89d241a7 356 userWithoutFlag = await servers[0].login.getAccessToken(user)
03371ad9 357
89d241a7 358 const { videoChannels } = await servers[0].users.getMyInfo({ token: userWithoutFlag })
7926c5f9 359 channelOfUserWithoutFlag = videoChannels[0].id
1eddc9a7
C
360 }
361
362 {
363 const user = { username: 'user_with_flag', password: 'password' }
89d241a7 364 await servers[0].users.create({
1eddc9a7 365 username: user.username,
3487330d 366 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST,
1eddc9a7
C
367 password: user.password,
368 role: UserRole.USER
369 })
370
89d241a7 371 userWithFlag = await servers[0].login.getAccessToken(user)
1eddc9a7
C
372 }
373
374 await waitJobs(servers)
375 })
376
03371ad9 377 it('Should auto blacklist a video on upload', async function () {
89d241a7 378 await servers[0].videos.upload({ token: userWithoutFlag, attributes: { name: 'blacklisted' } })
1eddc9a7 379
e3d15a6a
C
380 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
381 expect(body.total).to.equal(1)
382 expect(body.data[0].video.name).to.equal('blacklisted')
1eddc9a7
C
383 })
384
03371ad9 385 it('Should auto blacklist a video on URL import', async function () {
109d893f
C
386 this.timeout(15000)
387
03371ad9 388 const attributes = {
59bbcced 389 targetUrl: FIXTURE_URLS.goodVideo,
03371ad9
C
390 name: 'URL import',
391 channelId: channelOfUserWithoutFlag
392 }
89d241a7 393 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
03371ad9 394
e3d15a6a
C
395 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
396 expect(body.total).to.equal(2)
397 expect(body.data[1].video.name).to.equal('URL import')
03371ad9
C
398 })
399
400 it('Should auto blacklist a video on torrent import', async function () {
401 const attributes = {
59bbcced 402 magnetUri: FIXTURE_URLS.magnet,
03371ad9
C
403 name: 'Torrent import',
404 channelId: channelOfUserWithoutFlag
405 }
89d241a7 406 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
03371ad9 407
e3d15a6a
C
408 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
409 expect(body.total).to.equal(3)
410 expect(body.data[2].video.name).to.equal('Torrent import')
03371ad9
C
411 })
412
413 it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () {
89d241a7 414 await servers[0].videos.upload({ token: userWithFlag, attributes: { name: 'not blacklisted' } })
1eddc9a7 415
e3d15a6a
C
416 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
417 expect(body.total).to.equal(3)
1eddc9a7
C
418 })
419 })
420
7c3b7976
C
421 after(async function () {
422 await cleanupTests(servers)
0e1dc3e7
C
423 })
424})