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