1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { omit } from 'lodash'
5 import { join } from 'path'
6 import { LiveVideo, VideoPrivacy } from '@shared/models'
16 runAndTestFfmpegStreamError,
19 setAccessTokensToServers,
21 updateCustomSubConfig,
25 waitUntilLivePublished
26 } from '../../../../shared/extra-utils'
27 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
29 describe('Test video lives API validator', function () {
30 const path = '/api/v1/videos/live'
31 let server: ServerInfo
32 let userAccessToken = ''
35 let videoIdNotLive: number
37 // ---------------------------------------------------------------
39 before(async function () {
42 server = await flushAndRunServer(1)
44 await setAccessTokensToServers([ server ])
46 await updateCustomSubConfig(server.url, server.accessToken, {
55 const username = 'user1'
56 const password = 'my super password'
57 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
58 userAccessToken = await userLogin(server, { username, password })
61 const res = await getMyUserInformation(server.url, server.accessToken)
62 channelId = res.body.videoChannels[0].id
66 videoIdNotLive = (await uploadVideoAndGetId({ server, videoName: 'not live' })).id
70 describe('When creating a live', function () {
75 name: 'my super name',
80 commentsEnabled: true,
81 downloadEnabled: true,
82 waitTranscoding: true,
83 description: 'my super description',
84 support: 'my super support text',
85 tags: [ 'tag1', 'tag2' ],
86 privacy: VideoPrivacy.PUBLIC,
93 it('Should fail with nothing', async function () {
95 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
98 it('Should fail with a long name', async function () {
99 const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) })
101 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
104 it('Should fail with a bad category', async function () {
105 const fields = immutableAssign(baseCorrectParams, { category: 125 })
107 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
110 it('Should fail with a bad licence', async function () {
111 const fields = immutableAssign(baseCorrectParams, { licence: 125 })
113 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
116 it('Should fail with a bad language', async function () {
117 const fields = immutableAssign(baseCorrectParams, { language: 'a'.repeat(15) })
119 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
122 it('Should fail with a long description', async function () {
123 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(2500) })
125 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
128 it('Should fail with a long support text', async function () {
129 const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) })
131 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
134 it('Should fail without a channel', async function () {
135 const fields = omit(baseCorrectParams, 'channelId')
137 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
140 it('Should fail with a bad channel', async function () {
141 const fields = immutableAssign(baseCorrectParams, { channelId: 545454 })
143 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
146 it('Should fail with another user channel', async function () {
149 password: 'fake_password'
151 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
153 const accessTokenUser = await userLogin(server, user)
154 const res = await getMyUserInformation(server.url, accessTokenUser)
155 const customChannelId = res.body.videoChannels[0].id
157 const fields = immutableAssign(baseCorrectParams, { channelId: customChannelId })
159 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
162 it('Should fail with too many tags', async function () {
163 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] })
165 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
168 it('Should fail with a tag length too low', async function () {
169 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] })
171 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
174 it('Should fail with a tag length too big', async function () {
175 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] })
177 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
180 it('Should fail with an incorrect thumbnail file', async function () {
181 const fields = baseCorrectParams
183 thumbnailfile: join(__dirname, '..', '..', 'fixtures', 'video_short.mp4')
186 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
189 it('Should fail with a big thumbnail file', async function () {
190 const fields = baseCorrectParams
192 thumbnailfile: join(__dirname, '..', '..', 'fixtures', 'preview-big.png')
195 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
198 it('Should fail with an incorrect preview file', async function () {
199 const fields = baseCorrectParams
201 previewfile: join(__dirname, '..', '..', 'fixtures', 'video_short.mp4')
204 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
207 it('Should fail with a big preview file', async function () {
208 const fields = baseCorrectParams
210 previewfile: join(__dirname, '..', '..', 'fixtures', 'preview-big.png')
213 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
216 it('Should fail with save replay and permanent live set to true', async function () {
217 const fields = immutableAssign(baseCorrectParams, { saveReplay: true, permanentLive: true })
219 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
222 it('Should succeed with the correct parameters', async function () {
225 const res = await makePostBodyRequest({
228 token: server.accessToken,
229 fields: baseCorrectParams,
230 statusCodeExpected: HttpStatusCode.OK_200
233 videoId = res.body.video.id
236 it('Should forbid if live is disabled', async function () {
237 await updateCustomSubConfig(server.url, server.accessToken, {
243 await makePostBodyRequest({
246 token: server.accessToken,
247 fields: baseCorrectParams,
248 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
252 it('Should forbid to save replay if not enabled by the admin', async function () {
253 const fields = immutableAssign(baseCorrectParams, { saveReplay: true })
255 await updateCustomSubConfig(server.url, server.accessToken, {
262 await makePostBodyRequest({
265 token: server.accessToken,
267 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
271 it('Should allow to save replay if enabled by the admin', async function () {
272 const fields = immutableAssign(baseCorrectParams, { saveReplay: true })
274 await updateCustomSubConfig(server.url, server.accessToken, {
281 await makePostBodyRequest({
284 token: server.accessToken,
286 statusCodeExpected: HttpStatusCode.OK_200
290 it('Should not allow live if max instance lives is reached', async function () {
291 await updateCustomSubConfig(server.url, server.accessToken, {
298 await makePostBodyRequest({
301 token: server.accessToken,
302 fields: baseCorrectParams,
303 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
307 it('Should not allow live if max user lives is reached', async function () {
308 await updateCustomSubConfig(server.url, server.accessToken, {
311 maxInstanceLives: 20,
316 await makePostBodyRequest({
319 token: server.accessToken,
320 fields: baseCorrectParams,
321 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
326 describe('When getting live information', function () {
328 it('Should fail without access token', async function () {
329 await getLive(server.url, '', videoId, HttpStatusCode.UNAUTHORIZED_401)
332 it('Should fail with a bad access token', async function () {
333 await getLive(server.url, 'toto', videoId, HttpStatusCode.UNAUTHORIZED_401)
336 it('Should fail with access token of another user', async function () {
337 await getLive(server.url, userAccessToken, videoId, HttpStatusCode.FORBIDDEN_403)
340 it('Should fail with a bad video id', async function () {
341 await getLive(server.url, server.accessToken, 'toto', HttpStatusCode.BAD_REQUEST_400)
344 it('Should fail with an unknown video id', async function () {
345 await getLive(server.url, server.accessToken, 454555, HttpStatusCode.NOT_FOUND_404)
348 it('Should fail with a non live video', async function () {
349 await getLive(server.url, server.accessToken, videoIdNotLive, HttpStatusCode.NOT_FOUND_404)
352 it('Should succeed with the correct params', async function () {
353 await getLive(server.url, server.accessToken, videoId)
357 describe('When updating live information', async function () {
359 it('Should fail without access token', async function () {
360 await updateLive(server.url, '', videoId, {}, HttpStatusCode.UNAUTHORIZED_401)
363 it('Should fail with a bad access token', async function () {
364 await updateLive(server.url, 'toto', videoId, {}, HttpStatusCode.UNAUTHORIZED_401)
367 it('Should fail with access token of another user', async function () {
368 await updateLive(server.url, userAccessToken, videoId, {}, HttpStatusCode.FORBIDDEN_403)
371 it('Should fail with a bad video id', async function () {
372 await updateLive(server.url, server.accessToken, 'toto', {}, HttpStatusCode.BAD_REQUEST_400)
375 it('Should fail with an unknown video id', async function () {
376 await updateLive(server.url, server.accessToken, 454555, {}, HttpStatusCode.NOT_FOUND_404)
379 it('Should fail with a non live video', async function () {
380 await updateLive(server.url, server.accessToken, videoIdNotLive, {}, HttpStatusCode.NOT_FOUND_404)
383 it('Should fail with save replay and permanent live set to true', async function () {
384 const fields = { saveReplay: true, permanentLive: true }
386 await updateLive(server.url, server.accessToken, videoId, fields, HttpStatusCode.BAD_REQUEST_400)
389 it('Should succeed with the correct params', async function () {
390 await updateLive(server.url, server.accessToken, videoId, { saveReplay: false })
393 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
394 await updateCustomSubConfig(server.url, server.accessToken, {
401 await updateLive(server.url, server.accessToken, videoId, { saveReplay: true }, HttpStatusCode.FORBIDDEN_403)
404 it('Should fail to update a live if it has already started', async function () {
407 const resLive = await getLive(server.url, server.accessToken, videoId)
408 const live: LiveVideo = resLive.body
410 const command = sendRTMPStream(live.rtmpUrl, live.streamKey)
412 await waitUntilLivePublished(server.url, server.accessToken, videoId)
413 await updateLive(server.url, server.accessToken, videoId, {}, HttpStatusCode.BAD_REQUEST_400)
415 await stopFfmpeg(command)
418 it('Should fail to stream twice in the save live', async function () {
421 const resLive = await getLive(server.url, server.accessToken, videoId)
422 const live: LiveVideo = resLive.body
424 const command = sendRTMPStream(live.rtmpUrl, live.streamKey)
426 await waitUntilLivePublished(server.url, server.accessToken, videoId)
428 await runAndTestFfmpegStreamError(server.url, server.accessToken, videoId, true)
430 await stopFfmpeg(command)
434 after(async function () {
435 await cleanupTests([ server ])