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,
17 setAccessTokensToServers,
21 } from '@shared/extra-utils'
22 import { UserAdminFlag, UserRole, VideoBlacklist, VideoBlacklistType } from '@shared/models'
24 const expect = chai.expect
26 describe('Test video blacklist', function () {
27 let servers: ServerInfo[] = []
29 let command: BlacklistCommand
31 async function blacklistVideosOnServer (server: ServerInfo) {
32 const res = await getVideosList(server.url)
34 const videos = res.body.data
35 for (const video of videos) {
36 await server.blacklistCommand.add({ videoId: video.id, reason: 'super reason' })
40 before(async function () {
44 servers = await flushAndRunMultipleServers(2)
46 // Get the access tokens
47 await setAccessTokensToServers(servers)
49 // Server 1 and server 2 follow each other
50 await doubleFollow(servers[0], servers[1])
52 // Upload 2 videos on server 2
53 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'My 1st video', description: 'A video on server 2' })
54 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'My 2nd video', description: 'A video on server 2' })
56 // Wait videos propagation, server 2 has transcoding enabled
57 await waitJobs(servers)
59 command = servers[0].blacklistCommand
61 // Blacklist the two videos on server 1
62 await blacklistVideosOnServer(servers[0])
65 describe('When listing/searching videos', function () {
67 it('Should not have the video blacklisted in videos list/search on server 1', async function () {
69 const res = await getVideosList(servers[0].url)
71 expect(res.body.total).to.equal(0)
72 expect(res.body.data).to.be.an('array')
73 expect(res.body.data.length).to.equal(0)
77 const body = await servers[0].searchCommand.searchVideos({ search: 'video' })
79 expect(body.total).to.equal(0)
80 expect(body.data).to.be.an('array')
81 expect(body.data.length).to.equal(0)
85 it('Should have the blacklisted video in videos list/search on server 2', async function () {
87 const res = await getVideosList(servers[1].url)
89 expect(res.body.total).to.equal(2)
90 expect(res.body.data).to.be.an('array')
91 expect(res.body.data.length).to.equal(2)
95 const body = await servers[1].searchCommand.searchVideos({ search: 'video' })
97 expect(body.total).to.equal(2)
98 expect(body.data).to.be.an('array')
99 expect(body.data.length).to.equal(2)
104 describe('When listing manually blacklisted videos', function () {
105 it('Should display all the blacklisted videos', async function () {
106 const body = await command.list()
107 expect(body.total).to.equal(2)
109 const blacklistedVideos = body.data
110 expect(blacklistedVideos).to.be.an('array')
111 expect(blacklistedVideos.length).to.equal(2)
113 for (const blacklistedVideo of blacklistedVideos) {
114 expect(blacklistedVideo.reason).to.equal('super reason')
115 videoId = blacklistedVideo.video.id
119 it('Should display all the blacklisted videos when applying manual type filter', async function () {
120 const body = await command.list({ type: VideoBlacklistType.MANUAL })
121 expect(body.total).to.equal(2)
123 const blacklistedVideos = body.data
124 expect(blacklistedVideos).to.be.an('array')
125 expect(blacklistedVideos.length).to.equal(2)
128 it('Should display nothing when applying automatic type filter', async function () {
129 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
130 expect(body.total).to.equal(0)
132 const blacklistedVideos = body.data
133 expect(blacklistedVideos).to.be.an('array')
134 expect(blacklistedVideos.length).to.equal(0)
137 it('Should get the correct sort when sorting by descending id', async function () {
138 const body = await command.list({ sort: '-id' })
139 expect(body.total).to.equal(2)
141 const blacklistedVideos = body.data
142 expect(blacklistedVideos).to.be.an('array')
143 expect(blacklistedVideos.length).to.equal(2)
145 const result = orderBy(body.data, [ 'id' ], [ 'desc' ])
146 expect(blacklistedVideos).to.deep.equal(result)
149 it('Should get the correct sort when sorting by descending video name', async function () {
150 const body = await command.list({ sort: '-name' })
151 expect(body.total).to.equal(2)
153 const blacklistedVideos = body.data
154 expect(blacklistedVideos).to.be.an('array')
155 expect(blacklistedVideos.length).to.equal(2)
157 const result = orderBy(body.data, [ 'name' ], [ 'desc' ])
158 expect(blacklistedVideos).to.deep.equal(result)
161 it('Should get the correct sort when sorting by ascending creation date', async function () {
162 const body = await command.list({ sort: 'createdAt' })
163 expect(body.total).to.equal(2)
165 const blacklistedVideos = body.data
166 expect(blacklistedVideos).to.be.an('array')
167 expect(blacklistedVideos.length).to.equal(2)
169 const result = orderBy(body.data, [ 'createdAt' ])
170 expect(blacklistedVideos).to.deep.equal(result)
174 describe('When updating blacklisted videos', function () {
175 it('Should change the reason', async function () {
176 await command.update({ videoId, reason: 'my super reason updated' })
178 const body = await command.list({ sort: '-name' })
179 const video = body.data.find(b => b.video.id === videoId)
181 expect(video.reason).to.equal('my super reason updated')
185 describe('When listing my videos', function () {
186 it('Should display blacklisted videos', async function () {
187 await blacklistVideosOnServer(servers[1])
189 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 5)
191 expect(res.body.total).to.equal(2)
192 expect(res.body.data).to.have.lengthOf(2)
194 for (const video of res.body.data) {
195 expect(video.blacklisted).to.be.true
196 expect(video.blacklistedReason).to.equal('super reason')
201 describe('When removing a blacklisted video', function () {
202 let videoToRemove: VideoBlacklist
205 it('Should not have any video in videos list on server 1', async function () {
206 const res = await getVideosList(servers[0].url)
207 expect(res.body.total).to.equal(0)
208 expect(res.body.data).to.be.an('array')
209 expect(res.body.data.length).to.equal(0)
212 it('Should remove a video from the blacklist on server 1', async function () {
213 // Get one video in the blacklist
214 const body = await command.list({ sort: '-name' })
215 videoToRemove = body.data[0]
216 blacklist = body.data.slice(1)
219 await command.remove({ videoId: videoToRemove.video.id })
222 it('Should have the ex-blacklisted video in videos list on server 1', async function () {
223 const res = await getVideosList(servers[0].url)
224 expect(res.body.total).to.equal(1)
226 const videos = res.body.data
227 expect(videos).to.be.an('array')
228 expect(videos.length).to.equal(1)
230 expect(videos[0].name).to.equal(videoToRemove.video.name)
231 expect(videos[0].id).to.equal(videoToRemove.video.id)
234 it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () {
235 const body = await command.list({ sort: '-name' })
236 expect(body.total).to.equal(1)
238 const videos = body.data
239 expect(videos).to.be.an('array')
240 expect(videos.length).to.equal(1)
241 expect(videos).to.deep.equal(blacklist)
245 describe('When blacklisting local videos', function () {
246 let video3UUID: string
247 let video4UUID: string
249 before(async function () {
253 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'Video 3' })
254 video3UUID = res.body.video.uuid
257 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'Video 4' })
258 video4UUID = res.body.video.uuid
261 await waitJobs(servers)
264 it('Should blacklist video 3 and keep it federated', async function () {
267 await command.add({ videoId: video3UUID, reason: 'super reason', unfederate: false })
269 await waitJobs(servers)
272 const res = await getVideosList(servers[0].url)
273 expect(res.body.data.find(v => v.uuid === video3UUID)).to.be.undefined
277 const res = await getVideosList(servers[1].url)
278 expect(res.body.data.find(v => v.uuid === video3UUID)).to.not.be.undefined
282 it('Should unfederate the video', async function () {
285 await command.add({ videoId: video4UUID, reason: 'super reason', unfederate: true })
287 await waitJobs(servers)
289 for (const server of servers) {
290 const res = await getVideosList(server.url)
291 expect(res.body.data.find(v => v.uuid === video4UUID)).to.be.undefined
295 it('Should have the video unfederated even after an Update AP message', async function () {
298 await updateVideo(servers[0].url, servers[0].accessToken, video4UUID, { description: 'super description' })
300 await waitJobs(servers)
302 for (const server of servers) {
303 const res = await getVideosList(server.url)
304 expect(res.body.data.find(v => v.uuid === video4UUID)).to.be.undefined
308 it('Should have the correct video blacklist unfederate attribute', async function () {
309 const body = await command.list({ sort: 'createdAt' })
311 const blacklistedVideos = body.data
312 const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID)
313 const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID)
315 expect(video3Blacklisted.unfederated).to.be.false
316 expect(video4Blacklisted.unfederated).to.be.true
319 it('Should remove the video from blacklist and refederate the video', async function () {
322 await command.remove({ videoId: video4UUID })
324 await waitJobs(servers)
326 for (const server of servers) {
327 const res = await getVideosList(server.url)
328 expect(res.body.data.find(v => v.uuid === video4UUID)).to.not.be.undefined
334 describe('When auto blacklist videos', function () {
335 let userWithoutFlag: string
336 let userWithFlag: string
337 let channelOfUserWithoutFlag: number
339 before(async function () {
342 await killallServers([ servers[0] ])
353 await reRunServer(servers[0], config)
356 const user = { username: 'user_without_flag', password: 'password' }
357 await servers[0].usersCommand.create({
358 username: user.username,
359 adminFlags: UserAdminFlag.NONE,
360 password: user.password,
364 userWithoutFlag = await servers[0].loginCommand.getAccessToken(user)
366 const { videoChannels } = await servers[0].usersCommand.getMyInfo({ token: userWithoutFlag })
367 channelOfUserWithoutFlag = videoChannels[0].id
371 const user = { username: 'user_with_flag', password: 'password' }
372 await servers[0].usersCommand.create({
373 username: user.username,
374 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST,
375 password: user.password,
379 userWithFlag = await servers[0].loginCommand.getAccessToken(user)
382 await waitJobs(servers)
385 it('Should auto blacklist a video on upload', async function () {
386 await uploadVideo(servers[0].url, userWithoutFlag, { name: 'blacklisted' })
388 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
389 expect(body.total).to.equal(1)
390 expect(body.data[0].video.name).to.equal('blacklisted')
393 it('Should auto blacklist a video on URL import', async function () {
397 targetUrl: ImportsCommand.getGoodVideoUrl(),
399 channelId: channelOfUserWithoutFlag
401 await servers[0].importsCommand.importVideo({ token: userWithoutFlag, attributes })
403 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
404 expect(body.total).to.equal(2)
405 expect(body.data[1].video.name).to.equal('URL import')
408 it('Should auto blacklist a video on torrent import', async function () {
410 magnetUri: ImportsCommand.getMagnetURI(),
411 name: 'Torrent import',
412 channelId: channelOfUserWithoutFlag
414 await servers[0].importsCommand.importVideo({ token: userWithoutFlag, attributes })
416 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
417 expect(body.total).to.equal(3)
418 expect(body.data[2].video.name).to.equal('Torrent import')
421 it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () {
422 await uploadVideo(servers[0].url, userWithFlag, { name: 'not blacklisted' })
424 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
425 expect(body.total).to.equal(3)
429 after(async function () {
430 await cleanupTests(servers)