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