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