1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
4 import { orderBy } from 'lodash'
10 flushAndRunMultipleServers,
11 getBlacklistedVideosList, getMyUserInformation,
15 removeVideoFromBlacklist,
19 setAccessTokensToServers,
20 setDefaultVideoChannel,
25 } from '../../../../shared/extra-utils/index'
26 import { doubleFollow } from '../../../../shared/extra-utils/server/follows'
27 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
28 import { VideoBlacklist, VideoBlacklistType } from '../../../../shared/models/videos'
29 import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
30 import { User, UserRole, UserUpdateMe } from '../../../../shared/models/users'
31 import { getMagnetURI, getYoutubeVideoUrl, importVideo } from '../../../../shared/extra-utils/videos/video-imports'
33 const expect = chai.expect
35 describe('Test video blacklist', function () {
36 let servers: ServerInfo[] = []
39 async function blacklistVideosOnServer (server: ServerInfo) {
40 const res = await getVideosList(server.url)
42 const videos = res.body.data
43 for (let video of videos) {
44 await addVideoToBlacklist(server.url, server.accessToken, video.id, 'super reason')
48 before(async function () {
52 servers = await flushAndRunMultipleServers(2)
54 // Get the access tokens
55 await setAccessTokensToServers(servers)
57 // Server 1 and server 2 follow each other
58 await doubleFollow(servers[0], servers[1])
60 // Upload 2 videos on server 2
61 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'My 1st video', description: 'A video on server 2' })
62 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'My 2nd video', description: 'A video on server 2' })
64 // Wait videos propagation, server 2 has transcoding enabled
65 await waitJobs(servers)
67 // Blacklist the two videos on server 1
68 await blacklistVideosOnServer(servers[0])
71 describe('When listing/searching videos', function () {
73 it('Should not have the video blacklisted in videos list/search on server 1', async function () {
75 const res = await getVideosList(servers[ 0 ].url)
77 expect(res.body.total).to.equal(0)
78 expect(res.body.data).to.be.an('array')
79 expect(res.body.data.length).to.equal(0)
83 const res = await searchVideo(servers[ 0 ].url, 'name')
85 expect(res.body.total).to.equal(0)
86 expect(res.body.data).to.be.an('array')
87 expect(res.body.data.length).to.equal(0)
91 it('Should have the blacklisted video in videos list/search on server 2', async function () {
93 const res = await getVideosList(servers[ 1 ].url)
95 expect(res.body.total).to.equal(2)
96 expect(res.body.data).to.be.an('array')
97 expect(res.body.data.length).to.equal(2)
101 const res = await searchVideo(servers[ 1 ].url, 'video')
103 expect(res.body.total).to.equal(2)
104 expect(res.body.data).to.be.an('array')
105 expect(res.body.data.length).to.equal(2)
110 describe('When listing manually blacklisted videos', function () {
111 it('Should display all the blacklisted videos', async function () {
112 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken })
114 expect(res.body.total).to.equal(2)
116 const blacklistedVideos = res.body.data
117 expect(blacklistedVideos).to.be.an('array')
118 expect(blacklistedVideos.length).to.equal(2)
120 for (const blacklistedVideo of blacklistedVideos) {
121 expect(blacklistedVideo.reason).to.equal('super reason')
122 videoId = blacklistedVideo.video.id
126 it('Should display all the blacklisted videos when applying manual type filter', async function () {
127 const res = await getBlacklistedVideosList({
128 url: servers[ 0 ].url,
129 token: servers[ 0 ].accessToken,
130 type: VideoBlacklistType.MANUAL
133 expect(res.body.total).to.equal(2)
135 const blacklistedVideos = res.body.data
136 expect(blacklistedVideos).to.be.an('array')
137 expect(blacklistedVideos.length).to.equal(2)
140 it('Should display nothing when applying automatic type filter', async function () {
141 const res = await getBlacklistedVideosList({
142 url: servers[ 0 ].url,
143 token: servers[ 0 ].accessToken,
144 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
147 expect(res.body.total).to.equal(0)
149 const blacklistedVideos = res.body.data
150 expect(blacklistedVideos).to.be.an('array')
151 expect(blacklistedVideos.length).to.equal(0)
154 it('Should get the correct sort when sorting by descending id', async function () {
155 const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: '-id' })
156 expect(res.body.total).to.equal(2)
158 const blacklistedVideos = res.body.data
159 expect(blacklistedVideos).to.be.an('array')
160 expect(blacklistedVideos.length).to.equal(2)
162 const result = orderBy(res.body.data, [ 'id' ], [ 'desc' ])
164 expect(blacklistedVideos).to.deep.equal(result)
167 it('Should get the correct sort when sorting by descending video name', async function () {
168 const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: '-name' })
169 expect(res.body.total).to.equal(2)
171 const blacklistedVideos = res.body.data
172 expect(blacklistedVideos).to.be.an('array')
173 expect(blacklistedVideos.length).to.equal(2)
175 const result = orderBy(res.body.data, [ 'name' ], [ 'desc' ])
177 expect(blacklistedVideos).to.deep.equal(result)
180 it('Should get the correct sort when sorting by ascending creation date', async function () {
181 const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: 'createdAt' })
182 expect(res.body.total).to.equal(2)
184 const blacklistedVideos = res.body.data
185 expect(blacklistedVideos).to.be.an('array')
186 expect(blacklistedVideos.length).to.equal(2)
188 const result = orderBy(res.body.data, [ 'createdAt' ])
190 expect(blacklistedVideos).to.deep.equal(result)
194 describe('When updating blacklisted videos', function () {
195 it('Should change the reason', async function () {
196 await updateVideoBlacklist(servers[0].url, servers[0].accessToken, videoId, 'my super reason updated')
198 const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: '-name' })
199 const video = res.body.data.find(b => b.video.id === videoId)
201 expect(video.reason).to.equal('my super reason updated')
205 describe('When listing my videos', function () {
206 it('Should display blacklisted videos', async function () {
207 await blacklistVideosOnServer(servers[1])
209 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 5)
211 expect(res.body.total).to.equal(2)
212 expect(res.body.data).to.have.lengthOf(2)
214 for (const video of res.body.data) {
215 expect(video.blacklisted).to.be.true
216 expect(video.blacklistedReason).to.equal('super reason')
221 describe('When removing a blacklisted video', function () {
222 let videoToRemove: VideoBlacklist
225 it('Should not have any video in videos list on server 1', async function () {
226 const res = await getVideosList(servers[0].url)
227 expect(res.body.total).to.equal(0)
228 expect(res.body.data).to.be.an('array')
229 expect(res.body.data.length).to.equal(0)
232 it('Should remove a video from the blacklist on server 1', async function () {
233 // Get one video in the blacklist
234 const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: '-name' })
235 videoToRemove = res.body.data[0]
236 blacklist = res.body.data.slice(1)
239 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoToRemove.video.id)
242 it('Should have the ex-blacklisted video in videos list on server 1', async function () {
243 const res = await getVideosList(servers[0].url)
244 expect(res.body.total).to.equal(1)
246 const videos = res.body.data
247 expect(videos).to.be.an('array')
248 expect(videos.length).to.equal(1)
250 expect(videos[0].name).to.equal(videoToRemove.video.name)
251 expect(videos[0].id).to.equal(videoToRemove.video.id)
254 it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () {
255 const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: '-name' })
256 expect(res.body.total).to.equal(1)
258 const videos = res.body.data
259 expect(videos).to.be.an('array')
260 expect(videos.length).to.equal(1)
261 expect(videos).to.deep.equal(blacklist)
265 describe('When blacklisting local videos', function () {
266 let video3UUID: string
267 let video4UUID: string
269 before(async function () {
273 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'Video 3' })
274 video3UUID = res.body.video.uuid
277 const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'Video 4' })
278 video4UUID = res.body.video.uuid
281 await waitJobs(servers)
284 it('Should blacklist video 3 and keep it federated', async function () {
287 await addVideoToBlacklist(servers[ 0 ].url, servers[ 0 ].accessToken, video3UUID, 'super reason', false)
289 await waitJobs(servers)
292 const res = await getVideosList(servers[ 0 ].url)
293 expect(res.body.data.find(v => v.uuid === video3UUID)).to.be.undefined
297 const res = await getVideosList(servers[ 1 ].url)
298 expect(res.body.data.find(v => v.uuid === video3UUID)).to.not.be.undefined
302 it('Should unfederate the video', async function () {
305 await addVideoToBlacklist(servers[ 0 ].url, servers[ 0 ].accessToken, video4UUID, 'super reason', true)
307 await waitJobs(servers)
309 for (const server of servers) {
310 const res = await getVideosList(server.url)
311 expect(res.body.data.find(v => v.uuid === video4UUID)).to.be.undefined
315 it('Should have the video unfederated even after an Update AP message', async function () {
318 await updateVideo(servers[ 0 ].url, servers[ 0 ].accessToken, video4UUID, { description: 'super description' })
320 await waitJobs(servers)
322 for (const server of servers) {
323 const res = await getVideosList(server.url)
324 expect(res.body.data.find(v => v.uuid === video4UUID)).to.be.undefined
328 it('Should have the correct video blacklist unfederate attribute', async function () {
329 const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: 'createdAt' })
331 const blacklistedVideos: VideoBlacklist[] = res.body.data
332 const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID)
333 const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID)
335 expect(video3Blacklisted.unfederated).to.be.false
336 expect(video4Blacklisted.unfederated).to.be.true
339 it('Should remove the video from blacklist and refederate the video', async function () {
342 await removeVideoFromBlacklist(servers[ 0 ].url, servers[ 0 ].accessToken, video4UUID)
344 await waitJobs(servers)
346 for (const server of servers) {
347 const res = await getVideosList(server.url)
348 expect(res.body.data.find(v => v.uuid === video4UUID)).to.not.be.undefined
354 describe('When auto blacklist videos', function () {
355 let userWithoutFlag: string
356 let userWithFlag: string
357 let channelOfUserWithoutFlag: number
359 before(async function () {
362 killallServers([ servers[0] ])
373 await reRunServer(servers[0], config)
376 const user = { username: 'user_without_flag', password: 'password' }
378 url: servers[ 0 ].url,
379 accessToken: servers[ 0 ].accessToken,
380 username: user.username,
381 adminFlags: UserAdminFlag.NONE,
382 password: user.password,
386 userWithoutFlag = await userLogin(servers[0], user)
388 const res = await getMyUserInformation(servers[0].url, userWithoutFlag)
389 const body: User = res.body
390 channelOfUserWithoutFlag = body.videoChannels[0].id
394 const user = { username: 'user_with_flag', password: 'password' }
396 url: servers[ 0 ].url,
397 accessToken: servers[ 0 ].accessToken,
398 username: user.username,
399 adminFlags: UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST,
400 password: user.password,
404 userWithFlag = await userLogin(servers[0], user)
407 await waitJobs(servers)
410 it('Should auto blacklist a video on upload', async function () {
411 await uploadVideo(servers[0].url, userWithoutFlag, { name: 'blacklisted' })
413 const res = await getBlacklistedVideosList({
414 url: servers[ 0 ].url,
415 token: servers[ 0 ].accessToken,
416 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
419 expect(res.body.total).to.equal(1)
420 expect(res.body.data[0].video.name).to.equal('blacklisted')
423 it('Should auto blacklist a video on URL import', async function () {
425 targetUrl: getYoutubeVideoUrl(),
427 channelId: channelOfUserWithoutFlag
429 await importVideo(servers[ 0 ].url, userWithoutFlag, attributes)
431 const res = await getBlacklistedVideosList({
432 url: servers[ 0 ].url,
433 token: servers[ 0 ].accessToken,
435 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
438 expect(res.body.total).to.equal(2)
439 expect(res.body.data[1].video.name).to.equal('URL import')
442 it('Should auto blacklist a video on torrent import', async function () {
444 magnetUri: getMagnetURI(),
445 name: 'Torrent import',
446 channelId: channelOfUserWithoutFlag
448 await importVideo(servers[ 0 ].url, userWithoutFlag, attributes)
450 const res = await getBlacklistedVideosList({
451 url: servers[ 0 ].url,
452 token: servers[ 0 ].accessToken,
454 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
457 expect(res.body.total).to.equal(3)
458 expect(res.body.data[2].video.name).to.equal('Torrent import')
461 it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () {
462 await uploadVideo(servers[0].url, userWithFlag, { name: 'not blacklisted' })
464 const res = await getBlacklistedVideosList({
465 url: servers[ 0 ].url,
466 token: servers[ 0 ].accessToken,
467 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
470 expect(res.body.total).to.equal(3)
474 after(async function () {
475 await cleanupTests(servers)