diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-22 11:14:58 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | 6691c52280363fc42f7865230ebb3741f02fff23 (patch) | |
tree | 071f9a4d7814c46dcfec100268cb0a0cc76e5204 /server/tests | |
parent | 89cd12756035a146bbcc4db78cd3cd64f2f3d88d (diff) | |
download | PeerTube-6691c52280363fc42f7865230ebb3741f02fff23.tar.gz PeerTube-6691c52280363fc42f7865230ebb3741f02fff23.tar.zst PeerTube-6691c52280363fc42f7865230ebb3741f02fff23.zip |
Add filter hooks tests
Diffstat (limited to 'server/tests')
-rw-r--r-- | server/tests/fixtures/peertube-plugin-test/main.js | 57 | ||||
-rw-r--r-- | server/tests/plugins/filter-hooks.ts | 123 |
2 files changed, 173 insertions, 7 deletions
diff --git a/server/tests/fixtures/peertube-plugin-test/main.js b/server/tests/fixtures/peertube-plugin-test/main.js index c5317ab41..7c53f6afe 100644 --- a/server/tests/fixtures/peertube-plugin-test/main.js +++ b/server/tests/fixtures/peertube-plugin-test/main.js | |||
@@ -26,7 +26,7 @@ async function register ({ registerHook, registerSetting, settingsManager, stora | |||
26 | 26 | ||
27 | registerHook({ | 27 | registerHook({ |
28 | target: 'filter:api.videos.list.result', | 28 | target: 'filter:api.videos.list.result', |
29 | handler: obj => ({ data: obj.data, total: obj.total + 1 }) | 29 | handler: obj => addToTotal(obj) |
30 | }) | 30 | }) |
31 | 31 | ||
32 | registerHook({ | 32 | registerHook({ |
@@ -41,12 +41,51 @@ async function register ({ registerHook, registerSetting, settingsManager, stora | |||
41 | registerHook({ | 41 | registerHook({ |
42 | target: 'filter:api.video.upload.accept.result', | 42 | target: 'filter:api.video.upload.accept.result', |
43 | handler: ({ accepted }, { videoBody }) => { | 43 | handler: ({ accepted }, { videoBody }) => { |
44 | if (accepted !== false) return { accepted: true } | 44 | if (!accepted) return { accepted: false } |
45 | if (videoBody.name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '} | 45 | if (videoBody.name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '} |
46 | 46 | ||
47 | return { accepted: true } | 47 | return { accepted: true } |
48 | } | 48 | } |
49 | }) | 49 | }) |
50 | |||
51 | registerHook({ | ||
52 | target: 'filter:api.video-thread.create.accept.result', | ||
53 | handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody) | ||
54 | }) | ||
55 | |||
56 | registerHook({ | ||
57 | target: 'filter:api.video-comment-reply.create.accept.result', | ||
58 | handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody) | ||
59 | }) | ||
60 | |||
61 | registerHook({ | ||
62 | target: 'filter:api.video-threads.list.params', | ||
63 | handler: obj => addToCount(obj) | ||
64 | }) | ||
65 | |||
66 | registerHook({ | ||
67 | target: 'filter:api.video-threads.list.result', | ||
68 | handler: obj => addToTotal(obj) | ||
69 | }) | ||
70 | |||
71 | registerHook({ | ||
72 | target: 'filter:api.video-thread-comments.list.result', | ||
73 | handler: obj => { | ||
74 | obj.data.forEach(c => c.text += ' <3') | ||
75 | |||
76 | return obj | ||
77 | } | ||
78 | }) | ||
79 | |||
80 | registerHook({ | ||
81 | target: 'filter:video.auto-blacklist.result', | ||
82 | handler: (blacklisted, { video }) => { | ||
83 | if (blacklisted) return true | ||
84 | if (video.name.includes('please blacklist me')) return true | ||
85 | |||
86 | return false | ||
87 | } | ||
88 | }) | ||
50 | } | 89 | } |
51 | 90 | ||
52 | async function unregister () { | 91 | async function unregister () { |
@@ -63,3 +102,17 @@ module.exports = { | |||
63 | function addToCount (obj) { | 102 | function addToCount (obj) { |
64 | return Object.assign({}, obj, { count: obj.count + 1 }) | 103 | return Object.assign({}, obj, { count: obj.count + 1 }) |
65 | } | 104 | } |
105 | |||
106 | function addToTotal (result) { | ||
107 | return { | ||
108 | data: result.data, | ||
109 | total: result.total + 1 | ||
110 | } | ||
111 | } | ||
112 | |||
113 | function checkCommentBadWord (accepted, commentBody) { | ||
114 | if (!accepted) return { accepted: false } | ||
115 | if (commentBody.text.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '} | ||
116 | |||
117 | return { accepted: true } | ||
118 | } | ||
diff --git a/server/tests/plugins/filter-hooks.ts b/server/tests/plugins/filter-hooks.ts index 4fc2c437b..ec0679b04 100644 --- a/server/tests/plugins/filter-hooks.ts +++ b/server/tests/plugins/filter-hooks.ts | |||
@@ -11,15 +11,28 @@ import { | |||
11 | } from '../../../shared/extra-utils/server/servers' | 11 | } from '../../../shared/extra-utils/server/servers' |
12 | import { | 12 | import { |
13 | addVideoCommentReply, | 13 | addVideoCommentReply, |
14 | addVideoCommentThread, deleteVideoComment, | 14 | addVideoCommentThread, |
15 | getPluginTestPath, getVideosList, | 15 | deleteVideoComment, |
16 | installPlugin, removeVideo, | 16 | getPluginTestPath, |
17 | getVideosList, | ||
18 | installPlugin, | ||
19 | removeVideo, | ||
17 | setAccessTokensToServers, | 20 | setAccessTokensToServers, |
18 | updateVideo, | 21 | updateVideo, |
19 | uploadVideo, | 22 | uploadVideo, |
20 | viewVideo, | 23 | viewVideo, |
21 | getVideosListPagination, getVideo | 24 | getVideosListPagination, |
25 | getVideo, | ||
26 | getVideoCommentThreads, | ||
27 | getVideoThreadComments, | ||
28 | getVideoWithToken, | ||
29 | setDefaultVideoChannel, | ||
30 | waitJobs, | ||
31 | doubleFollow | ||
22 | } from '../../../shared/extra-utils' | 32 | } from '../../../shared/extra-utils' |
33 | import { VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model' | ||
34 | import { VideoDetails } from '../../../shared/models/videos' | ||
35 | import { getYoutubeVideoUrl, importVideo } from '../../../shared/extra-utils/videos/video-imports' | ||
23 | 36 | ||
24 | const expect = chai.expect | 37 | const expect = chai.expect |
25 | 38 | ||
@@ -33,6 +46,8 @@ describe('Test plugin filter hooks', function () { | |||
33 | 46 | ||
34 | servers = await flushAndRunMultipleServers(2) | 47 | servers = await flushAndRunMultipleServers(2) |
35 | await setAccessTokensToServers(servers) | 48 | await setAccessTokensToServers(servers) |
49 | await setDefaultVideoChannel(servers) | ||
50 | await doubleFollow(servers[0], servers[1]) | ||
36 | 51 | ||
37 | await installPlugin({ | 52 | await installPlugin({ |
38 | url: servers[0].url, | 53 | url: servers[0].url, |
@@ -54,7 +69,7 @@ describe('Test plugin filter hooks', function () { | |||
54 | videoUUID = res.body.data[0].uuid | 69 | videoUUID = res.body.data[0].uuid |
55 | }) | 70 | }) |
56 | 71 | ||
57 | it('Should run filter:api.videos.list.params hook', async function () { | 72 | it('Should run filter:api.videos.list.params', async function () { |
58 | const res = await getVideosListPagination(servers[0].url, 0, 2) | 73 | const res = await getVideosListPagination(servers[0].url, 0, 2) |
59 | 74 | ||
60 | // 2 plugins do +1 to the count parameter | 75 | // 2 plugins do +1 to the count parameter |
@@ -74,6 +89,104 @@ describe('Test plugin filter hooks', function () { | |||
74 | expect(res.body.name).to.contain('<3') | 89 | expect(res.body.name).to.contain('<3') |
75 | }) | 90 | }) |
76 | 91 | ||
92 | it('Should run filter:api.video.upload.accept.result', async function () { | ||
93 | await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, 403) | ||
94 | }) | ||
95 | |||
96 | it('Should run filter:api.video-thread.create.accept.result', async function () { | ||
97 | await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', 403) | ||
98 | }) | ||
99 | |||
100 | it('Should run filter:api.video-comment-reply.create.accept.result', async function () { | ||
101 | const res = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'thread') | ||
102 | threadId = res.body.comment.id | ||
103 | |||
104 | await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with bad word', 403) | ||
105 | await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with good word', 200) | ||
106 | }) | ||
107 | |||
108 | it('Should run filter:api.video-threads.list.params', async function () { | ||
109 | const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0) | ||
110 | |||
111 | // our plugin do +1 to the count parameter | ||
112 | expect(res.body.data).to.have.lengthOf(1) | ||
113 | }) | ||
114 | |||
115 | it('Should run filter:api.video-threads.list.result', async function () { | ||
116 | const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0) | ||
117 | |||
118 | // Plugin do +1 to the total result | ||
119 | expect(res.body.total).to.equal(2) | ||
120 | }) | ||
121 | |||
122 | it('Should run filter:api.video-thread-comments.list.params') | ||
123 | |||
124 | it('Should run filter:api.video-thread-comments.list.result', async function () { | ||
125 | const res = await getVideoThreadComments(servers[0].url, videoUUID, threadId) | ||
126 | |||
127 | const thread = res.body as VideoCommentThreadTree | ||
128 | expect(thread.comment.text.endsWith(' <3')).to.be.true | ||
129 | }) | ||
130 | |||
131 | describe('Should run filter:video.auto-blacklist.result', function () { | ||
132 | |||
133 | async function checkIsBlacklisted (oldRes: any, value: boolean) { | ||
134 | const videoId = oldRes.body.video.uuid | ||
135 | |||
136 | const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, videoId) | ||
137 | const video: VideoDetails = res.body | ||
138 | expect(video.blacklisted).to.equal(value) | ||
139 | } | ||
140 | |||
141 | it('Should blacklist on upload', async function () { | ||
142 | const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video please blacklist me' }) | ||
143 | await checkIsBlacklisted(res, true) | ||
144 | }) | ||
145 | |||
146 | it('Should blacklist on import', async function () { | ||
147 | const attributes = { | ||
148 | name: 'video please blacklist me', | ||
149 | targetUrl: getYoutubeVideoUrl(), | ||
150 | channelId: servers[0].videoChannel.id | ||
151 | } | ||
152 | const res = await importVideo(servers[0].url, servers[0].accessToken, attributes) | ||
153 | await checkIsBlacklisted(res, true) | ||
154 | }) | ||
155 | |||
156 | it('Should blacklist on update', async function () { | ||
157 | const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video' }) | ||
158 | const videoId = res.body.video.uuid | ||
159 | await checkIsBlacklisted(res, false) | ||
160 | |||
161 | await updateVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoId, { name: 'please blacklist me' }) | ||
162 | await checkIsBlacklisted(res, true) | ||
163 | }) | ||
164 | |||
165 | it('Should blacklist on remote upload', async function () { | ||
166 | this.timeout(45000) | ||
167 | |||
168 | const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'remote please blacklist me' }) | ||
169 | await waitJobs(servers) | ||
170 | |||
171 | await checkIsBlacklisted(res, true) | ||
172 | }) | ||
173 | |||
174 | it('Should blacklist on remote update', async function () { | ||
175 | this.timeout(45000) | ||
176 | |||
177 | const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video' }) | ||
178 | await waitJobs(servers) | ||
179 | |||
180 | const videoId = res.body.video.uuid | ||
181 | await checkIsBlacklisted(res, false) | ||
182 | |||
183 | await updateVideo(servers[1].url, servers[1].accessToken, videoId, { name: 'please blacklist me' }) | ||
184 | await waitJobs(servers) | ||
185 | |||
186 | await checkIsBlacklisted(res, true) | ||
187 | }) | ||
188 | }) | ||
189 | |||
77 | after(async function () { | 190 | after(async function () { |
78 | await cleanupTests(servers) | 191 | await cleanupTests(servers) |
79 | }) | 192 | }) |