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