1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
4 import { orderBy } from 'lodash'
7 addVideoToBlacklist, cleanupTests,
9 flushAndRunMultipleServers,
10 getBlacklistedVideosList,
14 removeVideoFromBlacklist,
18 setAccessTokensToServers,
23 } from '../../../../shared/extra-utils/index'
24 import { doubleFollow } from '../../../../shared/extra-utils/server/follows'
25 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
26 import { VideoBlacklist, VideoBlacklistType } from '../../../../shared/models/videos'
27 import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
28 import { UserRole } from '../../../../shared/models/users'
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 (let 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 res = await searchVideo(servers[ 0 ].url, 'name')
82 expect(res.body.total).to.equal(0)
83 expect(res.body.data).to.be.an('array')
84 expect(res.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 res = await searchVideo(servers[ 1 ].url, 'video')
100 expect(res.body.total).to.equal(2)
101 expect(res.body.data).to.be.an('array')
102 expect(res.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({
125 url: servers[ 0 ].url,
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({
139 url: servers[ 0 ].url,
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
355 before(async function () {
358 killallServers([ servers[0] ])
369 await reRunServer(servers[0], config)
372 const user = { username: 'user_without_flag', password: 'password' }
374 url: servers[ 0 ].url,
375 accessToken: servers[ 0 ].accessToken,
376 username: user.username,
377 adminFlags: UserAdminFlag.NONE,
378 password: user.password,
382 userWithoutFlag = await userLogin(servers[0], user)
386 const user = { username: 'user_with_flag', password: 'password' }
388 url: servers[ 0 ].url,
389 accessToken: servers[ 0 ].accessToken,
390 username: user.username,
391 adminFlags: UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST,
392 password: user.password,
396 userWithFlag = await userLogin(servers[0], user)
399 await waitJobs(servers)
402 it('Should auto blacklist a video', async function () {
403 await uploadVideo(servers[0].url, userWithoutFlag, { name: 'blacklisted' })
405 const res = await getBlacklistedVideosList({
406 url: servers[ 0 ].url,
407 token: servers[ 0 ].accessToken,
408 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
411 expect(res.body.total).to.equal(1)
412 expect(res.body.data[0].video.name).to.equal('blacklisted')
415 it('Should not auto blacklist a video', async function () {
416 await uploadVideo(servers[0].url, userWithFlag, { name: 'not blacklisted' })
418 const res = await getBlacklistedVideosList({
419 url: servers[ 0 ].url,
420 token: servers[ 0 ].accessToken,
421 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
424 expect(res.body.total).to.equal(1)
428 after(async function () {
429 await cleanupTests(servers)