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