1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { advancedVideoChannelSearch } from '@shared/extra-utils/search/video-channels'
6 import { ServerConfig } from '@shared/models'
7 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
10 addVideoCommentThread,
11 advancedVideoPlaylistSearch,
21 getVideoChannelVideos,
22 getVideoCommentThreads,
25 getVideosListPagination,
26 getVideoThreadComments,
31 setAccessTokensToServers,
32 setDefaultVideoChannel,
33 updateCustomSubConfig,
38 } from '../../../shared/extra-utils'
39 import { cleanupTests, flushAndRunMultipleServers, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers'
40 import { getGoodVideoUrl, getMyVideoImports, importVideo } from '../../../shared/extra-utils/videos/video-imports'
42 VideoCommentThreadTree,
49 } from '../../../shared/models/videos'
51 const expect = chai.expect
53 describe('Test plugin filter hooks', function () {
54 let servers: ServerInfo[]
58 before(async function () {
61 servers = await flushAndRunMultipleServers(2)
62 await setAccessTokensToServers(servers)
63 await setDefaultVideoChannel(servers)
64 await doubleFollow(servers[0], servers[1])
68 accessToken: servers[0].accessToken,
69 path: getPluginTestPath()
74 accessToken: servers[0].accessToken,
75 path: getPluginTestPath('-filter-translations')
78 for (let i = 0; i < 10; i++) {
79 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i })
82 const res = await getVideosList(servers[0].url)
83 videoUUID = res.body.data[0].uuid
85 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
86 live: { enabled: true },
87 signup: { enabled: true },
90 http: { enabled: true },
91 torrent: { enabled: true }
97 it('Should run filter:api.videos.list.params', async function () {
98 const res = await getVideosListPagination(servers[0].url, 0, 2)
100 // 2 plugins do +1 to the count parameter
101 expect(res.body.data).to.have.lengthOf(4)
104 it('Should run filter:api.videos.list.result', async function () {
105 const res = await getVideosListPagination(servers[0].url, 0, 0)
107 // Plugin do +1 to the total result
108 expect(res.body.total).to.equal(11)
111 it('Should run filter:api.accounts.videos.list.params', async function () {
112 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
114 // 1 plugin do +1 to the count parameter
115 expect(res.body.data).to.have.lengthOf(3)
118 it('Should run filter:api.accounts.videos.list.result', async function () {
119 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
121 // Plugin do +2 to the total result
122 expect(res.body.total).to.equal(12)
125 it('Should run filter:api.video-channels.videos.list.params', async function () {
126 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
128 // 1 plugin do +3 to the count parameter
129 expect(res.body.data).to.have.lengthOf(5)
132 it('Should run filter:api.video-channels.videos.list.result', async function () {
133 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
135 // Plugin do +3 to the total result
136 expect(res.body.total).to.equal(13)
139 it('Should run filter:api.user.me.videos.list.params', async function () {
140 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
142 // 1 plugin do +4 to the count parameter
143 expect(res.body.data).to.have.lengthOf(6)
146 it('Should run filter:api.user.me.videos.list.result', async function () {
147 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
149 // Plugin do +4 to the total result
150 expect(res.body.total).to.equal(14)
153 it('Should run filter:api.video.get.result', async function () {
154 const res = await getVideo(servers[0].url, videoUUID)
156 expect(res.body.name).to.contain('<3')
159 it('Should run filter:api.video.upload.accept.result', async function () {
160 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, HttpStatusCode.FORBIDDEN_403)
163 it('Should run filter:api.live-video.create.accept.result', async function () {
165 name: 'video with bad word',
166 privacy: VideoPrivacy.PUBLIC,
167 channelId: servers[0].videoChannel.id
170 await createLive(servers[0].url, servers[0].accessToken, attributes, HttpStatusCode.FORBIDDEN_403)
173 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
174 const baseAttributes = {
175 name: 'normal title',
176 privacy: VideoPrivacy.PUBLIC,
177 channelId: servers[0].videoChannel.id,
178 targetUrl: getGoodVideoUrl() + 'bad'
180 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
183 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
184 const baseAttributes = {
186 privacy: VideoPrivacy.PUBLIC,
187 channelId: servers[0].videoChannel.id,
188 torrentfile: 'video-720p.torrent' as any
190 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
193 it('Should run filter:api.video.post-import-url.accept.result', async function () {
196 let videoImportId: number
199 const baseAttributes = {
200 name: 'title with bad word',
201 privacy: VideoPrivacy.PUBLIC,
202 channelId: servers[0].videoChannel.id,
203 targetUrl: getGoodVideoUrl()
205 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
206 videoImportId = res.body.id
209 await waitJobs(servers)
212 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
213 const videoImports = res.body.data as VideoImport[]
215 const videoImport = videoImports.find(i => i.id === videoImportId)
217 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
218 expect(videoImport.state.label).to.equal('Rejected')
222 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
225 let videoImportId: number
228 const baseAttributes = {
229 name: 'title with bad word',
230 privacy: VideoPrivacy.PUBLIC,
231 channelId: servers[0].videoChannel.id,
232 torrentfile: 'video-720p.torrent' as any
234 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
235 videoImportId = res.body.id
238 await waitJobs(servers)
241 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
242 const videoImports = res.body.data as VideoImport[]
244 const videoImport = videoImports.find(i => i.id === videoImportId)
246 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
247 expect(videoImport.state.label).to.equal('Rejected')
251 it('Should run filter:api.video-thread.create.accept.result', async function () {
252 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', HttpStatusCode.FORBIDDEN_403)
255 it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
256 const res = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'thread')
257 threadId = res.body.comment.id
259 await addVideoCommentReply(
261 servers[0].accessToken,
264 'comment with bad word',
265 HttpStatusCode.FORBIDDEN_403
267 await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with good word', HttpStatusCode.OK_200)
270 it('Should run filter:api.video-threads.list.params', async function () {
271 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
273 // our plugin do +1 to the count parameter
274 expect(res.body.data).to.have.lengthOf(1)
277 it('Should run filter:api.video-threads.list.result', async function () {
278 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
280 // Plugin do +1 to the total result
281 expect(res.body.total).to.equal(2)
284 it('Should run filter:api.video-thread-comments.list.params')
286 it('Should run filter:api.video-thread-comments.list.result', async function () {
287 const res = await getVideoThreadComments(servers[0].url, videoUUID, threadId)
289 const thread = res.body as VideoCommentThreadTree
290 expect(thread.comment.text.endsWith(' <3')).to.be.true
293 describe('Should run filter:video.auto-blacklist.result', function () {
295 async function checkIsBlacklisted (oldRes: any, value: boolean) {
296 const videoId = oldRes.body.video.uuid
298 const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, videoId)
299 const video: VideoDetails = res.body
300 expect(video.blacklisted).to.equal(value)
303 it('Should blacklist on upload', async function () {
304 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video please blacklist me' })
305 await checkIsBlacklisted(res, true)
308 it('Should blacklist on import', async function () {
312 name: 'video please blacklist me',
313 targetUrl: getGoodVideoUrl(),
314 channelId: servers[0].videoChannel.id
316 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
317 await checkIsBlacklisted(res, true)
320 it('Should blacklist on update', async function () {
321 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
322 const videoId = res.body.video.uuid
323 await checkIsBlacklisted(res, false)
325 await updateVideo(servers[0].url, servers[0].accessToken, videoId, { name: 'please blacklist me' })
326 await checkIsBlacklisted(res, true)
329 it('Should blacklist on remote upload', async function () {
332 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'remote please blacklist me' })
333 await waitJobs(servers)
335 await checkIsBlacklisted(res, true)
338 it('Should blacklist on remote update', async function () {
341 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video' })
342 await waitJobs(servers)
344 const videoId = res.body.video.uuid
345 await checkIsBlacklisted(res, false)
347 await updateVideo(servers[1].url, servers[1].accessToken, videoId, { name: 'please blacklist me' })
348 await waitJobs(servers)
350 await checkIsBlacklisted(res, true)
354 describe('Should run filter:api.user.signup.allowed.result', function () {
356 it('Should run on config endpoint', async function () {
357 const res = await getConfig(servers[0].url)
358 expect((res.body as ServerConfig).signup.allowed).to.be.true
361 it('Should allow a signup', async function () {
362 await registerUser(servers[0].url, 'john', 'password')
365 it('Should not allow a signup', async function () {
366 const res = await registerUser(servers[0].url, 'jma', 'password', HttpStatusCode.FORBIDDEN_403)
368 expect(res.body.error).to.equal('No jma')
372 describe('Download hooks', function () {
373 const downloadVideos: VideoDetails[] = []
375 before(async function () {
378 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
389 const uuids: string[] = []
391 for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) {
392 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
396 await waitJobs(servers)
398 for (const uuid of uuids) {
399 const res = await getVideo(servers[0].url, uuid)
400 downloadVideos.push(res.body)
404 it('Should run filter:api.download.torrent.allowed.result', async function () {
405 const res = await makeRawRequest(downloadVideos[0].files[0].torrentDownloadUrl, 403)
406 expect(res.body.error).to.equal('Liu Bei')
408 await makeRawRequest(downloadVideos[1].files[0].torrentDownloadUrl, 200)
409 await makeRawRequest(downloadVideos[2].files[0].torrentDownloadUrl, 200)
412 it('Should run filter:api.download.video.allowed.result', async function () {
414 const res = await makeRawRequest(downloadVideos[1].files[0].fileDownloadUrl, 403)
415 expect(res.body.error).to.equal('Cao Cao')
417 await makeRawRequest(downloadVideos[0].files[0].fileDownloadUrl, 200)
418 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
422 const res = await makeRawRequest(downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl, 403)
423 expect(res.body.error).to.equal('Sun Jian')
425 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
427 await makeRawRequest(downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
428 await makeRawRequest(downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
433 describe('Embed filters', function () {
434 const embedVideos: VideoDetails[] = []
435 const embedPlaylists: VideoPlaylist[] = []
437 before(async function () {
440 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
446 for (const name of [ 'bad embed', 'good embed' ]) {
448 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
449 const res = await getVideo(servers[0].url, uuid)
450 embedVideos.push(res.body)
454 const playlistAttrs = { displayName: name, videoChannelId: servers[0].videoChannel.id, privacy: VideoPlaylistPrivacy.PUBLIC }
455 const res = await createVideoPlaylist({ url: servers[0].url, token: servers[0].accessToken, playlistAttrs })
457 const resPlaylist = await getVideoPlaylist(servers[0].url, res.body.videoPlaylist.id)
458 embedPlaylists.push(resPlaylist.body)
463 it('Should run filter:html.embed.video.allowed.result', async function () {
464 const res = await makeRawRequest(servers[0].url + embedVideos[0].embedPath, 200)
465 expect(res.text).to.equal('Lu Bu')
468 it('Should run filter:html.embed.video-playlist.allowed.result', async function () {
469 const res = await makeRawRequest(servers[0].url + embedPlaylists[0].embedPath, 200)
470 expect(res.text).to.equal('Diao Chan')
474 describe('Search filters', function () {
476 before(async function () {
477 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
481 isDefaultSearch: false,
482 disableLocalSearch: false
488 it('Should run filter:api.search.videos.local.list.{params,result}', async function () {
489 await advancedVideosSearch(servers[0].url, {
493 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.params', 1)
494 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.result', 1)
497 it('Should run filter:api.search.videos.index.list.{params,result}', async function () {
498 await advancedVideosSearch(servers[0].url, {
500 searchTarget: 'search-index'
503 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.params', 1)
504 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.result', 1)
505 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.index.list.params', 1)
506 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.index.list.result', 1)
509 it('Should run filter:api.search.video-channels.local.list.{params,result}', async function () {
510 await advancedVideoChannelSearch(servers[0].url, {
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 advancedVideoChannelSearch(servers[0].url, {
521 searchTarget: 'search-index'
524 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.params', 1)
525 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.result', 1)
526 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.index.list.params', 1)
527 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.index.list.result', 1)
530 it('Should run filter:api.search.video-playlists.local.list.{params,result}', async function () {
531 await advancedVideoPlaylistSearch(servers[0].url, {
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 advancedVideoPlaylistSearch(servers[0].url, {
542 searchTarget: 'search-index'
545 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.local.list.params', 1)
546 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.local.list.result', 1)
547 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.index.list.params', 1)
548 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-playlists.index.list.result', 1)
552 after(async function () {
553 await cleanupTests(servers)