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