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'
13 flushAndRunMultipleServers,
18 getVideoChannelVideos,
19 getVideoCommentThreads,
22 getVideosListPagination,
23 getVideoThreadComments,
29 setAccessTokensToServers,
30 setDefaultVideoChannel,
31 updateCustomSubConfig,
37 } from '@shared/extra-utils'
38 import { getGoodVideoUrl, getMyVideoImports, importVideo } from '@shared/extra-utils/videos/video-imports'
41 VideoCommentThreadTree,
48 } from '@shared/models'
50 const expect = chai.expect
52 describe('Test plugin filter hooks', function () {
53 let servers: ServerInfo[]
57 before(async function () {
60 servers = await flushAndRunMultipleServers(2)
61 await setAccessTokensToServers(servers)
62 await setDefaultVideoChannel(servers)
63 await doubleFollow(servers[0], servers[1])
65 await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath() })
66 await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-filter-translations') })
68 for (let i = 0; i < 10; i++) {
69 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i })
72 const res = await getVideosList(servers[0].url)
73 videoUUID = res.body.data[0].uuid
75 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
76 live: { enabled: true },
77 signup: { enabled: true },
80 http: { enabled: true },
81 torrent: { enabled: true }
87 it('Should run filter:api.videos.list.params', async function () {
88 const res = await getVideosListPagination(servers[0].url, 0, 2)
90 // 2 plugins do +1 to the count parameter
91 expect(res.body.data).to.have.lengthOf(4)
94 it('Should run filter:api.videos.list.result', async function () {
95 const res = await getVideosListPagination(servers[0].url, 0, 0)
97 // Plugin do +1 to the total result
98 expect(res.body.total).to.equal(11)
101 it('Should run filter:api.accounts.videos.list.params', async function () {
102 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
104 // 1 plugin do +1 to the count parameter
105 expect(res.body.data).to.have.lengthOf(3)
108 it('Should run filter:api.accounts.videos.list.result', async function () {
109 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
111 // Plugin do +2 to the total result
112 expect(res.body.total).to.equal(12)
115 it('Should run filter:api.video-channels.videos.list.params', async function () {
116 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
118 // 1 plugin do +3 to the count parameter
119 expect(res.body.data).to.have.lengthOf(5)
122 it('Should run filter:api.video-channels.videos.list.result', async function () {
123 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
125 // Plugin do +3 to the total result
126 expect(res.body.total).to.equal(13)
129 it('Should run filter:api.user.me.videos.list.params', async function () {
130 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
132 // 1 plugin do +4 to the count parameter
133 expect(res.body.data).to.have.lengthOf(6)
136 it('Should run filter:api.user.me.videos.list.result', async function () {
137 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
139 // Plugin do +4 to the total result
140 expect(res.body.total).to.equal(14)
143 it('Should run filter:api.video.get.result', async function () {
144 const res = await getVideo(servers[0].url, videoUUID)
146 expect(res.body.name).to.contain('<3')
149 it('Should run filter:api.video.upload.accept.result', async function () {
150 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, HttpStatusCode.FORBIDDEN_403)
153 it('Should run filter:api.live-video.create.accept.result', async function () {
155 name: 'video with bad word',
156 privacy: VideoPrivacy.PUBLIC,
157 channelId: servers[0].videoChannel.id
160 await createLive(servers[0].url, servers[0].accessToken, attributes, HttpStatusCode.FORBIDDEN_403)
163 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
164 const baseAttributes = {
165 name: 'normal title',
166 privacy: VideoPrivacy.PUBLIC,
167 channelId: servers[0].videoChannel.id,
168 targetUrl: getGoodVideoUrl() + 'bad'
170 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
173 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
174 const baseAttributes = {
176 privacy: VideoPrivacy.PUBLIC,
177 channelId: servers[0].videoChannel.id,
178 torrentfile: 'video-720p.torrent' as any
180 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
183 it('Should run filter:api.video.post-import-url.accept.result', async function () {
186 let videoImportId: number
189 const baseAttributes = {
190 name: 'title with bad word',
191 privacy: VideoPrivacy.PUBLIC,
192 channelId: servers[0].videoChannel.id,
193 targetUrl: getGoodVideoUrl()
195 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
196 videoImportId = res.body.id
199 await waitJobs(servers)
202 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
203 const videoImports = res.body.data as VideoImport[]
205 const videoImport = videoImports.find(i => i.id === videoImportId)
207 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
208 expect(videoImport.state.label).to.equal('Rejected')
212 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
215 let videoImportId: number
218 const baseAttributes = {
219 name: 'title with bad word',
220 privacy: VideoPrivacy.PUBLIC,
221 channelId: servers[0].videoChannel.id,
222 torrentfile: 'video-720p.torrent' as any
224 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
225 videoImportId = res.body.id
228 await waitJobs(servers)
231 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
232 const videoImports = res.body.data as VideoImport[]
234 const videoImport = videoImports.find(i => i.id === videoImportId)
236 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
237 expect(videoImport.state.label).to.equal('Rejected')
241 it('Should run filter:api.video-thread.create.accept.result', async function () {
242 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', HttpStatusCode.FORBIDDEN_403)
245 it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
246 const res = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'thread')
247 threadId = res.body.comment.id
249 await addVideoCommentReply(
251 servers[0].accessToken,
254 'comment with bad word',
255 HttpStatusCode.FORBIDDEN_403
257 await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with good word', HttpStatusCode.OK_200)
260 it('Should run filter:api.video-threads.list.params', async function () {
261 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
263 // our plugin do +1 to the count parameter
264 expect(res.body.data).to.have.lengthOf(1)
267 it('Should run filter:api.video-threads.list.result', async function () {
268 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
270 // Plugin do +1 to the total result
271 expect(res.body.total).to.equal(2)
274 it('Should run filter:api.video-thread-comments.list.params')
276 it('Should run filter:api.video-thread-comments.list.result', async function () {
277 const res = await getVideoThreadComments(servers[0].url, videoUUID, threadId)
279 const thread = res.body as VideoCommentThreadTree
280 expect(thread.comment.text.endsWith(' <3')).to.be.true
283 describe('Should run filter:video.auto-blacklist.result', function () {
285 async function checkIsBlacklisted (oldRes: any, value: boolean) {
286 const videoId = oldRes.body.video.uuid
288 const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, videoId)
289 const video: VideoDetails = res.body
290 expect(video.blacklisted).to.equal(value)
293 it('Should blacklist on upload', async function () {
294 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video please blacklist me' })
295 await checkIsBlacklisted(res, true)
298 it('Should blacklist on import', async function () {
302 name: 'video please blacklist me',
303 targetUrl: getGoodVideoUrl(),
304 channelId: servers[0].videoChannel.id
306 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
307 await checkIsBlacklisted(res, true)
310 it('Should blacklist on update', async function () {
311 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
312 const videoId = res.body.video.uuid
313 await checkIsBlacklisted(res, false)
315 await updateVideo(servers[0].url, servers[0].accessToken, videoId, { name: 'please blacklist me' })
316 await checkIsBlacklisted(res, true)
319 it('Should blacklist on remote upload', async function () {
322 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'remote please blacklist me' })
323 await waitJobs(servers)
325 await checkIsBlacklisted(res, true)
328 it('Should blacklist on remote update', async function () {
331 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video' })
332 await waitJobs(servers)
334 const videoId = res.body.video.uuid
335 await checkIsBlacklisted(res, false)
337 await updateVideo(servers[1].url, servers[1].accessToken, videoId, { name: 'please blacklist me' })
338 await waitJobs(servers)
340 await checkIsBlacklisted(res, true)
344 describe('Should run filter:api.user.signup.allowed.result', function () {
346 it('Should run on config endpoint', async function () {
347 const res = await getConfig(servers[0].url)
348 expect((res.body as ServerConfig).signup.allowed).to.be.true
351 it('Should allow a signup', async function () {
352 await registerUser(servers[0].url, 'john', 'password')
355 it('Should not allow a signup', async function () {
356 const res = await registerUser(servers[0].url, 'jma', 'password', HttpStatusCode.FORBIDDEN_403)
358 expect(res.body.error).to.equal('No jma')
362 describe('Download hooks', function () {
363 const downloadVideos: VideoDetails[] = []
365 before(async function () {
368 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
379 const uuids: string[] = []
381 for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) {
382 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
386 await waitJobs(servers)
388 for (const uuid of uuids) {
389 const res = await getVideo(servers[0].url, uuid)
390 downloadVideos.push(res.body)
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')
398 await makeRawRequest(downloadVideos[1].files[0].torrentDownloadUrl, 200)
399 await makeRawRequest(downloadVideos[2].files[0].torrentDownloadUrl, 200)
402 it('Should run filter:api.download.video.allowed.result', async function () {
404 const res = await makeRawRequest(downloadVideos[1].files[0].fileDownloadUrl, 403)
405 expect(res.body.error).to.equal('Cao Cao')
407 await makeRawRequest(downloadVideos[0].files[0].fileDownloadUrl, 200)
408 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
412 const res = await makeRawRequest(downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl, 403)
413 expect(res.body.error).to.equal('Sun Jian')
415 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
417 await makeRawRequest(downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
418 await makeRawRequest(downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
423 describe('Embed filters', function () {
424 const embedVideos: VideoDetails[] = []
425 const embedPlaylists: VideoPlaylist[] = []
427 before(async function () {
430 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
436 for (const name of [ 'bad embed', 'good embed' ]) {
438 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
439 const res = await getVideo(servers[0].url, uuid)
440 embedVideos.push(res.body)
444 const playlistAttrs = { displayName: name, videoChannelId: servers[0].videoChannel.id, privacy: VideoPlaylistPrivacy.PUBLIC }
445 const res = await createVideoPlaylist({ url: servers[0].url, token: servers[0].accessToken, playlistAttrs })
447 const resPlaylist = await getVideoPlaylist(servers[0].url, res.body.videoPlaylist.id)
448 embedPlaylists.push(resPlaylist.body)
453 it('Should run filter:html.embed.video.allowed.result', async function () {
454 const res = await makeRawRequest(servers[0].url + embedVideos[0].embedPath, 200)
455 expect(res.text).to.equal('Lu Bu')
458 it('Should run filter:html.embed.video-playlist.allowed.result', async function () {
459 const res = await makeRawRequest(servers[0].url + embedPlaylists[0].embedPath, 200)
460 expect(res.text).to.equal('Diao Chan')
464 describe('Search filters', function () {
466 before(async function () {
467 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
471 isDefaultSearch: false,
472 disableLocalSearch: false
478 it('Should run filter:api.search.videos.local.list.{params,result}', async function () {
479 await servers[0].searchCommand.advancedVideoSearch({
485 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.params', 1)
486 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.result', 1)
489 it('Should run filter:api.search.videos.index.list.{params,result}', async function () {
490 await servers[0].searchCommand.advancedVideoSearch({
493 searchTarget: 'search-index'
497 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.params', 1)
498 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.result', 1)
499 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.index.list.params', 1)
500 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.index.list.result', 1)
503 it('Should run filter:api.search.video-channels.local.list.{params,result}', async function () {
504 await servers[0].searchCommand.advancedChannelSearch({
510 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.params', 1)
511 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.result', 1)
514 it('Should run filter:api.search.video-channels.index.list.{params,result}', async function () {
515 await servers[0].searchCommand.advancedChannelSearch({
518 searchTarget: 'search-index'
522 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.params', 1)
523 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.result', 1)
524 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.index.list.params', 1)
525 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.index.list.result', 1)
528 it('Should run filter:api.search.video-playlists.local.list.{params,result}', async function () {
529 await servers[0].searchCommand.advancedPlaylistSearch({
535 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.local.list.params', 1)
536 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.local.list.result', 1)
539 it('Should run filter:api.search.video-playlists.index.list.{params,result}', async function () {
540 await servers[0].searchCommand.advancedPlaylistSearch({
543 searchTarget: 'search-index'
547 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.local.list.params', 1)
548 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.local.list.result', 1)
549 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.index.list.params', 1)
550 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.index.list.result', 1)
554 after(async function () {
555 await cleanupTests(servers)