]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/moderation/video-blacklist.ts
Implement avatar miniatures (#4639)
[github/Chocobozzz/PeerTube.git] / server / tests / api / moderation / video-blacklist.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
0e1dc3e7 2
b488ba1e 3import 'mocha'
0e1dc3e7 4import * as chai from 'chai'
5abb9fbb 5import { orderBy } from 'lodash'
c55e3d72
C
6import { FIXTURE_URLS } from '@server/tests/shared'
7import { UserAdminFlag, UserRole, VideoBlacklist, VideoBlacklistType } from '@shared/models'
0e1dc3e7 8import {
e3d15a6a 9 BlacklistCommand,
03371ad9 10 cleanupTests,
254d3579 11 createMultipleServers,
59bbcced 12 doubleFollow,
975e6e0e 13 killallServers,
254d3579 14 PeerTubeServer,
975e6e0e 15 setAccessTokensToServers,
d0800f76 16 setDefaultChannelAvatar,
af971e06 17 waitJobs
bf54587a 18} from '@shared/server-commands'
975e6e0e
C
19
20const expect = chai.expect
0e1dc3e7 21
1eddc9a7 22describe('Test video blacklist', function () {
254d3579 23 let servers: PeerTubeServer[] = []
5abb9fbb 24 let videoId: number
e3d15a6a 25 let command: BlacklistCommand
5abb9fbb 26
254d3579 27 async function blacklistVideosOnServer (server: PeerTubeServer) {
89d241a7 28 const { data } = await server.videos.list()
5abb9fbb 29
d23dd9fb 30 for (const video of data) {
89d241a7 31 await server.blacklist.add({ videoId: video.id, reason: 'super reason' })
5abb9fbb
C
32 }
33 }
0e1dc3e7
C
34
35 before(async function () {
ebee0c04 36 this.timeout(120000)
0e1dc3e7
C
37
38 // Run servers
254d3579 39 servers = await createMultipleServers(2)
0e1dc3e7
C
40
41 // Get the access tokens
42 await setAccessTokensToServers(servers)
43
975e6e0e
C
44 // Server 1 and server 2 follow each other
45 await doubleFollow(servers[0], servers[1])
d0800f76 46 await setDefaultChannelAvatar(servers[0])
0e1dc3e7 47
5abb9fbb 48 // Upload 2 videos on server 2
89d241a7
C
49 await servers[1].videos.upload({ attributes: { name: 'My 1st video', description: 'A video on server 2' } })
50 await servers[1].videos.upload({ attributes: { name: 'My 2nd video', description: 'A video on server 2' } })
0e1dc3e7 51
572f8d3d 52 // Wait videos propagation, server 2 has transcoding enabled
3cd0734f 53 await waitJobs(servers)
0e1dc3e7 54
89d241a7 55 command = servers[0].blacklist
e3d15a6a 56
5abb9fbb
C
57 // Blacklist the two videos on server 1
58 await blacklistVideosOnServer(servers[0])
59 })
60
61 describe('When listing/searching videos', function () {
0e1dc3e7 62
5abb9fbb
C
63 it('Should not have the video blacklisted in videos list/search on server 1', async function () {
64 {
89d241a7 65 const { total, data } = await servers[0].videos.list()
0e1dc3e7 66
d23dd9fb
C
67 expect(total).to.equal(0)
68 expect(data).to.be.an('array')
69 expect(data.length).to.equal(0)
5abb9fbb
C
70 }
71
72 {
89d241a7 73 const body = await servers[0].search.searchVideos({ search: 'video' })
5abb9fbb 74
af971e06
C
75 expect(body.total).to.equal(0)
76 expect(body.data).to.be.an('array')
77 expect(body.data.length).to.equal(0)
5abb9fbb
C
78 }
79 })
80
81 it('Should have the blacklisted video in videos list/search on server 2', async function () {
82 {
89d241a7 83 const { total, data } = await servers[1].videos.list()
5abb9fbb 84
d23dd9fb
C
85 expect(total).to.equal(2)
86 expect(data).to.be.an('array')
87 expect(data.length).to.equal(2)
5abb9fbb
C
88 }
89
90 {
89d241a7 91 const body = await servers[1].search.searchVideos({ search: 'video' })
5abb9fbb 92
af971e06
C
93 expect(body.total).to.equal(2)
94 expect(body.data).to.be.an('array')
95 expect(body.data.length).to.equal(2)
5abb9fbb
C
96 }
97 })
0e1dc3e7
C
98 })
99
7ccddd7b 100 describe('When listing manually blacklisted videos', function () {
5abb9fbb 101 it('Should display all the blacklisted videos', async function () {
e3d15a6a
C
102 const body = await command.list()
103 expect(body.total).to.equal(2)
5abb9fbb 104
e3d15a6a 105 const blacklistedVideos = body.data
5abb9fbb
C
106 expect(blacklistedVideos).to.be.an('array')
107 expect(blacklistedVideos.length).to.equal(2)
108
109 for (const blacklistedVideo of blacklistedVideos) {
110 expect(blacklistedVideo.reason).to.equal('super reason')
111 videoId = blacklistedVideo.video.id
112 }
113 })
114
7ccddd7b 115 it('Should display all the blacklisted videos when applying manual type filter', async function () {
e3d15a6a
C
116 const body = await command.list({ type: VideoBlacklistType.MANUAL })
117 expect(body.total).to.equal(2)
7ccddd7b 118
e3d15a6a 119 const blacklistedVideos = body.data
7ccddd7b
JM
120 expect(blacklistedVideos).to.be.an('array')
121 expect(blacklistedVideos.length).to.equal(2)
122 })
123
124 it('Should display nothing when applying automatic type filter', async function () {
e3d15a6a
C
125 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
126 expect(body.total).to.equal(0)
7ccddd7b 127
e3d15a6a 128 const blacklistedVideos = body.data
7ccddd7b
JM
129 expect(blacklistedVideos).to.be.an('array')
130 expect(blacklistedVideos.length).to.equal(0)
131 })
132
5abb9fbb 133 it('Should get the correct sort when sorting by descending id', async function () {
e3d15a6a
C
134 const body = await command.list({ sort: '-id' })
135 expect(body.total).to.equal(2)
5abb9fbb 136
e3d15a6a 137 const blacklistedVideos = body.data
5abb9fbb
C
138 expect(blacklistedVideos).to.be.an('array')
139 expect(blacklistedVideos.length).to.equal(2)
140
e3d15a6a 141 const result = orderBy(body.data, [ 'id' ], [ 'desc' ])
5abb9fbb
C
142 expect(blacklistedVideos).to.deep.equal(result)
143 })
144
145 it('Should get the correct sort when sorting by descending video name', async function () {
e3d15a6a
C
146 const body = await command.list({ sort: '-name' })
147 expect(body.total).to.equal(2)
5abb9fbb 148
e3d15a6a 149 const blacklistedVideos = body.data
5abb9fbb
C
150 expect(blacklistedVideos).to.be.an('array')
151 expect(blacklistedVideos.length).to.equal(2)
152
e3d15a6a 153 const result = orderBy(body.data, [ 'name' ], [ 'desc' ])
5abb9fbb
C
154 expect(blacklistedVideos).to.deep.equal(result)
155 })
156
157 it('Should get the correct sort when sorting by ascending creation date', async function () {
e3d15a6a
C
158 const body = await command.list({ sort: 'createdAt' })
159 expect(body.total).to.equal(2)
5abb9fbb 160
e3d15a6a 161 const blacklistedVideos = body.data
5abb9fbb
C
162 expect(blacklistedVideos).to.be.an('array')
163 expect(blacklistedVideos.length).to.equal(2)
164
e3d15a6a 165 const result = orderBy(body.data, [ 'createdAt' ])
5abb9fbb
C
166 expect(blacklistedVideos).to.deep.equal(result)
167 })
0e1dc3e7
C
168 })
169
5abb9fbb
C
170 describe('When updating blacklisted videos', function () {
171 it('Should change the reason', async function () {
e3d15a6a 172 await command.update({ videoId, reason: 'my super reason updated' })
5abb9fbb 173
e3d15a6a
C
174 const body = await command.list({ sort: '-name' })
175 const video = body.data.find(b => b.video.id === videoId)
0e1dc3e7 176
5abb9fbb
C
177 expect(video.reason).to.equal('my super reason updated')
178 })
0e1dc3e7
C
179 })
180
5abb9fbb
C
181 describe('When listing my videos', function () {
182 it('Should display blacklisted videos', async function () {
183 await blacklistVideosOnServer(servers[1])
184
89d241a7 185 const { total, data } = await servers[1].videos.listMyVideos()
0e1dc3e7 186
d23dd9fb
C
187 expect(total).to.equal(2)
188 expect(data).to.have.lengthOf(2)
5abb9fbb 189
d23dd9fb 190 for (const video of data) {
5abb9fbb
C
191 expect(video.blacklisted).to.be.true
192 expect(video.blacklistedReason).to.equal('super reason')
193 }
194 })
0e1dc3e7
C
195 })
196
5abb9fbb 197 describe('When removing a blacklisted video', function () {
3487330d 198 let videoToRemove: VideoBlacklist
5abb9fbb
C
199 let blacklist = []
200
201 it('Should not have any video in videos list on server 1', async function () {
89d241a7 202 const { total, data } = await servers[0].videos.list()
d23dd9fb
C
203 expect(total).to.equal(0)
204 expect(data).to.be.an('array')
205 expect(data.length).to.equal(0)
5abb9fbb
C
206 })
207
208 it('Should remove a video from the blacklist on server 1', async function () {
209 // Get one video in the blacklist
e3d15a6a
C
210 const body = await command.list({ sort: '-name' })
211 videoToRemove = body.data[0]
212 blacklist = body.data.slice(1)
5abb9fbb
C
213
214 // Remove it
e3d15a6a 215 await command.remove({ videoId: videoToRemove.video.id })
5abb9fbb
C
216 })
217
218 it('Should have the ex-blacklisted video in videos list on server 1', async function () {
89d241a7 219 const { total, data } = await servers[0].videos.list()
d23dd9fb 220 expect(total).to.equal(1)
5abb9fbb 221
d23dd9fb
C
222 expect(data).to.be.an('array')
223 expect(data.length).to.equal(1)
5abb9fbb 224
d23dd9fb
C
225 expect(data[0].name).to.equal(videoToRemove.video.name)
226 expect(data[0].id).to.equal(videoToRemove.video.id)
5abb9fbb
C
227 })
228
229 it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () {
e3d15a6a
C
230 const body = await command.list({ sort: '-name' })
231 expect(body.total).to.equal(1)
0e1dc3e7 232
e3d15a6a 233 const videos = body.data
5abb9fbb
C
234 expect(videos).to.be.an('array')
235 expect(videos.length).to.equal(1)
236 expect(videos).to.deep.equal(blacklist)
237 })
0e1dc3e7
C
238 })
239
5abb9fbb
C
240 describe('When blacklisting local videos', function () {
241 let video3UUID: string
242 let video4UUID: string
243
244 before(async function () {
245 this.timeout(10000)
246
247 {
89d241a7 248 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 3' } })
d23dd9fb 249 video3UUID = uuid
5abb9fbb
C
250 }
251 {
89d241a7 252 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 4' } })
d23dd9fb 253 video4UUID = uuid
5abb9fbb
C
254 }
255
256 await waitJobs(servers)
257 })
258
259 it('Should blacklist video 3 and keep it federated', async function () {
260 this.timeout(10000)
261
e3d15a6a 262 await command.add({ videoId: video3UUID, reason: 'super reason', unfederate: false })
5abb9fbb
C
263
264 await waitJobs(servers)
265
266 {
89d241a7 267 const { data } = await servers[0].videos.list()
d23dd9fb 268 expect(data.find(v => v.uuid === video3UUID)).to.be.undefined
5abb9fbb
C
269 }
270
271 {
89d241a7 272 const { data } = await servers[1].videos.list()
d23dd9fb 273 expect(data.find(v => v.uuid === video3UUID)).to.not.be.undefined
5abb9fbb
C
274 }
275 })
276
277 it('Should unfederate the video', async function () {
278 this.timeout(10000)
279
e3d15a6a 280 await command.add({ videoId: video4UUID, reason: 'super reason', unfederate: true })
5abb9fbb
C
281
282 await waitJobs(servers)
283
284 for (const server of servers) {
89d241a7 285 const { data } = await server.videos.list()
d23dd9fb 286 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
5abb9fbb
C
287 }
288 })
289
290 it('Should have the video unfederated even after an Update AP message', async function () {
291 this.timeout(10000)
292
89d241a7 293 await servers[0].videos.update({ id: video4UUID, attributes: { description: 'super description' } })
5abb9fbb
C
294
295 await waitJobs(servers)
296
297 for (const server of servers) {
89d241a7 298 const { data } = await server.videos.list()
d23dd9fb 299 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
5abb9fbb
C
300 }
301 })
302
303 it('Should have the correct video blacklist unfederate attribute', async function () {
e3d15a6a 304 const body = await command.list({ sort: 'createdAt' })
5abb9fbb 305
e3d15a6a 306 const blacklistedVideos = body.data
5abb9fbb
C
307 const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID)
308 const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID)
309
310 expect(video3Blacklisted.unfederated).to.be.false
311 expect(video4Blacklisted.unfederated).to.be.true
312 })
313
314 it('Should remove the video from blacklist and refederate the video', async function () {
315 this.timeout(10000)
316
e3d15a6a 317 await command.remove({ videoId: video4UUID })
5abb9fbb
C
318
319 await waitJobs(servers)
320
321 for (const server of servers) {
89d241a7 322 const { data } = await server.videos.list()
d23dd9fb 323 expect(data.find(v => v.uuid === video4UUID)).to.not.be.undefined
5abb9fbb
C
324 }
325 })
0e1dc3e7 326
0e1dc3e7
C
327 })
328
1eddc9a7
C
329 describe('When auto blacklist videos', function () {
330 let userWithoutFlag: string
331 let userWithFlag: string
03371ad9 332 let channelOfUserWithoutFlag: number
1eddc9a7
C
333
334 before(async function () {
335 this.timeout(20000)
336
9293139f 337 await killallServers([ servers[0] ])
1eddc9a7
C
338
339 const config = {
a1587156 340 auto_blacklist: {
1eddc9a7 341 videos: {
a1587156 342 of_users: {
1eddc9a7
C
343 enabled: true
344 }
345 }
346 }
347 }
254d3579 348 await servers[0].run(config)
1eddc9a7
C
349
350 {
351 const user = { username: 'user_without_flag', password: 'password' }
89d241a7 352 await servers[0].users.create({
1eddc9a7
C
353 username: user.username,
354 adminFlags: UserAdminFlag.NONE,
355 password: user.password,
356 role: UserRole.USER
357 })
358
89d241a7 359 userWithoutFlag = await servers[0].login.getAccessToken(user)
03371ad9 360
89d241a7 361 const { videoChannels } = await servers[0].users.getMyInfo({ token: userWithoutFlag })
7926c5f9 362 channelOfUserWithoutFlag = videoChannels[0].id
1eddc9a7
C
363 }
364
365 {
366 const user = { username: 'user_with_flag', password: 'password' }
89d241a7 367 await servers[0].users.create({
1eddc9a7 368 username: user.username,
3487330d 369 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST,
1eddc9a7
C
370 password: user.password,
371 role: UserRole.USER
372 })
373
89d241a7 374 userWithFlag = await servers[0].login.getAccessToken(user)
1eddc9a7
C
375 }
376
377 await waitJobs(servers)
378 })
379
03371ad9 380 it('Should auto blacklist a video on upload', async function () {
89d241a7 381 await servers[0].videos.upload({ token: userWithoutFlag, attributes: { name: 'blacklisted' } })
1eddc9a7 382
e3d15a6a
C
383 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
384 expect(body.total).to.equal(1)
385 expect(body.data[0].video.name).to.equal('blacklisted')
1eddc9a7
C
386 })
387
03371ad9 388 it('Should auto blacklist a video on URL import', async function () {
109d893f
C
389 this.timeout(15000)
390
03371ad9 391 const attributes = {
59bbcced 392 targetUrl: FIXTURE_URLS.goodVideo,
03371ad9
C
393 name: 'URL import',
394 channelId: channelOfUserWithoutFlag
395 }
89d241a7 396 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
03371ad9 397
e3d15a6a
C
398 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
399 expect(body.total).to.equal(2)
400 expect(body.data[1].video.name).to.equal('URL import')
03371ad9
C
401 })
402
403 it('Should auto blacklist a video on torrent import', async function () {
404 const attributes = {
59bbcced 405 magnetUri: FIXTURE_URLS.magnet,
03371ad9
C
406 name: 'Torrent import',
407 channelId: channelOfUserWithoutFlag
408 }
89d241a7 409 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
03371ad9 410
e3d15a6a
C
411 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
412 expect(body.total).to.equal(3)
413 expect(body.data[2].video.name).to.equal('Torrent import')
03371ad9
C
414 })
415
416 it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () {
89d241a7 417 await servers[0].videos.upload({ token: userWithFlag, attributes: { name: 'not blacklisted' } })
1eddc9a7 418
e3d15a6a
C
419 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
420 expect(body.total).to.equal(3)
1eddc9a7
C
421 })
422 })
423
7c3b7976
C
424 after(async function () {
425 await cleanupTests(servers)
0e1dc3e7
C
426 })
427})