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