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,
20 getVideoChannelVideos,
21 getVideoCommentThreads,
24 getVideosListPagination,
25 getVideoThreadComments,
30 setAccessTokensToServers,
31 setDefaultVideoChannel,
32 updateCustomSubConfig,
37 } from '../../../shared/extra-utils'
38 import { cleanupTests, flushAndRunMultipleServers, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers'
39 import { getGoodVideoUrl, getMyVideoImports, importVideo } from '../../../shared/extra-utils/videos/video-imports'
47 } from '../../../shared/models/videos'
48 import { VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model'
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])
67 accessToken: servers[0].accessToken,
68 path: getPluginTestPath()
73 accessToken: servers[0].accessToken,
74 path: getPluginTestPath('-two')
77 for (let i = 0; i < 10; i++) {
78 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i })
81 const res = await getVideosList(servers[0].url)
82 videoUUID = res.body.data[0].uuid
84 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
85 live: { enabled: true },
86 signup: { enabled: true },
89 http: { enabled: true },
90 torrent: { enabled: true }
96 it('Should run filter:api.videos.list.params', async function () {
97 const res = await getVideosListPagination(servers[0].url, 0, 2)
99 // 2 plugins do +1 to the count parameter
100 expect(res.body.data).to.have.lengthOf(4)
103 it('Should run filter:api.videos.list.result', async function () {
104 const res = await getVideosListPagination(servers[0].url, 0, 0)
106 // Plugin do +1 to the total result
107 expect(res.body.total).to.equal(11)
110 it('Should run filter:api.accounts.videos.list.params', async function () {
111 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
113 // 1 plugin do +1 to the count parameter
114 expect(res.body.data).to.have.lengthOf(3)
117 it('Should run filter:api.accounts.videos.list.result', async function () {
118 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
120 // Plugin do +2 to the total result
121 expect(res.body.total).to.equal(12)
124 it('Should run filter:api.video-channels.videos.list.params', async function () {
125 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
127 // 1 plugin do +3 to the count parameter
128 expect(res.body.data).to.have.lengthOf(5)
131 it('Should run filter:api.video-channels.videos.list.result', async function () {
132 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
134 // Plugin do +3 to the total result
135 expect(res.body.total).to.equal(13)
138 it('Should run filter:api.user.me.videos.list.params', async function () {
139 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
141 // 1 plugin do +4 to the count parameter
142 expect(res.body.data).to.have.lengthOf(6)
145 it('Should run filter:api.user.me.videos.list.result', async function () {
146 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
148 // Plugin do +4 to the total result
149 expect(res.body.total).to.equal(14)
152 it('Should run filter:api.video.get.result', async function () {
153 const res = await getVideo(servers[0].url, videoUUID)
155 expect(res.body.name).to.contain('<3')
158 it('Should run filter:api.video.upload.accept.result', async function () {
159 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, HttpStatusCode.FORBIDDEN_403)
162 it('Should run filter:api.live-video.create.accept.result', async function () {
164 name: 'video with bad word',
165 privacy: VideoPrivacy.PUBLIC,
166 channelId: servers[0].videoChannel.id
169 await createLive(servers[0].url, servers[0].accessToken, attributes, HttpStatusCode.FORBIDDEN_403)
172 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
173 const baseAttributes = {
174 name: 'normal title',
175 privacy: VideoPrivacy.PUBLIC,
176 channelId: servers[0].videoChannel.id,
177 targetUrl: getGoodVideoUrl() + 'bad'
179 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
182 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
183 const baseAttributes = {
185 privacy: VideoPrivacy.PUBLIC,
186 channelId: servers[0].videoChannel.id,
187 torrentfile: 'video-720p.torrent' as any
189 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
192 it('Should run filter:api.video.post-import-url.accept.result', async function () {
195 let videoImportId: number
198 const baseAttributes = {
199 name: 'title with bad word',
200 privacy: VideoPrivacy.PUBLIC,
201 channelId: servers[0].videoChannel.id,
202 targetUrl: getGoodVideoUrl()
204 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
205 videoImportId = res.body.id
208 await waitJobs(servers)
211 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
212 const videoImports = res.body.data as VideoImport[]
214 const videoImport = videoImports.find(i => i.id === videoImportId)
216 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
217 expect(videoImport.state.label).to.equal('Rejected')
221 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
224 let videoImportId: number
227 const baseAttributes = {
228 name: 'title with bad word',
229 privacy: VideoPrivacy.PUBLIC,
230 channelId: servers[0].videoChannel.id,
231 torrentfile: 'video-720p.torrent' as any
233 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
234 videoImportId = res.body.id
237 await waitJobs(servers)
240 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
241 const videoImports = res.body.data as VideoImport[]
243 const videoImport = videoImports.find(i => i.id === videoImportId)
245 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
246 expect(videoImport.state.label).to.equal('Rejected')
250 it('Should run filter:api.video-thread.create.accept.result', async function () {
251 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', HttpStatusCode.FORBIDDEN_403)
254 it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
255 const res = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'thread')
256 threadId = res.body.comment.id
258 await addVideoCommentReply(
260 servers[0].accessToken,
263 'comment with bad word',
264 HttpStatusCode.FORBIDDEN_403
266 await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with good word', HttpStatusCode.OK_200)
269 it('Should run filter:api.video-threads.list.params', async function () {
270 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
272 // our plugin do +1 to the count parameter
273 expect(res.body.data).to.have.lengthOf(1)
276 it('Should run filter:api.video-threads.list.result', async function () {
277 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
279 // Plugin do +1 to the total result
280 expect(res.body.total).to.equal(2)
283 it('Should run filter:api.video-thread-comments.list.params')
285 it('Should run filter:api.video-thread-comments.list.result', async function () {
286 const res = await getVideoThreadComments(servers[0].url, videoUUID, threadId)
288 const thread = res.body as VideoCommentThreadTree
289 expect(thread.comment.text.endsWith(' <3')).to.be.true
292 describe('Should run filter:video.auto-blacklist.result', function () {
294 async function checkIsBlacklisted (oldRes: any, value: boolean) {
295 const videoId = oldRes.body.video.uuid
297 const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, videoId)
298 const video: VideoDetails = res.body
299 expect(video.blacklisted).to.equal(value)
302 it('Should blacklist on upload', async function () {
303 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video please blacklist me' })
304 await checkIsBlacklisted(res, true)
307 it('Should blacklist on import', async function () {
311 name: 'video please blacklist me',
312 targetUrl: getGoodVideoUrl(),
313 channelId: servers[0].videoChannel.id
315 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
316 await checkIsBlacklisted(res, true)
319 it('Should blacklist on update', async function () {
320 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
321 const videoId = res.body.video.uuid
322 await checkIsBlacklisted(res, false)
324 await updateVideo(servers[0].url, servers[0].accessToken, videoId, { name: 'please blacklist me' })
325 await checkIsBlacklisted(res, true)
328 it('Should blacklist on remote upload', async function () {
331 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'remote please blacklist me' })
332 await waitJobs(servers)
334 await checkIsBlacklisted(res, true)
337 it('Should blacklist on remote update', async function () {
340 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video' })
341 await waitJobs(servers)
343 const videoId = res.body.video.uuid
344 await checkIsBlacklisted(res, false)
346 await updateVideo(servers[1].url, servers[1].accessToken, videoId, { name: 'please blacklist me' })
347 await waitJobs(servers)
349 await checkIsBlacklisted(res, true)
353 describe('Should run filter:api.user.signup.allowed.result', function () {
355 it('Should run on config endpoint', async function () {
356 const res = await getConfig(servers[0].url)
357 expect((res.body as ServerConfig).signup.allowed).to.be.true
360 it('Should allow a signup', async function () {
361 await registerUser(servers[0].url, 'john', 'password')
364 it('Should not allow a signup', async function () {
365 const res = await registerUser(servers[0].url, 'jma', 'password', HttpStatusCode.FORBIDDEN_403)
367 expect(res.body.error).to.equal('No jma')
371 describe('Download hooks', function () {
372 const downloadVideos: VideoDetails[] = []
374 before(async function () {
377 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
388 const uuids: string[] = []
390 for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) {
391 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
395 await waitJobs(servers)
397 for (const uuid of uuids) {
398 const res = await getVideo(servers[0].url, uuid)
399 downloadVideos.push(res.body)
403 it('Should run filter:api.download.torrent.allowed.result', async function () {
404 const res = await makeRawRequest(downloadVideos[0].files[0].torrentDownloadUrl, 403)
405 expect(res.body.error).to.equal('Liu Bei')
407 await makeRawRequest(downloadVideos[1].files[0].torrentDownloadUrl, 200)
408 await makeRawRequest(downloadVideos[2].files[0].torrentDownloadUrl, 200)
411 it('Should run filter:api.download.video.allowed.result', async function () {
413 const res = await makeRawRequest(downloadVideos[1].files[0].fileDownloadUrl, 403)
414 expect(res.body.error).to.equal('Cao Cao')
416 await makeRawRequest(downloadVideos[0].files[0].fileDownloadUrl, 200)
417 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
421 const res = await makeRawRequest(downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl, 403)
422 expect(res.body.error).to.equal('Sun Jian')
424 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
426 await makeRawRequest(downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
427 await makeRawRequest(downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
432 describe('Embed filters', function () {
433 const embedVideos: VideoDetails[] = []
434 const embedPlaylists: VideoPlaylist[] = []
436 before(async function () {
439 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
445 for (const name of [ 'bad embed', 'good embed' ]) {
447 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
448 const res = await getVideo(servers[0].url, uuid)
449 embedVideos.push(res.body)
453 const playlistAttrs = { displayName: name, videoChannelId: servers[0].videoChannel.id, privacy: VideoPlaylistPrivacy.PUBLIC }
454 const res = await createVideoPlaylist({ url: servers[0].url, token: servers[0].accessToken, playlistAttrs })
456 const resPlaylist = await getVideoPlaylist(servers[0].url, res.body.videoPlaylist.id)
457 embedPlaylists.push(resPlaylist.body)
462 it('Should run filter:html.embed.video.allowed.result', async function () {
463 const res = await makeRawRequest(servers[0].url + embedVideos[0].embedPath, 200)
464 expect(res.text).to.equal('Lu Bu')
467 it('Should run filter:html.embed.video-playlist.allowed.result', async function () {
468 const res = await makeRawRequest(servers[0].url + embedPlaylists[0].embedPath, 200)
469 expect(res.text).to.equal('Diao Chan')
473 describe('Search filters', function () {
475 before(async function () {
476 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
480 isDefaultSearch: false,
481 disableLocalSearch: false
487 it('Should run filter:api.search.videos.local.list.{params,result}', async function () {
488 await advancedVideosSearch(servers[0].url, {
492 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.params', 1)
493 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.result', 1)
496 it('Should run filter:api.search.videos.index.list.{params,result}', async function () {
497 await advancedVideosSearch(servers[0].url, {
499 searchTarget: 'search-index'
502 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.params', 1)
503 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.local.list.result', 1)
504 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.index.list.params', 1)
505 await waitUntilLog(servers[0], 'Run hook filter:api.search.videos.index.list.result', 1)
508 it('Should run filter:api.search.video-channels.local.list.{params,result}', async function () {
509 await advancedVideoChannelSearch(servers[0].url, {
513 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.params', 1)
514 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.result', 1)
517 it('Should run filter:api.search.video-channels.index.list.{params,result}', async function () {
518 await advancedVideoChannelSearch(servers[0].url, {
520 searchTarget: 'search-index'
523 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.params', 1)
524 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.local.list.result', 1)
525 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.index.list.params', 1)
526 await waitUntilLog(servers[0], 'Run hook filter:api.search.video-channels.index.list.result', 1)
530 after(async function () {
531 await cleanupTests(servers)