1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { ServerConfig } from '@shared/models'
6 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
18 getVideoChannelVideos,
19 getVideoCommentThreads,
22 getVideosListPagination,
23 getVideoThreadComments,
28 setAccessTokensToServers,
29 setDefaultVideoChannel,
30 updateCustomSubConfig,
35 } from '../../../shared/extra-utils'
36 import { cleanupTests, flushAndRunMultipleServers, ServerInfo } from '../../../shared/extra-utils/server/servers'
37 import { getGoodVideoUrl, getMyVideoImports, importVideo } from '../../../shared/extra-utils/videos/video-imports'
45 } from '../../../shared/models/videos'
46 import { VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model'
48 const expect = chai.expect
50 describe('Test plugin filter hooks', function () {
51 let servers: ServerInfo[]
55 before(async function () {
58 servers = await flushAndRunMultipleServers(2)
59 await setAccessTokensToServers(servers)
60 await setDefaultVideoChannel(servers)
61 await doubleFollow(servers[0], servers[1])
65 accessToken: servers[0].accessToken,
66 path: getPluginTestPath()
71 accessToken: servers[0].accessToken,
72 path: getPluginTestPath('-two')
75 for (let i = 0; i < 10; i++) {
76 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i })
79 const res = await getVideosList(servers[0].url)
80 videoUUID = res.body.data[0].uuid
82 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
83 live: { enabled: true },
84 signup: { enabled: true },
87 http: { enabled: true },
88 torrent: { enabled: true }
94 it('Should run filter:api.videos.list.params', async function () {
95 const res = await getVideosListPagination(servers[0].url, 0, 2)
97 // 2 plugins do +1 to the count parameter
98 expect(res.body.data).to.have.lengthOf(4)
101 it('Should run filter:api.videos.list.result', async function () {
102 const res = await getVideosListPagination(servers[0].url, 0, 0)
104 // Plugin do +1 to the total result
105 expect(res.body.total).to.equal(11)
108 it('Should run filter:api.accounts.videos.list.params', async function () {
109 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
111 // 1 plugin do +1 to the count parameter
112 expect(res.body.data).to.have.lengthOf(3)
115 it('Should run filter:api.accounts.videos.list.result', async function () {
116 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
118 // Plugin do +2 to the total result
119 expect(res.body.total).to.equal(12)
122 it('Should run filter:api.video-channels.videos.list.params', async function () {
123 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
125 // 1 plugin do +3 to the count parameter
126 expect(res.body.data).to.have.lengthOf(5)
129 it('Should run filter:api.video-channels.videos.list.result', async function () {
130 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
132 // Plugin do +3 to the total result
133 expect(res.body.total).to.equal(13)
136 it('Should run filter:api.user.me.videos.list.params', async function () {
137 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
139 // 1 plugin do +4 to the count parameter
140 expect(res.body.data).to.have.lengthOf(6)
143 it('Should run filter:api.user.me.videos.list.result', async function () {
144 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)
146 // Plugin do +4 to the total result
147 expect(res.body.total).to.equal(14)
150 it('Should run filter:api.video.get.result', async function () {
151 const res = await getVideo(servers[0].url, videoUUID)
153 expect(res.body.name).to.contain('<3')
156 it('Should run filter:api.video.upload.accept.result', async function () {
157 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, HttpStatusCode.FORBIDDEN_403)
160 it('Should run filter:api.live-video.create.accept.result', async function () {
162 name: 'video with bad word',
163 privacy: VideoPrivacy.PUBLIC,
164 channelId: servers[0].videoChannel.id
167 await createLive(servers[0].url, servers[0].accessToken, attributes, HttpStatusCode.FORBIDDEN_403)
170 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
171 const baseAttributes = {
172 name: 'normal title',
173 privacy: VideoPrivacy.PUBLIC,
174 channelId: servers[0].videoChannel.id,
175 targetUrl: getGoodVideoUrl() + 'bad'
177 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
180 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
181 const baseAttributes = {
183 privacy: VideoPrivacy.PUBLIC,
184 channelId: servers[0].videoChannel.id,
185 torrentfile: 'video-720p.torrent' as any
187 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
190 it('Should run filter:api.video.post-import-url.accept.result', async function () {
193 let videoImportId: number
196 const baseAttributes = {
197 name: 'title with bad word',
198 privacy: VideoPrivacy.PUBLIC,
199 channelId: servers[0].videoChannel.id,
200 targetUrl: getGoodVideoUrl()
202 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
203 videoImportId = res.body.id
206 await waitJobs(servers)
209 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
210 const videoImports = res.body.data as VideoImport[]
212 const videoImport = videoImports.find(i => i.id === videoImportId)
214 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
215 expect(videoImport.state.label).to.equal('Rejected')
219 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
222 let videoImportId: number
225 const baseAttributes = {
226 name: 'title with bad word',
227 privacy: VideoPrivacy.PUBLIC,
228 channelId: servers[0].videoChannel.id,
229 torrentfile: 'video-720p.torrent' as any
231 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
232 videoImportId = res.body.id
235 await waitJobs(servers)
238 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
239 const videoImports = res.body.data as VideoImport[]
241 const videoImport = videoImports.find(i => i.id === videoImportId)
243 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
244 expect(videoImport.state.label).to.equal('Rejected')
248 it('Should run filter:api.video-thread.create.accept.result', async function () {
249 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', HttpStatusCode.FORBIDDEN_403)
252 it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
253 const res = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'thread')
254 threadId = res.body.comment.id
256 await addVideoCommentReply(
258 servers[0].accessToken,
261 'comment with bad word',
262 HttpStatusCode.FORBIDDEN_403
264 await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with good word', HttpStatusCode.OK_200)
267 it('Should run filter:api.video-threads.list.params', async function () {
268 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
270 // our plugin do +1 to the count parameter
271 expect(res.body.data).to.have.lengthOf(1)
274 it('Should run filter:api.video-threads.list.result', async function () {
275 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
277 // Plugin do +1 to the total result
278 expect(res.body.total).to.equal(2)
281 it('Should run filter:api.video-thread-comments.list.params')
283 it('Should run filter:api.video-thread-comments.list.result', async function () {
284 const res = await getVideoThreadComments(servers[0].url, videoUUID, threadId)
286 const thread = res.body as VideoCommentThreadTree
287 expect(thread.comment.text.endsWith(' <3')).to.be.true
290 describe('Should run filter:video.auto-blacklist.result', function () {
292 async function checkIsBlacklisted (oldRes: any, value: boolean) {
293 const videoId = oldRes.body.video.uuid
295 const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, videoId)
296 const video: VideoDetails = res.body
297 expect(video.blacklisted).to.equal(value)
300 it('Should blacklist on upload', async function () {
301 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video please blacklist me' })
302 await checkIsBlacklisted(res, true)
305 it('Should blacklist on import', async function () {
309 name: 'video please blacklist me',
310 targetUrl: getGoodVideoUrl(),
311 channelId: servers[0].videoChannel.id
313 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
314 await checkIsBlacklisted(res, true)
317 it('Should blacklist on update', async function () {
318 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
319 const videoId = res.body.video.uuid
320 await checkIsBlacklisted(res, false)
322 await updateVideo(servers[0].url, servers[0].accessToken, videoId, { name: 'please blacklist me' })
323 await checkIsBlacklisted(res, true)
326 it('Should blacklist on remote upload', async function () {
329 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'remote please blacklist me' })
330 await waitJobs(servers)
332 await checkIsBlacklisted(res, true)
335 it('Should blacklist on remote update', async function () {
338 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video' })
339 await waitJobs(servers)
341 const videoId = res.body.video.uuid
342 await checkIsBlacklisted(res, false)
344 await updateVideo(servers[1].url, servers[1].accessToken, videoId, { name: 'please blacklist me' })
345 await waitJobs(servers)
347 await checkIsBlacklisted(res, true)
351 describe('Should run filter:api.user.signup.allowed.result', function () {
353 it('Should run on config endpoint', async function () {
354 const res = await getConfig(servers[0].url)
355 expect((res.body as ServerConfig).signup.allowed).to.be.true
358 it('Should allow a signup', async function () {
359 await registerUser(servers[0].url, 'john', 'password')
362 it('Should not allow a signup', async function () {
363 const res = await registerUser(servers[0].url, 'jma', 'password', HttpStatusCode.FORBIDDEN_403)
365 expect(res.body.error).to.equal('No jma')
369 describe('Download hooks', function () {
370 const downloadVideos: VideoDetails[] = []
372 before(async function () {
375 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
386 const uuids: string[] = []
388 for (const name of [ 'bad torrent', 'bad file', 'bad playlist file' ]) {
389 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
393 await waitJobs(servers)
395 for (const uuid of uuids) {
396 const res = await getVideo(servers[0].url, uuid)
397 downloadVideos.push(res.body)
401 it('Should run filter:api.download.torrent.allowed.result', async function () {
402 const res = await makeRawRequest(downloadVideos[0].files[0].torrentDownloadUrl, 403)
403 expect(res.body.error).to.equal('Liu Bei')
405 await makeRawRequest(downloadVideos[1].files[0].torrentDownloadUrl, 200)
406 await makeRawRequest(downloadVideos[2].files[0].torrentDownloadUrl, 200)
409 it('Should run filter:api.download.video.allowed.result', async function () {
411 const res = await makeRawRequest(downloadVideos[1].files[0].fileDownloadUrl, 403)
412 expect(res.body.error).to.equal('Cao Cao')
414 await makeRawRequest(downloadVideos[0].files[0].fileDownloadUrl, 200)
415 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
419 const res = await makeRawRequest(downloadVideos[2].streamingPlaylists[0].files[0].fileDownloadUrl, 403)
420 expect(res.body.error).to.equal('Sun Jian')
422 await makeRawRequest(downloadVideos[2].files[0].fileDownloadUrl, 200)
424 await makeRawRequest(downloadVideos[0].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
425 await makeRawRequest(downloadVideos[1].streamingPlaylists[0].files[0].fileDownloadUrl, 200)
430 describe('Embed filters', function () {
431 const embedVideos: VideoDetails[] = []
432 const embedPlaylists: VideoPlaylist[] = []
434 before(async function () {
437 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
443 for (const name of [ 'bad embed', 'good embed' ]) {
445 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: name })).uuid
446 const res = await getVideo(servers[0].url, uuid)
447 embedVideos.push(res.body)
451 const playlistAttrs = { displayName: name, videoChannelId: servers[0].videoChannel.id, privacy: VideoPlaylistPrivacy.PUBLIC }
452 const res = await createVideoPlaylist({ url: servers[0].url, token: servers[0].accessToken, playlistAttrs })
454 const resPlaylist = await getVideoPlaylist(servers[0].url, res.body.videoPlaylist.id)
455 embedPlaylists.push(resPlaylist.body)
460 it('Should run filter:html.embed.video.allowed.result', async function () {
461 const res = await makeRawRequest(servers[0].url + embedVideos[0].embedPath, 200)
462 expect(res.text).to.equal('Lu Bu')
465 it('Should run filter:html.embed.video-playlist.allowed.result', async function () {
466 const res = await makeRawRequest(servers[0].url + embedPlaylists[0].embedPath, 200)
467 expect(res.text).to.equal('Diao Chan')
471 after(async function () {
472 await cleanupTests(servers)