1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { HttpStatusCode, VideoDetails, VideoImportState, VideoPlaylist, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
12 setAccessTokensToServers,
13 setDefaultVideoChannel,
15 } from '@shared/server-commands'
16 import { FIXTURE_URLS } from '../shared'
18 const expect = chai.expect
20 describe('Test plugin filter hooks', function () {
21 let servers: PeerTubeServer[]
24 let videoPlaylistUUID: string
26 before(async function () {
29 servers = await createMultipleServers(2)
30 await setAccessTokensToServers(servers)
31 await setDefaultVideoChannel(servers)
32 await doubleFollow(servers[0], servers[1])
34 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath() })
35 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath('-filter-translations') })
37 ({ uuid: videoPlaylistUUID } = await servers[0].playlists.create({
39 displayName: 'my super playlist',
40 privacy: VideoPlaylistPrivacy.PUBLIC,
41 description: 'my super description',
42 videoChannelId: servers[0].store.channel.id
47 for (let i = 0; i < 10; i++) {
48 const video = await servers[0].videos.upload({ attributes: { name: 'default video ' + i } })
49 await servers[0].playlists.addElement({ playlistId: videoPlaylistUUID, attributes: { videoId: video.id } })
52 const { data } = await servers[0].videos.list()
53 videoUUID = data[0].uuid
55 await servers[0].config.updateCustomSubConfig({
57 live: { enabled: true },
58 signup: { enabled: true },
61 http: { enabled: true },
62 torrent: { enabled: true }
69 it('Should run filter:api.videos.list.params', async function () {
70 const { data } = await servers[0].videos.list({ start: 0, count: 2 })
72 // 2 plugins do +1 to the count parameter
73 expect(data).to.have.lengthOf(4)
76 it('Should run filter:api.videos.list.result', async function () {
77 const { total } = await servers[0].videos.list({ start: 0, count: 0 })
79 // Plugin do +1 to the total result
80 expect(total).to.equal(11)
83 it('Should run filter:api.video-playlist.videos.list.params', async function () {
84 const { data } = await servers[0].playlists.listVideos({
86 playlistId: videoPlaylistUUID
89 // 1 plugin do +1 to the count parameter
90 expect(data).to.have.lengthOf(3)
93 it('Should run filter:api.video-playlist.videos.list.result', async function () {
94 const { total } = await servers[0].playlists.listVideos({
96 playlistId: videoPlaylistUUID
99 // Plugin do +1 to the total result
100 expect(total).to.equal(11)
103 it('Should run filter:api.accounts.videos.list.params', async function () {
104 const { data } = await servers[0].videos.listByAccount({ handle: 'root', start: 0, count: 2 })
106 // 1 plugin do +1 to the count parameter
107 expect(data).to.have.lengthOf(3)
110 it('Should run filter:api.accounts.videos.list.result', async function () {
111 const { total } = await servers[0].videos.listByAccount({ handle: 'root', start: 0, count: 2 })
113 // Plugin do +2 to the total result
114 expect(total).to.equal(12)
117 it('Should run filter:api.video-channels.videos.list.params', async function () {
118 const { data } = await servers[0].videos.listByChannel({ handle: 'root_channel', start: 0, count: 2 })
120 // 1 plugin do +3 to the count parameter
121 expect(data).to.have.lengthOf(5)
124 it('Should run filter:api.video-channels.videos.list.result', async function () {
125 const { total } = await servers[0].videos.listByChannel({ handle: 'root_channel', start: 0, count: 2 })
127 // Plugin do +3 to the total result
128 expect(total).to.equal(13)
131 it('Should run filter:api.user.me.videos.list.params', async function () {
132 const { data } = await servers[0].videos.listMyVideos({ start: 0, count: 2 })
134 // 1 plugin do +4 to the count parameter
135 expect(data).to.have.lengthOf(6)
138 it('Should run filter:api.user.me.videos.list.result', async function () {
139 const { total } = await servers[0].videos.listMyVideos({ start: 0, count: 2 })
141 // Plugin do +4 to the total result
142 expect(total).to.equal(14)
145 it('Should run filter:api.video.get.result', async function () {
146 const video = await servers[0].videos.get({ id: videoUUID })
147 expect(video.name).to.contain('<3')
150 it('Should run filter:api.video.upload.accept.result', async function () {
151 await servers[0].videos.upload({ attributes: { name: 'video with bad word' }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
154 it('Should run filter:api.live-video.create.accept.result', async function () {
156 name: 'video with bad word',
157 privacy: VideoPrivacy.PUBLIC,
158 channelId: servers[0].store.channel.id
161 await servers[0].live.create({ fields: attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
164 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
166 name: 'normal title',
167 privacy: VideoPrivacy.PUBLIC,
168 channelId: servers[0].store.channel.id,
169 targetUrl: FIXTURE_URLS.goodVideo + 'bad'
171 await servers[0].imports.importVideo({ attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
174 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
177 privacy: VideoPrivacy.PUBLIC,
178 channelId: servers[0].store.channel.id,
179 torrentfile: 'video-720p.torrent' as any
181 await servers[0].imports.importVideo({ attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
184 it('Should run filter:api.video.post-import-url.accept.result', async function () {
187 let videoImportId: number
191 name: 'title with bad word',
192 privacy: VideoPrivacy.PUBLIC,
193 channelId: servers[0].store.channel.id,
194 targetUrl: FIXTURE_URLS.goodVideo
196 const body = await servers[0].imports.importVideo({ attributes })
197 videoImportId = body.id
200 await waitJobs(servers)
203 const body = await servers[0].imports.getMyVideoImports()
204 const videoImports = body.data
206 const videoImport = videoImports.find(i => i.id === videoImportId)
208 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
209 expect(videoImport.state.label).to.equal('Rejected')
213 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
216 let videoImportId: number
220 name: 'title with bad word',
221 privacy: VideoPrivacy.PUBLIC,
222 channelId: servers[0].store.channel.id,
223 torrentfile: 'video-720p.torrent' as any
225 const body = await servers[0].imports.importVideo({ attributes })
226 videoImportId = body.id
229 await waitJobs(servers)
232 const { data: videoImports } = await servers[0].imports.getMyVideoImports()
234 const videoImport = videoImports.find(i => i.id === videoImportId)
236 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
237 expect(videoImport.state.label).to.equal('Rejected')
241 it('Should run filter:api.video-thread.create.accept.result', async function () {
242 await servers[0].comments.createThread({
244 text: 'comment with bad word',
245 expectedStatus: HttpStatusCode.FORBIDDEN_403
249 it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
250 const created = await servers[0].comments.createThread({ videoId: videoUUID, text: 'thread' })
251 threadId = created.id
253 await servers[0].comments.addReply({
255 toCommentId: threadId,
256 text: 'comment with bad word',
257 expectedStatus: HttpStatusCode.FORBIDDEN_403
259 await servers[0].comments.addReply({
261 toCommentId: threadId,
262 text: 'comment with good word',
263 expectedStatus: HttpStatusCode.OK_200
267 it('Should run filter:api.video-threads.list.params', async function () {
268 const { data } = await servers[0].comments.listThreads({ videoId: videoUUID, start: 0, count: 0 })
270 // our plugin do +1 to the count parameter
271 expect(data).to.have.lengthOf(1)
274 it('Should run filter:api.video-threads.list.result', async function () {
275 const { total } = await servers[0].comments.listThreads({ videoId: videoUUID, start: 0, count: 0 })
277 // Plugin do +1 to the total result
278 expect(total).to.equal(2)
281 it('Should run filter:api.video-thread-comments.list.params')
283 it('Should run filter:api.video-thread-comments.list.result', async function () {
284 const thread = await servers[0].comments.getThread({ videoId: videoUUID, threadId })
286 expect(thread.comment.text.endsWith(' <3')).to.be.true
289 it('Should run filter:api.overviews.videos.list.{params,result}', async function () {
290 await servers[0].overviews.getVideos({ page: 1 })
292 // 3 because we get 3 samples per page
293 await servers[0].servers.waitUntilLog('Run hook filter:api.overviews.videos.list.params', 3)
294 await servers[0].servers.waitUntilLog('Run hook filter:api.overviews.videos.list.result', 3)
297 describe('filter:video.auto-blacklist.result', function () {
299 async function checkIsBlacklisted (id: number | string, value: boolean) {
300 const video = await servers[0].videos.getWithToken({ id })
301 expect(video.blacklisted).to.equal(value)
304 it('Should blacklist on upload', async function () {
305 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video please blacklist me' } })
306 await checkIsBlacklisted(uuid, true)
309 it('Should blacklist on import', async function () {
313 name: 'video please blacklist me',
314 targetUrl: FIXTURE_URLS.goodVideo,
315 channelId: servers[0].store.channel.id
317 const body = await servers[0].imports.importVideo({ attributes })
318 await checkIsBlacklisted(body.video.uuid, true)
321 it('Should blacklist on update', async function () {
322 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video' } })
323 await checkIsBlacklisted(uuid, false)
325 await servers[0].videos.update({ id: uuid, attributes: { name: 'please blacklist me' } })
326 await checkIsBlacklisted(uuid, true)
329 it('Should blacklist on remote upload', async function () {
332 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'remote please blacklist me' } })
333 await waitJobs(servers)
335 await checkIsBlacklisted(uuid, true)
338 it('Should blacklist on remote update', async function () {
341 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video' } })
342 await waitJobs(servers)
344 await checkIsBlacklisted(uuid, false)
346 await servers[1].videos.update({ id: uuid, attributes: { name: 'please blacklist me' } })
347 await waitJobs(servers)
349 await checkIsBlacklisted(uuid, true)
353 describe('Should run filter:api.user.signup.allowed.result', function () {
355 it('Should run on config endpoint', async function () {
356 const body = await servers[0].config.getConfig()
357 expect(body.signup.allowed).to.be.true
360 it('Should allow a signup', async function () {
361 await servers[0].users.register({ username: 'john', password: 'password' })
364 it('Should not allow a signup', async function () {
365 const res = await servers[0].users.register({
367 password: 'password',
368 expectedStatus: HttpStatusCode.FORBIDDEN_403
371 expect(res.body.error).to.equal('No jma')
375 describe('Download hooks', function () {
376 const downloadVideos: VideoDetails[] = []
378 before(async function () {
381 await servers[0].config.updateCustomSubConfig({
394 const uuids: string[] = []
396 for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) {
397 const uuid = (await servers[0].videos.quickUpload({ name })).uuid
401 await waitJobs(servers)
403 for (const uuid of uuids) {
404 downloadVideos.push(await servers[0].videos.get({ id: uuid }))
408 it('Should run filter:api.download.torrent.allowed.result', async function () {
409 const res = await makeRawRequest(downloadVideos[0].files[0].torrentDownloadUrl, 403)
410 expect(res.body.error).to.equal('Liu Bei')
412 await makeRawRequest(downloadVideos[1].files[0].torrentDownloadUrl, 200)
413 await makeRawRequest(downloadVideos[2].files[0].torrentDownloadUrl, 200)
416 it('Should run filter:api.download.video.allowed.result', async function () {
418 const res = await makeRawRequest(downloadVideos[1].files[0].fileDownloadUrl, 403)
419 expect(res.body.error).to.equal('Cao Cao')
421 await makeRawRequest(downloadVideos[0].files[0].fileDownloadUrl, 200)
422 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
426 const res = await makeRawRequest(downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl, 403)
427 expect(res.body.error).to.equal('Sun Jian')
429 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
431 await makeRawRequest(downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
432 await makeRawRequest(downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
437 describe('Embed filters', function () {
438 const embedVideos: VideoDetails[] = []
439 const embedPlaylists: VideoPlaylist[] = []
441 before(async function () {
444 await servers[0].config.disableTranscoding()
446 for (const name of [ 'bad embed', 'good embed' ]) {
448 const uuid = (await servers[0].videos.quickUpload({ name })).uuid
449 embedVideos.push(await servers[0].videos.get({ id: uuid }))
453 const attributes = { displayName: name, videoChannelId: servers[0].store.channel.id, privacy: VideoPlaylistPrivacy.PUBLIC }
454 const { id } = await servers[0].playlists.create({ attributes })
456 const playlist = await servers[0].playlists.get({ playlistId: id })
457 embedPlaylists.push(playlist)
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')
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')
473 describe('Search filters', function () {
475 before(async function () {
476 await servers[0].config.updateCustomSubConfig({
481 isDefaultSearch: false,
482 disableLocalSearch: false
489 it('Should run filter:api.search.videos.local.list.{params,result}', async function () {
490 await servers[0].search.advancedVideoSearch({
496 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.params', 1)
497 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.result', 1)
500 it('Should run filter:api.search.videos.index.list.{params,result}', async function () {
501 await servers[0].search.advancedVideoSearch({
504 searchTarget: 'search-index'
508 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.params', 1)
509 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.result', 1)
510 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.index.list.params', 1)
511 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.index.list.result', 1)
514 it('Should run filter:api.search.video-channels.local.list.{params,result}', async function () {
515 await servers[0].search.advancedChannelSearch({
521 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.params', 1)
522 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.result', 1)
525 it('Should run filter:api.search.video-channels.index.list.{params,result}', async function () {
526 await servers[0].search.advancedChannelSearch({
529 searchTarget: 'search-index'
533 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.params', 1)
534 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.result', 1)
535 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.index.list.params', 1)
536 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.index.list.result', 1)
539 it('Should run filter:api.search.video-playlists.local.list.{params,result}', async function () {
540 await servers[0].search.advancedPlaylistSearch({
546 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.params', 1)
547 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.result', 1)
550 it('Should run filter:api.search.video-playlists.index.list.{params,result}', async function () {
551 await servers[0].search.advancedPlaylistSearch({
554 searchTarget: 'search-index'
558 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.params', 1)
559 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.result', 1)
560 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.index.list.params', 1)
561 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.index.list.result', 1)
565 describe('Upload/import/live attributes filters', function () {
567 before(async function () {
568 await servers[0].config.enableLive({ transcoding: false, allowReplay: false })
569 await servers[0].config.enableImports()
570 await servers[0].config.disableTranscoding()
573 it('Should run filter:api.video.upload.video-attribute.result', async function () {
574 for (const mode of [ 'legacy' as 'legacy', 'resumable' as 'resumable' ]) {
575 const { id } = await servers[0].videos.upload({ attributes: { name: 'video', description: 'upload' }, mode })
577 const video = await servers[0].videos.get({ id })
578 expect(video.description).to.equal('upload - filter:api.video.upload.video-attribute.result')
582 it('Should run filter:api.video.import-url.video-attribute.result', async function () {
585 description: 'import url',
586 channelId: servers[0].store.channel.id,
587 targetUrl: FIXTURE_URLS.goodVideo,
588 privacy: VideoPrivacy.PUBLIC
590 const { video: { id } } = await servers[0].imports.importVideo({ attributes })
592 const video = await servers[0].videos.get({ id })
593 expect(video.description).to.equal('import url - filter:api.video.import-url.video-attribute.result')
596 it('Should run filter:api.video.import-torrent.video-attribute.result', async function () {
599 description: 'import torrent',
600 channelId: servers[0].store.channel.id,
601 magnetUri: FIXTURE_URLS.magnet,
602 privacy: VideoPrivacy.PUBLIC
604 const { video: { id } } = await servers[0].imports.importVideo({ attributes })
606 const video = await servers[0].videos.get({ id })
607 expect(video.description).to.equal('import torrent - filter:api.video.import-torrent.video-attribute.result')
610 it('Should run filter:api.video.live.video-attribute.result', async function () {
614 channelId: servers[0].store.channel.id,
615 privacy: VideoPrivacy.PUBLIC
617 const { id } = await servers[0].live.create({ fields })
619 const video = await servers[0].videos.get({ id })
620 expect(video.description).to.equal('live - filter:api.video.live.video-attribute.result')
624 describe('Stats filters', function () {
626 it('Should run filter:api.server.stats.get.result', async function () {
627 const data = await servers[0].stats.get()
629 expect((data as any).customStats).to.equal(14)
634 describe('Job queue filters', function () {
635 let videoUUID: string
637 before(async function () {
638 this.timeout(120_000)
640 await servers[0].config.enableMinimumTranscoding()
641 const { uuid } = await servers[0].videos.quickUpload({ name: 'studio' })
643 const video = await servers[0].videos.get({ id: uuid })
644 expect(video.duration).at.least(2)
645 videoUUID = video.uuid
647 await waitJobs(servers)
649 await servers[0].config.enableStudio()
652 it('Should run filter:job-queue.process.params', async function () {
653 this.timeout(120_000)
655 await servers[0].videoStudio.createEditionTasks({
661 file: 'video_very_short_240p.mp4'
667 await waitJobs(servers)
669 await servers[0].servers.waitUntilLog('Run hook filter:job-queue.process.params', 1, false)
671 const video = await servers[0].videos.get({ id: videoUUID })
672 expect(video.duration).at.most(2)
675 it('Should run filter:job-queue.process.result', async function () {
676 await servers[0].servers.waitUntilLog('Run hook filter:job-queue.process.result', 1, false)
680 describe('Transcoding filters', async function () {
682 it('Should run filter:transcoding.auto.resolutions-to-transcode.result', async function () {
683 const { uuid } = await servers[0].videos.quickUpload({ name: 'transcode-filter' })
685 await waitJobs(servers)
687 const video = await servers[0].videos.get({ id: uuid })
688 expect(video.files).to.have.lengthOf(2)
689 expect(video.files.find(f => f.resolution.id === 100 as any)).to.exist
693 describe('Video channel filters', async function () {
695 it('Should run filter:api.video-channels.list.params', async function () {
696 const { data } = await servers[0].channels.list({ start: 0, count: 0 })
698 // plugin do +1 to the count parameter
699 expect(data).to.have.lengthOf(1)
702 it('Should run filter:api.video-channels.list.result', async function () {
703 const { total } = await servers[0].channels.list({ start: 0, count: 1 })
705 // plugin do +1 to the total parameter
706 expect(total).to.equal(4)
709 it('Should run filter:api.video-channel.get.result', async function () {
710 const channel = await servers[0].channels.get({ channelName: 'root_channel' })
711 expect(channel.displayName).to.equal('Main root channel <3')
715 after(async function () {
716 await cleanupTests(servers)