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