1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { orderBy } from 'lodash'
6 import { FIXTURE_URLS } from '@server/tests/shared'
7 import { UserAdminFlag, UserRole, VideoBlacklist, VideoBlacklistType } from '@shared/models'
11 createMultipleServers,
15 setAccessTokensToServers,
17 } from '@shared/server-commands'
19 const expect = chai.expect
21 describe('Test video blacklist', function () {
22 let servers: PeerTubeServer[] = []
24 let command: BlacklistCommand
26 async function blacklistVideosOnServer (server: PeerTubeServer) {
27 const { data } = await server.videos.list()
29 for (const video of data) {
30 await server.blacklist.add({ videoId: video.id, reason: 'super reason' })
34 before(async function () {
38 servers = await createMultipleServers(2)
40 // Get the access tokens
41 await setAccessTokensToServers(servers)
43 // Server 1 and server 2 follow each other
44 await doubleFollow(servers[0], servers[1])
46 // Upload 2 videos on server 2
47 await servers[1].videos.upload({ attributes: { name: 'My 1st video', description: 'A video on server 2' } })
48 await servers[1].videos.upload({ attributes: { name: 'My 2nd video', description: 'A video on server 2' } })
50 // Wait videos propagation, server 2 has transcoding enabled
51 await waitJobs(servers)
53 command = servers[0].blacklist
55 // Blacklist the two videos on server 1
56 await blacklistVideosOnServer(servers[0])
59 describe('When listing/searching videos', function () {
61 it('Should not have the video blacklisted in videos list/search on server 1', async function () {
63 const { total, data } = await servers[0].videos.list()
65 expect(total).to.equal(0)
66 expect(data).to.be.an('array')
67 expect(data.length).to.equal(0)
71 const body = await servers[0].search.searchVideos({ search: 'video' })
73 expect(body.total).to.equal(0)
74 expect(body.data).to.be.an('array')
75 expect(body.data.length).to.equal(0)
79 it('Should have the blacklisted video in videos list/search on server 2', async function () {
81 const { total, data } = await servers[1].videos.list()
83 expect(total).to.equal(2)
84 expect(data).to.be.an('array')
85 expect(data.length).to.equal(2)
89 const body = await servers[1].search.searchVideos({ search: 'video' })
91 expect(body.total).to.equal(2)
92 expect(body.data).to.be.an('array')
93 expect(body.data.length).to.equal(2)
98 describe('When listing manually blacklisted videos', function () {
99 it('Should display all the blacklisted videos', async function () {
100 const body = await command.list()
101 expect(body.total).to.equal(2)
103 const blacklistedVideos = body.data
104 expect(blacklistedVideos).to.be.an('array')
105 expect(blacklistedVideos.length).to.equal(2)
107 for (const blacklistedVideo of blacklistedVideos) {
108 expect(blacklistedVideo.reason).to.equal('super reason')
109 videoId = blacklistedVideo.video.id
113 it('Should display all the blacklisted videos when applying manual type filter', async function () {
114 const body = await command.list({ type: VideoBlacklistType.MANUAL })
115 expect(body.total).to.equal(2)
117 const blacklistedVideos = body.data
118 expect(blacklistedVideos).to.be.an('array')
119 expect(blacklistedVideos.length).to.equal(2)
122 it('Should display nothing when applying automatic type filter', async function () {
123 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
124 expect(body.total).to.equal(0)
126 const blacklistedVideos = body.data
127 expect(blacklistedVideos).to.be.an('array')
128 expect(blacklistedVideos.length).to.equal(0)
131 it('Should get the correct sort when sorting by descending id', async function () {
132 const body = await command.list({ sort: '-id' })
133 expect(body.total).to.equal(2)
135 const blacklistedVideos = body.data
136 expect(blacklistedVideos).to.be.an('array')
137 expect(blacklistedVideos.length).to.equal(2)
139 const result = orderBy(body.data, [ 'id' ], [ 'desc' ])
140 expect(blacklistedVideos).to.deep.equal(result)
143 it('Should get the correct sort when sorting by descending video name', async function () {
144 const body = await command.list({ sort: '-name' })
145 expect(body.total).to.equal(2)
147 const blacklistedVideos = body.data
148 expect(blacklistedVideos).to.be.an('array')
149 expect(blacklistedVideos.length).to.equal(2)
151 const result = orderBy(body.data, [ 'name' ], [ 'desc' ])
152 expect(blacklistedVideos).to.deep.equal(result)
155 it('Should get the correct sort when sorting by ascending creation date', async function () {
156 const body = await command.list({ sort: 'createdAt' })
157 expect(body.total).to.equal(2)
159 const blacklistedVideos = body.data
160 expect(blacklistedVideos).to.be.an('array')
161 expect(blacklistedVideos.length).to.equal(2)
163 const result = orderBy(body.data, [ 'createdAt' ])
164 expect(blacklistedVideos).to.deep.equal(result)
168 describe('When updating blacklisted videos', function () {
169 it('Should change the reason', async function () {
170 await command.update({ videoId, reason: 'my super reason updated' })
172 const body = await command.list({ sort: '-name' })
173 const video = body.data.find(b => b.video.id === videoId)
175 expect(video.reason).to.equal('my super reason updated')
179 describe('When listing my videos', function () {
180 it('Should display blacklisted videos', async function () {
181 await blacklistVideosOnServer(servers[1])
183 const { total, data } = await servers[1].videos.listMyVideos()
185 expect(total).to.equal(2)
186 expect(data).to.have.lengthOf(2)
188 for (const video of data) {
189 expect(video.blacklisted).to.be.true
190 expect(video.blacklistedReason).to.equal('super reason')
195 describe('When removing a blacklisted video', function () {
196 let videoToRemove: VideoBlacklist
199 it('Should not have any video in videos list on server 1', async function () {
200 const { total, data } = await servers[0].videos.list()
201 expect(total).to.equal(0)
202 expect(data).to.be.an('array')
203 expect(data.length).to.equal(0)
206 it('Should remove a video from the blacklist on server 1', async function () {
207 // Get one video in the blacklist
208 const body = await command.list({ sort: '-name' })
209 videoToRemove = body.data[0]
210 blacklist = body.data.slice(1)
213 await command.remove({ videoId: videoToRemove.video.id })
216 it('Should have the ex-blacklisted video in videos list on server 1', async function () {
217 const { total, data } = await servers[0].videos.list()
218 expect(total).to.equal(1)
220 expect(data).to.be.an('array')
221 expect(data.length).to.equal(1)
223 expect(data[0].name).to.equal(videoToRemove.video.name)
224 expect(data[0].id).to.equal(videoToRemove.video.id)
227 it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () {
228 const body = await command.list({ sort: '-name' })
229 expect(body.total).to.equal(1)
231 const videos = body.data
232 expect(videos).to.be.an('array')
233 expect(videos.length).to.equal(1)
234 expect(videos).to.deep.equal(blacklist)
238 describe('When blacklisting local videos', function () {
239 let video3UUID: string
240 let video4UUID: string
242 before(async function () {
246 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 3' } })
250 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 4' } })
254 await waitJobs(servers)
257 it('Should blacklist video 3 and keep it federated', async function () {
260 await command.add({ videoId: video3UUID, reason: 'super reason', unfederate: false })
262 await waitJobs(servers)
265 const { data } = await servers[0].videos.list()
266 expect(data.find(v => v.uuid === video3UUID)).to.be.undefined
270 const { data } = await servers[1].videos.list()
271 expect(data.find(v => v.uuid === video3UUID)).to.not.be.undefined
275 it('Should unfederate the video', async function () {
278 await command.add({ videoId: video4UUID, reason: 'super reason', unfederate: true })
280 await waitJobs(servers)
282 for (const server of servers) {
283 const { data } = await server.videos.list()
284 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
288 it('Should have the video unfederated even after an Update AP message', async function () {
291 await servers[0].videos.update({ id: video4UUID, attributes: { description: 'super description' } })
293 await waitJobs(servers)
295 for (const server of servers) {
296 const { data } = await server.videos.list()
297 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
301 it('Should have the correct video blacklist unfederate attribute', async function () {
302 const body = await command.list({ sort: 'createdAt' })
304 const blacklistedVideos = body.data
305 const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID)
306 const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID)
308 expect(video3Blacklisted.unfederated).to.be.false
309 expect(video4Blacklisted.unfederated).to.be.true
312 it('Should remove the video from blacklist and refederate the video', async function () {
315 await command.remove({ videoId: video4UUID })
317 await waitJobs(servers)
319 for (const server of servers) {
320 const { data } = await server.videos.list()
321 expect(data.find(v => v.uuid === video4UUID)).to.not.be.undefined
327 describe('When auto blacklist videos', function () {
328 let userWithoutFlag: string
329 let userWithFlag: string
330 let channelOfUserWithoutFlag: number
332 before(async function () {
335 await killallServers([ servers[0] ])
346 await servers[0].run(config)
349 const user = { username: 'user_without_flag', password: 'password' }
350 await servers[0].users.create({
351 username: user.username,
352 adminFlags: UserAdminFlag.NONE,
353 password: user.password,
357 userWithoutFlag = await servers[0].login.getAccessToken(user)
359 const { videoChannels } = await servers[0].users.getMyInfo({ token: userWithoutFlag })
360 channelOfUserWithoutFlag = videoChannels[0].id
364 const user = { username: 'user_with_flag', password: 'password' }
365 await servers[0].users.create({
366 username: user.username,
367 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST,
368 password: user.password,
372 userWithFlag = await servers[0].login.getAccessToken(user)
375 await waitJobs(servers)
378 it('Should auto blacklist a video on upload', async function () {
379 await servers[0].videos.upload({ token: userWithoutFlag, attributes: { name: 'blacklisted' } })
381 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
382 expect(body.total).to.equal(1)
383 expect(body.data[0].video.name).to.equal('blacklisted')
386 it('Should auto blacklist a video on URL import', async function () {
390 targetUrl: FIXTURE_URLS.goodVideo,
392 channelId: channelOfUserWithoutFlag
394 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
396 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
397 expect(body.total).to.equal(2)
398 expect(body.data[1].video.name).to.equal('URL import')
401 it('Should auto blacklist a video on torrent import', async function () {
403 magnetUri: FIXTURE_URLS.magnet,
404 name: 'Torrent import',
405 channelId: channelOfUserWithoutFlag
407 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
409 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
410 expect(body.total).to.equal(3)
411 expect(body.data[2].video.name).to.equal('Torrent import')
414 it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () {
415 await servers[0].videos.upload({ token: userWithFlag, attributes: { name: 'not blacklisted' } })
417 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
418 expect(body.total).to.equal(3)
422 after(async function () {
423 await cleanupTests(servers)