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