diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-05-14 11:10:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-14 11:10:26 +0200 |
commit | 2158ac90341dc3fcae958540de65032da25c8d6e (patch) | |
tree | a780923d701f3daa130996768e38c1e1b6a0646c /server/tests | |
parent | 7405b6ba897dbce1b4fd50c92174f1df5ac15adc (diff) | |
download | PeerTube-2158ac90341dc3fcae958540de65032da25c8d6e.tar.gz PeerTube-2158ac90341dc3fcae958540de65032da25c8d6e.tar.zst PeerTube-2158ac90341dc3fcae958540de65032da25c8d6e.zip |
Add server plugin filter hooks for import with torrent and url (#2621)
* Add server plugin filter hooks for import with torrent and url
* WIP: pre and post-import filter hooks
* Rebased
* Cleanup filters to accept imports
Co-authored-by: Chocobozzz <me@florianbigard.com>
Diffstat (limited to 'server/tests')
-rw-r--r-- | server/tests/fixtures/peertube-plugin-test/main.js | 42 | ||||
-rw-r--r-- | server/tests/plugins/filter-hooks.ts | 88 |
2 files changed, 124 insertions, 6 deletions
diff --git a/server/tests/fixtures/peertube-plugin-test/main.js b/server/tests/fixtures/peertube-plugin-test/main.js index 69796ab07..a45e98fb5 100644 --- a/server/tests/fixtures/peertube-plugin-test/main.js +++ b/server/tests/fixtures/peertube-plugin-test/main.js | |||
@@ -50,7 +50,47 @@ async function register ({ registerHook, registerSetting, settingsManager, stora | |||
50 | target: 'filter:api.video.upload.accept.result', | 50 | target: 'filter:api.video.upload.accept.result', |
51 | handler: ({ accepted }, { videoBody }) => { | 51 | handler: ({ accepted }, { videoBody }) => { |
52 | if (!accepted) return { accepted: false } | 52 | if (!accepted) return { accepted: false } |
53 | if (videoBody.name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '} | 53 | if (videoBody.name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word' } |
54 | |||
55 | return { accepted: true } | ||
56 | } | ||
57 | }) | ||
58 | |||
59 | registerHook({ | ||
60 | target: 'filter:api.video.pre-import-url.accept.result', | ||
61 | handler: ({ accepted }, { videoImportBody }) => { | ||
62 | if (!accepted) return { accepted: false } | ||
63 | if (videoImportBody.targetUrl.includes('bad')) return { accepted: false, errorMessage: 'bad target url' } | ||
64 | |||
65 | return { accepted: true } | ||
66 | } | ||
67 | }) | ||
68 | |||
69 | registerHook({ | ||
70 | target: 'filter:api.video.pre-import-torrent.accept.result', | ||
71 | handler: ({ accepted }, { videoImportBody }) => { | ||
72 | if (!accepted) return { accepted: false } | ||
73 | if (videoImportBody.name.includes('bad torrent')) return { accepted: false, errorMessage: 'bad torrent' } | ||
74 | |||
75 | return { accepted: true } | ||
76 | } | ||
77 | }) | ||
78 | |||
79 | registerHook({ | ||
80 | target: 'filter:api.video.post-import-url.accept.result', | ||
81 | handler: ({ accepted }, { video }) => { | ||
82 | if (!accepted) return { accepted: false } | ||
83 | if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' } | ||
84 | |||
85 | return { accepted: true } | ||
86 | } | ||
87 | }) | ||
88 | |||
89 | registerHook({ | ||
90 | target: 'filter:api.video.post-import-torrent.accept.result', | ||
91 | handler: ({ accepted }, { video }) => { | ||
92 | if (!accepted) return { accepted: false } | ||
93 | if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' } | ||
54 | 94 | ||
55 | return { accepted: true } | 95 | return { accepted: true } |
56 | } | 96 | } |
diff --git a/server/tests/plugins/filter-hooks.ts b/server/tests/plugins/filter-hooks.ts index 6c1fd40ba..41242318e 100644 --- a/server/tests/plugins/filter-hooks.ts +++ b/server/tests/plugins/filter-hooks.ts | |||
@@ -1,8 +1,8 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | 2 | ||
3 | import * as chai from 'chai' | ||
4 | import 'mocha' | 3 | import 'mocha' |
5 | import { cleanupTests, flushAndRunMultipleServers, ServerInfo } from '../../../shared/extra-utils/server/servers' | 4 | import * as chai from 'chai' |
5 | import { ServerConfig } from '@shared/models' | ||
6 | import { | 6 | import { |
7 | addVideoCommentReply, | 7 | addVideoCommentReply, |
8 | addVideoCommentThread, | 8 | addVideoCommentThread, |
@@ -23,10 +23,10 @@ import { | |||
23 | uploadVideo, | 23 | uploadVideo, |
24 | waitJobs | 24 | waitJobs |
25 | } from '../../../shared/extra-utils' | 25 | } from '../../../shared/extra-utils' |
26 | import { cleanupTests, flushAndRunMultipleServers, ServerInfo } from '../../../shared/extra-utils/server/servers' | ||
27 | import { getMyVideoImports, getYoutubeVideoUrl, importVideo } from '../../../shared/extra-utils/videos/video-imports' | ||
28 | import { VideoDetails, VideoImport, VideoImportState, VideoPrivacy } from '../../../shared/models/videos' | ||
26 | import { VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model' | 29 | import { VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model' |
27 | import { VideoDetails } from '../../../shared/models/videos' | ||
28 | import { getYoutubeVideoUrl, importVideo } from '../../../shared/extra-utils/videos/video-imports' | ||
29 | import { ServerConfig } from '@shared/models' | ||
30 | 30 | ||
31 | const expect = chai.expect | 31 | const expect = chai.expect |
32 | 32 | ||
@@ -87,6 +87,84 @@ describe('Test plugin filter hooks', function () { | |||
87 | await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, 403) | 87 | await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, 403) |
88 | }) | 88 | }) |
89 | 89 | ||
90 | it('Should run filter:api.video.pre-import-url.accept.result', async function () { | ||
91 | const baseAttributes = { | ||
92 | name: 'normal title', | ||
93 | privacy: VideoPrivacy.PUBLIC, | ||
94 | channelId: servers[0].videoChannel.id, | ||
95 | targetUrl: getYoutubeVideoUrl() + 'bad' | ||
96 | } | ||
97 | await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, 403) | ||
98 | }) | ||
99 | |||
100 | it('Should run filter:api.video.pre-import-torrent.accept.result', async function () { | ||
101 | const baseAttributes = { | ||
102 | name: 'bad torrent', | ||
103 | privacy: VideoPrivacy.PUBLIC, | ||
104 | channelId: servers[0].videoChannel.id, | ||
105 | torrentfile: 'video-720p.torrent' as any | ||
106 | } | ||
107 | await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, 403) | ||
108 | }) | ||
109 | |||
110 | it('Should run filter:api.video.post-import-url.accept.result', async function () { | ||
111 | this.timeout(60000) | ||
112 | |||
113 | let videoImportId: number | ||
114 | |||
115 | { | ||
116 | const baseAttributes = { | ||
117 | name: 'title with bad word', | ||
118 | privacy: VideoPrivacy.PUBLIC, | ||
119 | channelId: servers[0].videoChannel.id, | ||
120 | targetUrl: getYoutubeVideoUrl() | ||
121 | } | ||
122 | const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes) | ||
123 | videoImportId = res.body.id | ||
124 | } | ||
125 | |||
126 | await waitJobs(servers) | ||
127 | |||
128 | { | ||
129 | const res = await getMyVideoImports(servers[0].url, servers[0].accessToken) | ||
130 | const videoImports = res.body.data as VideoImport[] | ||
131 | |||
132 | const videoImport = videoImports.find(i => i.id === videoImportId) | ||
133 | |||
134 | expect(videoImport.state.id).to.equal(VideoImportState.REJECTED) | ||
135 | expect(videoImport.state.label).to.equal('Rejected') | ||
136 | } | ||
137 | }) | ||
138 | |||
139 | it('Should run filter:api.video.post-import-torrent.accept.result', async function () { | ||
140 | this.timeout(60000) | ||
141 | |||
142 | let videoImportId: number | ||
143 | |||
144 | { | ||
145 | const baseAttributes = { | ||
146 | name: 'title with bad word', | ||
147 | privacy: VideoPrivacy.PUBLIC, | ||
148 | channelId: servers[0].videoChannel.id, | ||
149 | torrentfile: 'video-720p.torrent' as any | ||
150 | } | ||
151 | const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes) | ||
152 | videoImportId = res.body.id | ||
153 | } | ||
154 | |||
155 | await waitJobs(servers) | ||
156 | |||
157 | { | ||
158 | const res = await getMyVideoImports(servers[0].url, servers[0].accessToken) | ||
159 | const videoImports = res.body.data as VideoImport[] | ||
160 | |||
161 | const videoImport = videoImports.find(i => i.id === videoImportId) | ||
162 | |||
163 | expect(videoImport.state.id).to.equal(VideoImportState.REJECTED) | ||
164 | expect(videoImport.state.label).to.equal('Rejected') | ||
165 | } | ||
166 | }) | ||
167 | |||
90 | it('Should run filter:api.video-thread.create.accept.result', async function () { | 168 | it('Should run filter:api.video-thread.create.accept.result', async function () { |
91 | await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', 403) | 169 | await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', 403) |
92 | }) | 170 | }) |