1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
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'
11 createMultipleServers,
15 setAccessTokensToServers,
16 setDefaultChannelAvatar,
18 } from '@shared/server-commands'
20 const expect = chai.expect
22 describe('Test video blacklist', function () {
23 let servers: PeerTubeServer[] = []
25 let command: BlacklistCommand
27 async function blacklistVideosOnServer (server: PeerTubeServer) {
28 const { data } = await server.videos.list()
30 for (const video of data) {
31 await server.blacklist.add({ videoId: video.id, reason: 'super reason' })
35 before(async function () {
39 servers = await createMultipleServers(2)
41 // Get the access tokens
42 await setAccessTokensToServers(servers)
44 // Server 1 and server 2 follow each other
45 await doubleFollow(servers[0], servers[1])
46 await setDefaultChannelAvatar(servers[0])
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' } })
52 // Wait videos propagation, server 2 has transcoding enabled
53 await waitJobs(servers)
55 command = servers[0].blacklist
57 // Blacklist the two videos on server 1
58 await blacklistVideosOnServer(servers[0])
61 describe('When listing/searching videos', function () {
63 it('Should not have the video blacklisted in videos list/search on server 1', async function () {
65 const { total, data } = await servers[0].videos.list()
67 expect(total).to.equal(0)
68 expect(data).to.be.an('array')
69 expect(data.length).to.equal(0)
73 const body = await servers[0].search.searchVideos({ search: 'video' })
75 expect(body.total).to.equal(0)
76 expect(body.data).to.be.an('array')
77 expect(body.data.length).to.equal(0)
81 it('Should have the blacklisted video in videos list/search on server 2', async function () {
83 const { total, data } = await servers[1].videos.list()
85 expect(total).to.equal(2)
86 expect(data).to.be.an('array')
87 expect(data.length).to.equal(2)
91 const body = await servers[1].search.searchVideos({ search: 'video' })
93 expect(body.total).to.equal(2)
94 expect(body.data).to.be.an('array')
95 expect(body.data.length).to.equal(2)
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)
105 const blacklistedVideos = body.data
106 expect(blacklistedVideos).to.be.an('array')
107 expect(blacklistedVideos.length).to.equal(2)
109 for (const blacklistedVideo of blacklistedVideos) {
110 expect(blacklistedVideo.reason).to.equal('super reason')
111 videoId = blacklistedVideo.video.id
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)
119 const blacklistedVideos = body.data
120 expect(blacklistedVideos).to.be.an('array')
121 expect(blacklistedVideos.length).to.equal(2)
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)
128 const blacklistedVideos = body.data
129 expect(blacklistedVideos).to.be.an('array')
130 expect(blacklistedVideos.length).to.equal(0)
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)
137 const blacklistedVideos = body.data
138 expect(blacklistedVideos).to.be.an('array')
139 expect(blacklistedVideos.length).to.equal(2)
141 const result = orderBy(body.data, [ 'id' ], [ 'desc' ])
142 expect(blacklistedVideos).to.deep.equal(result)
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)
149 const blacklistedVideos = body.data
150 expect(blacklistedVideos).to.be.an('array')
151 expect(blacklistedVideos.length).to.equal(2)
153 const result = orderBy(body.data, [ 'name' ], [ 'desc' ])
154 expect(blacklistedVideos).to.deep.equal(result)
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)
161 const blacklistedVideos = body.data
162 expect(blacklistedVideos).to.be.an('array')
163 expect(blacklistedVideos.length).to.equal(2)
165 const result = orderBy(body.data, [ 'createdAt' ])
166 expect(blacklistedVideos).to.deep.equal(result)
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' })
174 const body = await command.list({ sort: '-name' })
175 const video = body.data.find(b => b.video.id === videoId)
177 expect(video.reason).to.equal('my super reason updated')
181 describe('When listing my videos', function () {
182 it('Should display blacklisted videos', async function () {
183 await blacklistVideosOnServer(servers[1])
185 const { total, data } = await servers[1].videos.listMyVideos()
187 expect(total).to.equal(2)
188 expect(data).to.have.lengthOf(2)
190 for (const video of data) {
191 expect(video.blacklisted).to.be.true
192 expect(video.blacklistedReason).to.equal('super reason')
197 describe('When removing a blacklisted video', function () {
198 let videoToRemove: VideoBlacklist
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)
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)
215 await command.remove({ videoId: videoToRemove.video.id })
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)
222 expect(data).to.be.an('array')
223 expect(data.length).to.equal(1)
225 expect(data[0].name).to.equal(videoToRemove.video.name)
226 expect(data[0].id).to.equal(videoToRemove.video.id)
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)
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)
240 describe('When blacklisting local videos', function () {
241 let video3UUID: string
242 let video4UUID: string
244 before(async function () {
248 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 3' } })
252 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 4' } })
256 await waitJobs(servers)
259 it('Should blacklist video 3 and keep it federated', async function () {
262 await command.add({ videoId: video3UUID, reason: 'super reason', unfederate: false })
264 await waitJobs(servers)
267 const { data } = await servers[0].videos.list()
268 expect(data.find(v => v.uuid === video3UUID)).to.be.undefined
272 const { data } = await servers[1].videos.list()
273 expect(data.find(v => v.uuid === video3UUID)).to.not.be.undefined
277 it('Should unfederate the video', async function () {
280 await command.add({ videoId: video4UUID, reason: 'super reason', unfederate: true })
282 await waitJobs(servers)
284 for (const server of servers) {
285 const { data } = await server.videos.list()
286 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
290 it('Should have the video unfederated even after an Update AP message', async function () {
293 await servers[0].videos.update({ id: video4UUID, attributes: { description: 'super description' } })
295 await waitJobs(servers)
297 for (const server of servers) {
298 const { data } = await server.videos.list()
299 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
303 it('Should have the correct video blacklist unfederate attribute', async function () {
304 const body = await command.list({ sort: 'createdAt' })
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)
310 expect(video3Blacklisted.unfederated).to.be.false
311 expect(video4Blacklisted.unfederated).to.be.true
314 it('Should remove the video from blacklist and refederate the video', async function () {
317 await command.remove({ videoId: video4UUID })
319 await waitJobs(servers)
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
329 describe('When auto blacklist videos', function () {
330 let userWithoutFlag: string
331 let userWithFlag: string
332 let channelOfUserWithoutFlag: number
334 before(async function () {
337 await killallServers([ servers[0] ])
348 await servers[0].run(config)
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,
359 userWithoutFlag = await servers[0].login.getAccessToken(user)
361 const { videoChannels } = await servers[0].users.getMyInfo({ token: userWithoutFlag })
362 channelOfUserWithoutFlag = videoChannels[0].id
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,
374 userWithFlag = await servers[0].login.getAccessToken(user)
377 await waitJobs(servers)
380 it('Should auto blacklist a video on upload', async function () {
381 await servers[0].videos.upload({ token: userWithoutFlag, attributes: { name: 'blacklisted' } })
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')
388 it('Should auto blacklist a video on URL import', async function () {
392 targetUrl: FIXTURE_URLS.goodVideo,
394 channelId: channelOfUserWithoutFlag
396 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
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')
403 it('Should auto blacklist a video on torrent import', async function () {
405 magnetUri: FIXTURE_URLS.magnet,
406 name: 'Torrent import',
407 channelId: channelOfUserWithoutFlag
409 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
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')
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' } })
419 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
420 expect(body.total).to.equal(3)
424 after(async function () {
425 await cleanupTests(servers)