Tests for totalRepliesFromVideoAuthor
[oweals/peertube.git] / server / tests / api / videos / video-blacklist.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import { orderBy } from 'lodash'
5 import 'mocha'
6 import {
7   addVideoToBlacklist,
8   cleanupTests,
9   createUser,
10   flushAndRunMultipleServers,
11   getBlacklistedVideosList, getMyUserInformation,
12   getMyVideos,
13   getVideosList,
14   killallServers,
15   removeVideoFromBlacklist,
16   reRunServer,
17   searchVideo,
18   ServerInfo,
19   setAccessTokensToServers,
20   setDefaultVideoChannel,
21   updateVideo,
22   updateVideoBlacklist,
23   uploadVideo,
24   userLogin
25 } from '../../../../shared/extra-utils/index'
26 import { doubleFollow } from '../../../../shared/extra-utils/server/follows'
27 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
28 import { VideoBlacklist, VideoBlacklistType } from '../../../../shared/models/videos'
29 import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
30 import { User, UserRole, UserUpdateMe } from '../../../../shared/models/users'
31 import { getMagnetURI, getYoutubeVideoUrl, importVideo } from '../../../../shared/extra-utils/videos/video-imports'
32
33 const expect = chai.expect
34
35 describe('Test video blacklist', function () {
36   let servers: ServerInfo[] = []
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
43     for (let video of videos) {
44       await addVideoToBlacklist(server.url, server.accessToken, video.id, 'super reason')
45     }
46   }
47
48   before(async function () {
49     this.timeout(50000)
50
51     // Run servers
52     servers = await flushAndRunMultipleServers(2)
53
54     // Get the access tokens
55     await setAccessTokensToServers(servers)
56
57     // Server 1 and server 2 follow each other
58     await doubleFollow(servers[0], servers[1])
59
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' })
63
64     // Wait videos propagation, server 2 has transcoding enabled
65     await waitJobs(servers)
66
67     // Blacklist the two videos on server 1
68     await blacklistVideosOnServer(servers[0])
69   })
70
71   describe('When listing/searching videos', function () {
72
73     it('Should not have the video blacklisted in videos list/search on server 1', async function () {
74       {
75         const res = await getVideosList(servers[ 0 ].url)
76
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       {
83         const res = await searchVideo(servers[ 0 ].url, 'name')
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       {
93         const res = await getVideosList(servers[ 1 ].url)
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       {
101         const res = await searchVideo(servers[ 1 ].url, 'video')
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     })
108   })
109
110   describe('When listing manually blacklisted videos', function () {
111     it('Should display all the blacklisted videos', async function () {
112       const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken })
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
126     it('Should display all the blacklisted videos when applying manual type filter', async function () {
127       const res = await getBlacklistedVideosList({
128         url: servers[ 0 ].url,
129         token: servers[ 0 ].accessToken,
130         type: VideoBlacklistType.MANUAL
131       })
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 () {
141       const res = await getBlacklistedVideosList({
142         url: servers[ 0 ].url,
143         token: servers[ 0 ].accessToken,
144         type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
145       })
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
154     it('Should get the correct sort when sorting by descending id', async function () {
155       const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: '-id' })
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 () {
168       const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: '-name' })
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 () {
181       const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: 'createdAt' })
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     })
192   })
193
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
198       const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: '-name' })
199       const video = res.body.data.find(b => b.video.id === videoId)
200
201       expect(video.reason).to.equal('my super reason updated')
202     })
203   })
204
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)
210
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     })
219   })
220
221   describe('When removing a blacklisted video', function () {
222     let videoToRemove: VideoBlacklist
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
234       const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: '-name' })
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 () {
255       const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: '-name' })
256       expect(res.body.total).to.equal(1)
257
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     })
263   })
264
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       {
277         const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'Video 4' })
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
287       await addVideoToBlacklist(servers[ 0 ].url, servers[ 0 ].accessToken, video3UUID, 'super reason', false)
288
289       await waitJobs(servers)
290
291       {
292         const res = await getVideosList(servers[ 0 ].url)
293         expect(res.body.data.find(v => v.uuid === video3UUID)).to.be.undefined
294       }
295
296       {
297         const res = await getVideosList(servers[ 1 ].url)
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
305       await addVideoToBlacklist(servers[ 0 ].url, servers[ 0 ].accessToken, video4UUID, 'super reason', true)
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
318       await updateVideo(servers[ 0 ].url, servers[ 0 ].accessToken, video4UUID, { description: 'super description' })
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 () {
329       const res = await getBlacklistedVideosList({ url: servers[ 0 ].url, token: servers[ 0 ].accessToken, sort: 'createdAt' })
330
331       const blacklistedVideos: VideoBlacklist[] = res.body.data
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
342       await removeVideoFromBlacklist(servers[ 0 ].url, servers[ 0 ].accessToken, video4UUID)
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     })
351
352   })
353
354   describe('When auto blacklist videos', function () {
355     let userWithoutFlag: string
356     let userWithFlag: string
357     let channelOfUserWithoutFlag: number
358
359     before(async function () {
360       this.timeout(20000)
361
362       killallServers([ servers[0] ])
363
364       const config = {
365         'auto_blacklist': {
366           videos: {
367             'of_users': {
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({
378           url: servers[ 0 ].url,
379           accessToken: servers[ 0 ].accessToken,
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)
387
388         const res = await getMyUserInformation(servers[0].url, userWithoutFlag)
389         const body: User = res.body
390         channelOfUserWithoutFlag = body.videoChannels[0].id
391       }
392
393       {
394         const user = { username: 'user_with_flag', password: 'password' }
395         await createUser({
396           url: servers[ 0 ].url,
397           accessToken: servers[ 0 ].accessToken,
398           username: user.username,
399           adminFlags: UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST,
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
410     it('Should auto blacklist a video on upload', async function () {
411       await uploadVideo(servers[0].url, userWithoutFlag, { name: 'blacklisted' })
412
413       const res = await getBlacklistedVideosList({
414         url: servers[ 0 ].url,
415         token: servers[ 0 ].accessToken,
416         type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
417       })
418
419       expect(res.body.total).to.equal(1)
420       expect(res.body.data[0].video.name).to.equal('blacklisted')
421     })
422
423     it('Should auto blacklist a video on URL import', async function () {
424       this.timeout(15000)
425
426       const attributes = {
427         targetUrl: getYoutubeVideoUrl(),
428         name: 'URL import',
429         channelId: channelOfUserWithoutFlag
430       }
431       await importVideo(servers[ 0 ].url, userWithoutFlag, attributes)
432
433       const res = await getBlacklistedVideosList({
434         url: servers[ 0 ].url,
435         token: servers[ 0 ].accessToken,
436         sort: 'createdAt',
437         type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
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       }
450       await importVideo(servers[ 0 ].url, userWithoutFlag, attributes)
451
452       const res = await getBlacklistedVideosList({
453         url: servers[ 0 ].url,
454         token: servers[ 0 ].accessToken,
455         sort: 'createdAt',
456         type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
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 () {
464       await uploadVideo(servers[0].url, userWithFlag, { name: 'not blacklisted' })
465
466       const res = await getBlacklistedVideosList({
467         url: servers[ 0 ].url,
468         token: servers[ 0 ].accessToken,
469         type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
470       })
471
472       expect(res.body.total).to.equal(3)
473     })
474   })
475
476   after(async function () {
477     await cleanupTests(servers)
478   })
479 })