]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/plugins/filter-hooks.ts
Faster ci using compiled ts files
[github/Chocobozzz/PeerTube.git] / server / tests / plugins / filter-hooks.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 { advancedVideoChannelSearch } from '@shared/extra-utils/search/video-channels'
6 import { ServerConfig } from '@shared/models'
7 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
8 import {
9 addVideoCommentReply,
10 addVideoCommentThread,
11 advancedVideosSearch,
12 createLive,
13 createVideoPlaylist,
14 doubleFollow,
15 getAccountVideos,
16 getConfig,
17 getMyVideos,
18 getPluginTestPath,
19 getVideo,
20 getVideoChannelVideos,
21 getVideoCommentThreads,
22 getVideoPlaylist,
23 getVideosList,
24 getVideosListPagination,
25 getVideoThreadComments,
26 getVideoWithToken,
27 installPlugin,
28 makeRawRequest,
29 registerUser,
30 setAccessTokensToServers,
31 setDefaultVideoChannel,
32 updateCustomSubConfig,
33 updateVideo,
34 uploadVideo,
35 uploadVideoAndGetId,
36 waitJobs
37 } from '../../../shared/extra-utils'
38 import { cleanupTests, flushAndRunMultipleServers, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers'
39 import { getGoodVideoUrl, getMyVideoImports, importVideo } from '../../../shared/extra-utils/videos/video-imports'
40 import {
41 VideoCommentThreadTree,
42 VideoDetails,
43 VideoImport,
44 VideoImportState,
45 VideoPlaylist,
46 VideoPlaylistPrivacy,
47 VideoPrivacy
48 } from '../../../shared/models/videos'
49
50 const expect = chai.expect
51
52 describe('Test plugin filter hooks', function () {
53 let servers: ServerInfo[]
54 let videoUUID: string
55 let threadId: number
56
57 before(async function () {
58 this.timeout(60000)
59
60 servers = await flushAndRunMultipleServers(2)
61 await setAccessTokensToServers(servers)
62 await setDefaultVideoChannel(servers)
63 await doubleFollow(servers[0], servers[1])
64
65 await installPlugin({
66 url: servers[0].url,
67 accessToken: servers[0].accessToken,
68 path: getPluginTestPath()
69 })
70
71 await installPlugin({
72 url: servers[0].url,
73 accessToken: servers[0].accessToken,
74 path: getPluginTestPath('-two')
75 })
76
77 for (let i = 0; i < 10; i++) {
78 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i })
79 }
80
81 const res = await getVideosList(servers[0].url)
82 videoUUID = res.body.data[0].uuid
83
84 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
85 live: { enabled: true },
86 signup: { enabled: true },
87 import: {
88 videos: {
89 http: { enabled: true },
90 torrent: { enabled: true }
91 }
92 }
93 })
94 })
95
96 it('Should run filter:api.videos.list.params', async function () {
97 const res = await getVideosListPagination(servers[0].url, 0, 2)
98
99 // 2 plugins do +1 to the count parameter
100 expect(res.body.data).to.have.lengthOf(4)
101 })
102
103 it('Should run filter:api.videos.list.result', async function () {
104 const res = await getVideosListPagination(servers[0].url, 0, 0)
105
106 // Plugin do +1 to the total result
107 expect(res.body.total).to.equal(11)
108 })
109
110 it('Should run filter:api.accounts.videos.list.params', async function () {
111 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
112
113 // 1 plugin do +1 to the count parameter
114 expect(res.body.data).to.have.lengthOf(3)
115 })
116
117 it('Should run filter:api.accounts.videos.list.result', async function () {
118 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
119
120 // Plugin do +2 to the total result
121 expect(res.body.total).to.equal(12)
122 })
123
124 it('Should run filter:api.video-channels.videos.list.params', async function () {
125 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
126
127 // 1 plugin do +3 to the count parameter
128 expect(res.body.data).to.have.lengthOf(5)
129 })
130
131 it('Should run filter:api.video-channels.videos.list.result', async function () {
132 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
133
134 // Plugin do +3 to the total result
135 expect(res.body.total).to.equal(13)
136 })
137
138 it('Should run filter:api.user.me.videos.list.params', async function () {
139 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
140
141 // 1 plugin do +4 to the count parameter
142 expect(res.body.data).to.have.lengthOf(6)
143 })
144
145 it('Should run filter:api.user.me.videos.list.result', async function () {
146 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
147
148 // Plugin do +4 to the total result
149 expect(res.body.total).to.equal(14)
150 })
151
152 it('Should run filter:api.video.get.result', async function () {
153 const res = await getVideo(servers[0].url, videoUUID)
154
155 expect(res.body.name).to.contain('<3')
156 })
157
158 it('Should run filter:api.video.upload.accept.result', async function () {
159 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, HttpStatusCode.FORBIDDEN_403)
160 })
161
162 it('Should run filter:api.live-video.create.accept.result', async function () {
163 const attributes = {
164 name: 'video with bad word',
165 privacy: VideoPrivacy.PUBLIC,
166 channelId: servers[0].videoChannel.id
167 }
168
169 await createLive(servers[0].url, servers[0].accessToken, attributes, HttpStatusCode.FORBIDDEN_403)
170 })
171
172 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
173 const baseAttributes = {
174 name: 'normal title',
175 privacy: VideoPrivacy.PUBLIC,
176 channelId: servers[0].videoChannel.id,
177 targetUrl: getGoodVideoUrl() + 'bad'
178 }
179 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
180 })
181
182 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
183 const baseAttributes = {
184 name: 'bad torrent',
185 privacy: VideoPrivacy.PUBLIC,
186 channelId: servers[0].videoChannel.id,
187 torrentfile: 'video-720p.torrent' as any
188 }
189 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
190 })
191
192 it('Should run filter:api.video.post-import-url.accept.result', async function () {
193 this.timeout(60000)
194
195 let videoImportId: number
196
197 {
198 const baseAttributes = {
199 name: 'title with bad word',
200 privacy: VideoPrivacy.PUBLIC,
201 channelId: servers[0].videoChannel.id,
202 targetUrl: getGoodVideoUrl()
203 }
204 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
205 videoImportId = res.body.id
206 }
207
208 await waitJobs(servers)
209
210 {
211 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
212 const videoImports = res.body.data as VideoImport[]
213
214 const videoImport = videoImports.find(i => i.id === videoImportId)
215
216 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
217 expect(videoImport.state.label).to.equal('Rejected')
218 }
219 })
220
221 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
222 this.timeout(60000)
223
224 let videoImportId: number
225
226 {
227 const baseAttributes = {
228 name: 'title with bad word',
229 privacy: VideoPrivacy.PUBLIC,
230 channelId: servers[0].videoChannel.id,
231 torrentfile: 'video-720p.torrent' as any
232 }
233 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
234 videoImportId = res.body.id
235 }
236
237 await waitJobs(servers)
238
239 {
240 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
241 const videoImports = res.body.data as VideoImport[]
242
243 const videoImport = videoImports.find(i => i.id === videoImportId)
244
245 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
246 expect(videoImport.state.label).to.equal('Rejected')
247 }
248 })
249
250 it('Should run filter:api.video-thread.create.accept.result', async function () {
251 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', HttpStatusCode.FORBIDDEN_403)
252 })
253
254 it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
255 const res = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'thread')
256 threadId = res.body.comment.id
257
258 await addVideoCommentReply(
259 servers[0].url,
260 servers[0].accessToken,
261 videoUUID,
262 threadId,
263 'comment with bad word',
264 HttpStatusCode.FORBIDDEN_403
265 )
266 await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with good word', HttpStatusCode.OK_200)
267 })
268
269 it('Should run filter:api.video-threads.list.params', async function () {
270 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
271
272 // our plugin do +1 to the count parameter
273 expect(res.body.data).to.have.lengthOf(1)
274 })
275
276 it('Should run filter:api.video-threads.list.result', async function () {
277 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
278
279 // Plugin do +1 to the total result
280 expect(res.body.total).to.equal(2)
281 })
282
283 it('Should run filter:api.video-thread-comments.list.params')
284
285 it('Should run filter:api.video-thread-comments.list.result', async function () {
286 const res = await getVideoThreadComments(servers[0].url, videoUUID, threadId)
287
288 const thread = res.body as VideoCommentThreadTree
289 expect(thread.comment.text.endsWith(' <3')).to.be.true
290 })
291
292 describe('Should run filter:video.auto-blacklist.result', function () {
293
294 async function checkIsBlacklisted (oldRes: any, value: boolean) {
295 const videoId = oldRes.body.video.uuid
296
297 const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, videoId)
298 const video: VideoDetails = res.body
299 expect(video.blacklisted).to.equal(value)
300 }
301
302 it('Should blacklist on upload', async function () {
303 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video please blacklist me' })
304 await checkIsBlacklisted(res, true)
305 })
306
307 it('Should blacklist on import', async function () {
308 this.timeout(15000)
309
310 const attributes = {
311 name: 'video please blacklist me',
312 targetUrl: getGoodVideoUrl(),
313 channelId: servers[0].videoChannel.id
314 }
315 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
316 await checkIsBlacklisted(res, true)
317 })
318
319 it('Should blacklist on update', async function () {
320 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
321 const videoId = res.body.video.uuid
322 await checkIsBlacklisted(res, false)
323
324 await updateVideo(servers[0].url, servers[0].accessToken, videoId, { name: 'please blacklist me' })
325 await checkIsBlacklisted(res, true)
326 })
327
328 it('Should blacklist on remote upload', async function () {
329 this.timeout(120000)
330
331 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'remote please blacklist me' })
332 await waitJobs(servers)
333
334 await checkIsBlacklisted(res, true)
335 })
336
337 it('Should blacklist on remote update', async function () {
338 this.timeout(120000)
339
340 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video' })
341 await waitJobs(servers)
342
343 const videoId = res.body.video.uuid
344 await checkIsBlacklisted(res, false)
345
346 await updateVideo(servers[1].url, servers[1].accessToken, videoId, { name: 'please blacklist me' })
347 await waitJobs(servers)
348
349 await checkIsBlacklisted(res, true)
350 })
351 })
352
353 describe('Should run filter:api.user.signup.allowed.result', function () {
354
355 it('Should run on config endpoint', async function () {
356 const res = await getConfig(servers[0].url)
357 expect((res.body as ServerConfig).signup.allowed).to.be.true
358 })
359
360 it('Should allow a signup', async function () {
361 await registerUser(servers[0].url, 'john', 'password')
362 })
363
364 it('Should not allow a signup', async function () {
365 const res = await registerUser(servers[0].url, 'jma', 'password', HttpStatusCode.FORBIDDEN_403)
366
367 expect(res.body.error).to.equal('No jma')
368 })
369 })
370
371 describe('Download hooks', function () {
372 const downloadVideos: VideoDetails[] = []
373
374 before(async function () {
375 this.timeout(60000)
376
377 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
378 transcoding: {
379 webtorrent: {
380 enabled: true
381 },
382 hls: {
383 enabled: true
384 }
385 }
386 })
387
388 const uuids: string[] = []
389
390 for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) {
391 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
392 uuids.push(uuid)
393 }
394
395 await waitJobs(servers)
396
397 for (const uuid of uuids) {
398 const res = await getVideo(servers[0].url, uuid)
399 downloadVideos.push(res.body)
400 }
401 })
402
403 it('Should run filter:api.download.torrent.allowed.result', async function () {
404 const res = await makeRawRequest(downloadVideos[0].files[0].torrentDownloadUrl, 403)
405 expect(res.body.error).to.equal('Liu Bei')
406
407 await makeRawRequest(downloadVideos[1].files[0].torrentDownloadUrl, 200)
408 await makeRawRequest(downloadVideos[2].files[0].torrentDownloadUrl, 200)
409 })
410
411 it('Should run filter:api.download.video.allowed.result', async function () {
412 {
413 const res = await makeRawRequest(downloadVideos[1].files[0].fileDownloadUrl, 403)
414 expect(res.body.error).to.equal('Cao Cao')
415
416 await makeRawRequest(downloadVideos[0].files[0].fileDownloadUrl, 200)
417 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
418 }
419
420 {
421 const res = await makeRawRequest(downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl, 403)
422 expect(res.body.error).to.equal('Sun Jian')
423
424 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
425
426 await makeRawRequest(downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
427 await makeRawRequest(downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
428 }
429 })
430 })
431
432 describe('Embed filters', function () {
433 const embedVideos: VideoDetails[] = []
434 const embedPlaylists: VideoPlaylist[] = []
435
436 before(async function () {
437 this.timeout(60000)
438
439 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
440 transcoding: {
441 enabled: false
442 }
443 })
444
445 for (const name of [ 'bad embed', 'good embed' ]) {
446 {
447 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
448 const res = await getVideo(servers[0].url, uuid)
449 embedVideos.push(res.body)
450 }
451
452 {
453 const playlistAttrs = { displayName: name, videoChannelId: servers[0].videoChannel.id, privacy: VideoPlaylistPrivacy.PUBLIC }
454 const res = await createVideoPlaylist({ url: servers[0].url, token: servers[0].accessToken, playlistAttrs })
455
456 const resPlaylist = await getVideoPlaylist(servers[0].url, res.body.videoPlaylist.id)
457 embedPlaylists.push(resPlaylist.body)
458 }
459 }
460 })
461
462 it('Should run filter:html.embed.video.allowed.result', async function () {
463 const res = await makeRawRequest(servers[0].url + embedVideos[0].embedPath, 200)
464 expect(res.text).to.equal('Lu Bu')
465 })
466
467 it('Should run filter:html.embed.video-playlist.allowed.result', async function () {
468 const res = await makeRawRequest(servers[0].url + embedPlaylists[0].embedPath, 200)
469 expect(res.text).to.equal('Diao Chan')
470 })
471 })
472
473 describe('Search filters', function () {
474
475 before(async function () {
476 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
477 search: {
478 searchIndex: {
479 enabled: true,
480 isDefaultSearch: false,
481 disableLocalSearch: false
482 }
483 }
484 })
485 })
486
487 it('Should run filter:api.search.videos.local.list.{params,result}', async function () {
488 await advancedVideosSearch(servers[0].url, {
489 search: 'Sun Quan'
490 })
491
492 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.params', 1)
493 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.result', 1)
494 })
495
496 it('Should run filter:api.search.videos.index.list.{params,result}', async function () {
497 await advancedVideosSearch(servers[0].url, {
498 search: 'Sun Quan',
499 searchTarget: 'search-index'
500 })
501
502 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.params', 1)
503 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.result', 1)
504 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.index.list.params', 1)
505 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.index.list.result', 1)
506 })
507
508 it('Should run filter:api.search.video-channels.local.list.{params,result}', async function () {
509 await advancedVideoChannelSearch(servers[0].url, {
510 search: 'Sun Ce'
511 })
512
513 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.params', 1)
514 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.result', 1)
515 })
516
517 it('Should run filter:api.search.video-channels.index.list.{params,result}', async function () {
518 await advancedVideoChannelSearch(servers[0].url, {
519 search: 'Sun Ce',
520 searchTarget: 'search-index'
521 })
522
523 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.params', 1)
524 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.result', 1)
525 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.index.list.params', 1)
526 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.index.list.result', 1)
527 })
528 })
529
530 after(async function () {
531 await cleanupTests(servers)
532 })
533 })