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'
12 flushAndRunMultipleServers,
16 getVideoChannelVideos,
17 getVideoCommentThreads,
20 getVideosListPagination,
21 getVideoThreadComments,
27 setAccessTokensToServers,
28 setDefaultVideoChannel,
34 } from '@shared/extra-utils'
35 import { getGoodVideoUrl, getMyVideoImports, importVideo } from '@shared/extra-utils/videos/video-imports'
37 VideoCommentThreadTree,
44 } from '@shared/models'
46 const expect = chai.expect
48 describe('Test plugin filter hooks', function () {
49 let servers: ServerInfo[]
53 before(async function () {
56 servers = await flushAndRunMultipleServers(2)
57 await setAccessTokensToServers(servers)
58 await setDefaultVideoChannel(servers)
59 await doubleFollow(servers[0], servers[1])
61 await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath() })
62 await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-filter-translations') })
64 for (let i = 0; i < 10; i++) {
65 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i })
68 const res = await getVideosList(servers[0].url)
69 videoUUID = res.body.data[0].uuid
71 await servers[0].configCommand.updateCustomSubConfig({
73 live: { enabled: true },
74 signup: { enabled: true },
77 http: { enabled: true },
78 torrent: { enabled: true }
85 it('Should run filter:api.videos.list.params', async function () {
86 const res = await getVideosListPagination(servers[0].url, 0, 2)
88 // 2 plugins do +1 to the count parameter
89 expect(res.body.data).to.have.lengthOf(4)
92 it('Should run filter:api.videos.list.result', async function () {
93 const res = await getVideosListPagination(servers[0].url, 0, 0)
95 // Plugin do +1 to the total result
96 expect(res.body.total).to.equal(11)
99 it('Should run filter:api.accounts.videos.list.params', async function () {
100 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
102 // 1 plugin do +1 to the count parameter
103 expect(res.body.data).to.have.lengthOf(3)
106 it('Should run filter:api.accounts.videos.list.result', async function () {
107 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
109 // Plugin do +2 to the total result
110 expect(res.body.total).to.equal(12)
113 it('Should run filter:api.video-channels.videos.list.params', async function () {
114 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
116 // 1 plugin do +3 to the count parameter
117 expect(res.body.data).to.have.lengthOf(5)
120 it('Should run filter:api.video-channels.videos.list.result', async function () {
121 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
123 // Plugin do +3 to the total result
124 expect(res.body.total).to.equal(13)
127 it('Should run filter:api.user.me.videos.list.params', async function () {
128 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
130 // 1 plugin do +4 to the count parameter
131 expect(res.body.data).to.have.lengthOf(6)
134 it('Should run filter:api.user.me.videos.list.result', async function () {
135 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
137 // Plugin do +4 to the total result
138 expect(res.body.total).to.equal(14)
141 it('Should run filter:api.video.get.result', async function () {
142 const res = await getVideo(servers[0].url, videoUUID)
144 expect(res.body.name).to.contain('<3')
147 it('Should run filter:api.video.upload.accept.result', async function () {
148 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, HttpStatusCode.FORBIDDEN_403)
151 it('Should run filter:api.live-video.create.accept.result', async function () {
153 name: 'video with bad word',
154 privacy: VideoPrivacy.PUBLIC,
155 channelId: servers[0].videoChannel.id
158 await servers[0].liveCommand.createLive({ fields: attributes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
161 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
162 const baseAttributes = {
163 name: 'normal title',
164 privacy: VideoPrivacy.PUBLIC,
165 channelId: servers[0].videoChannel.id,
166 targetUrl: getGoodVideoUrl() + 'bad'
168 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
171 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
172 const baseAttributes = {
174 privacy: VideoPrivacy.PUBLIC,
175 channelId: servers[0].videoChannel.id,
176 torrentfile: 'video-720p.torrent' as any
178 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
181 it('Should run filter:api.video.post-import-url.accept.result', async function () {
184 let videoImportId: number
187 const baseAttributes = {
188 name: 'title with bad word',
189 privacy: VideoPrivacy.PUBLIC,
190 channelId: servers[0].videoChannel.id,
191 targetUrl: getGoodVideoUrl()
193 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
194 videoImportId = res.body.id
197 await waitJobs(servers)
200 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
201 const videoImports = res.body.data as VideoImport[]
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.post-import-torrent.accept.result', async function () {
213 let videoImportId: number
216 const baseAttributes = {
217 name: 'title with bad word',
218 privacy: VideoPrivacy.PUBLIC,
219 channelId: servers[0].videoChannel.id,
220 torrentfile: 'video-720p.torrent' as any
222 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
223 videoImportId = res.body.id
226 await waitJobs(servers)
229 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
230 const videoImports = res.body.data as VideoImport[]
232 const videoImport = videoImports.find(i => i.id === videoImportId)
234 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
235 expect(videoImport.state.label).to.equal('Rejected')
239 it('Should run filter:api.video-thread.create.accept.result', async function () {
240 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', HttpStatusCode.FORBIDDEN_403)
243 it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
244 const res = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'thread')
245 threadId = res.body.comment.id
247 await addVideoCommentReply(
249 servers[0].accessToken,
252 'comment with bad word',
253 HttpStatusCode.FORBIDDEN_403
255 await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with good word', HttpStatusCode.OK_200)
258 it('Should run filter:api.video-threads.list.params', async function () {
259 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
261 // our plugin do +1 to the count parameter
262 expect(res.body.data).to.have.lengthOf(1)
265 it('Should run filter:api.video-threads.list.result', async function () {
266 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
268 // Plugin do +1 to the total result
269 expect(res.body.total).to.equal(2)
272 it('Should run filter:api.video-thread-comments.list.params')
274 it('Should run filter:api.video-thread-comments.list.result', async function () {
275 const res = await getVideoThreadComments(servers[0].url, videoUUID, threadId)
277 const thread = res.body as VideoCommentThreadTree
278 expect(thread.comment.text.endsWith(' <3')).to.be.true
281 describe('Should run filter:video.auto-blacklist.result', function () {
283 async function checkIsBlacklisted (oldRes: any, value: boolean) {
284 const videoId = oldRes.body.video.uuid
286 const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, videoId)
287 const video: VideoDetails = res.body
288 expect(video.blacklisted).to.equal(value)
291 it('Should blacklist on upload', async function () {
292 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video please blacklist me' })
293 await checkIsBlacklisted(res, true)
296 it('Should blacklist on import', async function () {
300 name: 'video please blacklist me',
301 targetUrl: getGoodVideoUrl(),
302 channelId: servers[0].videoChannel.id
304 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
305 await checkIsBlacklisted(res, true)
308 it('Should blacklist on update', async function () {
309 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
310 const videoId = res.body.video.uuid
311 await checkIsBlacklisted(res, false)
313 await updateVideo(servers[0].url, servers[0].accessToken, videoId, { name: 'please blacklist me' })
314 await checkIsBlacklisted(res, true)
317 it('Should blacklist on remote upload', async function () {
320 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'remote please blacklist me' })
321 await waitJobs(servers)
323 await checkIsBlacklisted(res, true)
326 it('Should blacklist on remote update', async function () {
329 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video' })
330 await waitJobs(servers)
332 const videoId = res.body.video.uuid
333 await checkIsBlacklisted(res, false)
335 await updateVideo(servers[1].url, servers[1].accessToken, videoId, { name: 'please blacklist me' })
336 await waitJobs(servers)
338 await checkIsBlacklisted(res, true)
342 describe('Should run filter:api.user.signup.allowed.result', function () {
344 it('Should run on config endpoint', async function () {
345 const body = await servers[0].configCommand.getConfig()
346 expect(body.signup.allowed).to.be.true
349 it('Should allow a signup', async function () {
350 await registerUser(servers[0].url, 'john', 'password')
353 it('Should not allow a signup', async function () {
354 const res = await registerUser(servers[0].url, 'jma', 'password', HttpStatusCode.FORBIDDEN_403)
356 expect(res.body.error).to.equal('No jma')
360 describe('Download hooks', function () {
361 const downloadVideos: VideoDetails[] = []
363 before(async function () {
366 await servers[0].configCommand.updateCustomSubConfig({
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 servers[0].configCommand.updateCustomSubConfig({
438 for (const name of [ 'bad embed', 'good embed' ]) {
440 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
441 const res = await getVideo(servers[0].url, uuid)
442 embedVideos.push(res.body)
446 const playlistAttrs = { displayName: name, videoChannelId: servers[0].videoChannel.id, privacy: VideoPlaylistPrivacy.PUBLIC }
447 const res = await createVideoPlaylist({ url: servers[0].url, token: servers[0].accessToken, playlistAttrs })
449 const resPlaylist = await getVideoPlaylist(servers[0].url, res.body.videoPlaylist.id)
450 embedPlaylists.push(resPlaylist.body)
455 it('Should run filter:html.embed.video.allowed.result', async function () {
456 const res = await makeRawRequest(servers[0].url + embedVideos[0].embedPath, 200)
457 expect(res.text).to.equal('Lu Bu')
460 it('Should run filter:html.embed.video-playlist.allowed.result', async function () {
461 const res = await makeRawRequest(servers[0].url + embedPlaylists[0].embedPath, 200)
462 expect(res.text).to.equal('Diao Chan')
466 describe('Search filters', function () {
468 before(async function () {
469 await servers[0].configCommand.updateCustomSubConfig({
474 isDefaultSearch: false,
475 disableLocalSearch: false
482 it('Should run filter:api.search.videos.local.list.{params,result}', async function () {
483 await servers[0].searchCommand.advancedVideoSearch({
489 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.params', 1)
490 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.result', 1)
493 it('Should run filter:api.search.videos.index.list.{params,result}', async function () {
494 await servers[0].searchCommand.advancedVideoSearch({
497 searchTarget: 'search-index'
501 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.params', 1)
502 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.result', 1)
503 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.index.list.params', 1)
504 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.index.list.result', 1)
507 it('Should run filter:api.search.video-channels.local.list.{params,result}', async function () {
508 await servers[0].searchCommand.advancedChannelSearch({
514 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.params', 1)
515 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.result', 1)
518 it('Should run filter:api.search.video-channels.index.list.{params,result}', async function () {
519 await servers[0].searchCommand.advancedChannelSearch({
522 searchTarget: 'search-index'
526 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.params', 1)
527 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.result', 1)
528 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.index.list.params', 1)
529 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.index.list.result', 1)
532 it('Should run filter:api.search.video-playlists.local.list.{params,result}', async function () {
533 await servers[0].searchCommand.advancedPlaylistSearch({
539 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.local.list.params', 1)
540 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.local.list.result', 1)
543 it('Should run filter:api.search.video-playlists.index.list.{params,result}', async function () {
544 await servers[0].searchCommand.advancedPlaylistSearch({
547 searchTarget: 'search-index'
551 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.local.list.params', 1)
552 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.local.list.result', 1)
553 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.index.list.params', 1)
554 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.index.list.result', 1)
558 after(async function () {
559 await cleanupTests(servers)