]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/moderation/video-blacklist.ts
Introduce sql command
[github/Chocobozzz/PeerTube.git] / server / tests / api / moderation / video-blacklist.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { orderBy } from 'lodash'
6 import {
7 BlacklistCommand,
8 cleanupTests,
9 createUser,
10 doubleFollow,
11 flushAndRunMultipleServers,
12 getMyUserInformation,
13 getMyVideos,
14 getVideosList,
15 ImportsCommand,
16 killallServers,
17 reRunServer,
18 ServerInfo,
19 setAccessTokensToServers,
20 updateVideo,
21 uploadVideo,
22 userLogin,
23 waitJobs
24 } from '@shared/extra-utils'
25 import { User, UserAdminFlag, UserRole, VideoBlacklist, VideoBlacklistType } from '@shared/models'
26
27 const expect = chai.expect
28
29 describe('Test video blacklist', function () {
30 let servers: ServerInfo[] = []
31 let videoId: number
32 let command: BlacklistCommand
33
34 async function blacklistVideosOnServer (server: ServerInfo) {
35 const res = await getVideosList(server.url)
36
37 const videos = res.body.data
38 for (const video of videos) {
39 await server.blacklistCommand.add({ videoId: video.id, reason: 'super reason' })
40 }
41 }
42
43 before(async function () {
44 this.timeout(50000)
45
46 // Run servers
47 servers = await flushAndRunMultipleServers(2)
48
49 // Get the access tokens
50 await setAccessTokensToServers(servers)
51
52 // Server 1 and server 2 follow each other
53 await doubleFollow(servers[0], servers[1])
54
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' })
58
59 // Wait videos propagation, server 2 has transcoding enabled
60 await waitJobs(servers)
61
62 command = servers[0].blacklistCommand
63
64 // Blacklist the two videos on server 1
65 await blacklistVideosOnServer(servers[0])
66 })
67
68 describe('When listing/searching videos', function () {
69
70 it('Should not have the video blacklisted in videos list/search on server 1', async function () {
71 {
72 const res = await getVideosList(servers[0].url)
73
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 {
80 const body = await servers[0].searchCommand.searchVideos({ search: 'video' })
81
82 expect(body.total).to.equal(0)
83 expect(body.data).to.be.an('array')
84 expect(body.data.length).to.equal(0)
85 }
86 })
87
88 it('Should have the blacklisted video in videos list/search on server 2', async function () {
89 {
90 const res = await getVideosList(servers[1].url)
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 {
98 const body = await servers[1].searchCommand.searchVideos({ search: 'video' })
99
100 expect(body.total).to.equal(2)
101 expect(body.data).to.be.an('array')
102 expect(body.data.length).to.equal(2)
103 }
104 })
105 })
106
107 describe('When listing manually blacklisted videos', function () {
108 it('Should display all the blacklisted videos', async function () {
109 const body = await command.list()
110 expect(body.total).to.equal(2)
111
112 const blacklistedVideos = body.data
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
122 it('Should display all the blacklisted videos when applying manual type filter', async function () {
123 const body = await command.list({ type: VideoBlacklistType.MANUAL })
124 expect(body.total).to.equal(2)
125
126 const blacklistedVideos = body.data
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 () {
132 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
133 expect(body.total).to.equal(0)
134
135 const blacklistedVideos = body.data
136 expect(blacklistedVideos).to.be.an('array')
137 expect(blacklistedVideos.length).to.equal(0)
138 })
139
140 it('Should get the correct sort when sorting by descending id', async function () {
141 const body = await command.list({ sort: '-id' })
142 expect(body.total).to.equal(2)
143
144 const blacklistedVideos = body.data
145 expect(blacklistedVideos).to.be.an('array')
146 expect(blacklistedVideos.length).to.equal(2)
147
148 const result = orderBy(body.data, [ 'id' ], [ 'desc' ])
149 expect(blacklistedVideos).to.deep.equal(result)
150 })
151
152 it('Should get the correct sort when sorting by descending video name', async function () {
153 const body = await command.list({ sort: '-name' })
154 expect(body.total).to.equal(2)
155
156 const blacklistedVideos = body.data
157 expect(blacklistedVideos).to.be.an('array')
158 expect(blacklistedVideos.length).to.equal(2)
159
160 const result = orderBy(body.data, [ 'name' ], [ 'desc' ])
161 expect(blacklistedVideos).to.deep.equal(result)
162 })
163
164 it('Should get the correct sort when sorting by ascending creation date', async function () {
165 const body = await command.list({ sort: 'createdAt' })
166 expect(body.total).to.equal(2)
167
168 const blacklistedVideos = body.data
169 expect(blacklistedVideos).to.be.an('array')
170 expect(blacklistedVideos.length).to.equal(2)
171
172 const result = orderBy(body.data, [ 'createdAt' ])
173 expect(blacklistedVideos).to.deep.equal(result)
174 })
175 })
176
177 describe('When updating blacklisted videos', function () {
178 it('Should change the reason', async function () {
179 await command.update({ videoId, reason: 'my super reason updated' })
180
181 const body = await command.list({ sort: '-name' })
182 const video = body.data.find(b => b.video.id === videoId)
183
184 expect(video.reason).to.equal('my super reason updated')
185 })
186 })
187
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)
193
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 })
202 })
203
204 describe('When removing a blacklisted video', function () {
205 let videoToRemove: VideoBlacklist
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
217 const body = await command.list({ sort: '-name' })
218 videoToRemove = body.data[0]
219 blacklist = body.data.slice(1)
220
221 // Remove it
222 await command.remove({ videoId: videoToRemove.video.id })
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 () {
238 const body = await command.list({ sort: '-name' })
239 expect(body.total).to.equal(1)
240
241 const videos = body.data
242 expect(videos).to.be.an('array')
243 expect(videos.length).to.equal(1)
244 expect(videos).to.deep.equal(blacklist)
245 })
246 })
247
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 {
260 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'Video 4' })
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
270 await command.add({ videoId: video3UUID, reason: 'super reason', unfederate: false })
271
272 await waitJobs(servers)
273
274 {
275 const res = await getVideosList(servers[0].url)
276 expect(res.body.data.find(v => v.uuid === video3UUID)).to.be.undefined
277 }
278
279 {
280 const res = await getVideosList(servers[1].url)
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
288 await command.add({ videoId: video4UUID, reason: 'super reason', unfederate: true })
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
301 await updateVideo(servers[0].url, servers[0].accessToken, video4UUID, { description: 'super description' })
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 () {
312 const body = await command.list({ sort: 'createdAt' })
313
314 const blacklistedVideos = body.data
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
325 await command.remove({ videoId: video4UUID })
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 })
334
335 })
336
337 describe('When auto blacklist videos', function () {
338 let userWithoutFlag: string
339 let userWithFlag: string
340 let channelOfUserWithoutFlag: number
341
342 before(async function () {
343 this.timeout(20000)
344
345 await killallServers([ servers[0] ])
346
347 const config = {
348 auto_blacklist: {
349 videos: {
350 of_users: {
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({
361 url: servers[0].url,
362 accessToken: servers[0].accessToken,
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)
370
371 const res = await getMyUserInformation(servers[0].url, userWithoutFlag)
372 const body: User = res.body
373 channelOfUserWithoutFlag = body.videoChannels[0].id
374 }
375
376 {
377 const user = { username: 'user_with_flag', password: 'password' }
378 await createUser({
379 url: servers[0].url,
380 accessToken: servers[0].accessToken,
381 username: user.username,
382 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST,
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
393 it('Should auto blacklist a video on upload', async function () {
394 await uploadVideo(servers[0].url, userWithoutFlag, { name: 'blacklisted' })
395
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')
399 })
400
401 it('Should auto blacklist a video on URL import', async function () {
402 this.timeout(15000)
403
404 const attributes = {
405 targetUrl: ImportsCommand.getGoodVideoUrl(),
406 name: 'URL import',
407 channelId: channelOfUserWithoutFlag
408 }
409 await servers[0].importsCommand.importVideo({ token: userWithoutFlag, attributes })
410
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')
414 })
415
416 it('Should auto blacklist a video on torrent import', async function () {
417 const attributes = {
418 magnetUri: ImportsCommand.getMagnetURI(),
419 name: 'Torrent import',
420 channelId: channelOfUserWithoutFlag
421 }
422 await servers[0].importsCommand.importVideo({ token: userWithoutFlag, attributes })
423
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')
427 })
428
429 it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () {
430 await uploadVideo(servers[0].url, userWithFlag, { name: 'not blacklisted' })
431
432 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
433 expect(body.total).to.equal(3)
434 })
435 })
436
437 after(async function () {
438 await cleanupTests(servers)
439 })
440 })