]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/plugins/filter-hooks.ts
be47b20bafc51e14b82856f0da778fc4d3f627df
[github/Chocobozzz/PeerTube.git] / server / tests / plugins / filter-hooks.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { ServerConfig } from '@shared/models'
6 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
7 import {
8 addVideoCommentReply,
9 addVideoCommentThread,
10 createLive,
11 createVideoPlaylist,
12 doubleFollow,
13 getAccountVideos,
14 getConfig,
15 getMyVideos,
16 getPluginTestPath,
17 getVideo,
18 getVideoChannelVideos,
19 getVideoCommentThreads,
20 getVideoPlaylist,
21 getVideosList,
22 getVideosListPagination,
23 getVideoThreadComments,
24 getVideoWithToken,
25 installPlugin,
26 makeRawRequest,
27 registerUser,
28 setAccessTokensToServers,
29 setDefaultVideoChannel,
30 updateCustomSubConfig,
31 updateVideo,
32 uploadVideo,
33 uploadVideoAndGetId,
34 waitJobs
35 } from '../../../shared/extra-utils'
36 import { cleanupTests, flushAndRunMultipleServers, ServerInfo } from '../../../shared/extra-utils/server/servers'
37 import { getGoodVideoUrl, getMyVideoImports, importVideo } from '../../../shared/extra-utils/videos/video-imports'
38 import {
39 VideoDetails,
40 VideoImport,
41 VideoImportState,
42 VideoPlaylist,
43 VideoPlaylistPrivacy,
44 VideoPrivacy
45 } from '../../../shared/models/videos'
46 import { VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model'
47
48 const expect = chai.expect
49
50 describe('Test plugin filter hooks', function () {
51 let servers: ServerInfo[]
52 let videoUUID: string
53 let threadId: number
54
55 before(async function () {
56 this.timeout(30000)
57
58 servers = await flushAndRunMultipleServers(2)
59 await setAccessTokensToServers(servers)
60 await setDefaultVideoChannel(servers)
61 await doubleFollow(servers[0], servers[1])
62
63 await installPlugin({
64 url: servers[0].url,
65 accessToken: servers[0].accessToken,
66 path: getPluginTestPath()
67 })
68
69 await installPlugin({
70 url: servers[0].url,
71 accessToken: servers[0].accessToken,
72 path: getPluginTestPath('-two')
73 })
74
75 for (let i = 0; i < 10; i++) {
76 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i })
77 }
78
79 const res = await getVideosList(servers[0].url)
80 videoUUID = res.body.data[0].uuid
81
82 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
83 live: { enabled: true },
84 signup: { enabled: true },
85 import: {
86 videos: {
87 http: { enabled: true },
88 torrent: { enabled: true }
89 }
90 }
91 })
92 })
93
94 it('Should run filter:api.videos.list.params', async function () {
95 const res = await getVideosListPagination(servers[0].url, 0, 2)
96
97 // 2 plugins do +1 to the count parameter
98 expect(res.body.data).to.have.lengthOf(4)
99 })
100
101 it('Should run filter:api.videos.list.result', async function () {
102 const res = await getVideosListPagination(servers[0].url, 0, 0)
103
104 // Plugin do +1 to the total result
105 expect(res.body.total).to.equal(11)
106 })
107
108 it('Should run filter:api.accounts.videos.list.params', async function () {
109 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
110
111 // 1 plugin do +1 to the count parameter
112 expect(res.body.data).to.have.lengthOf(3)
113 })
114
115 it('Should run filter:api.accounts.videos.list.result', async function () {
116 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
117
118 // Plugin do +2 to the total result
119 expect(res.body.total).to.equal(12)
120 })
121
122 it('Should run filter:api.video-channels.videos.list.params', async function () {
123 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
124
125 // 1 plugin do +3 to the count parameter
126 expect(res.body.data).to.have.lengthOf(5)
127 })
128
129 it('Should run filter:api.video-channels.videos.list.result', async function () {
130 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
131
132 // Plugin do +3 to the total result
133 expect(res.body.total).to.equal(13)
134 })
135
136 it('Should run filter:api.user.me.videos.list.params', async function () {
137 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
138
139 // 1 plugin do +4 to the count parameter
140 expect(res.body.data).to.have.lengthOf(6)
141 })
142
143 it('Should run filter:api.user.me.videos.list.result', async function () {
144 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
145
146 // Plugin do +4 to the total result
147 expect(res.body.total).to.equal(14)
148 })
149
150 it('Should run filter:api.video.get.result', async function () {
151 const res = await getVideo(servers[0].url, videoUUID)
152
153 expect(res.body.name).to.contain('<3')
154 })
155
156 it('Should run filter:api.video.upload.accept.result', async function () {
157 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, HttpStatusCode.FORBIDDEN_403)
158 })
159
160 it('Should run filter:api.live-video.create.accept.result', async function () {
161 const attributes = {
162 name: 'video with bad word',
163 privacy: VideoPrivacy.PUBLIC,
164 channelId: servers[0].videoChannel.id
165 }
166
167 await createLive(servers[0].url, servers[0].accessToken, attributes, HttpStatusCode.FORBIDDEN_403)
168 })
169
170 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
171 const baseAttributes = {
172 name: 'normal title',
173 privacy: VideoPrivacy.PUBLIC,
174 channelId: servers[0].videoChannel.id,
175 targetUrl: getGoodVideoUrl() + 'bad'
176 }
177 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
178 })
179
180 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
181 const baseAttributes = {
182 name: 'bad torrent',
183 privacy: VideoPrivacy.PUBLIC,
184 channelId: servers[0].videoChannel.id,
185 torrentfile: 'video-720p.torrent' as any
186 }
187 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
188 })
189
190 it('Should run filter:api.video.post-import-url.accept.result', async function () {
191 this.timeout(60000)
192
193 let videoImportId: number
194
195 {
196 const baseAttributes = {
197 name: 'title with bad word',
198 privacy: VideoPrivacy.PUBLIC,
199 channelId: servers[0].videoChannel.id,
200 targetUrl: getGoodVideoUrl()
201 }
202 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
203 videoImportId = res.body.id
204 }
205
206 await waitJobs(servers)
207
208 {
209 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
210 const videoImports = res.body.data as VideoImport[]
211
212 const videoImport = videoImports.find(i => i.id === videoImportId)
213
214 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
215 expect(videoImport.state.label).to.equal('Rejected')
216 }
217 })
218
219 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
220 this.timeout(60000)
221
222 let videoImportId: number
223
224 {
225 const baseAttributes = {
226 name: 'title with bad word',
227 privacy: VideoPrivacy.PUBLIC,
228 channelId: servers[0].videoChannel.id,
229 torrentfile: 'video-720p.torrent' as any
230 }
231 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
232 videoImportId = res.body.id
233 }
234
235 await waitJobs(servers)
236
237 {
238 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
239 const videoImports = res.body.data as VideoImport[]
240
241 const videoImport = videoImports.find(i => i.id === videoImportId)
242
243 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
244 expect(videoImport.state.label).to.equal('Rejected')
245 }
246 })
247
248 it('Should run filter:api.video-thread.create.accept.result', async function () {
249 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', HttpStatusCode.FORBIDDEN_403)
250 })
251
252 it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
253 const res = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'thread')
254 threadId = res.body.comment.id
255
256 await addVideoCommentReply(
257 servers[0].url,
258 servers[0].accessToken,
259 videoUUID,
260 threadId,
261 'comment with bad word',
262 HttpStatusCode.FORBIDDEN_403
263 )
264 await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with good word', HttpStatusCode.OK_200)
265 })
266
267 it('Should run filter:api.video-threads.list.params', async function () {
268 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
269
270 // our plugin do +1 to the count parameter
271 expect(res.body.data).to.have.lengthOf(1)
272 })
273
274 it('Should run filter:api.video-threads.list.result', async function () {
275 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
276
277 // Plugin do +1 to the total result
278 expect(res.body.total).to.equal(2)
279 })
280
281 it('Should run filter:api.video-thread-comments.list.params')
282
283 it('Should run filter:api.video-thread-comments.list.result', async function () {
284 const res = await getVideoThreadComments(servers[0].url, videoUUID, threadId)
285
286 const thread = res.body as VideoCommentThreadTree
287 expect(thread.comment.text.endsWith(' <3')).to.be.true
288 })
289
290 describe('Should run filter:video.auto-blacklist.result', function () {
291
292 async function checkIsBlacklisted (oldRes: any, value: boolean) {
293 const videoId = oldRes.body.video.uuid
294
295 const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, videoId)
296 const video: VideoDetails = res.body
297 expect(video.blacklisted).to.equal(value)
298 }
299
300 it('Should blacklist on upload', async function () {
301 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video please blacklist me' })
302 await checkIsBlacklisted(res, true)
303 })
304
305 it('Should blacklist on import', async function () {
306 this.timeout(15000)
307
308 const attributes = {
309 name: 'video please blacklist me',
310 targetUrl: getGoodVideoUrl(),
311 channelId: servers[0].videoChannel.id
312 }
313 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
314 await checkIsBlacklisted(res, true)
315 })
316
317 it('Should blacklist on update', async function () {
318 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
319 const videoId = res.body.video.uuid
320 await checkIsBlacklisted(res, false)
321
322 await updateVideo(servers[0].url, servers[0].accessToken, videoId, { name: 'please blacklist me' })
323 await checkIsBlacklisted(res, true)
324 })
325
326 it('Should blacklist on remote upload', async function () {
327 this.timeout(45000)
328
329 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'remote please blacklist me' })
330 await waitJobs(servers)
331
332 await checkIsBlacklisted(res, true)
333 })
334
335 it('Should blacklist on remote update', async function () {
336 this.timeout(45000)
337
338 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video' })
339 await waitJobs(servers)
340
341 const videoId = res.body.video.uuid
342 await checkIsBlacklisted(res, false)
343
344 await updateVideo(servers[1].url, servers[1].accessToken, videoId, { name: 'please blacklist me' })
345 await waitJobs(servers)
346
347 await checkIsBlacklisted(res, true)
348 })
349 })
350
351 describe('Should run filter:api.user.signup.allowed.result', function () {
352
353 it('Should run on config endpoint', async function () {
354 const res = await getConfig(servers[0].url)
355 expect((res.body as ServerConfig).signup.allowed).to.be.true
356 })
357
358 it('Should allow a signup', async function () {
359 await registerUser(servers[0].url, 'john', 'password')
360 })
361
362 it('Should not allow a signup', async function () {
363 const res = await registerUser(servers[0].url, 'jma', 'password', HttpStatusCode.FORBIDDEN_403)
364
365 expect(res.body.error).to.equal('No jma')
366 })
367 })
368
369 describe('Download hooks', function () {
370 const downloadVideos: VideoDetails[] = []
371
372 before(async function () {
373 this.timeout(60000)
374
375 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
376 transcoding: {
377 webtorrent: {
378 enabled: true
379 },
380 hls: {
381 enabled: true
382 }
383 }
384 })
385
386 const uuids: string[] = []
387
388 for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) {
389 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
390 uuids.push(uuid)
391 }
392
393 await waitJobs(servers)
394
395 for (const uuid of uuids) {
396 const res = await getVideo(servers[0].url, uuid)
397 downloadVideos.push(res.body)
398 }
399 })
400
401 it('Should run filter:api.download.torrent.allowed.result', async function () {
402 const res = await makeRawRequest(downloadVideos[0].files[0].torrentDownloadUrl, 403)
403 expect(res.body.error).to.equal('Liu Bei')
404
405 await makeRawRequest(downloadVideos[1].files[0].torrentDownloadUrl, 200)
406 await makeRawRequest(downloadVideos[2].files[0].torrentDownloadUrl, 200)
407 })
408
409 it('Should run filter:api.download.video.allowed.result', async function () {
410 {
411 const res = await makeRawRequest(downloadVideos[1].files[0].fileDownloadUrl, 403)
412 expect(res.body.error).to.equal('Cao Cao')
413
414 await makeRawRequest(downloadVideos[0].files[0].fileDownloadUrl, 200)
415 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
416 }
417
418 {
419 const res = await makeRawRequest(downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl, 403)
420 expect(res.body.error).to.equal('Sun Jian')
421
422 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
423
424 await makeRawRequest(downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
425 await makeRawRequest(downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
426 }
427 })
428 })
429
430 describe('Embed filters', function () {
431 const embedVideos: VideoDetails[] = []
432 const embedPlaylists: VideoPlaylist[] = []
433
434 before(async function () {
435 this.timeout(60000)
436
437 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
438 transcoding: {
439 enabled: false
440 }
441 })
442
443 for (const name of [ 'bad embed', 'good embed' ]) {
444 {
445 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
446 const res = await getVideo(servers[0].url, uuid)
447 embedVideos.push(res.body)
448 }
449
450 {
451 const playlistAttrs = { displayName: name, videoChannelId: servers[0].videoChannel.id, privacy: VideoPlaylistPrivacy.PUBLIC }
452 const res = await createVideoPlaylist({ url: servers[0].url, token: servers[0].accessToken, playlistAttrs })
453
454 const resPlaylist = await getVideoPlaylist(servers[0].url, res.body.videoPlaylist.id)
455 embedPlaylists.push(resPlaylist.body)
456 }
457 }
458 })
459
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')
463 })
464
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')
468 })
469 })
470
471 after(async function () {
472 await cleanupTests(servers)
473 })
474 })