1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { orderBy } from 'lodash'
10 flushAndRunMultipleServers,
15 setAccessTokensToServers,
17 } from '@shared/extra-utils'
18 import { UserAdminFlag, UserRole, VideoBlacklist, VideoBlacklistType } from '@shared/models'
20 const expect = chai.expect
22 describe('Test video blacklist', function () {
23 let servers: ServerInfo[] = []
25 let command: BlacklistCommand
27 async function blacklistVideosOnServer (server: ServerInfo) {
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 flushAndRunMultipleServers(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])
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' } })
51 // Wait videos propagation, server 2 has transcoding enabled
52 await waitJobs(servers)
54 command = servers[0].blacklist
56 // Blacklist the two videos on server 1
57 await blacklistVideosOnServer(servers[0])
60 describe('When listing/searching videos', function () {
62 it('Should not have the video blacklisted in videos list/search on server 1', async function () {
64 const { total, data } = await servers[0].videos.list()
66 expect(total).to.equal(0)
67 expect(data).to.be.an('array')
68 expect(data.length).to.equal(0)
72 const body = await servers[0].search.searchVideos({ search: 'video' })
74 expect(body.total).to.equal(0)
75 expect(body.data).to.be.an('array')
76 expect(body.data.length).to.equal(0)
80 it('Should have the blacklisted video in videos list/search on server 2', async function () {
82 const { total, data } = await servers[1].videos.list()
84 expect(total).to.equal(2)
85 expect(data).to.be.an('array')
86 expect(data.length).to.equal(2)
90 const body = await servers[1].search.searchVideos({ search: 'video' })
92 expect(body.total).to.equal(2)
93 expect(body.data).to.be.an('array')
94 expect(body.data.length).to.equal(2)
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)
104 const blacklistedVideos = body.data
105 expect(blacklistedVideos).to.be.an('array')
106 expect(blacklistedVideos.length).to.equal(2)
108 for (const blacklistedVideo of blacklistedVideos) {
109 expect(blacklistedVideo.reason).to.equal('super reason')
110 videoId = blacklistedVideo.video.id
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)
118 const blacklistedVideos = body.data
119 expect(blacklistedVideos).to.be.an('array')
120 expect(blacklistedVideos.length).to.equal(2)
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)
127 const blacklistedVideos = body.data
128 expect(blacklistedVideos).to.be.an('array')
129 expect(blacklistedVideos.length).to.equal(0)
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)
136 const blacklistedVideos = body.data
137 expect(blacklistedVideos).to.be.an('array')
138 expect(blacklistedVideos.length).to.equal(2)
140 const result = orderBy(body.data, [ 'id' ], [ 'desc' ])
141 expect(blacklistedVideos).to.deep.equal(result)
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)
148 const blacklistedVideos = body.data
149 expect(blacklistedVideos).to.be.an('array')
150 expect(blacklistedVideos.length).to.equal(2)
152 const result = orderBy(body.data, [ 'name' ], [ 'desc' ])
153 expect(blacklistedVideos).to.deep.equal(result)
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)
160 const blacklistedVideos = body.data
161 expect(blacklistedVideos).to.be.an('array')
162 expect(blacklistedVideos.length).to.equal(2)
164 const result = orderBy(body.data, [ 'createdAt' ])
165 expect(blacklistedVideos).to.deep.equal(result)
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' })
173 const body = await command.list({ sort: '-name' })
174 const video = body.data.find(b => b.video.id === videoId)
176 expect(video.reason).to.equal('my super reason updated')
180 describe('When listing my videos', function () {
181 it('Should display blacklisted videos', async function () {
182 await blacklistVideosOnServer(servers[1])
184 const { total, data } = await servers[1].videos.listMyVideos()
186 expect(total).to.equal(2)
187 expect(data).to.have.lengthOf(2)
189 for (const video of data) {
190 expect(video.blacklisted).to.be.true
191 expect(video.blacklistedReason).to.equal('super reason')
196 describe('When removing a blacklisted video', function () {
197 let videoToRemove: VideoBlacklist
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)
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)
214 await command.remove({ videoId: videoToRemove.video.id })
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)
221 expect(data).to.be.an('array')
222 expect(data.length).to.equal(1)
224 expect(data[0].name).to.equal(videoToRemove.video.name)
225 expect(data[0].id).to.equal(videoToRemove.video.id)
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)
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)
239 describe('When blacklisting local videos', function () {
240 let video3UUID: string
241 let video4UUID: string
243 before(async function () {
247 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 3' } })
251 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 4' } })
255 await waitJobs(servers)
258 it('Should blacklist video 3 and keep it federated', async function () {
261 await command.add({ videoId: video3UUID, reason: 'super reason', unfederate: false })
263 await waitJobs(servers)
266 const { data } = await servers[0].videos.list()
267 expect(data.find(v => v.uuid === video3UUID)).to.be.undefined
271 const { data } = await servers[1].videos.list()
272 expect(data.find(v => v.uuid === video3UUID)).to.not.be.undefined
276 it('Should unfederate the video', async function () {
279 await command.add({ videoId: video4UUID, reason: 'super reason', unfederate: true })
281 await waitJobs(servers)
283 for (const server of servers) {
284 const { data } = await server.videos.list()
285 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
289 it('Should have the video unfederated even after an Update AP message', async function () {
292 await servers[0].videos.update({ id: video4UUID, attributes: { description: 'super description' } })
294 await waitJobs(servers)
296 for (const server of servers) {
297 const { data } = await server.videos.list()
298 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
302 it('Should have the correct video blacklist unfederate attribute', async function () {
303 const body = await command.list({ sort: 'createdAt' })
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)
309 expect(video3Blacklisted.unfederated).to.be.false
310 expect(video4Blacklisted.unfederated).to.be.true
313 it('Should remove the video from blacklist and refederate the video', async function () {
316 await command.remove({ videoId: video4UUID })
318 await waitJobs(servers)
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
328 describe('When auto blacklist videos', function () {
329 let userWithoutFlag: string
330 let userWithFlag: string
331 let channelOfUserWithoutFlag: number
333 before(async function () {
336 await killallServers([ servers[0] ])
347 await reRunServer(servers[0], config)
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,
358 userWithoutFlag = await servers[0].login.getAccessToken(user)
360 const { videoChannels } = await servers[0].users.getMyInfo({ token: userWithoutFlag })
361 channelOfUserWithoutFlag = videoChannels[0].id
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,
373 userWithFlag = await servers[0].login.getAccessToken(user)
376 await waitJobs(servers)
379 it('Should auto blacklist a video on upload', async function () {
380 await servers[0].videos.upload({ token: userWithoutFlag, attributes: { name: 'blacklisted' } })
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')
387 it('Should auto blacklist a video on URL import', async function () {
391 targetUrl: ImportsCommand.getGoodVideoUrl(),
393 channelId: channelOfUserWithoutFlag
395 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
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')
402 it('Should auto blacklist a video on torrent import', async function () {
404 magnetUri: ImportsCommand.getMagnetURI(),
405 name: 'Torrent import',
406 channelId: channelOfUserWithoutFlag
408 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
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')
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' } })
418 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
419 expect(body.total).to.equal(3)
423 after(async function () {
424 await cleanupTests(servers)