From 4bc45da342597fb49593fc14c40f8dc5a97bb64e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 23 Mar 2021 11:54:08 +0100 Subject: Add hooks support for video download --- server/tests/fixtures/peertube-plugin-test/main.js | 26 +++++++++ server/tests/plugins/filter-hooks.ts | 63 ++++++++++++++++++++++ 2 files changed, 89 insertions(+) (limited to 'server/tests') diff --git a/server/tests/fixtures/peertube-plugin-test/main.js b/server/tests/fixtures/peertube-plugin-test/main.js index 305d92002..9913d0846 100644 --- a/server/tests/fixtures/peertube-plugin-test/main.js +++ b/server/tests/fixtures/peertube-plugin-test/main.js @@ -184,6 +184,32 @@ async function register ({ registerHook, registerSetting, settingsManager, stora return result } }) + + registerHook({ + target: 'filter:api.download.torrent.allowed.result', + handler: (result, params) => { + if (params && params.downloadName.includes('bad torrent')) { + return { allowed: false, errorMessage: 'Liu Bei' } + } + + return result + } + }) + + registerHook({ + target: 'filter:api.download.video.allowed.result', + handler: (result, params) => { + if (params && !params.streamingPlaylist && params.video.name.includes('bad file')) { + return { allowed: false, errorMessage: 'Cao Cao' } + } + + if (params && params.streamingPlaylist && params.video.name.includes('bad playlist file')) { + return { allowed: false, errorMessage: 'Sun Jian' } + } + + return result + } + }) } async function unregister () { diff --git a/server/tests/plugins/filter-hooks.ts b/server/tests/plugins/filter-hooks.ts index d88170201..6996ae788 100644 --- a/server/tests/plugins/filter-hooks.ts +++ b/server/tests/plugins/filter-hooks.ts @@ -20,12 +20,14 @@ import { getVideoThreadComments, getVideoWithToken, installPlugin, + makeRawRequest, registerUser, setAccessTokensToServers, setDefaultVideoChannel, updateCustomSubConfig, updateVideo, uploadVideo, + uploadVideoAndGetId, waitJobs } from '../../../shared/extra-utils' import { cleanupTests, flushAndRunMultipleServers, ServerInfo } from '../../../shared/extra-utils/server/servers' @@ -355,6 +357,67 @@ describe('Test plugin filter hooks', function () { }) }) + describe('Download hooks', function () { + const downloadVideos: VideoDetails[] = [] + + before(async function () { + this.timeout(60000) + + await updateCustomSubConfig(servers[0].url, servers[0].accessToken, { + transcoding: { + webtorrent: { + enabled: true + }, + hls: { + enabled: true + } + } + }) + + const uuids: string[] = [] + + for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) { + const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid + uuids.push(uuid) + } + + await waitJobs(servers) + + for (const uuid of uuids) { + const res = await getVideo(servers[0].url, uuid) + downloadVideos.push(res.body) + } + }) + + it('Should run filter:api.download.torrent.allowed.result', async function () { + const res = await makeRawRequest(downloadVideos[0].files[0].torrentDownloadUrl, 403) + expect(res.body.error).to.equal('Liu Bei') + + await makeRawRequest(downloadVideos[1].files[0].torrentDownloadUrl, 200) + await makeRawRequest(downloadVideos[2].files[0].torrentDownloadUrl, 200) + }) + + it('Should run filter:api.download.video.allowed.result', async function () { + { + const res = await makeRawRequest(downloadVideos[1].files[0].fileDownloadUrl, 403) + expect(res.body.error).to.equal('Cao Cao') + + await makeRawRequest(downloadVideos[0].files[0].fileDownloadUrl, 200) + await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200) + } + + { + const res = await makeRawRequest(downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl, 403) + expect(res.body.error).to.equal('Sun Jian') + + await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200) + + await makeRawRequest(downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl, 200) + await makeRawRequest(downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl, 200) + } + }) + }) + after(async function () { await cleanupTests(servers) }) -- cgit v1.2.3