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