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