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'
13 setAccessTokensToServers,
14 setDefaultVideoChannel,
16 } from '@shared/server-commands'
17 import { FIXTURE_URLS } from '../shared'
19 describe('Test plugin filter hooks', function () {
20 let servers: PeerTubeServer[]
23 let videoPlaylistUUID: string
25 before(async function () {
28 servers = await createMultipleServers(2)
29 await setAccessTokensToServers(servers)
30 await setDefaultVideoChannel(servers)
31 await doubleFollow(servers[0], servers[1])
33 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath() })
34 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath('-filter-translations') })
36 ({ uuid: videoPlaylistUUID } = await servers[0].playlists.create({
38 displayName: 'my super playlist',
39 privacy: VideoPlaylistPrivacy.PUBLIC,
40 description: 'my super description',
41 videoChannelId: servers[0].store.channel.id
46 for (let i = 0; i < 10; i++) {
47 const video = await servers[0].videos.upload({ attributes: { name: 'default video ' + i } })
48 await servers[0].playlists.addElement({ playlistId: videoPlaylistUUID, attributes: { videoId: video.id } })
51 const { data } = await servers[0].videos.list()
52 videoUUID = data[0].uuid
54 await servers[0].config.updateCustomSubConfig({
56 live: { enabled: true },
57 signup: { enabled: true },
60 http: { enabled: true },
61 torrent: { enabled: true }
68 describe('Videos', function () {
70 it('Should run filter:api.videos.list.params', async function () {
71 const { data } = await servers[0].videos.list({ start: 0, count: 2 })
73 // 2 plugins do +1 to the count parameter
74 expect(data).to.have.lengthOf(4)
77 it('Should run filter:api.videos.list.result', async function () {
78 const { total } = await servers[0].videos.list({ start: 0, count: 0 })
80 // Plugin do +1 to the total result
81 expect(total).to.equal(11)
84 it('Should run filter:api.video-playlist.videos.list.params', async function () {
85 const { data } = await servers[0].playlists.listVideos({
87 playlistId: videoPlaylistUUID
90 // 1 plugin do +1 to the count parameter
91 expect(data).to.have.lengthOf(3)
94 it('Should run filter:api.video-playlist.videos.list.result', async function () {
95 const { total } = await servers[0].playlists.listVideos({
97 playlistId: videoPlaylistUUID
100 // Plugin do +1 to the total result
101 expect(total).to.equal(11)
104 it('Should run filter:api.accounts.videos.list.params', async function () {
105 const { data } = await servers[0].videos.listByAccount({ handle: 'root', start: 0, count: 2 })
107 // 1 plugin do +1 to the count parameter
108 expect(data).to.have.lengthOf(3)
111 it('Should run filter:api.accounts.videos.list.result', async function () {
112 const { total } = await servers[0].videos.listByAccount({ handle: 'root', start: 0, count: 2 })
114 // Plugin do +2 to the total result
115 expect(total).to.equal(12)
118 it('Should run filter:api.video-channels.videos.list.params', async function () {
119 const { data } = await servers[0].videos.listByChannel({ handle: 'root_channel', start: 0, count: 2 })
121 // 1 plugin do +3 to the count parameter
122 expect(data).to.have.lengthOf(5)
125 it('Should run filter:api.video-channels.videos.list.result', async function () {
126 const { total } = await servers[0].videos.listByChannel({ handle: 'root_channel', start: 0, count: 2 })
128 // Plugin do +3 to the total result
129 expect(total).to.equal(13)
132 it('Should run filter:api.user.me.videos.list.params', async function () {
133 const { data } = await servers[0].videos.listMyVideos({ start: 0, count: 2 })
135 // 1 plugin do +4 to the count parameter
136 expect(data).to.have.lengthOf(6)
139 it('Should run filter:api.user.me.videos.list.result', async function () {
140 const { total } = await servers[0].videos.listMyVideos({ start: 0, count: 2 })
142 // Plugin do +4 to the total result
143 expect(total).to.equal(14)
146 it('Should run filter:api.video.get.result', async function () {
147 const video = await servers[0].videos.get({ id: videoUUID })
148 expect(video.name).to.contain('<3')
152 describe('Video/live/import accept', function () {
154 it('Should run filter:api.video.upload.accept.result', async function () {
155 await servers[0].videos.upload({ attributes: { name: 'video with bad word' }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
158 it('Should run filter:api.live-video.create.accept.result', async function () {
160 name: 'video with bad word',
161 privacy: VideoPrivacy.PUBLIC,
162 channelId: servers[0].store.channel.id
165 await servers[0].live.create({ fields: attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
168 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
170 name: 'normal title',
171 privacy: VideoPrivacy.PUBLIC,
172 channelId: servers[0].store.channel.id,
173 targetUrl: FIXTURE_URLS.goodVideo + 'bad'
175 await servers[0].imports.importVideo({ attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
178 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
181 privacy: VideoPrivacy.PUBLIC,
182 channelId: servers[0].store.channel.id,
183 torrentfile: 'video-720p.torrent' as any
185 await servers[0].imports.importVideo({ attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
188 it('Should run filter:api.video.post-import-url.accept.result', async function () {
191 let videoImportId: number
195 name: 'title with bad word',
196 privacy: VideoPrivacy.PUBLIC,
197 channelId: servers[0].store.channel.id,
198 targetUrl: FIXTURE_URLS.goodVideo
200 const body = await servers[0].imports.importVideo({ attributes })
201 videoImportId = body.id
204 await waitJobs(servers)
207 const body = await servers[0].imports.getMyVideoImports()
208 const videoImports = body.data
210 const videoImport = videoImports.find(i => i.id === videoImportId)
212 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
213 expect(videoImport.state.label).to.equal('Rejected')
217 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
220 let videoImportId: number
224 name: 'title with bad word',
225 privacy: VideoPrivacy.PUBLIC,
226 channelId: servers[0].store.channel.id,
227 torrentfile: 'video-720p.torrent' as any
229 const body = await servers[0].imports.importVideo({ attributes })
230 videoImportId = body.id
233 await waitJobs(servers)
236 const { data: videoImports } = await servers[0].imports.getMyVideoImports()
238 const videoImport = videoImports.find(i => i.id === videoImportId)
240 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
241 expect(videoImport.state.label).to.equal('Rejected')
246 describe('Video comments accept', function () {
248 it('Should run filter:api.video-thread.create.accept.result', async function () {
249 await servers[0].comments.createThread({
251 text: 'comment with bad word',
252 expectedStatus: HttpStatusCode.FORBIDDEN_403
256 it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
257 const created = await servers[0].comments.createThread({ videoId: videoUUID, text: 'thread' })
258 threadId = created.id
260 await servers[0].comments.addReply({
262 toCommentId: threadId,
263 text: 'comment with bad word',
264 expectedStatus: HttpStatusCode.FORBIDDEN_403
266 await servers[0].comments.addReply({
268 toCommentId: threadId,
269 text: 'comment with good word',
270 expectedStatus: HttpStatusCode.OK_200
274 it('Should run filter:activity-pub.remote-video-comment.create.accept.result on a thread creation', async function () {
277 await servers[1].comments.createThread({ videoId: videoUUID, text: 'comment with bad word' })
279 await waitJobs(servers)
282 const thread = await servers[0].comments.listThreads({ videoId: videoUUID })
283 expect(thread.data).to.have.lengthOf(1)
284 expect(thread.data[0].text).to.not.include(' bad ')
288 const thread = await servers[1].comments.listThreads({ videoId: videoUUID })
289 expect(thread.data).to.have.lengthOf(2)
293 it('Should run filter:activity-pub.remote-video-comment.create.accept.result on a reply creation', async function () {
296 const { data } = await servers[1].comments.listThreads({ videoId: videoUUID })
297 const threadIdServer2 = data.find(t => t.text === 'thread').id
299 await servers[1].comments.addReply({
301 toCommentId: threadIdServer2,
302 text: 'comment with bad word'
305 await waitJobs(servers)
308 const tree = await servers[0].comments.getThread({ videoId: videoUUID, threadId })
309 expect(tree.children).to.have.lengthOf(1)
310 expect(tree.children[0].comment.text).to.not.include(' bad ')
314 const tree = await servers[1].comments.getThread({ videoId: videoUUID, threadId: threadIdServer2 })
315 expect(tree.children).to.have.lengthOf(2)
320 describe('Video comments', function () {
322 it('Should run filter:api.video-threads.list.params', async function () {
323 const { data } = await servers[0].comments.listThreads({ videoId: videoUUID, start: 0, count: 0 })
325 // our plugin do +1 to the count parameter
326 expect(data).to.have.lengthOf(1)
329 it('Should run filter:api.video-threads.list.result', async function () {
330 const { total } = await servers[0].comments.listThreads({ videoId: videoUUID, start: 0, count: 0 })
332 // Plugin do +1 to the total result
333 expect(total).to.equal(2)
336 it('Should run filter:api.video-thread-comments.list.params')
338 it('Should run filter:api.video-thread-comments.list.result', async function () {
339 const thread = await servers[0].comments.getThread({ videoId: videoUUID, threadId })
341 expect(thread.comment.text.endsWith(' <3')).to.be.true
344 it('Should run filter:api.overviews.videos.list.{params,result}', async function () {
345 await servers[0].overviews.getVideos({ page: 1 })
347 // 3 because we get 3 samples per page
348 await servers[0].servers.waitUntilLog('Run hook filter:api.overviews.videos.list.params', 3)
349 await servers[0].servers.waitUntilLog('Run hook filter:api.overviews.videos.list.result', 3)
353 describe('filter:video.auto-blacklist.result', function () {
355 async function checkIsBlacklisted (id: number | string, value: boolean) {
356 const video = await servers[0].videos.getWithToken({ id })
357 expect(video.blacklisted).to.equal(value)
360 it('Should blacklist on upload', async function () {
361 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video please blacklist me' } })
362 await checkIsBlacklisted(uuid, true)
365 it('Should blacklist on import', async function () {
369 name: 'video please blacklist me',
370 targetUrl: FIXTURE_URLS.goodVideo,
371 channelId: servers[0].store.channel.id
373 const body = await servers[0].imports.importVideo({ attributes })
374 await checkIsBlacklisted(body.video.uuid, true)
377 it('Should blacklist on update', async function () {
378 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video' } })
379 await checkIsBlacklisted(uuid, false)
381 await servers[0].videos.update({ id: uuid, attributes: { name: 'please blacklist me' } })
382 await checkIsBlacklisted(uuid, true)
385 it('Should blacklist on remote upload', async function () {
388 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'remote please blacklist me' } })
389 await waitJobs(servers)
391 await checkIsBlacklisted(uuid, true)
394 it('Should blacklist on remote update', async function () {
397 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video' } })
398 await waitJobs(servers)
400 await checkIsBlacklisted(uuid, false)
402 await servers[1].videos.update({ id: uuid, attributes: { name: 'please blacklist me' } })
403 await waitJobs(servers)
405 await checkIsBlacklisted(uuid, true)
409 describe('Should run filter:api.user.signup.allowed.result', function () {
411 it('Should run on config endpoint', async function () {
412 const body = await servers[0].config.getConfig()
413 expect(body.signup.allowed).to.be.true
416 it('Should allow a signup', async function () {
417 await servers[0].users.register({ username: 'john', password: 'password' })
420 it('Should not allow a signup', async function () {
421 const res = await servers[0].users.register({
423 password: 'password',
424 expectedStatus: HttpStatusCode.FORBIDDEN_403
427 expect(res.body.error).to.equal('No jma')
431 describe('Download hooks', function () {
432 const downloadVideos: VideoDetails[] = []
434 before(async function () {
437 await servers[0].config.updateCustomSubConfig({
450 const uuids: string[] = []
452 for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) {
453 const uuid = (await servers[0].videos.quickUpload({ name })).uuid
457 await waitJobs(servers)
459 for (const uuid of uuids) {
460 downloadVideos.push(await servers[0].videos.get({ id: uuid }))
464 it('Should run filter:api.download.torrent.allowed.result', async function () {
465 const res = await makeRawRequest({ url: downloadVideos[0].files[0].torrentDownloadUrl, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
466 expect(res.body.error).to.equal('Liu Bei')
468 await makeRawRequest({ url: downloadVideos[1].files[0].torrentDownloadUrl, expectedStatus: HttpStatusCode.OK_200 })
469 await makeRawRequest({ url: downloadVideos[2].files[0].torrentDownloadUrl, expectedStatus: HttpStatusCode.OK_200 })
472 it('Should run filter:api.download.video.allowed.result', async function () {
474 const res = await makeRawRequest({ url: downloadVideos[1].files[0].fileDownloadUrl, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
475 expect(res.body.error).to.equal('Cao Cao')
477 await makeRawRequest({ url: downloadVideos[0].files[0].fileDownloadUrl, expectedStatus: HttpStatusCode.OK_200 })
478 await makeRawRequest({ url: downloadVideos[2].files[0].fileDownloadUrl, expectedStatus: HttpStatusCode.OK_200 })
482 const res = await makeRawRequest({
483 url: downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl,
484 expectedStatus: HttpStatusCode.FORBIDDEN_403
487 expect(res.body.error).to.equal('Sun Jian')
489 await makeRawRequest({ url: downloadVideos[2].files[0].fileDownloadUrl, expectedStatus: HttpStatusCode.OK_200 })
491 await makeRawRequest({
492 url: downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl,
493 expectedStatus: HttpStatusCode.OK_200
496 await makeRawRequest({
497 url: downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl,
498 expectedStatus: HttpStatusCode.OK_200
504 describe('Embed filters', function () {
505 const embedVideos: VideoDetails[] = []
506 const embedPlaylists: VideoPlaylist[] = []
508 before(async function () {
511 await servers[0].config.disableTranscoding()
513 for (const name of [ 'bad embed', 'good embed' ]) {
515 const uuid = (await servers[0].videos.quickUpload({ name })).uuid
516 embedVideos.push(await servers[0].videos.get({ id: uuid }))
520 const attributes = { displayName: name, videoChannelId: servers[0].store.channel.id, privacy: VideoPlaylistPrivacy.PUBLIC }
521 const { id } = await servers[0].playlists.create({ attributes })
523 const playlist = await servers[0].playlists.get({ playlistId: id })
524 embedPlaylists.push(playlist)
529 it('Should run filter:html.embed.video.allowed.result', async function () {
530 const res = await makeGetRequest({ url: servers[0].url, path: embedVideos[0].embedPath, expectedStatus: HttpStatusCode.OK_200 })
531 expect(res.text).to.equal('Lu Bu')
534 it('Should run filter:html.embed.video-playlist.allowed.result', async function () {
535 const res = await makeGetRequest({ url: servers[0].url, path: embedPlaylists[0].embedPath, expectedStatus: HttpStatusCode.OK_200 })
536 expect(res.text).to.equal('Diao Chan')
540 describe('Search filters', function () {
542 before(async function () {
543 await servers[0].config.updateCustomSubConfig({
548 isDefaultSearch: false,
549 disableLocalSearch: false
556 it('Should run filter:api.search.videos.local.list.{params,result}', async function () {
557 await servers[0].search.advancedVideoSearch({
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)
567 it('Should run filter:api.search.videos.index.list.{params,result}', async function () {
568 await servers[0].search.advancedVideoSearch({
571 searchTarget: 'search-index'
575 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.params', 1)
576 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.result', 1)
577 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.index.list.params', 1)
578 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.index.list.result', 1)
581 it('Should run filter:api.search.video-channels.local.list.{params,result}', async function () {
582 await servers[0].search.advancedChannelSearch({
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)
592 it('Should run filter:api.search.video-channels.index.list.{params,result}', async function () {
593 await servers[0].search.advancedChannelSearch({
596 searchTarget: 'search-index'
600 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.params', 1)
601 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.result', 1)
602 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.index.list.params', 1)
603 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.index.list.result', 1)
606 it('Should run filter:api.search.video-playlists.local.list.{params,result}', async function () {
607 await servers[0].search.advancedPlaylistSearch({
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)
617 it('Should run filter:api.search.video-playlists.index.list.{params,result}', async function () {
618 await servers[0].search.advancedPlaylistSearch({
621 searchTarget: 'search-index'
625 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.params', 1)
626 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.result', 1)
627 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.index.list.params', 1)
628 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.index.list.result', 1)
632 describe('Upload/import/live attributes filters', function () {
634 before(async function () {
635 await servers[0].config.enableLive({ transcoding: false, allowReplay: false })
636 await servers[0].config.enableImports()
637 await servers[0].config.disableTranscoding()
640 it('Should run filter:api.video.upload.video-attribute.result', async function () {
641 for (const mode of [ 'legacy' as 'legacy', 'resumable' as 'resumable' ]) {
642 const { id } = await servers[0].videos.upload({ attributes: { name: 'video', description: 'upload' }, mode })
644 const video = await servers[0].videos.get({ id })
645 expect(video.description).to.equal('upload - filter:api.video.upload.video-attribute.result')
649 it('Should run filter:api.video.import-url.video-attribute.result', async function () {
652 description: 'import url',
653 channelId: servers[0].store.channel.id,
654 targetUrl: FIXTURE_URLS.goodVideo,
655 privacy: VideoPrivacy.PUBLIC
657 const { video: { id } } = await servers[0].imports.importVideo({ attributes })
659 const video = await servers[0].videos.get({ id })
660 expect(video.description).to.equal('import url - filter:api.video.import-url.video-attribute.result')
663 it('Should run filter:api.video.import-torrent.video-attribute.result', async function () {
666 description: 'import torrent',
667 channelId: servers[0].store.channel.id,
668 magnetUri: FIXTURE_URLS.magnet,
669 privacy: VideoPrivacy.PUBLIC
671 const { video: { id } } = await servers[0].imports.importVideo({ attributes })
673 const video = await servers[0].videos.get({ id })
674 expect(video.description).to.equal('import torrent - filter:api.video.import-torrent.video-attribute.result')
677 it('Should run filter:api.video.live.video-attribute.result', async function () {
681 channelId: servers[0].store.channel.id,
682 privacy: VideoPrivacy.PUBLIC
684 const { id } = await servers[0].live.create({ fields })
686 const video = await servers[0].videos.get({ id })
687 expect(video.description).to.equal('live - filter:api.video.live.video-attribute.result')
691 describe('Stats filters', function () {
693 it('Should run filter:api.server.stats.get.result', async function () {
694 const data = await servers[0].stats.get()
696 expect((data as any).customStats).to.equal(14)
701 describe('Job queue filters', function () {
702 let videoUUID: string
704 before(async function () {
705 this.timeout(120_000)
707 await servers[0].config.enableMinimumTranscoding()
708 const { uuid } = await servers[0].videos.quickUpload({ name: 'studio' })
710 const video = await servers[0].videos.get({ id: uuid })
711 expect(video.duration).at.least(2)
712 videoUUID = video.uuid
714 await waitJobs(servers)
716 await servers[0].config.enableStudio()
719 it('Should run filter:job-queue.process.params', async function () {
720 this.timeout(120_000)
722 await servers[0].videoStudio.createEditionTasks({
728 file: 'video_very_short_240p.mp4'
734 await waitJobs(servers)
736 await servers[0].servers.waitUntilLog('Run hook filter:job-queue.process.params', 1, false)
738 const video = await servers[0].videos.get({ id: videoUUID })
739 expect(video.duration).at.most(2)
742 it('Should run filter:job-queue.process.result', async function () {
743 await servers[0].servers.waitUntilLog('Run hook filter:job-queue.process.result', 1, false)
747 describe('Transcoding filters', async function () {
749 it('Should run filter:transcoding.auto.resolutions-to-transcode.result', async function () {
750 const { uuid } = await servers[0].videos.quickUpload({ name: 'transcode-filter' })
752 await waitJobs(servers)
754 const video = await servers[0].videos.get({ id: uuid })
755 expect(video.files).to.have.lengthOf(2)
756 expect(video.files.find(f => f.resolution.id === 100 as any)).to.exist
760 describe('Video channel filters', async function () {
762 it('Should run filter:api.video-channels.list.params', async function () {
763 const { data } = await servers[0].channels.list({ start: 0, count: 0 })
765 // plugin do +1 to the count parameter
766 expect(data).to.have.lengthOf(1)
769 it('Should run filter:api.video-channels.list.result', async function () {
770 const { total } = await servers[0].channels.list({ start: 0, count: 1 })
772 // plugin do +1 to the total parameter
773 expect(total).to.equal(4)
776 it('Should run filter:api.video-channel.get.result', async function () {
777 const channel = await servers[0].channels.get({ channelName: 'root_channel' })
778 expect(channel.displayName).to.equal('Main root channel <3')
782 after(async function () {
783 await cleanupTests(servers)