1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { FIXTURE_URLS } from '@server/tests/shared'
5 import { sortObjectComparator } from '@shared/core-utils'
6 import { UserAdminFlag, UserRole, VideoBlacklist, VideoBlacklistType } from '@shared/models'
10 createMultipleServers,
14 setAccessTokensToServers,
15 setDefaultChannelAvatar,
17 } from '@shared/server-commands'
19 describe('Test video blacklist', function () {
20 let servers: PeerTubeServer[] = []
22 let command: BlacklistCommand
24 async function blacklistVideosOnServer (server: PeerTubeServer) {
25 const { data } = await server.videos.list()
27 for (const video of data) {
28 await server.blacklist.add({ videoId: video.id, reason: 'super reason' })
32 before(async function () {
36 servers = await createMultipleServers(2)
38 // Get the access tokens
39 await setAccessTokensToServers(servers)
41 // Server 1 and server 2 follow each other
42 await doubleFollow(servers[0], servers[1])
43 await setDefaultChannelAvatar(servers[0])
45 // Upload 2 videos on server 2
46 await servers[1].videos.upload({ attributes: { name: 'My 1st video', description: 'A video on server 2' } })
47 await servers[1].videos.upload({ attributes: { name: 'My 2nd video', description: 'A video on server 2' } })
49 // Wait videos propagation, server 2 has transcoding enabled
50 await waitJobs(servers)
52 command = servers[0].blacklist
54 // Blacklist the two videos on server 1
55 await blacklistVideosOnServer(servers[0])
58 describe('When listing/searching videos', function () {
60 it('Should not have the video blacklisted in videos list/search on server 1', async function () {
62 const { total, data } = await servers[0].videos.list()
64 expect(total).to.equal(0)
65 expect(data).to.be.an('array')
66 expect(data.length).to.equal(0)
70 const body = await servers[0].search.searchVideos({ search: 'video' })
72 expect(body.total).to.equal(0)
73 expect(body.data).to.be.an('array')
74 expect(body.data.length).to.equal(0)
78 it('Should have the blacklisted video in videos list/search on server 2', async function () {
80 const { total, data } = await servers[1].videos.list()
82 expect(total).to.equal(2)
83 expect(data).to.be.an('array')
84 expect(data.length).to.equal(2)
88 const body = await servers[1].search.searchVideos({ search: 'video' })
90 expect(body.total).to.equal(2)
91 expect(body.data).to.be.an('array')
92 expect(body.data.length).to.equal(2)
97 describe('When listing manually blacklisted videos', function () {
98 it('Should display all the blacklisted videos', async function () {
99 const body = await command.list()
100 expect(body.total).to.equal(2)
102 const blacklistedVideos = body.data
103 expect(blacklistedVideos).to.be.an('array')
104 expect(blacklistedVideos.length).to.equal(2)
106 for (const blacklistedVideo of blacklistedVideos) {
107 expect(blacklistedVideo.reason).to.equal('super reason')
108 videoId = blacklistedVideo.video.id
112 it('Should display all the blacklisted videos when applying manual type filter', async function () {
113 const body = await command.list({ type: VideoBlacklistType.MANUAL })
114 expect(body.total).to.equal(2)
116 const blacklistedVideos = body.data
117 expect(blacklistedVideos).to.be.an('array')
118 expect(blacklistedVideos.length).to.equal(2)
121 it('Should display nothing when applying automatic type filter', async function () {
122 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
123 expect(body.total).to.equal(0)
125 const blacklistedVideos = body.data
126 expect(blacklistedVideos).to.be.an('array')
127 expect(blacklistedVideos.length).to.equal(0)
130 it('Should get the correct sort when sorting by descending id', async function () {
131 const body = await command.list({ sort: '-id' })
132 expect(body.total).to.equal(2)
134 const blacklistedVideos = body.data
135 expect(blacklistedVideos).to.be.an('array')
136 expect(blacklistedVideos.length).to.equal(2)
138 const result = [ ...body.data ].sort(sortObjectComparator('id', 'desc'))
139 expect(blacklistedVideos).to.deep.equal(result)
142 it('Should get the correct sort when sorting by descending video name', async function () {
143 const body = await command.list({ sort: '-name' })
144 expect(body.total).to.equal(2)
146 const blacklistedVideos = body.data
147 expect(blacklistedVideos).to.be.an('array')
148 expect(blacklistedVideos.length).to.equal(2)
150 const result = [ ...body.data ].sort(sortObjectComparator('name', 'desc'))
151 expect(blacklistedVideos).to.deep.equal(result)
154 it('Should get the correct sort when sorting by ascending creation date', async function () {
155 const body = await command.list({ sort: 'createdAt' })
156 expect(body.total).to.equal(2)
158 const blacklistedVideos = body.data
159 expect(blacklistedVideos).to.be.an('array')
160 expect(blacklistedVideos.length).to.equal(2)
162 const result = [ ...body.data ].sort(sortObjectComparator('createdAt', 'asc'))
163 expect(blacklistedVideos).to.deep.equal(result)
167 describe('When updating blacklisted videos', function () {
168 it('Should change the reason', async function () {
169 await command.update({ videoId, reason: 'my super reason updated' })
171 const body = await command.list({ sort: '-name' })
172 const video = body.data.find(b => b.video.id === videoId)
174 expect(video.reason).to.equal('my super reason updated')
178 describe('When listing my videos', function () {
179 it('Should display blacklisted videos', async function () {
180 await blacklistVideosOnServer(servers[1])
182 const { total, data } = await servers[1].videos.listMyVideos()
184 expect(total).to.equal(2)
185 expect(data).to.have.lengthOf(2)
187 for (const video of data) {
188 expect(video.blacklisted).to.be.true
189 expect(video.blacklistedReason).to.equal('super reason')
194 describe('When removing a blacklisted video', function () {
195 let videoToRemove: VideoBlacklist
198 it('Should not have any video in videos list on server 1', async function () {
199 const { total, data } = await servers[0].videos.list()
200 expect(total).to.equal(0)
201 expect(data).to.be.an('array')
202 expect(data.length).to.equal(0)
205 it('Should remove a video from the blacklist on server 1', async function () {
206 // Get one video in the blacklist
207 const body = await command.list({ sort: '-name' })
208 videoToRemove = body.data[0]
209 blacklist = body.data.slice(1)
212 await command.remove({ videoId: videoToRemove.video.id })
215 it('Should have the ex-blacklisted video in videos list on server 1', async function () {
216 const { total, data } = await servers[0].videos.list()
217 expect(total).to.equal(1)
219 expect(data).to.be.an('array')
220 expect(data.length).to.equal(1)
222 expect(data[0].name).to.equal(videoToRemove.video.name)
223 expect(data[0].id).to.equal(videoToRemove.video.id)
226 it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () {
227 const body = await command.list({ sort: '-name' })
228 expect(body.total).to.equal(1)
230 const videos = body.data
231 expect(videos).to.be.an('array')
232 expect(videos.length).to.equal(1)
233 expect(videos).to.deep.equal(blacklist)
237 describe('When blacklisting local videos', function () {
238 let video3UUID: string
239 let video4UUID: string
241 before(async function () {
245 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 3' } })
249 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 4' } })
253 await waitJobs(servers)
256 it('Should blacklist video 3 and keep it federated', async function () {
259 await command.add({ videoId: video3UUID, reason: 'super reason', unfederate: false })
261 await waitJobs(servers)
264 const { data } = await servers[0].videos.list()
265 expect(data.find(v => v.uuid === video3UUID)).to.be.undefined
269 const { data } = await servers[1].videos.list()
270 expect(data.find(v => v.uuid === video3UUID)).to.not.be.undefined
274 it('Should unfederate the video', async function () {
277 await command.add({ videoId: video4UUID, reason: 'super reason', unfederate: true })
279 await waitJobs(servers)
281 for (const server of servers) {
282 const { data } = await server.videos.list()
283 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
287 it('Should have the video unfederated even after an Update AP message', async function () {
290 await servers[0].videos.update({ id: video4UUID, attributes: { description: 'super description' } })
292 await waitJobs(servers)
294 for (const server of servers) {
295 const { data } = await server.videos.list()
296 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
300 it('Should have the correct video blacklist unfederate attribute', async function () {
301 const body = await command.list({ sort: 'createdAt' })
303 const blacklistedVideos = body.data
304 const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID)
305 const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID)
307 expect(video3Blacklisted.unfederated).to.be.false
308 expect(video4Blacklisted.unfederated).to.be.true
311 it('Should remove the video from blacklist and refederate the video', async function () {
314 await command.remove({ videoId: video4UUID })
316 await waitJobs(servers)
318 for (const server of servers) {
319 const { data } = await server.videos.list()
320 expect(data.find(v => v.uuid === video4UUID)).to.not.be.undefined
326 describe('When auto blacklist videos', function () {
327 let userWithoutFlag: string
328 let userWithFlag: string
329 let channelOfUserWithoutFlag: number
331 before(async function () {
334 await killallServers([ servers[0] ])
345 await servers[0].run(config)
348 const user = { username: 'user_without_flag', password: 'password' }
349 await servers[0].users.create({
350 username: user.username,
351 adminFlags: UserAdminFlag.NONE,
352 password: user.password,
356 userWithoutFlag = await servers[0].login.getAccessToken(user)
358 const { videoChannels } = await servers[0].users.getMyInfo({ token: userWithoutFlag })
359 channelOfUserWithoutFlag = videoChannels[0].id
363 const user = { username: 'user_with_flag', password: 'password' }
364 await servers[0].users.create({
365 username: user.username,
366 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST,
367 password: user.password,
371 userWithFlag = await servers[0].login.getAccessToken(user)
374 await waitJobs(servers)
377 it('Should auto blacklist a video on upload', async function () {
378 await servers[0].videos.upload({ token: userWithoutFlag, attributes: { name: 'blacklisted' } })
380 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
381 expect(body.total).to.equal(1)
382 expect(body.data[0].video.name).to.equal('blacklisted')
385 it('Should auto blacklist a video on URL import', async function () {
389 targetUrl: FIXTURE_URLS.goodVideo,
391 channelId: channelOfUserWithoutFlag
393 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
395 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
396 expect(body.total).to.equal(2)
397 expect(body.data[1].video.name).to.equal('URL import')
400 it('Should auto blacklist a video on torrent import', async function () {
402 magnetUri: FIXTURE_URLS.magnet,
403 name: 'Torrent import',
404 channelId: channelOfUserWithoutFlag
406 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
408 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
409 expect(body.total).to.equal(3)
410 expect(body.data[2].video.name).to.equal('Torrent import')
413 it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () {
414 await servers[0].videos.upload({ token: userWithFlag, attributes: { name: 'not blacklisted' } })
416 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
417 expect(body.total).to.equal(3)
421 after(async function () {
422 await cleanupTests(servers)