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