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