1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { omit } from 'lodash'
6 import { buildAbsoluteFixturePath } from '@shared/core-utils'
7 import { HttpStatusCode, LiveVideoLatencyMode, VideoCreateResult, VideoPrivacy } from '@shared/models'
16 setAccessTokensToServers,
18 } from '@shared/server-commands'
20 describe('Test video lives API validator', function () {
21 const path = '/api/v1/videos/live'
22 let server: PeerTubeServer
23 let userAccessToken = ''
25 let video: VideoCreateResult
26 let videoIdNotLive: number
27 let command: LiveCommand
29 // ---------------------------------------------------------------
31 before(async function () {
34 server = await createSingleServer(1)
36 await setAccessTokensToServers([ server ])
38 await server.config.updateCustomSubConfig({
52 const username = 'user1'
53 const password = 'my super password'
54 await server.users.create({ username, password })
55 userAccessToken = await server.login.getAccessToken({ username, password })
58 const { videoChannels } = await server.users.getMyInfo()
59 channelId = videoChannels[0].id
63 videoIdNotLive = (await server.videos.quickUpload({ name: 'not live' })).id
69 describe('When creating a live', function () {
74 name: 'my super name',
79 commentsEnabled: true,
80 downloadEnabled: true,
81 waitTranscoding: true,
82 description: 'my super description',
83 support: 'my super support text',
84 tags: [ 'tag1', 'tag2' ],
85 privacy: VideoPrivacy.PUBLIC,
89 latencyMode: LiveVideoLatencyMode.DEFAULT
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 = { ...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 = { ...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 = { ...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 = { ...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 = { ...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 = { ...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 = { ...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 server.users.create({ username: user.username, password: user.password })
153 const accessTokenUser = await server.login.getAccessToken(user)
154 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
155 const customChannelId = videoChannels[0].id
157 const fields = { ...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 = { ...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 = { ...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 = { ...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: buildAbsoluteFixturePath('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: buildAbsoluteFixturePath('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: buildAbsoluteFixturePath('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: buildAbsoluteFixturePath('preview-big.png')
213 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
216 it('Should fail with bad latency setting', async function () {
217 const fields = { ...baseCorrectParams, latencyMode: 42 }
219 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
222 it('Should fail to set latency if the server does not allow it', async function () {
223 const fields = { ...baseCorrectParams, latencyMode: LiveVideoLatencyMode.HIGH_LATENCY }
225 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
228 it('Should succeed with the correct parameters', async function () {
231 const res = await makePostBodyRequest({
234 token: server.accessToken,
235 fields: baseCorrectParams,
236 expectedStatus: HttpStatusCode.OK_200
239 video = res.body.video
242 it('Should forbid if live is disabled', async function () {
243 await server.config.updateCustomSubConfig({
251 await makePostBodyRequest({
254 token: server.accessToken,
255 fields: baseCorrectParams,
256 expectedStatus: HttpStatusCode.FORBIDDEN_403
260 it('Should forbid to save replay if not enabled by the admin', async function () {
261 const fields = { ...baseCorrectParams, saveReplay: true }
263 await server.config.updateCustomSubConfig({
272 await makePostBodyRequest({
275 token: server.accessToken,
277 expectedStatus: HttpStatusCode.FORBIDDEN_403
281 it('Should allow to save replay if enabled by the admin', async function () {
282 const fields = { ...baseCorrectParams, saveReplay: true }
284 await server.config.updateCustomSubConfig({
293 await makePostBodyRequest({
296 token: server.accessToken,
298 expectedStatus: HttpStatusCode.OK_200
302 it('Should not allow live if max instance lives is reached', async function () {
303 await server.config.updateCustomSubConfig({
312 await makePostBodyRequest({
315 token: server.accessToken,
316 fields: baseCorrectParams,
317 expectedStatus: HttpStatusCode.FORBIDDEN_403
321 it('Should not allow live if max user lives is reached', async function () {
322 await server.config.updateCustomSubConfig({
326 maxInstanceLives: 20,
332 await makePostBodyRequest({
335 token: server.accessToken,
336 fields: baseCorrectParams,
337 expectedStatus: HttpStatusCode.FORBIDDEN_403
342 describe('When getting live information', function () {
344 it('Should fail with a bad access token', async function () {
345 await command.get({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
348 it('Should not display private information without access token', async function () {
349 const live = await command.get({ token: '', videoId: video.id })
351 expect(live.rtmpUrl).to.not.exist
352 expect(live.streamKey).to.not.exist
353 expect(live.latencyMode).to.exist
356 it('Should not display private information with token of another user', async function () {
357 const live = await command.get({ token: userAccessToken, videoId: video.id })
359 expect(live.rtmpUrl).to.not.exist
360 expect(live.streamKey).to.not.exist
361 expect(live.latencyMode).to.exist
364 it('Should display private information with appropriate token', async function () {
365 const live = await command.get({ videoId: video.id })
367 expect(live.rtmpUrl).to.exist
368 expect(live.streamKey).to.exist
369 expect(live.latencyMode).to.exist
372 it('Should fail with a bad video id', async function () {
373 await command.get({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
376 it('Should fail with an unknown video id', async function () {
377 await command.get({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
380 it('Should fail with a non live video', async function () {
381 await command.get({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
384 it('Should succeed with the correct params', async function () {
385 await command.get({ videoId: video.id })
386 await command.get({ videoId: video.uuid })
387 await command.get({ videoId: video.shortUUID })
391 describe('When getting live sessions', function () {
393 it('Should fail with a bad access token', async function () {
394 await command.listSessions({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
397 it('Should fail without token', async function () {
398 await command.listSessions({ token: null, videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
401 it('Should fail with the token of another user', async function () {
402 await command.listSessions({ token: userAccessToken, videoId: video.id, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
405 it('Should fail with a bad video id', async function () {
406 await command.listSessions({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
409 it('Should fail with an unknown video id', async function () {
410 await command.listSessions({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
413 it('Should fail with a non live video', async function () {
414 await command.listSessions({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
417 it('Should succeed with the correct params', async function () {
418 await command.listSessions({ videoId: video.id })
422 describe('When getting live session of a replay', function () {
424 it('Should fail with a bad video id', async function () {
425 await command.getReplaySession({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
428 it('Should fail with an unknown video id', async function () {
429 await command.getReplaySession({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
432 it('Should fail with a non replay video', async function () {
433 await command.getReplaySession({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
437 describe('When updating live information', async function () {
439 it('Should fail without access token', async function () {
440 await command.update({ token: '', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
443 it('Should fail with a bad access token', async function () {
444 await command.update({ token: 'toto', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
447 it('Should fail with access token of another user', async function () {
448 await command.update({ token: userAccessToken, videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
451 it('Should fail with a bad video id', async function () {
452 await command.update({ videoId: 'toto', fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
455 it('Should fail with an unknown video id', async function () {
456 await command.update({ videoId: 454555, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
459 it('Should fail with a non live video', async function () {
460 await command.update({ videoId: videoIdNotLive, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
463 it('Should fail with bad latency setting', async function () {
464 const fields = { latencyMode: 42 }
466 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
469 it('Should fail to set latency if the server does not allow it', async function () {
470 const fields = { latencyMode: LiveVideoLatencyMode.HIGH_LATENCY }
472 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
475 it('Should succeed with the correct params', async function () {
476 await command.update({ videoId: video.id, fields: { saveReplay: false } })
477 await command.update({ videoId: video.uuid, fields: { saveReplay: false } })
478 await command.update({ videoId: video.shortUUID, fields: { saveReplay: false } })
481 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
482 await server.config.updateCustomSubConfig({
491 await command.update({ videoId: video.id, fields: { saveReplay: true }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
494 it('Should fail to update a live if it has already started', async function () {
497 const live = await command.get({ videoId: video.id })
499 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
501 await command.waitUntilPublished({ videoId: video.id })
502 await command.update({ videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
504 await stopFfmpeg(ffmpegCommand)
507 it('Should fail to stream twice in the save live', async function () {
510 const live = await command.get({ videoId: video.id })
512 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
514 await command.waitUntilPublished({ videoId: video.id })
516 await command.runAndTestStreamError({ videoId: video.id, shouldHaveError: true })
518 await stopFfmpeg(ffmpegCommand)
522 after(async function () {
523 await cleanupTests([ server ])