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