1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { buildAbsoluteFixturePath, omit } from '@shared/core-utils'
5 import { HttpStatusCode, LiveVideoLatencyMode, VideoCreateResult, VideoPrivacy } from '@shared/models'
14 setAccessTokensToServers,
16 } from '@shared/server-commands'
18 describe('Test video lives API validator', function () {
19 const path = '/api/v1/videos/live'
20 let server: PeerTubeServer
21 let userAccessToken = ''
23 let video: VideoCreateResult
24 let videoIdNotLive: number
25 let command: LiveCommand
27 // ---------------------------------------------------------------
29 before(async function () {
32 server = await createSingleServer(1)
34 await setAccessTokensToServers([ server ])
36 await server.config.updateCustomSubConfig({
50 const username = 'user1'
51 const password = 'my super password'
52 await server.users.create({ username, password })
53 userAccessToken = await server.login.getAccessToken({ username, password })
56 const { videoChannels } = await server.users.getMyInfo()
57 channelId = videoChannels[0].id
61 videoIdNotLive = (await server.videos.quickUpload({ name: 'not live' })).id
67 describe('When creating a live', function () {
72 name: 'my super name',
77 commentsEnabled: true,
78 downloadEnabled: true,
79 waitTranscoding: true,
80 description: 'my super description',
81 support: 'my super support text',
82 tags: [ 'tag1', 'tag2' ],
83 privacy: VideoPrivacy.PUBLIC,
87 latencyMode: LiveVideoLatencyMode.DEFAULT
91 it('Should fail with nothing', async function () {
93 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
96 it('Should fail with a long name', async function () {
97 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
99 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
102 it('Should fail with a bad category', async function () {
103 const fields = { ...baseCorrectParams, category: 125 }
105 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
108 it('Should fail with a bad licence', async function () {
109 const fields = { ...baseCorrectParams, licence: 125 }
111 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
114 it('Should fail with a bad language', async function () {
115 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
117 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
120 it('Should fail with a long description', async function () {
121 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
123 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
126 it('Should fail with a long support text', async function () {
127 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
129 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
132 it('Should fail without a channel', async function () {
133 const fields = omit(baseCorrectParams, [ 'channelId' ])
135 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
138 it('Should fail with a bad channel', async function () {
139 const fields = { ...baseCorrectParams, channelId: 545454 }
141 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
144 it('Should fail with another user channel', async function () {
147 password: 'fake_password'
149 await server.users.create({ username: user.username, password: user.password })
151 const accessTokenUser = await server.login.getAccessToken(user)
152 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
153 const customChannelId = videoChannels[0].id
155 const fields = { ...baseCorrectParams, channelId: customChannelId }
157 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
160 it('Should fail with too many tags', async function () {
161 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
163 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
166 it('Should fail with a tag length too low', async function () {
167 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
169 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
172 it('Should fail with a tag length too big', async function () {
173 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
175 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
178 it('Should fail with an incorrect thumbnail file', async function () {
179 const fields = baseCorrectParams
181 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
184 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
187 it('Should fail with a big thumbnail file', async function () {
188 const fields = baseCorrectParams
190 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
193 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
196 it('Should fail with an incorrect preview file', async function () {
197 const fields = baseCorrectParams
199 previewfile: buildAbsoluteFixturePath('video_short.mp4')
202 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
205 it('Should fail with a big preview file', async function () {
206 const fields = baseCorrectParams
208 previewfile: buildAbsoluteFixturePath('preview-big.png')
211 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
214 it('Should fail with bad latency setting', async function () {
215 const fields = { ...baseCorrectParams, latencyMode: 42 }
217 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
220 it('Should fail to set latency if the server does not allow it', async function () {
221 const fields = { ...baseCorrectParams, latencyMode: LiveVideoLatencyMode.HIGH_LATENCY }
223 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
226 it('Should succeed with the correct parameters', async function () {
229 const res = await makePostBodyRequest({
232 token: server.accessToken,
233 fields: baseCorrectParams,
234 expectedStatus: HttpStatusCode.OK_200
237 video = res.body.video
240 it('Should forbid if live is disabled', async function () {
241 await server.config.updateCustomSubConfig({
249 await makePostBodyRequest({
252 token: server.accessToken,
253 fields: baseCorrectParams,
254 expectedStatus: HttpStatusCode.FORBIDDEN_403
258 it('Should forbid to save replay if not enabled by the admin', async function () {
259 const fields = { ...baseCorrectParams, saveReplay: true }
261 await server.config.updateCustomSubConfig({
270 await makePostBodyRequest({
273 token: server.accessToken,
275 expectedStatus: HttpStatusCode.FORBIDDEN_403
279 it('Should allow to save replay if enabled by the admin', async function () {
280 const fields = { ...baseCorrectParams, saveReplay: true }
282 await server.config.updateCustomSubConfig({
291 await makePostBodyRequest({
294 token: server.accessToken,
296 expectedStatus: HttpStatusCode.OK_200
300 it('Should not allow live if max instance lives is reached', async function () {
301 await server.config.updateCustomSubConfig({
310 await makePostBodyRequest({
313 token: server.accessToken,
314 fields: baseCorrectParams,
315 expectedStatus: HttpStatusCode.FORBIDDEN_403
319 it('Should not allow live if max user lives is reached', async function () {
320 await server.config.updateCustomSubConfig({
324 maxInstanceLives: 20,
330 await makePostBodyRequest({
333 token: server.accessToken,
334 fields: baseCorrectParams,
335 expectedStatus: HttpStatusCode.FORBIDDEN_403
340 describe('When getting live information', function () {
342 it('Should fail with a bad access token', async function () {
343 await command.get({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
346 it('Should not display private information without access token', async function () {
347 const live = await command.get({ token: '', videoId: video.id })
349 expect(live.rtmpUrl).to.not.exist
350 expect(live.streamKey).to.not.exist
351 expect(live.latencyMode).to.exist
354 it('Should not display private information with token of another user', async function () {
355 const live = await command.get({ token: userAccessToken, videoId: video.id })
357 expect(live.rtmpUrl).to.not.exist
358 expect(live.streamKey).to.not.exist
359 expect(live.latencyMode).to.exist
362 it('Should display private information with appropriate token', async function () {
363 const live = await command.get({ videoId: video.id })
365 expect(live.rtmpUrl).to.exist
366 expect(live.streamKey).to.exist
367 expect(live.latencyMode).to.exist
370 it('Should fail with a bad video id', async function () {
371 await command.get({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
374 it('Should fail with an unknown video id', async function () {
375 await command.get({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
378 it('Should fail with a non live video', async function () {
379 await command.get({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
382 it('Should succeed with the correct params', async function () {
383 await command.get({ videoId: video.id })
384 await command.get({ videoId: video.uuid })
385 await command.get({ videoId: video.shortUUID })
389 describe('When getting live sessions', function () {
391 it('Should fail with a bad access token', async function () {
392 await command.listSessions({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
395 it('Should fail without token', async function () {
396 await command.listSessions({ token: null, videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
399 it('Should fail with the token of another user', async function () {
400 await command.listSessions({ token: userAccessToken, videoId: video.id, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
403 it('Should fail with a bad video id', async function () {
404 await command.listSessions({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
407 it('Should fail with an unknown video id', async function () {
408 await command.listSessions({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
411 it('Should fail with a non live video', async function () {
412 await command.listSessions({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
415 it('Should succeed with the correct params', async function () {
416 await command.listSessions({ videoId: video.id })
420 describe('When getting live session of a replay', function () {
422 it('Should fail with a bad video id', async function () {
423 await command.getReplaySession({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
426 it('Should fail with an unknown video id', async function () {
427 await command.getReplaySession({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
430 it('Should fail with a non replay video', async function () {
431 await command.getReplaySession({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
435 describe('When updating live information', async function () {
437 it('Should fail without access token', async function () {
438 await command.update({ token: '', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
441 it('Should fail with a bad access token', async function () {
442 await command.update({ token: 'toto', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
445 it('Should fail with access token of another user', async function () {
446 await command.update({ token: userAccessToken, videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
449 it('Should fail with a bad video id', async function () {
450 await command.update({ videoId: 'toto', fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
453 it('Should fail with an unknown video id', async function () {
454 await command.update({ videoId: 454555, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
457 it('Should fail with a non live video', async function () {
458 await command.update({ videoId: videoIdNotLive, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
461 it('Should fail with bad latency setting', async function () {
462 const fields = { latencyMode: 42 }
464 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
467 it('Should fail to set latency if the server does not allow it', async function () {
468 const fields = { latencyMode: LiveVideoLatencyMode.HIGH_LATENCY }
470 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
473 it('Should succeed with the correct params', async function () {
474 await command.update({ videoId: video.id, fields: { saveReplay: false } })
475 await command.update({ videoId: video.uuid, fields: { saveReplay: false } })
476 await command.update({ videoId: video.shortUUID, fields: { saveReplay: false } })
479 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
480 await server.config.updateCustomSubConfig({
489 await command.update({ videoId: video.id, fields: { saveReplay: true }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
492 it('Should fail to update a live if it has already started', async function () {
495 const live = await command.get({ videoId: video.id })
497 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
499 await command.waitUntilPublished({ videoId: video.id })
500 await command.update({ videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
502 await stopFfmpeg(ffmpegCommand)
505 it('Should fail to stream twice in the save live', async function () {
508 const live = await command.get({ videoId: video.id })
510 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
512 await command.waitUntilPublished({ videoId: video.id })
514 await command.runAndTestStreamError({ videoId: video.id, shouldHaveError: true })
516 await stopFfmpeg(ffmpegCommand)
520 after(async function () {
521 await cleanupTests([ server ])