1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { orderBy } from 'lodash'
11 flushAndRunMultipleServers,
19 setAccessTokensToServers,
23 } from '@shared/extra-utils'
24 import { User, UserAdminFlag, UserRole, VideoBlacklist, VideoBlacklistType } from '@shared/models'
26 const expect = chai.expect
28 describe('Test video blacklist', function () {
29 let servers: ServerInfo[] = []
31 let command: BlacklistCommand
33 async function blacklistVideosOnServer (server: ServerInfo) {
34 const res = await getVideosList(server.url)
36 const videos = res.body.data
37 for (const video of videos) {
38 await server.blacklistCommand.add({ videoId: video.id, reason: 'super reason' })
42 before(async function () {
46 servers = await flushAndRunMultipleServers(2)
48 // Get the access tokens
49 await setAccessTokensToServers(servers)
51 // Server 1 and server 2 follow each other
52 await doubleFollow(servers[0], servers[1])
54 // Upload 2 videos on server 2
55 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'My 1st video', description: 'A video on server 2' })
56 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'My 2nd video', description: 'A video on server 2' })
58 // Wait videos propagation, server 2 has transcoding enabled
59 await waitJobs(servers)
61 command = servers[0].blacklistCommand
63 // Blacklist the two videos on server 1
64 await blacklistVideosOnServer(servers[0])
67 describe('When listing/searching videos', function () {
69 it('Should not have the video blacklisted in videos list/search on server 1', async function () {
71 const res = await getVideosList(servers[0].url)
73 expect(res.body.total).to.equal(0)
74 expect(res.body.data).to.be.an('array')
75 expect(res.body.data.length).to.equal(0)
79 const body = await servers[0].searchCommand.searchVideos({ search: 'video' })
81 expect(body.total).to.equal(0)
82 expect(body.data).to.be.an('array')
83 expect(body.data.length).to.equal(0)
87 it('Should have the blacklisted video in videos list/search on server 2', async function () {
89 const res = await getVideosList(servers[1].url)
91 expect(res.body.total).to.equal(2)
92 expect(res.body.data).to.be.an('array')
93 expect(res.body.data.length).to.equal(2)
97 const body = await servers[1].searchCommand.searchVideos({ search: 'video' })
99 expect(body.total).to.equal(2)
100 expect(body.data).to.be.an('array')
101 expect(body.data.length).to.equal(2)
106 describe('When listing manually blacklisted videos', function () {
107 it('Should display all the blacklisted videos', async function () {
108 const body = await command.list()
109 expect(body.total).to.equal(2)
111 const blacklistedVideos = body.data
112 expect(blacklistedVideos).to.be.an('array')
113 expect(blacklistedVideos.length).to.equal(2)
115 for (const blacklistedVideo of blacklistedVideos) {
116 expect(blacklistedVideo.reason).to.equal('super reason')
117 videoId = blacklistedVideo.video.id
121 it('Should display all the blacklisted videos when applying manual type filter', async function () {
122 const body = await command.list({ type: VideoBlacklistType.MANUAL })
123 expect(body.total).to.equal(2)
125 const blacklistedVideos = body.data
126 expect(blacklistedVideos).to.be.an('array')
127 expect(blacklistedVideos.length).to.equal(2)
130 it('Should display nothing when applying automatic type filter', async function () {
131 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
132 expect(body.total).to.equal(0)
134 const blacklistedVideos = body.data
135 expect(blacklistedVideos).to.be.an('array')
136 expect(blacklistedVideos.length).to.equal(0)
139 it('Should get the correct sort when sorting by descending id', async function () {
140 const body = await command.list({ sort: '-id' })
141 expect(body.total).to.equal(2)
143 const blacklistedVideos = body.data
144 expect(blacklistedVideos).to.be.an('array')
145 expect(blacklistedVideos.length).to.equal(2)
147 const result = orderBy(body.data, [ 'id' ], [ 'desc' ])
148 expect(blacklistedVideos).to.deep.equal(result)
151 it('Should get the correct sort when sorting by descending video name', async function () {
152 const body = await command.list({ sort: '-name' })
153 expect(body.total).to.equal(2)
155 const blacklistedVideos = body.data
156 expect(blacklistedVideos).to.be.an('array')
157 expect(blacklistedVideos.length).to.equal(2)
159 const result = orderBy(body.data, [ 'name' ], [ 'desc' ])
160 expect(blacklistedVideos).to.deep.equal(result)
163 it('Should get the correct sort when sorting by ascending creation date', async function () {
164 const body = await command.list({ sort: 'createdAt' })
165 expect(body.total).to.equal(2)
167 const blacklistedVideos = body.data
168 expect(blacklistedVideos).to.be.an('array')
169 expect(blacklistedVideos.length).to.equal(2)
171 const result = orderBy(body.data, [ 'createdAt' ])
172 expect(blacklistedVideos).to.deep.equal(result)
176 describe('When updating blacklisted videos', function () {
177 it('Should change the reason', async function () {
178 await command.update({ videoId, reason: 'my super reason updated' })
180 const body = await command.list({ sort: '-name' })
181 const video = body.data.find(b => b.video.id === videoId)
183 expect(video.reason).to.equal('my super reason updated')
187 describe('When listing my videos', function () {
188 it('Should display blacklisted videos', async function () {
189 await blacklistVideosOnServer(servers[1])
191 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 5)
193 expect(res.body.total).to.equal(2)
194 expect(res.body.data).to.have.lengthOf(2)
196 for (const video of res.body.data) {
197 expect(video.blacklisted).to.be.true
198 expect(video.blacklistedReason).to.equal('super reason')
203 describe('When removing a blacklisted video', function () {
204 let videoToRemove: VideoBlacklist
207 it('Should not have any video in videos list on server 1', async function () {
208 const res = await getVideosList(servers[0].url)
209 expect(res.body.total).to.equal(0)
210 expect(res.body.data).to.be.an('array')
211 expect(res.body.data.length).to.equal(0)
214 it('Should remove a video from the blacklist on server 1', async function () {
215 // Get one video in the blacklist
216 const body = await command.list({ sort: '-name' })
217 videoToRemove = body.data[0]
218 blacklist = body.data.slice(1)
221 await command.remove({ videoId: videoToRemove.video.id })
224 it('Should have the ex-blacklisted video in videos list on server 1', async function () {
225 const res = await getVideosList(servers[0].url)
226 expect(res.body.total).to.equal(1)
228 const videos = res.body.data
229 expect(videos).to.be.an('array')
230 expect(videos.length).to.equal(1)
232 expect(videos[0].name).to.equal(videoToRemove.video.name)
233 expect(videos[0].id).to.equal(videoToRemove.video.id)
236 it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () {
237 const body = await command.list({ sort: '-name' })
238 expect(body.total).to.equal(1)
240 const videos = body.data
241 expect(videos).to.be.an('array')
242 expect(videos.length).to.equal(1)
243 expect(videos).to.deep.equal(blacklist)
247 describe('When blacklisting local videos', function () {
248 let video3UUID: string
249 let video4UUID: string
251 before(async function () {
255 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'Video 3' })
256 video3UUID = res.body.video.uuid
259 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'Video 4' })
260 video4UUID = res.body.video.uuid
263 await waitJobs(servers)
266 it('Should blacklist video 3 and keep it federated', async function () {
269 await command.add({ videoId: video3UUID, reason: 'super reason', unfederate: false })
271 await waitJobs(servers)
274 const res = await getVideosList(servers[0].url)
275 expect(res.body.data.find(v => v.uuid === video3UUID)).to.be.undefined
279 const res = await getVideosList(servers[1].url)
280 expect(res.body.data.find(v => v.uuid === video3UUID)).to.not.be.undefined
284 it('Should unfederate the video', async function () {
287 await command.add({ videoId: video4UUID, reason: 'super reason', unfederate: true })
289 await waitJobs(servers)
291 for (const server of servers) {
292 const res = await getVideosList(server.url)
293 expect(res.body.data.find(v => v.uuid === video4UUID)).to.be.undefined
297 it('Should have the video unfederated even after an Update AP message', async function () {
300 await updateVideo(servers[0].url, servers[0].accessToken, video4UUID, { description: 'super description' })
302 await waitJobs(servers)
304 for (const server of servers) {
305 const res = await getVideosList(server.url)
306 expect(res.body.data.find(v => v.uuid === video4UUID)).to.be.undefined
310 it('Should have the correct video blacklist unfederate attribute', async function () {
311 const body = await command.list({ sort: 'createdAt' })
313 const blacklistedVideos = body.data
314 const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID)
315 const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID)
317 expect(video3Blacklisted.unfederated).to.be.false
318 expect(video4Blacklisted.unfederated).to.be.true
321 it('Should remove the video from blacklist and refederate the video', async function () {
324 await command.remove({ videoId: video4UUID })
326 await waitJobs(servers)
328 for (const server of servers) {
329 const res = await getVideosList(server.url)
330 expect(res.body.data.find(v => v.uuid === video4UUID)).to.not.be.undefined
336 describe('When auto blacklist videos', function () {
337 let userWithoutFlag: string
338 let userWithFlag: string
339 let channelOfUserWithoutFlag: number
341 before(async function () {
344 await killallServers([ servers[0] ])
355 await reRunServer(servers[0], config)
358 const user = { username: 'user_without_flag', password: 'password' }
361 accessToken: servers[0].accessToken,
362 username: user.username,
363 adminFlags: UserAdminFlag.NONE,
364 password: user.password,
368 userWithoutFlag = await servers[0].loginCommand.getAccessToken(user)
370 const res = await getMyUserInformation(servers[0].url, userWithoutFlag)
371 const body: User = res.body
372 channelOfUserWithoutFlag = body.videoChannels[0].id
376 const user = { username: 'user_with_flag', password: 'password' }
379 accessToken: servers[0].accessToken,
380 username: user.username,
381 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST,
382 password: user.password,
386 userWithFlag = await servers[0].loginCommand.getAccessToken(user)
389 await waitJobs(servers)
392 it('Should auto blacklist a video on upload', async function () {
393 await uploadVideo(servers[0].url, userWithoutFlag, { name: 'blacklisted' })
395 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
396 expect(body.total).to.equal(1)
397 expect(body.data[0].video.name).to.equal('blacklisted')
400 it('Should auto blacklist a video on URL import', async function () {
404 targetUrl: ImportsCommand.getGoodVideoUrl(),
406 channelId: channelOfUserWithoutFlag
408 await servers[0].importsCommand.importVideo({ token: userWithoutFlag, attributes })
410 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
411 expect(body.total).to.equal(2)
412 expect(body.data[1].video.name).to.equal('URL import')
415 it('Should auto blacklist a video on torrent import', async function () {
417 magnetUri: ImportsCommand.getMagnetURI(),
418 name: 'Torrent import',
419 channelId: channelOfUserWithoutFlag
421 await servers[0].importsCommand.importVideo({ token: userWithoutFlag, attributes })
423 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
424 expect(body.total).to.equal(3)
425 expect(body.data[2].video.name).to.equal('Torrent import')
428 it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () {
429 await uploadVideo(servers[0].url, userWithFlag, { name: 'not blacklisted' })
431 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
432 expect(body.total).to.equal(3)
436 after(async function () {
437 await cleanupTests(servers)