add quarantine videos feature (#1637)
[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   flushAndRunMultipleServers,
9   getBlacklistedVideosList,
10   getBlacklistedVideosListWithTypeFilter,
11   getMyVideos,
12   getSortedBlacklistedVideosList,
13   getVideosList,
14   killallServers,
15   removeVideoFromBlacklist,
16   searchVideo,
17   ServerInfo,
18   setAccessTokensToServers,
19   updateVideo,
20   updateVideoBlacklist,
21   uploadVideo,
22   viewVideo
23 } from '../../../../shared/utils/index'
24 import { doubleFollow } from '../../../../shared/utils/server/follows'
25 import { waitJobs } from '../../../../shared/utils/server/jobs'
26 import { VideoBlacklist, VideoBlacklistType } from '../../../../shared/models/videos'
27
28 const expect = chai.expect
29
30 describe('Test video blacklist management', function () {
31   let servers: ServerInfo[] = []
32   let videoId: number
33
34   async function blacklistVideosOnServer (server: ServerInfo) {
35     const res = await getVideosList(server.url)
36
37     const videos = res.body.data
38     for (let video of videos) {
39       await addVideoToBlacklist(server.url, server.accessToken, video.id, '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     // Blacklist the two videos on server 1
63     await blacklistVideosOnServer(servers[0])
64   })
65
66   describe('When listing/searching videos', function () {
67
68     it('Should not have the video blacklisted in videos list/search on server 1', async function () {
69       {
70         const res = await getVideosList(servers[ 0 ].url)
71
72         expect(res.body.total).to.equal(0)
73         expect(res.body.data).to.be.an('array')
74         expect(res.body.data.length).to.equal(0)
75       }
76
77       {
78         const res = await searchVideo(servers[ 0 ].url, 'name')
79
80         expect(res.body.total).to.equal(0)
81         expect(res.body.data).to.be.an('array')
82         expect(res.body.data.length).to.equal(0)
83       }
84     })
85
86     it('Should have the blacklisted video in videos list/search on server 2', async function () {
87       {
88         const res = await getVideosList(servers[ 1 ].url)
89
90         expect(res.body.total).to.equal(2)
91         expect(res.body.data).to.be.an('array')
92         expect(res.body.data.length).to.equal(2)
93       }
94
95       {
96         const res = await searchVideo(servers[ 1 ].url, 'video')
97
98         expect(res.body.total).to.equal(2)
99         expect(res.body.data).to.be.an('array')
100         expect(res.body.data.length).to.equal(2)
101       }
102     })
103   })
104
105   describe('When listing manually blacklisted videos', function () {
106     it('Should display all the blacklisted videos', async function () {
107       const res = await getBlacklistedVideosList(servers[0].url, servers[0].accessToken)
108
109       expect(res.body.total).to.equal(2)
110
111       const blacklistedVideos = res.body.data
112       expect(blacklistedVideos).to.be.an('array')
113       expect(blacklistedVideos.length).to.equal(2)
114
115       for (const blacklistedVideo of blacklistedVideos) {
116         expect(blacklistedVideo.reason).to.equal('super reason')
117         videoId = blacklistedVideo.video.id
118       }
119     })
120
121     it('Should display all the blacklisted videos when applying manual type filter', async function () {
122       const res = await getBlacklistedVideosListWithTypeFilter(servers[0].url, servers[0].accessToken, VideoBlacklistType.MANUAL)
123
124       expect(res.body.total).to.equal(2)
125
126       const blacklistedVideos = res.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 res = await getBlacklistedVideosListWithTypeFilter(servers[0].url, servers[0].accessToken, VideoBlacklistType.AUTO_BEFORE_PUBLISHED) // tslint:disable:max-line-length
133
134       expect(res.body.total).to.equal(0)
135
136       const blacklistedVideos = res.body.data
137       expect(blacklistedVideos).to.be.an('array')
138       expect(blacklistedVideos.length).to.equal(0)
139     })
140
141     it('Should get the correct sort when sorting by descending id', async function () {
142       const res = await getSortedBlacklistedVideosList(servers[0].url, servers[0].accessToken, '-id')
143       expect(res.body.total).to.equal(2)
144
145       const blacklistedVideos = res.body.data
146       expect(blacklistedVideos).to.be.an('array')
147       expect(blacklistedVideos.length).to.equal(2)
148
149       const result = orderBy(res.body.data, [ 'id' ], [ 'desc' ])
150
151       expect(blacklistedVideos).to.deep.equal(result)
152     })
153
154     it('Should get the correct sort when sorting by descending video name', async function () {
155       const res = await getSortedBlacklistedVideosList(servers[0].url, servers[0].accessToken, '-name')
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, [ 'name' ], [ 'desc' ])
163
164       expect(blacklistedVideos).to.deep.equal(result)
165     })
166
167     it('Should get the correct sort when sorting by ascending creation date', async function () {
168       const res = await getSortedBlacklistedVideosList(servers[0].url, servers[0].accessToken, 'createdAt')
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, [ 'createdAt' ])
176
177       expect(blacklistedVideos).to.deep.equal(result)
178     })
179   })
180
181   describe('When updating blacklisted videos', function () {
182     it('Should change the reason', async function () {
183       await updateVideoBlacklist(servers[0].url, servers[0].accessToken, videoId, 'my super reason updated')
184
185       const res = await getSortedBlacklistedVideosList(servers[0].url, servers[0].accessToken, '-name')
186       const video = res.body.data.find(b => b.video.id === videoId)
187
188       expect(video.reason).to.equal('my super reason updated')
189     })
190   })
191
192   describe('When listing my videos', function () {
193     it('Should display blacklisted videos', async function () {
194       await blacklistVideosOnServer(servers[1])
195
196       const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 5)
197
198       expect(res.body.total).to.equal(2)
199       expect(res.body.data).to.have.lengthOf(2)
200
201       for (const video of res.body.data) {
202         expect(video.blacklisted).to.be.true
203         expect(video.blacklistedReason).to.equal('super reason')
204       }
205     })
206   })
207
208   describe('When removing a blacklisted video', function () {
209     let videoToRemove: VideoBlacklist
210     let blacklist = []
211
212     it('Should not have any video in videos list on server 1', async function () {
213       const res = await getVideosList(servers[0].url)
214       expect(res.body.total).to.equal(0)
215       expect(res.body.data).to.be.an('array')
216       expect(res.body.data.length).to.equal(0)
217     })
218
219     it('Should remove a video from the blacklist on server 1', async function () {
220       // Get one video in the blacklist
221       const res = await getSortedBlacklistedVideosList(servers[0].url, servers[0].accessToken, '-name')
222       videoToRemove = res.body.data[0]
223       blacklist = res.body.data.slice(1)
224
225       // Remove it
226       await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoToRemove.video.id)
227     })
228
229     it('Should have the ex-blacklisted video in videos list on server 1', async function () {
230       const res = await getVideosList(servers[0].url)
231       expect(res.body.total).to.equal(1)
232
233       const videos = res.body.data
234       expect(videos).to.be.an('array')
235       expect(videos.length).to.equal(1)
236
237       expect(videos[0].name).to.equal(videoToRemove.video.name)
238       expect(videos[0].id).to.equal(videoToRemove.video.id)
239     })
240
241     it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () {
242       const res = await getSortedBlacklistedVideosList(servers[0].url, servers[0].accessToken, '-name')
243       expect(res.body.total).to.equal(1)
244
245       const videos = res.body.data
246       expect(videos).to.be.an('array')
247       expect(videos.length).to.equal(1)
248       expect(videos).to.deep.equal(blacklist)
249     })
250   })
251
252   describe('When blacklisting local videos', function () {
253     let video3UUID: string
254     let video4UUID: string
255
256     before(async function () {
257       this.timeout(10000)
258
259       {
260         const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'Video 3' })
261         video3UUID = res.body.video.uuid
262       }
263       {
264         const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'Video 4' })
265         video4UUID = res.body.video.uuid
266       }
267
268       await waitJobs(servers)
269     })
270
271     it('Should blacklist video 3 and keep it federated', async function () {
272       this.timeout(10000)
273
274       await addVideoToBlacklist(servers[ 0 ].url, servers[ 0 ].accessToken, video3UUID, 'super reason', false)
275
276       await waitJobs(servers)
277
278       {
279         const res = await getVideosList(servers[ 0 ].url)
280         expect(res.body.data.find(v => v.uuid === video3UUID)).to.be.undefined
281       }
282
283       {
284         const res = await getVideosList(servers[ 1 ].url)
285         expect(res.body.data.find(v => v.uuid === video3UUID)).to.not.be.undefined
286       }
287     })
288
289     it('Should unfederate the video', async function () {
290       this.timeout(10000)
291
292       await addVideoToBlacklist(servers[ 0 ].url, servers[ 0 ].accessToken, video4UUID, 'super reason', true)
293
294       await waitJobs(servers)
295
296       for (const server of servers) {
297         const res = await getVideosList(server.url)
298         expect(res.body.data.find(v => v.uuid === video4UUID)).to.be.undefined
299       }
300     })
301
302     it('Should have the video unfederated even after an Update AP message', async function () {
303       this.timeout(10000)
304
305       await updateVideo(servers[ 0 ].url, servers[ 0 ].accessToken, video4UUID, { description: 'super description' })
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 correct video blacklist unfederate attribute', async function () {
316       const res = await getSortedBlacklistedVideosList(servers[0].url, servers[0].accessToken, 'createdAt')
317
318       const blacklistedVideos: VideoBlacklist[] = res.body.data
319       const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID)
320       const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID)
321
322       expect(video3Blacklisted.unfederated).to.be.false
323       expect(video4Blacklisted.unfederated).to.be.true
324     })
325
326     it('Should remove the video from blacklist and refederate the video', async function () {
327       this.timeout(10000)
328
329       await removeVideoFromBlacklist(servers[ 0 ].url, servers[ 0 ].accessToken, video4UUID)
330
331       await waitJobs(servers)
332
333       for (const server of servers) {
334         const res = await getVideosList(server.url)
335         expect(res.body.data.find(v => v.uuid === video4UUID)).to.not.be.undefined
336       }
337     })
338
339   })
340
341   after(async function () {
342     killallServers(servers)
343   })
344 })