]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/plugins/filter-hooks.ts
Add hooks support for video download
[github/Chocobozzz/PeerTube.git] / server / tests / plugins / filter-hooks.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
9b474844 2
9b474844 3import 'mocha'
2158ac90
RK
4import * as chai from 'chai'
5import { ServerConfig } from '@shared/models'
89cd1275
C
6import {
7 addVideoCommentReply,
6691c522 8 addVideoCommentThread,
3cabf353 9 createLive,
a1587156 10 doubleFollow,
38267c0c 11 getAccountVideos,
a1587156 12 getConfig,
a4d2ca07 13 getMyVideos,
6691c522 14 getPluginTestPath,
6691c522 15 getVideo,
38267c0c 16 getVideoChannelVideos,
6691c522 17 getVideoCommentThreads,
a1587156
C
18 getVideosList,
19 getVideosListPagination,
6691c522
C
20 getVideoThreadComments,
21 getVideoWithToken,
a1587156 22 installPlugin,
4bc45da3 23 makeRawRequest,
a1587156
C
24 registerUser,
25 setAccessTokensToServers,
6691c522 26 setDefaultVideoChannel,
3cabf353 27 updateCustomSubConfig,
a1587156
C
28 updateVideo,
29 uploadVideo,
4bc45da3 30 uploadVideoAndGetId,
a1587156 31 waitJobs
89cd1275 32} from '../../../shared/extra-utils'
2158ac90 33import { cleanupTests, flushAndRunMultipleServers, ServerInfo } from '../../../shared/extra-utils/server/servers'
b488ba1e 34import { getGoodVideoUrl, getMyVideoImports, importVideo } from '../../../shared/extra-utils/videos/video-imports'
2158ac90 35import { VideoDetails, VideoImport, VideoImportState, VideoPrivacy } from '../../../shared/models/videos'
6691c522 36import { VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model'
f2eb23cd 37import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
9b474844
C
38
39const expect = chai.expect
40
41describe('Test plugin filter hooks', function () {
89cd1275
C
42 let servers: ServerInfo[]
43 let videoUUID: string
44 let threadId: number
9b474844
C
45
46 before(async function () {
47 this.timeout(30000)
9b474844 48
89cd1275
C
49 servers = await flushAndRunMultipleServers(2)
50 await setAccessTokensToServers(servers)
6691c522
C
51 await setDefaultVideoChannel(servers)
52 await doubleFollow(servers[0], servers[1])
89cd1275
C
53
54 await installPlugin({
55 url: servers[0].url,
56 accessToken: servers[0].accessToken,
57 path: getPluginTestPath()
58 })
59
60 await installPlugin({
61 url: servers[0].url,
62 accessToken: servers[0].accessToken,
63 path: getPluginTestPath('-two')
64 })
65
66 for (let i = 0; i < 10; i++) {
67 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i })
68 }
69
70 const res = await getVideosList(servers[0].url)
71 videoUUID = res.body.data[0].uuid
3cabf353
C
72
73 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
74 live: { enabled: true },
75 signup: { enabled: true },
76 import: {
77 videos: {
78 http: { enabled: true },
79 torrent: { enabled: true }
80 }
81 }
82 })
9b474844
C
83 })
84
6691c522 85 it('Should run filter:api.videos.list.params', async function () {
89cd1275
C
86 const res = await getVideosListPagination(servers[0].url, 0, 2)
87
88 // 2 plugins do +1 to the count parameter
89 expect(res.body.data).to.have.lengthOf(4)
90 })
91
92 it('Should run filter:api.videos.list.result', async function () {
93 const res = await getVideosListPagination(servers[0].url, 0, 0)
94
95 // Plugin do +1 to the total result
96 expect(res.body.total).to.equal(11)
97 })
98
38267c0c
C
99 it('Should run filter:api.accounts.videos.list.params', async function () {
100 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
101
102 // 1 plugin do +1 to the count parameter
103 expect(res.body.data).to.have.lengthOf(3)
104 })
105
106 it('Should run filter:api.accounts.videos.list.result', async function () {
107 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
108
109 // Plugin do +2 to the total result
110 expect(res.body.total).to.equal(12)
111 })
112
113 it('Should run filter:api.video-channels.videos.list.params', async function () {
114 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
115
116 // 1 plugin do +3 to the count parameter
117 expect(res.body.data).to.have.lengthOf(5)
118 })
119
120 it('Should run filter:api.video-channels.videos.list.result', async function () {
c824e8a0 121 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
38267c0c
C
122
123 // Plugin do +3 to the total result
124 expect(res.body.total).to.equal(13)
125 })
126
a4d2ca07
C
127 it('Should run filter:api.user.me.videos.list.params', async function () {
128 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
129
130 // 1 plugin do +4 to the count parameter
131 expect(res.body.data).to.have.lengthOf(6)
132 })
133
134 it('Should run filter:api.user.me.videos.list.result', async function () {
135 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
136
137 // Plugin do +4 to the total result
138 expect(res.body.total).to.equal(14)
139 })
140
89cd1275
C
141 it('Should run filter:api.video.get.result', async function () {
142 const res = await getVideo(servers[0].url, videoUUID)
143
144 expect(res.body.name).to.contain('<3')
9b474844
C
145 })
146
6691c522 147 it('Should run filter:api.video.upload.accept.result', async function () {
f2eb23cd 148 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, HttpStatusCode.FORBIDDEN_403)
6691c522
C
149 })
150
3cabf353
C
151 it('Should run filter:api.live-video.create.accept.result', async function () {
152 const attributes = {
153 name: 'video with bad word',
154 privacy: VideoPrivacy.PUBLIC,
155 channelId: servers[0].videoChannel.id
156 }
157
f2eb23cd 158 await createLive(servers[0].url, servers[0].accessToken, attributes, HttpStatusCode.FORBIDDEN_403)
3cabf353
C
159 })
160
2158ac90
RK
161 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
162 const baseAttributes = {
163 name: 'normal title',
164 privacy: VideoPrivacy.PUBLIC,
165 channelId: servers[0].videoChannel.id,
b488ba1e 166 targetUrl: getGoodVideoUrl() + 'bad'
2158ac90 167 }
f2eb23cd 168 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
2158ac90
RK
169 })
170
171 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
172 const baseAttributes = {
173 name: 'bad torrent',
174 privacy: VideoPrivacy.PUBLIC,
175 channelId: servers[0].videoChannel.id,
176 torrentfile: 'video-720p.torrent' as any
177 }
f2eb23cd 178 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
2158ac90
RK
179 })
180
181 it('Should run filter:api.video.post-import-url.accept.result', async function () {
182 this.timeout(60000)
183
184 let videoImportId: number
185
186 {
187 const baseAttributes = {
188 name: 'title with bad word',
189 privacy: VideoPrivacy.PUBLIC,
190 channelId: servers[0].videoChannel.id,
b488ba1e 191 targetUrl: getGoodVideoUrl()
2158ac90
RK
192 }
193 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
194 videoImportId = res.body.id
195 }
196
197 await waitJobs(servers)
198
199 {
200 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
201 const videoImports = res.body.data as VideoImport[]
202
203 const videoImport = videoImports.find(i => i.id === videoImportId)
204
205 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
206 expect(videoImport.state.label).to.equal('Rejected')
207 }
208 })
209
210 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
211 this.timeout(60000)
212
213 let videoImportId: number
214
215 {
216 const baseAttributes = {
217 name: 'title with bad word',
218 privacy: VideoPrivacy.PUBLIC,
219 channelId: servers[0].videoChannel.id,
220 torrentfile: 'video-720p.torrent' as any
221 }
222 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
223 videoImportId = res.body.id
224 }
225
226 await waitJobs(servers)
227
228 {
229 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
230 const videoImports = res.body.data as VideoImport[]
231
232 const videoImport = videoImports.find(i => i.id === videoImportId)
233
234 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
235 expect(videoImport.state.label).to.equal('Rejected')
236 }
237 })
238
6691c522 239 it('Should run filter:api.video-thread.create.accept.result', async function () {
f2eb23cd 240 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', HttpStatusCode.FORBIDDEN_403)
6691c522
C
241 })
242
243 it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
244 const res = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'thread')
245 threadId = res.body.comment.id
246
f2eb23cd
RK
247 await addVideoCommentReply(
248 servers[0].url,
249 servers[0].accessToken,
250 videoUUID,
251 threadId,
252 'comment with bad word',
253 HttpStatusCode.FORBIDDEN_403
254 )
255 await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with good word', HttpStatusCode.OK_200)
6691c522
C
256 })
257
258 it('Should run filter:api.video-threads.list.params', async function () {
259 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
260
261 // our plugin do +1 to the count parameter
262 expect(res.body.data).to.have.lengthOf(1)
263 })
264
265 it('Should run filter:api.video-threads.list.result', async function () {
266 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
267
268 // Plugin do +1 to the total result
269 expect(res.body.total).to.equal(2)
270 })
271
272 it('Should run filter:api.video-thread-comments.list.params')
273
274 it('Should run filter:api.video-thread-comments.list.result', async function () {
275 const res = await getVideoThreadComments(servers[0].url, videoUUID, threadId)
276
277 const thread = res.body as VideoCommentThreadTree
278 expect(thread.comment.text.endsWith(' <3')).to.be.true
279 })
280
281 describe('Should run filter:video.auto-blacklist.result', function () {
282
283 async function checkIsBlacklisted (oldRes: any, value: boolean) {
284 const videoId = oldRes.body.video.uuid
285
286 const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, videoId)
287 const video: VideoDetails = res.body
288 expect(video.blacklisted).to.equal(value)
289 }
290
291 it('Should blacklist on upload', async function () {
a1587156 292 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video please blacklist me' })
6691c522
C
293 await checkIsBlacklisted(res, true)
294 })
295
296 it('Should blacklist on import', async function () {
89566f77
C
297 this.timeout(15000)
298
6691c522
C
299 const attributes = {
300 name: 'video please blacklist me',
b488ba1e 301 targetUrl: getGoodVideoUrl(),
6691c522
C
302 channelId: servers[0].videoChannel.id
303 }
304 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
305 await checkIsBlacklisted(res, true)
306 })
307
308 it('Should blacklist on update', async function () {
a1587156 309 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
6691c522
C
310 const videoId = res.body.video.uuid
311 await checkIsBlacklisted(res, false)
312
a1587156 313 await updateVideo(servers[0].url, servers[0].accessToken, videoId, { name: 'please blacklist me' })
6691c522
C
314 await checkIsBlacklisted(res, true)
315 })
316
317 it('Should blacklist on remote upload', async function () {
318 this.timeout(45000)
319
a1587156 320 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'remote please blacklist me' })
6691c522
C
321 await waitJobs(servers)
322
323 await checkIsBlacklisted(res, true)
324 })
325
326 it('Should blacklist on remote update', async function () {
327 this.timeout(45000)
328
a1587156 329 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video' })
6691c522
C
330 await waitJobs(servers)
331
332 const videoId = res.body.video.uuid
333 await checkIsBlacklisted(res, false)
334
335 await updateVideo(servers[1].url, servers[1].accessToken, videoId, { name: 'please blacklist me' })
336 await waitJobs(servers)
337
338 await checkIsBlacklisted(res, true)
339 })
340 })
341
4ce7eb71
C
342 describe('Should run filter:api.user.signup.allowed.result', function () {
343
344 it('Should run on config endpoint', async function () {
345 const res = await getConfig(servers[0].url)
346 expect((res.body as ServerConfig).signup.allowed).to.be.true
347 })
348
349 it('Should allow a signup', async function () {
350 await registerUser(servers[0].url, 'john', 'password')
351 })
352
353 it('Should not allow a signup', async function () {
f2eb23cd 354 const res = await registerUser(servers[0].url, 'jma', 'password', HttpStatusCode.FORBIDDEN_403)
4ce7eb71
C
355
356 expect(res.body.error).to.equal('No jma')
357 })
358 })
359
4bc45da3
C
360 describe('Download hooks', function () {
361 const downloadVideos: VideoDetails[] = []
362
363 before(async function () {
364 this.timeout(60000)
365
366 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
367 transcoding: {
368 webtorrent: {
369 enabled: true
370 },
371 hls: {
372 enabled: true
373 }
374 }
375 })
376
377 const uuids: string[] = []
378
379 for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) {
380 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
381 uuids.push(uuid)
382 }
383
384 await waitJobs(servers)
385
386 for (const uuid of uuids) {
387 const res = await getVideo(servers[0].url, uuid)
388 downloadVideos.push(res.body)
389 }
390 })
391
392 it('Should run filter:api.download.torrent.allowed.result', async function () {
393 const res = await makeRawRequest(downloadVideos[0].files[0].torrentDownloadUrl, 403)
394 expect(res.body.error).to.equal('Liu Bei')
395
396 await makeRawRequest(downloadVideos[1].files[0].torrentDownloadUrl, 200)
397 await makeRawRequest(downloadVideos[2].files[0].torrentDownloadUrl, 200)
398 })
399
400 it('Should run filter:api.download.video.allowed.result', async function () {
401 {
402 const res = await makeRawRequest(downloadVideos[1].files[0].fileDownloadUrl, 403)
403 expect(res.body.error).to.equal('Cao Cao')
404
405 await makeRawRequest(downloadVideos[0].files[0].fileDownloadUrl, 200)
406 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
407 }
408
409 {
410 const res = await makeRawRequest(downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl, 403)
411 expect(res.body.error).to.equal('Sun Jian')
412
413 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
414
415 await makeRawRequest(downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
416 await makeRawRequest(downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
417 }
418 })
419 })
420
9b474844 421 after(async function () {
89cd1275 422 await cleanupTests(servers)
9b474844
C
423 })
424})