1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { omit } from 'lodash'
6 buildAbsoluteFixturePath,
14 setAccessTokensToServers,
16 } from '@shared/extra-utils'
17 import { HttpStatusCode, VideoCreateResult, VideoPrivacy } from '@shared/models'
19 describe('Test video lives API validator', function () {
20 const path = '/api/v1/videos/live'
21 let server: PeerTubeServer
22 let userAccessToken = ''
24 let video: VideoCreateResult
25 let videoIdNotLive: number
26 let command: LiveCommand
28 // ---------------------------------------------------------------
30 before(async function () {
33 server = await createSingleServer(1)
35 await setAccessTokensToServers([ server ])
37 await server.config.updateCustomSubConfig({
48 const username = 'user1'
49 const password = 'my super password'
50 await server.users.create({ username: username, password: password })
51 userAccessToken = await server.login.getAccessToken({ username, password })
54 const { videoChannels } = await server.users.getMyInfo()
55 channelId = videoChannels[0].id
59 videoIdNotLive = (await server.videos.quickUpload({ name: 'not live' })).id
65 describe('When creating a live', function () {
70 name: 'my super name',
75 commentsEnabled: true,
76 downloadEnabled: true,
77 waitTranscoding: true,
78 description: 'my super description',
79 support: 'my super support text',
80 tags: [ 'tag1', 'tag2' ],
81 privacy: VideoPrivacy.PUBLIC,
88 it('Should fail with nothing', async function () {
90 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
93 it('Should fail with a long name', async function () {
94 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
96 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
99 it('Should fail with a bad category', async function () {
100 const fields = { ...baseCorrectParams, category: 125 }
102 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
105 it('Should fail with a bad licence', async function () {
106 const fields = { ...baseCorrectParams, licence: 125 }
108 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
111 it('Should fail with a bad language', async function () {
112 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
114 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
117 it('Should fail with a long description', async function () {
118 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
120 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
123 it('Should fail with a long support text', async function () {
124 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
126 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
129 it('Should fail without a channel', async function () {
130 const fields = omit(baseCorrectParams, 'channelId')
132 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
135 it('Should fail with a bad channel', async function () {
136 const fields = { ...baseCorrectParams, channelId: 545454 }
138 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
141 it('Should fail with another user channel', async function () {
144 password: 'fake_password'
146 await server.users.create({ username: user.username, password: user.password })
148 const accessTokenUser = await server.login.getAccessToken(user)
149 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
150 const customChannelId = videoChannels[0].id
152 const fields = { ...baseCorrectParams, channelId: customChannelId }
154 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
157 it('Should fail with too many tags', async function () {
158 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
160 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
163 it('Should fail with a tag length too low', async function () {
164 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
166 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
169 it('Should fail with a tag length too big', async function () {
170 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
172 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
175 it('Should fail with an incorrect thumbnail file', async function () {
176 const fields = baseCorrectParams
178 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
181 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
184 it('Should fail with a big thumbnail file', async function () {
185 const fields = baseCorrectParams
187 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
190 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
193 it('Should fail with an incorrect preview file', async function () {
194 const fields = baseCorrectParams
196 previewfile: buildAbsoluteFixturePath('video_short.mp4')
199 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
202 it('Should fail with a big preview file', async function () {
203 const fields = baseCorrectParams
205 previewfile: buildAbsoluteFixturePath('preview-big.png')
208 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
211 it('Should fail with save replay and permanent live set to true', async function () {
212 const fields = { ...baseCorrectParams, saveReplay: true, permanentLive: true }
214 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
217 it('Should succeed with the correct parameters', async function () {
220 const res = await makePostBodyRequest({
223 token: server.accessToken,
224 fields: baseCorrectParams,
225 expectedStatus: HttpStatusCode.OK_200
228 video = res.body.video
231 it('Should forbid if live is disabled', async function () {
232 await server.config.updateCustomSubConfig({
240 await makePostBodyRequest({
243 token: server.accessToken,
244 fields: baseCorrectParams,
245 expectedStatus: HttpStatusCode.FORBIDDEN_403
249 it('Should forbid to save replay if not enabled by the admin', async function () {
250 const fields = { ...baseCorrectParams, saveReplay: true }
252 await server.config.updateCustomSubConfig({
261 await makePostBodyRequest({
264 token: server.accessToken,
266 expectedStatus: HttpStatusCode.FORBIDDEN_403
270 it('Should allow to save replay if enabled by the admin', async function () {
271 const fields = { ...baseCorrectParams, saveReplay: true }
273 await server.config.updateCustomSubConfig({
282 await makePostBodyRequest({
285 token: server.accessToken,
287 expectedStatus: HttpStatusCode.OK_200
291 it('Should not allow live if max instance lives is reached', async function () {
292 await server.config.updateCustomSubConfig({
301 await makePostBodyRequest({
304 token: server.accessToken,
305 fields: baseCorrectParams,
306 expectedStatus: HttpStatusCode.FORBIDDEN_403
310 it('Should not allow live if max user lives is reached', async function () {
311 await server.config.updateCustomSubConfig({
315 maxInstanceLives: 20,
321 await makePostBodyRequest({
324 token: server.accessToken,
325 fields: baseCorrectParams,
326 expectedStatus: HttpStatusCode.FORBIDDEN_403
331 describe('When getting live information', function () {
333 it('Should fail without access token', async function () {
334 await command.get({ token: '', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
337 it('Should fail with a bad access token', async function () {
338 await command.get({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
341 it('Should fail with access token of another user', async function () {
342 await command.get({ token: userAccessToken, videoId: video.id, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
345 it('Should fail with a bad video id', async function () {
346 await command.get({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
349 it('Should fail with an unknown video id', async function () {
350 await command.get({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
353 it('Should fail with a non live video', async function () {
354 await command.get({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
357 it('Should succeed with the correct params', async function () {
358 await command.get({ videoId: video.id })
359 await command.get({ videoId: video.uuid })
360 await command.get({ videoId: video.shortUUID })
364 describe('When updating live information', async function () {
366 it('Should fail without access token', async function () {
367 await command.update({ token: '', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
370 it('Should fail with a bad access token', async function () {
371 await command.update({ token: 'toto', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
374 it('Should fail with access token of another user', async function () {
375 await command.update({ token: userAccessToken, videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
378 it('Should fail with a bad video id', async function () {
379 await command.update({ videoId: 'toto', fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
382 it('Should fail with an unknown video id', async function () {
383 await command.update({ videoId: 454555, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
386 it('Should fail with a non live video', async function () {
387 await command.update({ videoId: videoIdNotLive, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
390 it('Should fail with save replay and permanent live set to true', async function () {
391 const fields = { saveReplay: true, permanentLive: true }
393 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
396 it('Should succeed with the correct params', async function () {
397 await command.update({ videoId: video.id, fields: { saveReplay: false } })
398 await command.update({ videoId: video.uuid, fields: { saveReplay: false } })
399 await command.update({ videoId: video.shortUUID, fields: { saveReplay: false } })
402 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
403 await server.config.updateCustomSubConfig({
412 await command.update({ videoId: video.id, fields: { saveReplay: true }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
415 it('Should fail to update a live if it has already started', async function () {
418 const live = await command.get({ videoId: video.id })
420 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
422 await command.waitUntilPublished({ videoId: video.id })
423 await command.update({ videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
425 await stopFfmpeg(ffmpegCommand)
428 it('Should fail to stream twice in the save live', async function () {
431 const live = await command.get({ videoId: video.id })
433 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
435 await command.waitUntilPublished({ videoId: video.id })
437 await command.runAndTestStreamError({ videoId: video.id, shouldHaveError: true })
439 await stopFfmpeg(ffmpegCommand)
443 after(async function () {
444 await cleanupTests([ server ])