1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
13 setAccessTokensToServers,
14 setDefaultVideoChannel,
16 } from '@shared/extra-utils'
17 import { HttpStatusCode, VideoDetails, VideoImportState, VideoPlaylist, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
19 const expect = chai.expect
21 describe('Test plugin filter hooks', function () {
22 let servers: PeerTubeServer[]
26 before(async function () {
29 servers = await createMultipleServers(2)
30 await setAccessTokensToServers(servers)
31 await setDefaultVideoChannel(servers)
32 await doubleFollow(servers[0], servers[1])
34 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath() })
35 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath('-filter-translations') })
37 for (let i = 0; i < 10; i++) {
38 await servers[0].videos.upload({ attributes: { name: 'default video ' + i } })
41 const { data } = await servers[0].videos.list()
42 videoUUID = data[0].uuid
44 await servers[0].config.updateCustomSubConfig({
46 live: { enabled: true },
47 signup: { enabled: true },
50 http: { enabled: true },
51 torrent: { enabled: true }
58 it('Should run filter:api.videos.list.params', async function () {
59 const { data } = await servers[0].videos.list({ start: 0, count: 2 })
61 // 2 plugins do +1 to the count parameter
62 expect(data).to.have.lengthOf(4)
65 it('Should run filter:api.videos.list.result', async function () {
66 const { total } = await servers[0].videos.list({ start: 0, count: 0 })
68 // Plugin do +1 to the total result
69 expect(total).to.equal(11)
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 })
75 // 1 plugin do +1 to the count parameter
76 expect(data).to.have.lengthOf(3)
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 })
82 // Plugin do +2 to the total result
83 expect(total).to.equal(12)
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 })
89 // 1 plugin do +3 to the count parameter
90 expect(data).to.have.lengthOf(5)
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 })
96 // Plugin do +3 to the total result
97 expect(total).to.equal(13)
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 })
103 // 1 plugin do +4 to the count parameter
104 expect(data).to.have.lengthOf(6)
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 })
110 // Plugin do +4 to the total result
111 expect(total).to.equal(14)
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')
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 })
123 it('Should run filter:api.live-video.create.accept.result', async function () {
125 name: 'video with bad word',
126 privacy: VideoPrivacy.PUBLIC,
127 channelId: servers[0].store.channel.id
130 await servers[0].live.create({ fields: attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
133 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
135 name: 'normal title',
136 privacy: VideoPrivacy.PUBLIC,
137 channelId: servers[0].store.channel.id,
138 targetUrl: FIXTURE_URLS.goodVideo + 'bad'
140 await servers[0].imports.importVideo({ attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
143 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
146 privacy: VideoPrivacy.PUBLIC,
147 channelId: servers[0].store.channel.id,
148 torrentfile: 'video-720p.torrent' as any
150 await servers[0].imports.importVideo({ attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
153 it('Should run filter:api.video.post-import-url.accept.result', async function () {
156 let videoImportId: number
160 name: 'title with bad word',
161 privacy: VideoPrivacy.PUBLIC,
162 channelId: servers[0].store.channel.id,
163 targetUrl: FIXTURE_URLS.goodVideo
165 const body = await servers[0].imports.importVideo({ attributes })
166 videoImportId = body.id
169 await waitJobs(servers)
172 const body = await servers[0].imports.getMyVideoImports()
173 const videoImports = body.data
175 const videoImport = videoImports.find(i => i.id === videoImportId)
177 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
178 expect(videoImport.state.label).to.equal('Rejected')
182 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
185 let videoImportId: number
189 name: 'title with bad word',
190 privacy: VideoPrivacy.PUBLIC,
191 channelId: servers[0].store.channel.id,
192 torrentfile: 'video-720p.torrent' as any
194 const body = await servers[0].imports.importVideo({ attributes })
195 videoImportId = body.id
198 await waitJobs(servers)
201 const { data: videoImports } = await servers[0].imports.getMyVideoImports()
203 const videoImport = videoImports.find(i => i.id === videoImportId)
205 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
206 expect(videoImport.state.label).to.equal('Rejected')
210 it('Should run filter:api.video-thread.create.accept.result', async function () {
211 await servers[0].comments.createThread({
213 text: 'comment with bad word',
214 expectedStatus: HttpStatusCode.FORBIDDEN_403
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
222 await servers[0].comments.addReply({
224 toCommentId: threadId,
225 text: 'comment with bad word',
226 expectedStatus: HttpStatusCode.FORBIDDEN_403
228 await servers[0].comments.addReply({
230 toCommentId: threadId,
231 text: 'comment with good word',
232 expectedStatus: HttpStatusCode.OK_200
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 })
239 // our plugin do +1 to the count parameter
240 expect(data).to.have.lengthOf(1)
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 })
246 // Plugin do +1 to the total result
247 expect(total).to.equal(2)
250 it('Should run filter:api.video-thread-comments.list.params')
252 it('Should run filter:api.video-thread-comments.list.result', async function () {
253 const thread = await servers[0].comments.getThread({ videoId: videoUUID, threadId })
255 expect(thread.comment.text.endsWith(' <3')).to.be.true
258 it('Should run filter:api.overviews.videos.list.{params,result}', async function () {
259 await servers[0].overviews.getVideos({ page: 1 })
261 // 3 because we get 3 samples per page
262 await servers[0].servers.waitUntilLog('Run hook filter:api.overviews.videos.list.params', 3)
263 await servers[0].servers.waitUntilLog('Run hook filter:api.overviews.videos.list.result', 3)
266 describe('Should run filter:video.auto-blacklist.result', function () {
268 async function checkIsBlacklisted (id: number | string, value: boolean) {
269 const video = await servers[0].videos.getWithToken({ id })
270 expect(video.blacklisted).to.equal(value)
273 it('Should blacklist on upload', async function () {
274 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video please blacklist me' } })
275 await checkIsBlacklisted(uuid, true)
278 it('Should blacklist on import', async function () {
282 name: 'video please blacklist me',
283 targetUrl: FIXTURE_URLS.goodVideo,
284 channelId: servers[0].store.channel.id
286 const body = await servers[0].imports.importVideo({ attributes })
287 await checkIsBlacklisted(body.video.uuid, true)
290 it('Should blacklist on update', async function () {
291 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video' } })
292 await checkIsBlacklisted(uuid, false)
294 await servers[0].videos.update({ id: uuid, attributes: { name: 'please blacklist me' } })
295 await checkIsBlacklisted(uuid, true)
298 it('Should blacklist on remote upload', async function () {
301 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'remote please blacklist me' } })
302 await waitJobs(servers)
304 await checkIsBlacklisted(uuid, true)
307 it('Should blacklist on remote update', async function () {
310 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video' } })
311 await waitJobs(servers)
313 await checkIsBlacklisted(uuid, false)
315 await servers[1].videos.update({ id: uuid, attributes: { name: 'please blacklist me' } })
316 await waitJobs(servers)
318 await checkIsBlacklisted(uuid, true)
322 describe('Should run filter:api.user.signup.allowed.result', function () {
324 it('Should run on config endpoint', async function () {
325 const body = await servers[0].config.getConfig()
326 expect(body.signup.allowed).to.be.true
329 it('Should allow a signup', async function () {
330 await servers[0].users.register({ username: 'john', password: 'password' })
333 it('Should not allow a signup', async function () {
334 const res = await servers[0].users.register({
336 password: 'password',
337 expectedStatus: HttpStatusCode.FORBIDDEN_403
340 expect(res.body.error).to.equal('No jma')
344 describe('Download hooks', function () {
345 const downloadVideos: VideoDetails[] = []
347 before(async function () {
350 await servers[0].config.updateCustomSubConfig({
363 const uuids: string[] = []
365 for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) {
366 const uuid = (await servers[0].videos.quickUpload({ name: name })).uuid
370 await waitJobs(servers)
372 for (const uuid of uuids) {
373 downloadVideos.push(await servers[0].videos.get({ id: uuid }))
377 it('Should run filter:api.download.torrent.allowed.result', async function () {
378 const res = await makeRawRequest(downloadVideos[0].files[0].torrentDownloadUrl, 403)
379 expect(res.body.error).to.equal('Liu Bei')
381 await makeRawRequest(downloadVideos[1].files[0].torrentDownloadUrl, 200)
382 await makeRawRequest(downloadVideos[2].files[0].torrentDownloadUrl, 200)
385 it('Should run filter:api.download.video.allowed.result', async function () {
387 const res = await makeRawRequest(downloadVideos[1].files[0].fileDownloadUrl, 403)
388 expect(res.body.error).to.equal('Cao Cao')
390 await makeRawRequest(downloadVideos[0].files[0].fileDownloadUrl, 200)
391 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
395 const res = await makeRawRequest(downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl, 403)
396 expect(res.body.error).to.equal('Sun Jian')
398 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
400 await makeRawRequest(downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
401 await makeRawRequest(downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
406 describe('Embed filters', function () {
407 const embedVideos: VideoDetails[] = []
408 const embedPlaylists: VideoPlaylist[] = []
410 before(async function () {
413 await servers[0].config.updateCustomSubConfig({
421 for (const name of [ 'bad embed', 'good embed' ]) {
423 const uuid = (await servers[0].videos.quickUpload({ name: name })).uuid
424 embedVideos.push(await servers[0].videos.get({ id: uuid }))
428 const attributes = { displayName: name, videoChannelId: servers[0].store.channel.id, privacy: VideoPlaylistPrivacy.PUBLIC }
429 const { id } = await servers[0].playlists.create({ attributes })
431 const playlist = await servers[0].playlists.get({ playlistId: id })
432 embedPlaylists.push(playlist)
437 it('Should run filter:html.embed.video.allowed.result', async function () {
438 const res = await makeRawRequest(servers[0].url + embedVideos[0].embedPath, 200)
439 expect(res.text).to.equal('Lu Bu')
442 it('Should run filter:html.embed.video-playlist.allowed.result', async function () {
443 const res = await makeRawRequest(servers[0].url + embedPlaylists[0].embedPath, 200)
444 expect(res.text).to.equal('Diao Chan')
448 describe('Search filters', function () {
450 before(async function () {
451 await servers[0].config.updateCustomSubConfig({
456 isDefaultSearch: false,
457 disableLocalSearch: false
464 it('Should run filter:api.search.videos.local.list.{params,result}', async function () {
465 await servers[0].search.advancedVideoSearch({
471 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.params', 1)
472 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.result', 1)
475 it('Should run filter:api.search.videos.index.list.{params,result}', async function () {
476 await servers[0].search.advancedVideoSearch({
479 searchTarget: 'search-index'
483 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.params', 1)
484 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.local.list.result', 1)
485 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.index.list.params', 1)
486 await servers[0].servers.waitUntilLog('Run hook filter:api.search.videos.index.list.result', 1)
489 it('Should run filter:api.search.video-channels.local.list.{params,result}', async function () {
490 await servers[0].search.advancedChannelSearch({
496 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.params', 1)
497 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.result', 1)
500 it('Should run filter:api.search.video-channels.index.list.{params,result}', async function () {
501 await servers[0].search.advancedChannelSearch({
504 searchTarget: 'search-index'
508 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.params', 1)
509 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.local.list.result', 1)
510 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.index.list.params', 1)
511 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-channels.index.list.result', 1)
514 it('Should run filter:api.search.video-playlists.local.list.{params,result}', async function () {
515 await servers[0].search.advancedPlaylistSearch({
521 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.params', 1)
522 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.result', 1)
525 it('Should run filter:api.search.video-playlists.index.list.{params,result}', async function () {
526 await servers[0].search.advancedPlaylistSearch({
529 searchTarget: 'search-index'
533 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.params', 1)
534 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.local.list.result', 1)
535 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.index.list.params', 1)
536 await servers[0].servers.waitUntilLog('Run hook filter:api.search.video-playlists.index.list.result', 1)
540 after(async function () {
541 await cleanupTests(servers)