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,
86 replaySettings: undefined,
88 latencyMode: LiveVideoLatencyMode.DEFAULT
92 it('Should fail with nothing', async function () {
94 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
97 it('Should fail with a long name', async function () {
98 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
100 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
103 it('Should fail with a bad category', async function () {
104 const fields = { ...baseCorrectParams, category: 125 }
106 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
109 it('Should fail with a bad licence', async function () {
110 const fields = { ...baseCorrectParams, licence: 125 }
112 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
115 it('Should fail with a bad language', async function () {
116 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
118 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
121 it('Should fail with a long description', async function () {
122 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
124 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
127 it('Should fail with a long support text', async function () {
128 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
130 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
133 it('Should fail without a channel', async function () {
134 const fields = omit(baseCorrectParams, [ 'channelId' ])
136 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
139 it('Should fail with a bad channel', async function () {
140 const fields = { ...baseCorrectParams, channelId: 545454 }
142 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
145 it('Should fail with a bad privacy for replay settings', async function () {
146 const fields = { ...baseCorrectParams, replaySettings: { privacy: 5 } }
148 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
151 it('Should fail with another user channel', async function () {
154 password: 'fake_password'
156 await server.users.create({ username: user.username, password: user.password })
158 const accessTokenUser = await server.login.getAccessToken(user)
159 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
160 const customChannelId = videoChannels[0].id
162 const fields = { ...baseCorrectParams, channelId: customChannelId }
164 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
167 it('Should fail with too many tags', async function () {
168 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
170 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
173 it('Should fail with a tag length too low', async function () {
174 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
176 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
179 it('Should fail with a tag length too big', async function () {
180 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
182 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
185 it('Should fail with an incorrect thumbnail file', async function () {
186 const fields = baseCorrectParams
188 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
191 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
194 it('Should fail with a big thumbnail file', async function () {
195 const fields = baseCorrectParams
197 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
200 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
203 it('Should fail with an incorrect preview file', async function () {
204 const fields = baseCorrectParams
206 previewfile: buildAbsoluteFixturePath('video_short.mp4')
209 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
212 it('Should fail with a big preview file', async function () {
213 const fields = baseCorrectParams
215 previewfile: buildAbsoluteFixturePath('preview-big.png')
218 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
221 it('Should fail with bad latency setting', async function () {
222 const fields = { ...baseCorrectParams, latencyMode: 42 }
224 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
227 it('Should fail to set latency if the server does not allow it', async function () {
228 const fields = { ...baseCorrectParams, latencyMode: LiveVideoLatencyMode.HIGH_LATENCY }
230 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
233 it('Should succeed with the correct parameters', async function () {
236 const res = await makePostBodyRequest({
239 token: server.accessToken,
240 fields: baseCorrectParams,
241 expectedStatus: HttpStatusCode.OK_200
244 video = res.body.video
247 it('Should forbid if live is disabled', async function () {
248 await server.config.updateCustomSubConfig({
256 await makePostBodyRequest({
259 token: server.accessToken,
260 fields: baseCorrectParams,
261 expectedStatus: HttpStatusCode.FORBIDDEN_403
265 it('Should forbid to save replay if not enabled by the admin', async function () {
266 const fields = { ...baseCorrectParams, saveReplay: true, replaySettings: { privacy: VideoPrivacy.PUBLIC } }
268 await server.config.updateCustomSubConfig({
277 await makePostBodyRequest({
280 token: server.accessToken,
282 expectedStatus: HttpStatusCode.FORBIDDEN_403
286 it('Should allow to save replay if enabled by the admin', async function () {
287 const fields = { ...baseCorrectParams, saveReplay: true, replaySettings: { privacy: VideoPrivacy.PUBLIC } }
289 await server.config.updateCustomSubConfig({
298 await makePostBodyRequest({
301 token: server.accessToken,
303 expectedStatus: HttpStatusCode.OK_200
307 it('Should not allow live if max instance lives is reached', async function () {
308 await server.config.updateCustomSubConfig({
317 await makePostBodyRequest({
320 token: server.accessToken,
321 fields: baseCorrectParams,
322 expectedStatus: HttpStatusCode.FORBIDDEN_403
326 it('Should not allow live if max user lives is reached', async function () {
327 await server.config.updateCustomSubConfig({
331 maxInstanceLives: 20,
337 await makePostBodyRequest({
340 token: server.accessToken,
341 fields: baseCorrectParams,
342 expectedStatus: HttpStatusCode.FORBIDDEN_403
347 describe('When getting live information', function () {
349 it('Should fail with a bad access token', async function () {
350 await command.get({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
353 it('Should not display private information without access token', async function () {
354 const live = await command.get({ token: '', videoId: video.id })
356 expect(live.rtmpUrl).to.not.exist
357 expect(live.streamKey).to.not.exist
358 expect(live.latencyMode).to.exist
361 it('Should not display private information with token of another user', async function () {
362 const live = await command.get({ token: userAccessToken, videoId: video.id })
364 expect(live.rtmpUrl).to.not.exist
365 expect(live.streamKey).to.not.exist
366 expect(live.latencyMode).to.exist
369 it('Should display private information with appropriate token', async function () {
370 const live = await command.get({ videoId: video.id })
372 expect(live.rtmpUrl).to.exist
373 expect(live.streamKey).to.exist
374 expect(live.latencyMode).to.exist
377 it('Should fail with a bad video id', async function () {
378 await command.get({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
381 it('Should fail with an unknown video id', async function () {
382 await command.get({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
385 it('Should fail with a non live video', async function () {
386 await command.get({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
389 it('Should succeed with the correct params', async function () {
390 await command.get({ videoId: video.id })
391 await command.get({ videoId: video.uuid })
392 await command.get({ videoId: video.shortUUID })
396 describe('When getting live sessions', function () {
398 it('Should fail with a bad access token', async function () {
399 await command.listSessions({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
402 it('Should fail without token', async function () {
403 await command.listSessions({ token: null, videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
406 it('Should fail with the token of another user', async function () {
407 await command.listSessions({ token: userAccessToken, videoId: video.id, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
410 it('Should fail with a bad video id', async function () {
411 await command.listSessions({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
414 it('Should fail with an unknown video id', async function () {
415 await command.listSessions({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
418 it('Should fail with a non live video', async function () {
419 await command.listSessions({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
422 it('Should succeed with the correct params', async function () {
423 await command.listSessions({ videoId: video.id })
427 describe('When getting live session of a replay', function () {
429 it('Should fail with a bad video id', async function () {
430 await command.getReplaySession({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
433 it('Should fail with an unknown video id', async function () {
434 await command.getReplaySession({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
437 it('Should fail with a non replay video', async function () {
438 await command.getReplaySession({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
442 describe('When updating live information', async function () {
444 it('Should fail without access token', async function () {
445 await command.update({ token: '', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
448 it('Should fail with a bad access token', async function () {
449 await command.update({ token: 'toto', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
452 it('Should fail with access token of another user', async function () {
453 await command.update({ token: userAccessToken, videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
456 it('Should fail with a bad video id', async function () {
457 await command.update({ videoId: 'toto', fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
460 it('Should fail with an unknown video id', async function () {
461 await command.update({ videoId: 454555, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
464 it('Should fail with a non live video', async function () {
465 await command.update({ videoId: videoIdNotLive, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
468 it('Should fail with bad latency setting', async function () {
469 const fields = { latencyMode: 42 }
471 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
474 it('Should fail with a bad privacy for replay settings', async function () {
475 const fields = { saveReplay: true, replaySettings: { privacy: 5 } }
477 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
480 it('Should fail with save replay enabled but without replay settings', async function () {
481 await server.config.updateCustomSubConfig({
490 const fields = { saveReplay: true }
492 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
495 it('Should fail with save replay disabled and replay settings', async function () {
496 const fields = { saveReplay: false, replaySettings: { privacy: VideoPrivacy.INTERNAL } }
498 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
501 it('Should fail with only replay settings when save replay is disabled', async function () {
502 const fields = { replaySettings: { privacy: VideoPrivacy.INTERNAL } }
504 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
507 it('Should fail to set latency if the server does not allow it', async function () {
508 const fields = { latencyMode: LiveVideoLatencyMode.HIGH_LATENCY }
510 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
513 it('Should succeed with the correct params', async function () {
514 await command.update({ videoId: video.id, fields: { saveReplay: false } })
515 await command.update({ videoId: video.uuid, fields: { saveReplay: false } })
516 await command.update({ videoId: video.shortUUID, fields: { saveReplay: false } })
518 await command.update({ videoId: video.id, fields: { saveReplay: true, replaySettings: { privacy: VideoPrivacy.PUBLIC } } })
522 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
523 await server.config.updateCustomSubConfig({
532 await command.update({ videoId: video.id, fields: { saveReplay: true }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
535 it('Should fail to update a live if it has already started', async function () {
538 const live = await command.get({ videoId: video.id })
540 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
542 await command.waitUntilPublished({ videoId: video.id })
543 await command.update({ videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
545 await stopFfmpeg(ffmpegCommand)
548 it('Should fail to change live privacy if it has already started', async function () {
551 const live = await command.get({ videoId: video.id })
553 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
555 await command.waitUntilPublished({ videoId: video.id })
557 await server.videos.update({
559 attributes: { privacy: VideoPrivacy.PUBLIC } // Same privacy, it's fine
562 await server.videos.update({
564 attributes: { privacy: VideoPrivacy.UNLISTED },
565 expectedStatus: HttpStatusCode.BAD_REQUEST_400
568 await stopFfmpeg(ffmpegCommand)
571 it('Should fail to stream twice in the save live', async function () {
574 const live = await command.get({ videoId: video.id })
576 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
578 await command.waitUntilPublished({ videoId: video.id })
580 await command.runAndTestStreamError({ videoId: video.id, shouldHaveError: true })
582 await stopFfmpeg(ffmpegCommand)
586 after(async function () {
587 await cleanupTests([ server ])