1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
9 flushAndRunMultipleServers,
14 setAccessTokensToServers,
15 setDefaultVideoChannel,
17 } from '@shared/extra-utils'
18 import { VideoDetails, VideoImportState, VideoPlaylist, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
20 const expect = chai.expect
22 describe('Test plugin filter hooks', function () {
23 let servers: ServerInfo[]
27 before(async function () {
30 servers = await flushAndRunMultipleServers(2)
31 await setAccessTokensToServers(servers)
32 await setDefaultVideoChannel(servers)
33 await doubleFollow(servers[0], servers[1])
35 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath() })
36 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath('-filter-translations') })
38 for (let i = 0; i < 10; i++) {
39 await servers[0].videos.upload({ attributes: { name: 'default video ' + i } })
42 const { data } = await servers[0].videos.list()
43 videoUUID = data[0].uuid
45 await servers[0].config.updateCustomSubConfig({
47 live: { enabled: true },
48 signup: { enabled: true },
51 http: { enabled: true },
52 torrent: { enabled: true }
59 it('Should run filter:api.videos.list.params', async function () {
60 const { data } = await servers[0].videos.list({ start: 0, count: 2 })
62 // 2 plugins do +1 to the count parameter
63 expect(data).to.have.lengthOf(4)
66 it('Should run filter:api.videos.list.result', async function () {
67 const { total } = await servers[0].videos.list({ start: 0, count: 0 })
69 // Plugin do +1 to the total result
70 expect(total).to.equal(11)
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 })
76 // 1 plugin do +1 to the count parameter
77 expect(data).to.have.lengthOf(3)
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 })
83 // Plugin do +2 to the total result
84 expect(total).to.equal(12)
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 })
90 // 1 plugin do +3 to the count parameter
91 expect(data).to.have.lengthOf(5)
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 })
97 // Plugin do +3 to the total result
98 expect(total).to.equal(13)
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 })
104 // 1 plugin do +4 to the count parameter
105 expect(data).to.have.lengthOf(6)
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 })
111 // Plugin do +4 to the total result
112 expect(total).to.equal(14)
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')
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 })
124 it('Should run filter:api.live-video.create.accept.result', async function () {
126 name: 'video with bad word',
127 privacy: VideoPrivacy.PUBLIC,
128 channelId: servers[0].store.channel.id
131 await servers[0].live.create({ fields: attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
134 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
136 name: 'normal title',
137 privacy: VideoPrivacy.PUBLIC,
138 channelId: servers[0].store.channel.id,
139 targetUrl: ImportsCommand.getGoodVideoUrl() + 'bad'
141 await servers[0].imports.importVideo({ attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
144 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
147 privacy: VideoPrivacy.PUBLIC,
148 channelId: servers[0].store.channel.id,
149 torrentfile: 'video-720p.torrent' as any
151 await servers[0].imports.importVideo({ attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
154 it('Should run filter:api.video.post-import-url.accept.result', async function () {
157 let videoImportId: number
161 name: 'title with bad word',
162 privacy: VideoPrivacy.PUBLIC,
163 channelId: servers[0].store.channel.id,
164 targetUrl: ImportsCommand.getGoodVideoUrl()
166 const body = await servers[0].imports.importVideo({ attributes })
167 videoImportId = body.id
170 await waitJobs(servers)
173 const body = await servers[0].imports.getMyVideoImports()
174 const videoImports = body.data
176 const videoImport = videoImports.find(i => i.id === videoImportId)
178 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
179 expect(videoImport.state.label).to.equal('Rejected')
183 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
186 let videoImportId: number
190 name: 'title with bad word',
191 privacy: VideoPrivacy.PUBLIC,
192 channelId: servers[0].store.channel.id,
193 torrentfile: 'video-720p.torrent' as any
195 const body = await servers[0].imports.importVideo({ attributes })
196 videoImportId = body.id
199 await waitJobs(servers)
202 const { data: videoImports } = await servers[0].imports.getMyVideoImports()
204 const videoImport = videoImports.find(i => i.id === videoImportId)
206 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
207 expect(videoImport.state.label).to.equal('Rejected')
211 it('Should run filter:api.video-thread.create.accept.result', async function () {
212 await servers[0].comments.createThread({
214 text: 'comment with bad word',
215 expectedStatus: HttpStatusCode.FORBIDDEN_403
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
223 await servers[0].comments.addReply({
225 toCommentId: threadId,
226 text: 'comment with bad word',
227 expectedStatus: HttpStatusCode.FORBIDDEN_403
229 await servers[0].comments.addReply({
231 toCommentId: threadId,
232 text: 'comment with good word',
233 expectedStatus: HttpStatusCode.OK_200
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 })
240 // our plugin do +1 to the count parameter
241 expect(data).to.have.lengthOf(1)
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 })
247 // Plugin do +1 to the total result
248 expect(total).to.equal(2)
251 it('Should run filter:api.video-thread-comments.list.params')
253 it('Should run filter:api.video-thread-comments.list.result', async function () {
254 const thread = await servers[0].comments.getThread({ videoId: videoUUID, threadId })
256 expect(thread.comment.text.endsWith(' <3')).to.be.true
259 describe('Should run filter:video.auto-blacklist.result', function () {
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)
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)
271 it('Should blacklist on import', async function () {
275 name: 'video please blacklist me',
276 targetUrl: ImportsCommand.getGoodVideoUrl(),
277 channelId: servers[0].store.channel.id
279 const body = await servers[0].imports.importVideo({ attributes })
280 await checkIsBlacklisted(body.video.uuid, true)
283 it('Should blacklist on update', async function () {
284 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video' } })
285 await checkIsBlacklisted(uuid, false)
287 await servers[0].videos.update({ id: uuid, attributes: { name: 'please blacklist me' } })
288 await checkIsBlacklisted(uuid, true)
291 it('Should blacklist on remote upload', async function () {
294 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'remote please blacklist me' } })
295 await waitJobs(servers)
297 await checkIsBlacklisted(uuid, true)
300 it('Should blacklist on remote update', async function () {
303 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video' } })
304 await waitJobs(servers)
306 await checkIsBlacklisted(uuid, false)
308 await servers[1].videos.update({ id: uuid, attributes: { name: 'please blacklist me' } })
309 await waitJobs(servers)
311 await checkIsBlacklisted(uuid, true)
315 describe('Should run filter:api.user.signup.allowed.result', function () {
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
322 it('Should allow a signup', async function () {
323 await servers[0].users.register({ username: 'john', password: 'password' })
326 it('Should not allow a signup', async function () {
327 const res = await servers[0].users.register({
329 password: 'password',
330 expectedStatus: HttpStatusCode.FORBIDDEN_403
333 expect(res.body.error).to.equal('No jma')
337 describe('Download hooks', function () {
338 const downloadVideos: VideoDetails[] = []
340 before(async function () {
343 await servers[0].config.updateCustomSubConfig({
356 const uuids: string[] = []
358 for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) {
359 const uuid = (await servers[0].videos.quickUpload({ name: name })).uuid
363 await waitJobs(servers)
365 for (const uuid of uuids) {
366 downloadVideos.push(await servers[0].videos.get({ id: uuid }))
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')
374 await makeRawRequest(downloadVideos[1].files[0].torrentDownloadUrl, 200)
375 await makeRawRequest(downloadVideos[2].files[0].torrentDownloadUrl, 200)
378 it('Should run filter:api.download.video.allowed.result', async function () {
380 const res = await makeRawRequest(downloadVideos[1].files[0].fileDownloadUrl, 403)
381 expect(res.body.error).to.equal('Cao Cao')
383 await makeRawRequest(downloadVideos[0].files[0].fileDownloadUrl, 200)
384 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
388 const res = await makeRawRequest(downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl, 403)
389 expect(res.body.error).to.equal('Sun Jian')
391 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
393 await makeRawRequest(downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
394 await makeRawRequest(downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
399 describe('Embed filters', function () {
400 const embedVideos: VideoDetails[] = []
401 const embedPlaylists: VideoPlaylist[] = []
403 before(async function () {
406 await servers[0].config.updateCustomSubConfig({
414 for (const name of [ 'bad embed', 'good embed' ]) {
416 const uuid = (await servers[0].videos.quickUpload({ name: name })).uuid
417 embedVideos.push(await servers[0].videos.get({ id: uuid }))
421 const attributes = { displayName: name, videoChannelId: servers[0].store.channel.id, privacy: VideoPlaylistPrivacy.PUBLIC }
422 const { id } = await servers[0].playlists.create({ attributes })
424 const playlist = await servers[0].playlists.get({ playlistId: id })
425 embedPlaylists.push(playlist)
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')
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')
441 describe('Search filters', function () {
443 before(async function () {
444 await servers[0].config.updateCustomSubConfig({
449 isDefaultSearch: false,
450 disableLocalSearch: false
457 it('Should run filter:api.search.videos.local.list.{params,result}', async function () {
458 await servers[0].search.advancedVideoSearch({
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)
468 it('Should run filter:api.search.videos.index.list.{params,result}', async function () {
469 await servers[0].search.advancedVideoSearch({
472 searchTarget: 'search-index'
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)
482 it('Should run filter:api.search.video-channels.local.list.{params,result}', async function () {
483 await servers[0].search.advancedChannelSearch({
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)
493 it('Should run filter:api.search.video-channels.index.list.{params,result}', async function () {
494 await servers[0].search.advancedChannelSearch({
497 searchTarget: 'search-index'
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)
507 it('Should run filter:api.search.video-playlists.local.list.{params,result}', async function () {
508 await servers[0].search.advancedPlaylistSearch({
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)
518 it('Should run filter:api.search.video-playlists.index.list.{params,result}', async function () {
519 await servers[0].search.advancedPlaylistSearch({
522 searchTarget: 'search-index'
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)
533 after(async function () {
534 await cleanupTests(servers)