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