]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/plugins/filter-hooks.ts
Introduce config command
[github/Chocobozzz/PeerTube.git] / server / tests / plugins / filter-hooks.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
9b474844 2
9b474844 3import 'mocha'
2158ac90 4import * as chai from 'chai'
af971e06 5import { HttpStatusCode } from '@shared/core-utils'
89cd1275
C
6import {
7 addVideoCommentReply,
6691c522 8 addVideoCommentThread,
af971e06 9 cleanupTests,
3cabf353 10 createLive,
eebd9838 11 createVideoPlaylist,
a1587156 12 doubleFollow,
af971e06 13 flushAndRunMultipleServers,
38267c0c 14 getAccountVideos,
a4d2ca07 15 getMyVideos,
6691c522 16 getVideo,
38267c0c 17 getVideoChannelVideos,
6691c522 18 getVideoCommentThreads,
eebd9838 19 getVideoPlaylist,
a1587156
C
20 getVideosList,
21 getVideosListPagination,
6691c522
C
22 getVideoThreadComments,
23 getVideoWithToken,
4bc45da3 24 makeRawRequest,
ae2abfd3 25 PluginsCommand,
a1587156 26 registerUser,
af971e06 27 ServerInfo,
a1587156 28 setAccessTokensToServers,
6691c522 29 setDefaultVideoChannel,
a1587156
C
30 updateVideo,
31 uploadVideo,
4bc45da3 32 uploadVideoAndGetId,
af971e06
C
33 waitJobs,
34 waitUntilLog
35} from '@shared/extra-utils'
36import { getGoodVideoUrl, getMyVideoImports, importVideo } from '@shared/extra-utils/videos/video-imports'
eebd9838 37import {
2b02c520 38 VideoCommentThreadTree,
eebd9838
C
39 VideoDetails,
40 VideoImport,
41 VideoImportState,
42 VideoPlaylist,
43 VideoPlaylistPrivacy,
44 VideoPrivacy
af971e06 45} from '@shared/models'
9b474844
C
46
47const expect = chai.expect
48
49describe('Test plugin filter hooks', function () {
89cd1275
C
50 let servers: ServerInfo[]
51 let videoUUID: string
52 let threadId: number
9b474844
C
53
54 before(async function () {
4076e2ef 55 this.timeout(60000)
9b474844 56
89cd1275
C
57 servers = await flushAndRunMultipleServers(2)
58 await setAccessTokensToServers(servers)
6691c522
C
59 await setDefaultVideoChannel(servers)
60 await doubleFollow(servers[0], servers[1])
89cd1275 61
ae2abfd3
C
62 await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath() })
63 await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-filter-translations') })
89cd1275
C
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
3cabf353 71
65e6e260
C
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 }
3cabf353
C
81 }
82 }
83 })
9b474844
C
84 })
85
6691c522 86 it('Should run filter:api.videos.list.params', async function () {
89cd1275
C
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
38267c0c
C
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 () {
c824e8a0 122 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
38267c0c
C
123
124 // Plugin do +3 to the total result
125 expect(res.body.total).to.equal(13)
126 })
127
a4d2ca07
C
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
89cd1275
C
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')
9b474844
C
146 })
147
6691c522 148 it('Should run filter:api.video.upload.accept.result', async function () {
f2eb23cd 149 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, HttpStatusCode.FORBIDDEN_403)
6691c522
C
150 })
151
3cabf353
C
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
f2eb23cd 159 await createLive(servers[0].url, servers[0].accessToken, attributes, HttpStatusCode.FORBIDDEN_403)
3cabf353
C
160 })
161
2158ac90
RK
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,
b488ba1e 167 targetUrl: getGoodVideoUrl() + 'bad'
2158ac90 168 }
f2eb23cd 169 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
2158ac90
RK
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 }
f2eb23cd 179 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
2158ac90
RK
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,
b488ba1e 192 targetUrl: getGoodVideoUrl()
2158ac90
RK
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
6691c522 240 it('Should run filter:api.video-thread.create.accept.result', async function () {
f2eb23cd 241 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', HttpStatusCode.FORBIDDEN_403)
6691c522
C
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
f2eb23cd
RK
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)
6691c522
C
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 () {
a1587156 293 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video please blacklist me' })
6691c522
C
294 await checkIsBlacklisted(res, true)
295 })
296
297 it('Should blacklist on import', async function () {
89566f77
C
298 this.timeout(15000)
299
6691c522
C
300 const attributes = {
301 name: 'video please blacklist me',
b488ba1e 302 targetUrl: getGoodVideoUrl(),
6691c522
C
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 () {
a1587156 310 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
6691c522
C
311 const videoId = res.body.video.uuid
312 await checkIsBlacklisted(res, false)
313
a1587156 314 await updateVideo(servers[0].url, servers[0].accessToken, videoId, { name: 'please blacklist me' })
6691c522
C
315 await checkIsBlacklisted(res, true)
316 })
317
318 it('Should blacklist on remote upload', async function () {
3d470a53 319 this.timeout(120000)
6691c522 320
a1587156 321 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'remote please blacklist me' })
6691c522
C
322 await waitJobs(servers)
323
324 await checkIsBlacklisted(res, true)
325 })
326
327 it('Should blacklist on remote update', async function () {
3d470a53 328 this.timeout(120000)
6691c522 329
a1587156 330 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video' })
6691c522
C
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
4ce7eb71
C
343 describe('Should run filter:api.user.signup.allowed.result', function () {
344
345 it('Should run on config endpoint', async function () {
65e6e260
C
346 const body = await servers[0].configCommand.getConfig()
347 expect(body.signup.allowed).to.be.true
4ce7eb71
C
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 () {
f2eb23cd 355 const res = await registerUser(servers[0].url, 'jma', 'password', HttpStatusCode.FORBIDDEN_403)
4ce7eb71
C
356
357 expect(res.body.error).to.equal('No jma')
358 })
359 })
360
4bc45da3
C
361 describe('Download hooks', function () {
362 const downloadVideos: VideoDetails[] = []
363
364 before(async function () {
c4244cfd 365 this.timeout(120000)
4bc45da3 366
65e6e260
C
367 await servers[0].configCommand.updateCustomSubConfig({
368 newConfig: {
369 transcoding: {
370 webtorrent: {
371 enabled: true
372 },
373 hls: {
374 enabled: true
375 }
4bc45da3
C
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
eebd9838
C
424 describe('Embed filters', function () {
425 const embedVideos: VideoDetails[] = []
426 const embedPlaylists: VideoPlaylist[] = []
427
428 before(async function () {
429 this.timeout(60000)
430
65e6e260
C
431 await servers[0].configCommand.updateCustomSubConfig({
432 newConfig: {
433 transcoding: {
434 enabled: false
435 }
eebd9838
C
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
74a4d531
C
467 describe('Search filters', function () {
468
469 before(async function () {
65e6e260
C
470 await servers[0].configCommand.updateCustomSubConfig({
471 newConfig: {
472 search: {
473 searchIndex: {
474 enabled: true,
475 isDefaultSearch: false,
476 disableLocalSearch: false
477 }
74a4d531
C
478 }
479 }
480 })
481 })
482
483 it('Should run filter:api.search.videos.local.list.{params,result}', async function () {
af971e06
C
484 await servers[0].searchCommand.advancedVideoSearch({
485 search: {
486 search: 'Sun Quan'
487 }
74a4d531
C
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 () {
af971e06
C
495 await servers[0].searchCommand.advancedVideoSearch({
496 search: {
497 search: 'Sun Quan',
498 searchTarget: 'search-index'
499 }
74a4d531
C
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 () {
af971e06
C
509 await servers[0].searchCommand.advancedChannelSearch({
510 search: {
511 search: 'Sun Ce'
512 }
74a4d531
C
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 () {
af971e06
C
520 await servers[0].searchCommand.advancedChannelSearch({
521 search: {
522 search: 'Sun Ce',
523 searchTarget: 'search-index'
524 }
74a4d531
C
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 })
37a44fc9
C
532
533 it('Should run filter:api.search.video-playlists.local.list.{params,result}', async function () {
af971e06
C
534 await servers[0].searchCommand.advancedPlaylistSearch({
535 search: {
536 search: 'Sun Jian'
537 }
37a44fc9
C
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 () {
af971e06
C
545 await servers[0].searchCommand.advancedPlaylistSearch({
546 search: {
547 search: 'Sun Jian',
548 searchTarget: 'search-index'
549 }
37a44fc9
C
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 })
74a4d531
C
557 })
558
9b474844 559 after(async function () {
89cd1275 560 await cleanupTests(servers)
9b474844
C
561 })
562})