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