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 it('Should run filter:api.videos.list.params', async function () {
68 const { data } = await servers[0].videos.list({ start: 0, count: 2 })
70 // 2 plugins do +1 to the count parameter
71 expect(data).to.have.lengthOf(4)
74 it('Should run filter:api.videos.list.result', async function () {
75 const { total } = await servers[0].videos.list({ start: 0, count: 0 })
77 // Plugin do +1 to the total result
78 expect(total).to.equal(11)
81 it('Should run filter:api.video-playlist.videos.list.params', async function () {
82 const { data } = await servers[0].playlists.listVideos({
84 playlistId: videoPlaylistUUID
87 // 1 plugin do +1 to the count parameter
88 expect(data).to.have.lengthOf(3)
91 it('Should run filter:api.video-playlist.videos.list.result', async function () {
92 const { total } = await servers[0].playlists.listVideos({
94 playlistId: videoPlaylistUUID
97 // Plugin do +1 to the total result
98 expect(total).to.equal(11)
101 it('Should run filter:api.accounts.videos.list.params', async function () {
102 const { data } = await servers[0].videos.listByAccount({ handle: 'root', start: 0, count: 2 })
104 // 1 plugin do +1 to the count parameter
105 expect(data).to.have.lengthOf(3)
108 it('Should run filter:api.accounts.videos.list.result', async function () {
109 const { total } = await servers[0].videos.listByAccount({ handle: 'root', start: 0, count: 2 })
111 // Plugin do +2 to the total result
112 expect(total).to.equal(12)
115 it('Should run filter:api.video-channels.videos.list.params', async function () {
116 const { data } = await servers[0].videos.listByChannel({ handle: 'root_channel', start: 0, count: 2 })
118 // 1 plugin do +3 to the count parameter
119 expect(data).to.have.lengthOf(5)
122 it('Should run filter:api.video-channels.videos.list.result', async function () {
123 const { total } = await servers[0].videos.listByChannel({ handle: 'root_channel', start: 0, count: 2 })
125 // Plugin do +3 to the total result
126 expect(total).to.equal(13)
129 it('Should run filter:api.user.me.videos.list.params', async function () {
130 const { data } = await servers[0].videos.listMyVideos({ start: 0, count: 2 })
132 // 1 plugin do +4 to the count parameter
133 expect(data).to.have.lengthOf(6)
136 it('Should run filter:api.user.me.videos.list.result', async function () {
137 const { total } = await servers[0].videos.listMyVideos({ start: 0, count: 2 })
139 // Plugin do +4 to the total result
140 expect(total).to.equal(14)
143 it('Should run filter:api.video.get.result', async function () {
144 const video = await servers[0].videos.get({ id: videoUUID })
145 expect(video.name).to.contain('<3')
148 it('Should run filter:api.video.upload.accept.result', async function () {
149 await servers[0].videos.upload({ attributes: { name: 'video with bad word' }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
152 it('Should run filter:api.live-video.create.accept.result', async function () {
154 name: 'video with bad word',
155 privacy: VideoPrivacy.PUBLIC,
156 channelId: servers[0].store.channel.id
159 await servers[0].live.create({ fields: attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
162 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
164 name: 'normal title',
165 privacy: VideoPrivacy.PUBLIC,
166 channelId: servers[0].store.channel.id,
167 targetUrl: FIXTURE_URLS.goodVideo + 'bad'
169 await servers[0].imports.importVideo({ attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
172 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
175 privacy: VideoPrivacy.PUBLIC,
176 channelId: servers[0].store.channel.id,
177 torrentfile: 'video-720p.torrent' as any
179 await servers[0].imports.importVideo({ attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
182 it('Should run filter:api.video.post-import-url.accept.result', async function () {
185 let videoImportId: number
189 name: 'title with bad word',
190 privacy: VideoPrivacy.PUBLIC,
191 channelId: servers[0].store.channel.id,
192 targetUrl: FIXTURE_URLS.goodVideo
194 const body = await servers[0].imports.importVideo({ attributes })
195 videoImportId = body.id
198 await waitJobs(servers)
201 const body = await servers[0].imports.getMyVideoImports()
202 const videoImports = body.data
204 const videoImport = videoImports.find(i => i.id === videoImportId)
206 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
207 expect(videoImport.state.label).to.equal('Rejected')
211 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
214 let videoImportId: number
218 name: 'title with bad word',
219 privacy: VideoPrivacy.PUBLIC,
220 channelId: servers[0].store.channel.id,
221 torrentfile: 'video-720p.torrent' as any
223 const body = await servers[0].imports.importVideo({ attributes })
224 videoImportId = body.id
227 await waitJobs(servers)
230 const { data: videoImports } = await servers[0].imports.getMyVideoImports()
232 const videoImport = videoImports.find(i => i.id === videoImportId)
234 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
235 expect(videoImport.state.label).to.equal('Rejected')
239 it('Should run filter:api.video-thread.create.accept.result', async function () {
240 await servers[0].comments.createThread({
242 text: 'comment with bad word',
243 expectedStatus: HttpStatusCode.FORBIDDEN_403
247 it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
248 const created = await servers[0].comments.createThread({ videoId: videoUUID, text: 'thread' })
249 threadId = created.id
251 await servers[0].comments.addReply({
253 toCommentId: threadId,
254 text: 'comment with bad word',
255 expectedStatus: HttpStatusCode.FORBIDDEN_403
257 await servers[0].comments.addReply({
259 toCommentId: threadId,
260 text: 'comment with good word',
261 expectedStatus: HttpStatusCode.OK_200
265 it('Should run filter:api.video-threads.list.params', async function () {
266 const { data } = await servers[0].comments.listThreads({ videoId: videoUUID, start: 0, count: 0 })
268 // our plugin do +1 to the count parameter
269 expect(data).to.have.lengthOf(1)
272 it('Should run filter:api.video-threads.list.result', async function () {
273 const { total } = await servers[0].comments.listThreads({ videoId: videoUUID, start: 0, count: 0 })
275 // Plugin do +1 to the total result
276 expect(total).to.equal(2)
279 it('Should run filter:api.video-thread-comments.list.params')
281 it('Should run filter:api.video-thread-comments.list.result', async function () {
282 const thread = await servers[0].comments.getThread({ videoId: videoUUID, threadId })
284 expect(thread.comment.text.endsWith(' <3')).to.be.true
287 it('Should run filter:api.overviews.videos.list.{params,result}', async function () {
288 await servers[0].overviews.getVideos({ page: 1 })
290 // 3 because we get 3 samples per page
291 await servers[0].servers.waitUntilLog('Run hook filter:api.overviews.videos.list.params', 3)
292 await servers[0].servers.waitUntilLog('Run hook filter:api.overviews.videos.list.result', 3)
295 describe('filter:video.auto-blacklist.result', function () {
297 async function checkIsBlacklisted (id: number | string, value: boolean) {
298 const video = await servers[0].videos.getWithToken({ id })
299 expect(video.blacklisted).to.equal(value)
302 it('Should blacklist on upload', async function () {
303 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video please blacklist me' } })
304 await checkIsBlacklisted(uuid, true)
307 it('Should blacklist on import', async function () {
311 name: 'video please blacklist me',
312 targetUrl: FIXTURE_URLS.goodVideo,
313 channelId: servers[0].store.channel.id
315 const body = await servers[0].imports.importVideo({ attributes })
316 await checkIsBlacklisted(body.video.uuid, true)
319 it('Should blacklist on update', async function () {
320 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video' } })
321 await checkIsBlacklisted(uuid, false)
323 await servers[0].videos.update({ id: uuid, attributes: { name: 'please blacklist me' } })
324 await checkIsBlacklisted(uuid, true)
327 it('Should blacklist on remote upload', async function () {
330 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'remote please blacklist me' } })
331 await waitJobs(servers)
333 await checkIsBlacklisted(uuid, true)
336 it('Should blacklist on remote update', async function () {
339 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video' } })
340 await waitJobs(servers)
342 await checkIsBlacklisted(uuid, false)
344 await servers[1].videos.update({ id: uuid, attributes: { name: 'please blacklist me' } })
345 await waitJobs(servers)
347 await checkIsBlacklisted(uuid, true)
351 describe('Should run filter:api.user.signup.allowed.result', function () {
353 it('Should run on config endpoint', async function () {
354 const body = await servers[0].config.getConfig()
355 expect(body.signup.allowed).to.be.true
358 it('Should allow a signup', async function () {
359 await servers[0].users.register({ username: 'john', password: 'password' })
362 it('Should not allow a signup', async function () {
363 const res = await servers[0].users.register({
365 password: 'password',
366 expectedStatus: HttpStatusCode.FORBIDDEN_403
369 expect(res.body.error).to.equal('No jma')
373 describe('Download hooks', function () {
374 const downloadVideos: VideoDetails[] = []
376 before(async function () {
379 await servers[0].config.updateCustomSubConfig({
392 const uuids: string[] = []
394 for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) {
395 const uuid = (await servers[0].videos.quickUpload({ name })).uuid
399 await waitJobs(servers)
401 for (const uuid of uuids) {
402 downloadVideos.push(await servers[0].videos.get({ id: uuid }))
406 it('Should run filter:api.download.torrent.allowed.result', async function () {
407 const res = await makeRawRequest(downloadVideos[0].files[0].torrentDownloadUrl, 403)
408 expect(res.body.error).to.equal('Liu Bei')
410 await makeRawRequest(downloadVideos[1].files[0].torrentDownloadUrl, 200)
411 await makeRawRequest(downloadVideos[2].files[0].torrentDownloadUrl, 200)
414 it('Should run filter:api.download.video.allowed.result', async function () {
416 const res = await makeRawRequest(downloadVideos[1].files[0].fileDownloadUrl, 403)
417 expect(res.body.error).to.equal('Cao Cao')
419 await makeRawRequest(downloadVideos[0].files[0].fileDownloadUrl, 200)
420 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
424 const res = await makeRawRequest(downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl, 403)
425 expect(res.body.error).to.equal('Sun Jian')
427 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
429 await makeRawRequest(downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
430 await makeRawRequest(downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
435 describe('Embed filters', function () {
436 const embedVideos: VideoDetails[] = []
437 const embedPlaylists: VideoPlaylist[] = []
439 before(async function () {
442 await servers[0].config.disableTranscoding()
444 for (const name of [ 'bad embed', 'good embed' ]) {
446 const uuid = (await servers[0].videos.quickUpload({ name })).uuid
447 embedVideos.push(await servers[0].videos.get({ id: uuid }))
451 const attributes = { displayName: name, videoChannelId: servers[0].store.channel.id, privacy: VideoPlaylistPrivacy.PUBLIC }
452 const { id } = await servers[0].playlists.create({ attributes })
454 const playlist = await servers[0].playlists.get({ playlistId: id })
455 embedPlaylists.push(playlist)
460 it('Should run filter:html.embed.video.allowed.result', async function () {
461 const res = await makeRawRequest(servers[0].url + embedVideos[0].embedPath, 200)
462 expect(res.text).to.equal('Lu Bu')
465 it('Should run filter:html.embed.video-playlist.allowed.result', async function () {
466 const res = await makeRawRequest(servers[0].url + embedPlaylists[0].embedPath, 200)
467 expect(res.text).to.equal('Diao Chan')
471 describe('Search filters', function () {
473 before(async function () {
474 await servers[0].config.updateCustomSubConfig({
479 isDefaultSearch: false,
480 disableLocalSearch: false
487 it('Should run filter:api.search.videos.local.list.{params,result}', async function () {
488 await servers[0].search.advancedVideoSearch({
494 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.params', 1)
495 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.result', 1)
498 it('Should run filter:api.search.videos.index.list.{params,result}', async function () {
499 await servers[0].search.advancedVideoSearch({
502 searchTarget: 'search-index'
506 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.params', 1)
507 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.result', 1)
508 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.index.list.params', 1)
509 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.index.list.result', 1)
512 it('Should run filter:api.search.video-channels.local.list.{params,result}', async function () {
513 await servers[0].search.advancedChannelSearch({
519 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.params', 1)
520 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.result', 1)
523 it('Should run filter:api.search.video-channels.index.list.{params,result}', async function () {
524 await servers[0].search.advancedChannelSearch({
527 searchTarget: 'search-index'
531 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.params', 1)
532 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.result', 1)
533 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.index.list.params', 1)
534 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.index.list.result', 1)
537 it('Should run filter:api.search.video-playlists.local.list.{params,result}', async function () {
538 await servers[0].search.advancedPlaylistSearch({
544 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.params', 1)
545 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.result', 1)
548 it('Should run filter:api.search.video-playlists.index.list.{params,result}', async function () {
549 await servers[0].search.advancedPlaylistSearch({
552 searchTarget: 'search-index'
556 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.params', 1)
557 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.result', 1)
558 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.index.list.params', 1)
559 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.index.list.result', 1)
563 describe('Upload/import/live attributes filters', function () {
565 before(async function () {
566 await servers[0].config.enableLive({ transcoding: false, allowReplay: false })
567 await servers[0].config.enableImports()
568 await servers[0].config.disableTranscoding()
571 it('Should run filter:api.video.upload.video-attribute.result', async function () {
572 for (const mode of [ 'legacy' as 'legacy', 'resumable' as 'resumable' ]) {
573 const { id } = await servers[0].videos.upload({ attributes: { name: 'video', description: 'upload' }, mode })
575 const video = await servers[0].videos.get({ id })
576 expect(video.description).to.equal('upload - filter:api.video.upload.video-attribute.result')
580 it('Should run filter:api.video.import-url.video-attribute.result', async function () {
583 description: 'import url',
584 channelId: servers[0].store.channel.id,
585 targetUrl: FIXTURE_URLS.goodVideo,
586 privacy: VideoPrivacy.PUBLIC
588 const { video: { id } } = await servers[0].imports.importVideo({ attributes })
590 const video = await servers[0].videos.get({ id })
591 expect(video.description).to.equal('import url - filter:api.video.import-url.video-attribute.result')
594 it('Should run filter:api.video.import-torrent.video-attribute.result', async function () {
597 description: 'import torrent',
598 channelId: servers[0].store.channel.id,
599 magnetUri: FIXTURE_URLS.magnet,
600 privacy: VideoPrivacy.PUBLIC
602 const { video: { id } } = await servers[0].imports.importVideo({ attributes })
604 const video = await servers[0].videos.get({ id })
605 expect(video.description).to.equal('import torrent - filter:api.video.import-torrent.video-attribute.result')
608 it('Should run filter:api.video.live.video-attribute.result', async function () {
612 channelId: servers[0].store.channel.id,
613 privacy: VideoPrivacy.PUBLIC
615 const { id } = await servers[0].live.create({ fields })
617 const video = await servers[0].videos.get({ id })
618 expect(video.description).to.equal('live - filter:api.video.live.video-attribute.result')
622 describe('Stats filters', function () {
624 it('Should run filter:api.server.stats.get.result', async function () {
625 const data = await servers[0].stats.get()
627 expect((data as any).customStats).to.equal(14)
632 describe('Job queue filters', function () {
633 let videoUUID: string
635 before(async function () {
636 this.timeout(120_000)
638 await servers[0].config.enableMinimumTranscoding()
639 const { uuid } = await servers[0].videos.quickUpload({ name: 'studio' })
641 const video = await servers[0].videos.get({ id: uuid })
642 expect(video.duration).at.least(2)
643 videoUUID = video.uuid
645 await waitJobs(servers)
647 await servers[0].config.enableStudio()
650 it('Should run filter:job-queue.process.params', async function () {
651 this.timeout(120_000)
653 await servers[0].videoStudio.createEditionTasks({
659 file: 'video_very_short_240p.mp4'
665 await waitJobs(servers)
667 await servers[0].servers.waitUntilLog('Run hook filter:job-queue.process.params', 1, false)
669 const video = await servers[0].videos.get({ id: videoUUID })
670 expect(video.duration).at.most(2)
673 it('Should run filter:job-queue.process.result', async function () {
674 await servers[0].servers.waitUntilLog('Run hook filter:job-queue.process.result', 1, false)
678 describe('Transcoding filters', async function () {
680 it('Should run filter:transcoding.auto.resolutions-to-transcode.result', async function () {
681 const { uuid } = await servers[0].videos.quickUpload({ name: 'transcode-filter' })
683 await waitJobs(servers)
685 const video = await servers[0].videos.get({ id: uuid })
686 expect(video.files).to.have.lengthOf(2)
687 expect(video.files.find(f => f.resolution.id === 100 as any)).to.exist
691 describe('Video channel filters', async function () {
693 it('Should run filter:api.video-channels.list.params', async function () {
694 const { data } = await servers[0].channels.list({ start: 0, count: 0 })
696 // plugin do +1 to the count parameter
697 expect(data).to.have.lengthOf(1)
700 it('Should run filter:api.video-channels.list.result', async function () {
701 const { total } = await servers[0].channels.list({ start: 0, count: 1 })
703 // plugin do +1 to the total parameter
704 expect(total).to.equal(4)
707 it('Should run filter:api.video-channel.get.result', async function () {
708 const channel = await servers[0].channels.get({ channelName: 'root_channel' })
709 expect(channel.displayName).to.equal('Main root channel <3')
713 after(async function () {
714 await cleanupTests(servers)