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,
12 getBlacklistedVideosList,
17 removeVideoFromBlacklist,
20 setAccessTokensToServers,
26 } from '@shared/extra-utils'
27 import { getGoodVideoUrl, getMagnetURI, importVideo } from '@shared/extra-utils/videos/video-imports'
28 import { User, UserAdminFlag, UserRole, VideoBlacklist, VideoBlacklistType } from '@shared/models'
30 const expect = chai.expect
32 describe('Test video blacklist', function () {
33 let servers: ServerInfo[] = []
36 async function blacklistVideosOnServer (server: ServerInfo) {
37 const res = await getVideosList(server.url)
39 const videos = res.body.data
40 for (const video of videos) {
41 await addVideoToBlacklist(server.url, server.accessToken, video.id, 'super reason')
45 before(async function () {
49 servers = await flushAndRunMultipleServers(2)
51 // Get the access tokens
52 await setAccessTokensToServers(servers)
54 // Server 1 and server 2 follow each other
55 await doubleFollow(servers[0], servers[1])
57 // Upload 2 videos on server 2
58 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'My 1st video', description: 'A video on server 2' })
59 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'My 2nd video', description: 'A video on server 2' })
61 // Wait videos propagation, server 2 has transcoding enabled
62 await waitJobs(servers)
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: 'name' })
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: 'name' })
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 res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken })
111 expect(res.body.total).to.equal(2)
113 const blacklistedVideos = res.body.data
114 expect(blacklistedVideos).to.be.an('array')
115 expect(blacklistedVideos.length).to.equal(2)
117 for (const blacklistedVideo of blacklistedVideos) {
118 expect(blacklistedVideo.reason).to.equal('super reason')
119 videoId = blacklistedVideo.video.id
123 it('Should display all the blacklisted videos when applying manual type filter', async function () {
124 const res = await getBlacklistedVideosList({
126 token: servers[0].accessToken,
127 type: VideoBlacklistType.MANUAL
130 expect(res.body.total).to.equal(2)
132 const blacklistedVideos = res.body.data
133 expect(blacklistedVideos).to.be.an('array')
134 expect(blacklistedVideos.length).to.equal(2)
137 it('Should display nothing when applying automatic type filter', async function () {
138 const res = await getBlacklistedVideosList({
140 token: servers[0].accessToken,
141 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
144 expect(res.body.total).to.equal(0)
146 const blacklistedVideos = res.body.data
147 expect(blacklistedVideos).to.be.an('array')
148 expect(blacklistedVideos.length).to.equal(0)
151 it('Should get the correct sort when sorting by descending id', async function () {
152 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-id' })
153 expect(res.body.total).to.equal(2)
155 const blacklistedVideos = res.body.data
156 expect(blacklistedVideos).to.be.an('array')
157 expect(blacklistedVideos.length).to.equal(2)
159 const result = orderBy(res.body.data, [ 'id' ], [ 'desc' ])
161 expect(blacklistedVideos).to.deep.equal(result)
164 it('Should get the correct sort when sorting by descending video name', async function () {
165 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-name' })
166 expect(res.body.total).to.equal(2)
168 const blacklistedVideos = res.body.data
169 expect(blacklistedVideos).to.be.an('array')
170 expect(blacklistedVideos.length).to.equal(2)
172 const result = orderBy(res.body.data, [ 'name' ], [ 'desc' ])
174 expect(blacklistedVideos).to.deep.equal(result)
177 it('Should get the correct sort when sorting by ascending creation date', async function () {
178 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: 'createdAt' })
179 expect(res.body.total).to.equal(2)
181 const blacklistedVideos = res.body.data
182 expect(blacklistedVideos).to.be.an('array')
183 expect(blacklistedVideos.length).to.equal(2)
185 const result = orderBy(res.body.data, [ 'createdAt' ])
187 expect(blacklistedVideos).to.deep.equal(result)
191 describe('When updating blacklisted videos', function () {
192 it('Should change the reason', async function () {
193 await updateVideoBlacklist(servers[0].url, servers[0].accessToken, videoId, 'my super reason updated')
195 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-name' })
196 const video = res.body.data.find(b => b.video.id === videoId)
198 expect(video.reason).to.equal('my super reason updated')
202 describe('When listing my videos', function () {
203 it('Should display blacklisted videos', async function () {
204 await blacklistVideosOnServer(servers[1])
206 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 5)
208 expect(res.body.total).to.equal(2)
209 expect(res.body.data).to.have.lengthOf(2)
211 for (const video of res.body.data) {
212 expect(video.blacklisted).to.be.true
213 expect(video.blacklistedReason).to.equal('super reason')
218 describe('When removing a blacklisted video', function () {
219 let videoToRemove: VideoBlacklist
222 it('Should not have any video in videos list on server 1', async function () {
223 const res = await getVideosList(servers[0].url)
224 expect(res.body.total).to.equal(0)
225 expect(res.body.data).to.be.an('array')
226 expect(res.body.data.length).to.equal(0)
229 it('Should remove a video from the blacklist on server 1', async function () {
230 // Get one video in the blacklist
231 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-name' })
232 videoToRemove = res.body.data[0]
233 blacklist = res.body.data.slice(1)
236 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoToRemove.video.id)
239 it('Should have the ex-blacklisted video in videos list on server 1', async function () {
240 const res = await getVideosList(servers[0].url)
241 expect(res.body.total).to.equal(1)
243 const videos = res.body.data
244 expect(videos).to.be.an('array')
245 expect(videos.length).to.equal(1)
247 expect(videos[0].name).to.equal(videoToRemove.video.name)
248 expect(videos[0].id).to.equal(videoToRemove.video.id)
251 it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () {
252 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-name' })
253 expect(res.body.total).to.equal(1)
255 const videos = res.body.data
256 expect(videos).to.be.an('array')
257 expect(videos.length).to.equal(1)
258 expect(videos).to.deep.equal(blacklist)
262 describe('When blacklisting local videos', function () {
263 let video3UUID: string
264 let video4UUID: string
266 before(async function () {
270 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'Video 3' })
271 video3UUID = res.body.video.uuid
274 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'Video 4' })
275 video4UUID = res.body.video.uuid
278 await waitJobs(servers)
281 it('Should blacklist video 3 and keep it federated', async function () {
284 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, video3UUID, 'super reason', false)
286 await waitJobs(servers)
289 const res = await getVideosList(servers[0].url)
290 expect(res.body.data.find(v => v.uuid === video3UUID)).to.be.undefined
294 const res = await getVideosList(servers[1].url)
295 expect(res.body.data.find(v => v.uuid === video3UUID)).to.not.be.undefined
299 it('Should unfederate the video', async function () {
302 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, video4UUID, 'super reason', true)
304 await waitJobs(servers)
306 for (const server of servers) {
307 const res = await getVideosList(server.url)
308 expect(res.body.data.find(v => v.uuid === video4UUID)).to.be.undefined
312 it('Should have the video unfederated even after an Update AP message', async function () {
315 await updateVideo(servers[0].url, servers[0].accessToken, video4UUID, { description: 'super description' })
317 await waitJobs(servers)
319 for (const server of servers) {
320 const res = await getVideosList(server.url)
321 expect(res.body.data.find(v => v.uuid === video4UUID)).to.be.undefined
325 it('Should have the correct video blacklist unfederate attribute', async function () {
326 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: 'createdAt' })
328 const blacklistedVideos: VideoBlacklist[] = res.body.data
329 const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID)
330 const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID)
332 expect(video3Blacklisted.unfederated).to.be.false
333 expect(video4Blacklisted.unfederated).to.be.true
336 it('Should remove the video from blacklist and refederate the video', async function () {
339 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, video4UUID)
341 await waitJobs(servers)
343 for (const server of servers) {
344 const res = await getVideosList(server.url)
345 expect(res.body.data.find(v => v.uuid === video4UUID)).to.not.be.undefined
351 describe('When auto blacklist videos', function () {
352 let userWithoutFlag: string
353 let userWithFlag: string
354 let channelOfUserWithoutFlag: number
356 before(async function () {
359 killallServers([ servers[0] ])
370 await reRunServer(servers[0], config)
373 const user = { username: 'user_without_flag', password: 'password' }
376 accessToken: servers[0].accessToken,
377 username: user.username,
378 adminFlags: UserAdminFlag.NONE,
379 password: user.password,
383 userWithoutFlag = await userLogin(servers[0], user)
385 const res = await getMyUserInformation(servers[0].url, userWithoutFlag)
386 const body: User = res.body
387 channelOfUserWithoutFlag = body.videoChannels[0].id
391 const user = { username: 'user_with_flag', password: 'password' }
394 accessToken: servers[0].accessToken,
395 username: user.username,
396 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST,
397 password: user.password,
401 userWithFlag = await userLogin(servers[0], user)
404 await waitJobs(servers)
407 it('Should auto blacklist a video on upload', async function () {
408 await uploadVideo(servers[0].url, userWithoutFlag, { name: 'blacklisted' })
410 const res = await getBlacklistedVideosList({
412 token: servers[0].accessToken,
413 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
416 expect(res.body.total).to.equal(1)
417 expect(res.body.data[0].video.name).to.equal('blacklisted')
420 it('Should auto blacklist a video on URL import', async function () {
424 targetUrl: getGoodVideoUrl(),
426 channelId: channelOfUserWithoutFlag
428 await importVideo(servers[0].url, userWithoutFlag, attributes)
430 const res = await getBlacklistedVideosList({
432 token: servers[0].accessToken,
434 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
437 expect(res.body.total).to.equal(2)
438 expect(res.body.data[1].video.name).to.equal('URL import')
441 it('Should auto blacklist a video on torrent import', async function () {
443 magnetUri: getMagnetURI(),
444 name: 'Torrent import',
445 channelId: channelOfUserWithoutFlag
447 await importVideo(servers[0].url, userWithoutFlag, attributes)
449 const res = await getBlacklistedVideosList({
451 token: servers[0].accessToken,
453 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
456 expect(res.body.total).to.equal(3)
457 expect(res.body.data[2].video.name).to.equal('Torrent import')
460 it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () {
461 await uploadVideo(servers[0].url, userWithFlag, { name: 'not blacklisted' })
463 const res = await getBlacklistedVideosList({
465 token: servers[0].accessToken,
466 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
469 expect(res.body.total).to.equal(3)
473 after(async function () {
474 await cleanupTests(servers)