1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } 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 describe('Test plugin filter hooks', function () {
19 let servers: PeerTubeServer[]
22 let videoPlaylistUUID: string
24 before(async function () {
27 servers = await createMultipleServers(2)
28 await setAccessTokensToServers(servers)
29 await setDefaultVideoChannel(servers)
30 await doubleFollow(servers[0], servers[1])
32 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath() })
33 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath('-filter-translations') })
35 ({ uuid: videoPlaylistUUID } = await servers[0].playlists.create({
37 displayName: 'my super playlist',
38 privacy: VideoPlaylistPrivacy.PUBLIC,
39 description: 'my super description',
40 videoChannelId: servers[0].store.channel.id
45 for (let i = 0; i < 10; i++) {
46 const video = await servers[0].videos.upload({ attributes: { name: 'default video ' + i } })
47 await servers[0].playlists.addElement({ playlistId: videoPlaylistUUID, attributes: { videoId: video.id } })
50 const { data } = await servers[0].videos.list()
51 videoUUID = data[0].uuid
53 await servers[0].config.updateCustomSubConfig({
55 live: { enabled: true },
56 signup: { enabled: true },
59 http: { enabled: true },
60 torrent: { enabled: true }
67 describe('Videos', function () {
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')
151 describe('Video/live/import accept', function () {
153 it('Should run filter:api.video.upload.accept.result', async function () {
154 await servers[0].videos.upload({ attributes: { name: 'video with bad word' }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
157 it('Should run filter:api.live-video.create.accept.result', async function () {
159 name: 'video with bad word',
160 privacy: VideoPrivacy.PUBLIC,
161 channelId: servers[0].store.channel.id
164 await servers[0].live.create({ fields: attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
167 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
169 name: 'normal title',
170 privacy: VideoPrivacy.PUBLIC,
171 channelId: servers[0].store.channel.id,
172 targetUrl: FIXTURE_URLS.goodVideo + 'bad'
174 await servers[0].imports.importVideo({ attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
177 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
180 privacy: VideoPrivacy.PUBLIC,
181 channelId: servers[0].store.channel.id,
182 torrentfile: 'video-720p.torrent' as any
184 await servers[0].imports.importVideo({ attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
187 it('Should run filter:api.video.post-import-url.accept.result', async function () {
190 let videoImportId: number
194 name: 'title with bad word',
195 privacy: VideoPrivacy.PUBLIC,
196 channelId: servers[0].store.channel.id,
197 targetUrl: FIXTURE_URLS.goodVideo
199 const body = await servers[0].imports.importVideo({ attributes })
200 videoImportId = body.id
203 await waitJobs(servers)
206 const body = await servers[0].imports.getMyVideoImports()
207 const videoImports = body.data
209 const videoImport = videoImports.find(i => i.id === videoImportId)
211 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
212 expect(videoImport.state.label).to.equal('Rejected')
216 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
219 let videoImportId: number
223 name: 'title with bad word',
224 privacy: VideoPrivacy.PUBLIC,
225 channelId: servers[0].store.channel.id,
226 torrentfile: 'video-720p.torrent' as any
228 const body = await servers[0].imports.importVideo({ attributes })
229 videoImportId = body.id
232 await waitJobs(servers)
235 const { data: videoImports } = await servers[0].imports.getMyVideoImports()
237 const videoImport = videoImports.find(i => i.id === videoImportId)
239 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
240 expect(videoImport.state.label).to.equal('Rejected')
245 describe('Video comments accept', function () {
247 it('Should run filter:api.video-thread.create.accept.result', async function () {
248 await servers[0].comments.createThread({
250 text: 'comment with bad word',
251 expectedStatus: HttpStatusCode.FORBIDDEN_403
255 it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
256 const created = await servers[0].comments.createThread({ videoId: videoUUID, text: 'thread' })
257 threadId = created.id
259 await servers[0].comments.addReply({
261 toCommentId: threadId,
262 text: 'comment with bad word',
263 expectedStatus: HttpStatusCode.FORBIDDEN_403
265 await servers[0].comments.addReply({
267 toCommentId: threadId,
268 text: 'comment with good word',
269 expectedStatus: HttpStatusCode.OK_200
273 it('Should run filter:activity-pub.remote-video-comment.create.accept.result on a thread creation', async function () {
276 await servers[1].comments.createThread({ videoId: videoUUID, text: 'comment with bad word' })
278 await waitJobs(servers)
281 const thread = await servers[0].comments.listThreads({ videoId: videoUUID })
282 expect(thread.data).to.have.lengthOf(1)
283 expect(thread.data[0].text).to.not.include(' bad ')
287 const thread = await servers[1].comments.listThreads({ videoId: videoUUID })
288 expect(thread.data).to.have.lengthOf(2)
292 it('Should run filter:activity-pub.remote-video-comment.create.accept.result on a reply creation', async function () {
295 const { data } = await servers[1].comments.listThreads({ videoId: videoUUID })
296 const threadIdServer2 = data.find(t => t.text === 'thread').id
298 await servers[1].comments.addReply({
300 toCommentId: threadIdServer2,
301 text: 'comment with bad word'
304 await waitJobs(servers)
307 const tree = await servers[0].comments.getThread({ videoId: videoUUID, threadId })
308 expect(tree.children).to.have.lengthOf(1)
309 expect(tree.children[0].comment.text).to.not.include(' bad ')
313 const tree = await servers[1].comments.getThread({ videoId: videoUUID, threadId: threadIdServer2 })
314 expect(tree.children).to.have.lengthOf(2)
319 describe('Video comments', function () {
321 it('Should run filter:api.video-threads.list.params', async function () {
322 const { data } = await servers[0].comments.listThreads({ videoId: videoUUID, start: 0, count: 0 })
324 // our plugin do +1 to the count parameter
325 expect(data).to.have.lengthOf(1)
328 it('Should run filter:api.video-threads.list.result', async function () {
329 const { total } = await servers[0].comments.listThreads({ videoId: videoUUID, start: 0, count: 0 })
331 // Plugin do +1 to the total result
332 expect(total).to.equal(2)
335 it('Should run filter:api.video-thread-comments.list.params')
337 it('Should run filter:api.video-thread-comments.list.result', async function () {
338 const thread = await servers[0].comments.getThread({ videoId: videoUUID, threadId })
340 expect(thread.comment.text.endsWith(' <3')).to.be.true
343 it('Should run filter:api.overviews.videos.list.{params,result}', async function () {
344 await servers[0].overviews.getVideos({ page: 1 })
346 // 3 because we get 3 samples per page
347 await servers[0].servers.waitUntilLog('Run hook filter:api.overviews.videos.list.params', 3)
348 await servers[0].servers.waitUntilLog('Run hook filter:api.overviews.videos.list.result', 3)
352 describe('filter:video.auto-blacklist.result', function () {
354 async function checkIsBlacklisted (id: number | string, value: boolean) {
355 const video = await servers[0].videos.getWithToken({ id })
356 expect(video.blacklisted).to.equal(value)
359 it('Should blacklist on upload', async function () {
360 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video please blacklist me' } })
361 await checkIsBlacklisted(uuid, true)
364 it('Should blacklist on import', async function () {
368 name: 'video please blacklist me',
369 targetUrl: FIXTURE_URLS.goodVideo,
370 channelId: servers[0].store.channel.id
372 const body = await servers[0].imports.importVideo({ attributes })
373 await checkIsBlacklisted(body.video.uuid, true)
376 it('Should blacklist on update', async function () {
377 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video' } })
378 await checkIsBlacklisted(uuid, false)
380 await servers[0].videos.update({ id: uuid, attributes: { name: 'please blacklist me' } })
381 await checkIsBlacklisted(uuid, true)
384 it('Should blacklist on remote upload', async function () {
387 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'remote please blacklist me' } })
388 await waitJobs(servers)
390 await checkIsBlacklisted(uuid, true)
393 it('Should blacklist on remote update', async function () {
396 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video' } })
397 await waitJobs(servers)
399 await checkIsBlacklisted(uuid, false)
401 await servers[1].videos.update({ id: uuid, attributes: { name: 'please blacklist me' } })
402 await waitJobs(servers)
404 await checkIsBlacklisted(uuid, true)
408 describe('Should run filter:api.user.signup.allowed.result', function () {
410 it('Should run on config endpoint', async function () {
411 const body = await servers[0].config.getConfig()
412 expect(body.signup.allowed).to.be.true
415 it('Should allow a signup', async function () {
416 await servers[0].users.register({ username: 'john', password: 'password' })
419 it('Should not allow a signup', async function () {
420 const res = await servers[0].users.register({
422 password: 'password',
423 expectedStatus: HttpStatusCode.FORBIDDEN_403
426 expect(res.body.error).to.equal('No jma')
430 describe('Download hooks', function () {
431 const downloadVideos: VideoDetails[] = []
433 before(async function () {
436 await servers[0].config.updateCustomSubConfig({
449 const uuids: string[] = []
451 for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) {
452 const uuid = (await servers[0].videos.quickUpload({ name })).uuid
456 await waitJobs(servers)
458 for (const uuid of uuids) {
459 downloadVideos.push(await servers[0].videos.get({ id: uuid }))
463 it('Should run filter:api.download.torrent.allowed.result', async function () {
464 const res = await makeRawRequest(downloadVideos[0].files[0].torrentDownloadUrl, 403)
465 expect(res.body.error).to.equal('Liu Bei')
467 await makeRawRequest(downloadVideos[1].files[0].torrentDownloadUrl, 200)
468 await makeRawRequest(downloadVideos[2].files[0].torrentDownloadUrl, 200)
471 it('Should run filter:api.download.video.allowed.result', async function () {
473 const res = await makeRawRequest(downloadVideos[1].files[0].fileDownloadUrl, 403)
474 expect(res.body.error).to.equal('Cao Cao')
476 await makeRawRequest(downloadVideos[0].files[0].fileDownloadUrl, 200)
477 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
481 const res = await makeRawRequest(downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl, 403)
482 expect(res.body.error).to.equal('Sun Jian')
484 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
486 await makeRawRequest(downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
487 await makeRawRequest(downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
492 describe('Embed filters', function () {
493 const embedVideos: VideoDetails[] = []
494 const embedPlaylists: VideoPlaylist[] = []
496 before(async function () {
499 await servers[0].config.disableTranscoding()
501 for (const name of [ 'bad embed', 'good embed' ]) {
503 const uuid = (await servers[0].videos.quickUpload({ name })).uuid
504 embedVideos.push(await servers[0].videos.get({ id: uuid }))
508 const attributes = { displayName: name, videoChannelId: servers[0].store.channel.id, privacy: VideoPlaylistPrivacy.PUBLIC }
509 const { id } = await servers[0].playlists.create({ attributes })
511 const playlist = await servers[0].playlists.get({ playlistId: id })
512 embedPlaylists.push(playlist)
517 it('Should run filter:html.embed.video.allowed.result', async function () {
518 const res = await makeRawRequest(servers[0].url + embedVideos[0].embedPath, 200)
519 expect(res.text).to.equal('Lu Bu')
522 it('Should run filter:html.embed.video-playlist.allowed.result', async function () {
523 const res = await makeRawRequest(servers[0].url + embedPlaylists[0].embedPath, 200)
524 expect(res.text).to.equal('Diao Chan')
528 describe('Search filters', function () {
530 before(async function () {
531 await servers[0].config.updateCustomSubConfig({
536 isDefaultSearch: false,
537 disableLocalSearch: false
544 it('Should run filter:api.search.videos.local.list.{params,result}', async function () {
545 await servers[0].search.advancedVideoSearch({
551 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.params', 1)
552 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.result', 1)
555 it('Should run filter:api.search.videos.index.list.{params,result}', async function () {
556 await servers[0].search.advancedVideoSearch({
559 searchTarget: 'search-index'
563 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.params', 1)
564 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.result', 1)
565 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.index.list.params', 1)
566 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.index.list.result', 1)
569 it('Should run filter:api.search.video-channels.local.list.{params,result}', async function () {
570 await servers[0].search.advancedChannelSearch({
576 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.params', 1)
577 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.result', 1)
580 it('Should run filter:api.search.video-channels.index.list.{params,result}', async function () {
581 await servers[0].search.advancedChannelSearch({
584 searchTarget: 'search-index'
588 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.params', 1)
589 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.result', 1)
590 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.index.list.params', 1)
591 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.index.list.result', 1)
594 it('Should run filter:api.search.video-playlists.local.list.{params,result}', async function () {
595 await servers[0].search.advancedPlaylistSearch({
601 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.params', 1)
602 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.result', 1)
605 it('Should run filter:api.search.video-playlists.index.list.{params,result}', async function () {
606 await servers[0].search.advancedPlaylistSearch({
609 searchTarget: 'search-index'
613 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.params', 1)
614 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.result', 1)
615 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.index.list.params', 1)
616 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.index.list.result', 1)
620 describe('Upload/import/live attributes filters', function () {
622 before(async function () {
623 await servers[0].config.enableLive({ transcoding: false, allowReplay: false })
624 await servers[0].config.enableImports()
625 await servers[0].config.disableTranscoding()
628 it('Should run filter:api.video.upload.video-attribute.result', async function () {
629 for (const mode of [ 'legacy' as 'legacy', 'resumable' as 'resumable' ]) {
630 const { id } = await servers[0].videos.upload({ attributes: { name: 'video', description: 'upload' }, mode })
632 const video = await servers[0].videos.get({ id })
633 expect(video.description).to.equal('upload - filter:api.video.upload.video-attribute.result')
637 it('Should run filter:api.video.import-url.video-attribute.result', async function () {
640 description: 'import url',
641 channelId: servers[0].store.channel.id,
642 targetUrl: FIXTURE_URLS.goodVideo,
643 privacy: VideoPrivacy.PUBLIC
645 const { video: { id } } = await servers[0].imports.importVideo({ attributes })
647 const video = await servers[0].videos.get({ id })
648 expect(video.description).to.equal('import url - filter:api.video.import-url.video-attribute.result')
651 it('Should run filter:api.video.import-torrent.video-attribute.result', async function () {
654 description: 'import torrent',
655 channelId: servers[0].store.channel.id,
656 magnetUri: FIXTURE_URLS.magnet,
657 privacy: VideoPrivacy.PUBLIC
659 const { video: { id } } = await servers[0].imports.importVideo({ attributes })
661 const video = await servers[0].videos.get({ id })
662 expect(video.description).to.equal('import torrent - filter:api.video.import-torrent.video-attribute.result')
665 it('Should run filter:api.video.live.video-attribute.result', async function () {
669 channelId: servers[0].store.channel.id,
670 privacy: VideoPrivacy.PUBLIC
672 const { id } = await servers[0].live.create({ fields })
674 const video = await servers[0].videos.get({ id })
675 expect(video.description).to.equal('live - filter:api.video.live.video-attribute.result')
679 describe('Stats filters', function () {
681 it('Should run filter:api.server.stats.get.result', async function () {
682 const data = await servers[0].stats.get()
684 expect((data as any).customStats).to.equal(14)
689 describe('Job queue filters', function () {
690 let videoUUID: string
692 before(async function () {
693 this.timeout(120_000)
695 await servers[0].config.enableMinimumTranscoding()
696 const { uuid } = await servers[0].videos.quickUpload({ name: 'studio' })
698 const video = await servers[0].videos.get({ id: uuid })
699 expect(video.duration).at.least(2)
700 videoUUID = video.uuid
702 await waitJobs(servers)
704 await servers[0].config.enableStudio()
707 it('Should run filter:job-queue.process.params', async function () {
708 this.timeout(120_000)
710 await servers[0].videoStudio.createEditionTasks({
716 file: 'video_very_short_240p.mp4'
722 await waitJobs(servers)
724 await servers[0].servers.waitUntilLog('Run hook filter:job-queue.process.params', 1, false)
726 const video = await servers[0].videos.get({ id: videoUUID })
727 expect(video.duration).at.most(2)
730 it('Should run filter:job-queue.process.result', async function () {
731 await servers[0].servers.waitUntilLog('Run hook filter:job-queue.process.result', 1, false)
735 describe('Transcoding filters', async function () {
737 it('Should run filter:transcoding.auto.resolutions-to-transcode.result', async function () {
738 const { uuid } = await servers[0].videos.quickUpload({ name: 'transcode-filter' })
740 await waitJobs(servers)
742 const video = await servers[0].videos.get({ id: uuid })
743 expect(video.files).to.have.lengthOf(2)
744 expect(video.files.find(f => f.resolution.id === 100 as any)).to.exist
748 describe('Video channel filters', async function () {
750 it('Should run filter:api.video-channels.list.params', async function () {
751 const { data } = await servers[0].channels.list({ start: 0, count: 0 })
753 // plugin do +1 to the count parameter
754 expect(data).to.have.lengthOf(1)
757 it('Should run filter:api.video-channels.list.result', async function () {
758 const { total } = await servers[0].channels.list({ start: 0, count: 1 })
760 // plugin do +1 to the total parameter
761 expect(total).to.equal(4)
764 it('Should run filter:api.video-channel.get.result', async function () {
765 const channel = await servers[0].channels.get({ channelName: 'root_channel' })
766 expect(channel.displayName).to.equal('Main root channel <3')
770 after(async function () {
771 await cleanupTests(servers)