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,
18 setAccessTokensToServers,
23 } from '@shared/extra-utils'
24 import { getGoodVideoUrl, getMagnetURI, importVideo } from '@shared/extra-utils/videos/video-imports'
25 import { User, UserAdminFlag, UserRole, VideoBlacklist, VideoBlacklistType } from '@shared/models'
27 const expect = chai.expect
29 describe('Test video blacklist', function () {
30 let servers: ServerInfo[] = []
32 let command: BlacklistCommand
34 async function blacklistVideosOnServer (server: ServerInfo) {
35 const res = await getVideosList(server.url)
37 const videos = res.body.data
38 for (const video of videos) {
39 await server.blacklistCommand.add({ videoId: video.id, reason: 'super reason' })
43 before(async function () {
47 servers = await flushAndRunMultipleServers(2)
49 // Get the access tokens
50 await setAccessTokensToServers(servers)
52 // Server 1 and server 2 follow each other
53 await doubleFollow(servers[0], servers[1])
55 // Upload 2 videos on server 2
56 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'My 1st video', description: 'A video on server 2' })
57 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'My 2nd video', description: 'A video on server 2' })
59 // Wait videos propagation, server 2 has transcoding enabled
60 await waitJobs(servers)
62 command = servers[0].blacklistCommand
64 // Blacklist the two videos on server 1
65 await blacklistVideosOnServer(servers[0])
68 describe('When listing/searching videos', function () {
70 it('Should not have the video blacklisted in videos list/search on server 1', async function () {
72 const res = await getVideosList(servers[0].url)
74 expect(res.body.total).to.equal(0)
75 expect(res.body.data).to.be.an('array')
76 expect(res.body.data.length).to.equal(0)
80 const body = await servers[0].searchCommand.searchVideos({ search: 'video' })
82 expect(body.total).to.equal(0)
83 expect(body.data).to.be.an('array')
84 expect(body.data.length).to.equal(0)
88 it('Should have the blacklisted video in videos list/search on server 2', async function () {
90 const res = await getVideosList(servers[1].url)
92 expect(res.body.total).to.equal(2)
93 expect(res.body.data).to.be.an('array')
94 expect(res.body.data.length).to.equal(2)
98 const body = await servers[1].searchCommand.searchVideos({ search: 'video' })
100 expect(body.total).to.equal(2)
101 expect(body.data).to.be.an('array')
102 expect(body.data.length).to.equal(2)
107 describe('When listing manually blacklisted videos', function () {
108 it('Should display all the blacklisted videos', async function () {
109 const body = await command.list()
110 expect(body.total).to.equal(2)
112 const blacklistedVideos = body.data
113 expect(blacklistedVideos).to.be.an('array')
114 expect(blacklistedVideos.length).to.equal(2)
116 for (const blacklistedVideo of blacklistedVideos) {
117 expect(blacklistedVideo.reason).to.equal('super reason')
118 videoId = blacklistedVideo.video.id
122 it('Should display all the blacklisted videos when applying manual type filter', async function () {
123 const body = await command.list({ type: VideoBlacklistType.MANUAL })
124 expect(body.total).to.equal(2)
126 const blacklistedVideos = body.data
127 expect(blacklistedVideos).to.be.an('array')
128 expect(blacklistedVideos.length).to.equal(2)
131 it('Should display nothing when applying automatic type filter', async function () {
132 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
133 expect(body.total).to.equal(0)
135 const blacklistedVideos = body.data
136 expect(blacklistedVideos).to.be.an('array')
137 expect(blacklistedVideos.length).to.equal(0)
140 it('Should get the correct sort when sorting by descending id', async function () {
141 const body = await command.list({ sort: '-id' })
142 expect(body.total).to.equal(2)
144 const blacklistedVideos = body.data
145 expect(blacklistedVideos).to.be.an('array')
146 expect(blacklistedVideos.length).to.equal(2)
148 const result = orderBy(body.data, [ 'id' ], [ 'desc' ])
149 expect(blacklistedVideos).to.deep.equal(result)
152 it('Should get the correct sort when sorting by descending video name', async function () {
153 const body = await command.list({ sort: '-name' })
154 expect(body.total).to.equal(2)
156 const blacklistedVideos = body.data
157 expect(blacklistedVideos).to.be.an('array')
158 expect(blacklistedVideos.length).to.equal(2)
160 const result = orderBy(body.data, [ 'name' ], [ 'desc' ])
161 expect(blacklistedVideos).to.deep.equal(result)
164 it('Should get the correct sort when sorting by ascending creation date', async function () {
165 const body = await command.list({ sort: 'createdAt' })
166 expect(body.total).to.equal(2)
168 const blacklistedVideos = body.data
169 expect(blacklistedVideos).to.be.an('array')
170 expect(blacklistedVideos.length).to.equal(2)
172 const result = orderBy(body.data, [ 'createdAt' ])
173 expect(blacklistedVideos).to.deep.equal(result)
177 describe('When updating blacklisted videos', function () {
178 it('Should change the reason', async function () {
179 await command.update({ videoId, reason: 'my super reason updated' })
181 const body = await command.list({ sort: '-name' })
182 const video = body.data.find(b => b.video.id === videoId)
184 expect(video.reason).to.equal('my super reason updated')
188 describe('When listing my videos', function () {
189 it('Should display blacklisted videos', async function () {
190 await blacklistVideosOnServer(servers[1])
192 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 5)
194 expect(res.body.total).to.equal(2)
195 expect(res.body.data).to.have.lengthOf(2)
197 for (const video of res.body.data) {
198 expect(video.blacklisted).to.be.true
199 expect(video.blacklistedReason).to.equal('super reason')
204 describe('When removing a blacklisted video', function () {
205 let videoToRemove: VideoBlacklist
208 it('Should not have any video in videos list on server 1', async function () {
209 const res = await getVideosList(servers[0].url)
210 expect(res.body.total).to.equal(0)
211 expect(res.body.data).to.be.an('array')
212 expect(res.body.data.length).to.equal(0)
215 it('Should remove a video from the blacklist on server 1', async function () {
216 // Get one video in the blacklist
217 const body = await command.list({ sort: '-name' })
218 videoToRemove = body.data[0]
219 blacklist = body.data.slice(1)
222 await command.remove({ videoId: videoToRemove.video.id })
225 it('Should have the ex-blacklisted video in videos list on server 1', async function () {
226 const res = await getVideosList(servers[0].url)
227 expect(res.body.total).to.equal(1)
229 const videos = res.body.data
230 expect(videos).to.be.an('array')
231 expect(videos.length).to.equal(1)
233 expect(videos[0].name).to.equal(videoToRemove.video.name)
234 expect(videos[0].id).to.equal(videoToRemove.video.id)
237 it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () {
238 const body = await command.list({ sort: '-name' })
239 expect(body.total).to.equal(1)
241 const videos = body.data
242 expect(videos).to.be.an('array')
243 expect(videos.length).to.equal(1)
244 expect(videos).to.deep.equal(blacklist)
248 describe('When blacklisting local videos', function () {
249 let video3UUID: string
250 let video4UUID: string
252 before(async function () {
256 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'Video 3' })
257 video3UUID = res.body.video.uuid
260 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'Video 4' })
261 video4UUID = res.body.video.uuid
264 await waitJobs(servers)
267 it('Should blacklist video 3 and keep it federated', async function () {
270 await command.add({ videoId: video3UUID, reason: 'super reason', unfederate: false })
272 await waitJobs(servers)
275 const res = await getVideosList(servers[0].url)
276 expect(res.body.data.find(v => v.uuid === video3UUID)).to.be.undefined
280 const res = await getVideosList(servers[1].url)
281 expect(res.body.data.find(v => v.uuid === video3UUID)).to.not.be.undefined
285 it('Should unfederate the video', async function () {
288 await command.add({ videoId: video4UUID, reason: 'super reason', unfederate: true })
290 await waitJobs(servers)
292 for (const server of servers) {
293 const res = await getVideosList(server.url)
294 expect(res.body.data.find(v => v.uuid === video4UUID)).to.be.undefined
298 it('Should have the video unfederated even after an Update AP message', async function () {
301 await updateVideo(servers[0].url, servers[0].accessToken, video4UUID, { description: 'super description' })
303 await waitJobs(servers)
305 for (const server of servers) {
306 const res = await getVideosList(server.url)
307 expect(res.body.data.find(v => v.uuid === video4UUID)).to.be.undefined
311 it('Should have the correct video blacklist unfederate attribute', async function () {
312 const body = await command.list({ sort: 'createdAt' })
314 const blacklistedVideos = body.data
315 const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID)
316 const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID)
318 expect(video3Blacklisted.unfederated).to.be.false
319 expect(video4Blacklisted.unfederated).to.be.true
322 it('Should remove the video from blacklist and refederate the video', async function () {
325 await command.remove({ videoId: video4UUID })
327 await waitJobs(servers)
329 for (const server of servers) {
330 const res = await getVideosList(server.url)
331 expect(res.body.data.find(v => v.uuid === video4UUID)).to.not.be.undefined
337 describe('When auto blacklist videos', function () {
338 let userWithoutFlag: string
339 let userWithFlag: string
340 let channelOfUserWithoutFlag: number
342 before(async function () {
345 killallServers([ servers[0] ])
356 await reRunServer(servers[0], config)
359 const user = { username: 'user_without_flag', password: 'password' }
362 accessToken: servers[0].accessToken,
363 username: user.username,
364 adminFlags: UserAdminFlag.NONE,
365 password: user.password,
369 userWithoutFlag = await userLogin(servers[0], user)
371 const res = await getMyUserInformation(servers[0].url, userWithoutFlag)
372 const body: User = res.body
373 channelOfUserWithoutFlag = body.videoChannels[0].id
377 const user = { username: 'user_with_flag', password: 'password' }
380 accessToken: servers[0].accessToken,
381 username: user.username,
382 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST,
383 password: user.password,
387 userWithFlag = await userLogin(servers[0], user)
390 await waitJobs(servers)
393 it('Should auto blacklist a video on upload', async function () {
394 await uploadVideo(servers[0].url, userWithoutFlag, { name: 'blacklisted' })
396 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
397 expect(body.total).to.equal(1)
398 expect(body.data[0].video.name).to.equal('blacklisted')
401 it('Should auto blacklist a video on URL import', async function () {
405 targetUrl: getGoodVideoUrl(),
407 channelId: channelOfUserWithoutFlag
409 await importVideo(servers[0].url, userWithoutFlag, attributes)
411 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
412 expect(body.total).to.equal(2)
413 expect(body.data[1].video.name).to.equal('URL import')
416 it('Should auto blacklist a video on torrent import', async function () {
418 magnetUri: getMagnetURI(),
419 name: 'Torrent import',
420 channelId: channelOfUserWithoutFlag
422 await importVideo(servers[0].url, userWithoutFlag, attributes)
424 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
425 expect(body.total).to.equal(3)
426 expect(body.data[2].video.name).to.equal('Torrent import')
429 it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () {
430 await uploadVideo(servers[0].url, userWithFlag, { name: 'not blacklisted' })
432 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
433 expect(body.total).to.equal(3)
437 after(async function () {
438 await cleanupTests(servers)