]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/live.ts
Add ability to save replay of permanent lives
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / live.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { omit } from 'lodash'
5 import { buildAbsoluteFixturePath } from '@shared/core-utils'
6 import { HttpStatusCode, LiveVideoLatencyMode, VideoCreateResult, VideoPrivacy } from '@shared/models'
7 import {
8 cleanupTests,
9 createSingleServer,
10 LiveCommand,
11 makePostBodyRequest,
12 makeUploadRequest,
13 PeerTubeServer,
14 sendRTMPStream,
15 setAccessTokensToServers,
16 stopFfmpeg
17 } from '@shared/server-commands'
18
19 describe('Test video lives API validator', function () {
20 const path = '/api/v1/videos/live'
21 let server: PeerTubeServer
22 let userAccessToken = ''
23 let channelId: number
24 let video: VideoCreateResult
25 let videoIdNotLive: number
26 let command: LiveCommand
27
28 // ---------------------------------------------------------------
29
30 before(async function () {
31 this.timeout(30000)
32
33 server = await createSingleServer(1)
34
35 await setAccessTokensToServers([ server ])
36
37 await server.config.updateCustomSubConfig({
38 newConfig: {
39 live: {
40 enabled: true,
41 latencySetting: {
42 enabled: false
43 },
44 maxInstanceLives: 20,
45 maxUserLives: 20,
46 allowReplay: true
47 }
48 }
49 })
50
51 const username = 'user1'
52 const password = 'my super password'
53 await server.users.create({ username: username, password: password })
54 userAccessToken = await server.login.getAccessToken({ username, password })
55
56 {
57 const { videoChannels } = await server.users.getMyInfo()
58 channelId = videoChannels[0].id
59 }
60
61 {
62 videoIdNotLive = (await server.videos.quickUpload({ name: 'not live' })).id
63 }
64
65 command = server.live
66 })
67
68 describe('When creating a live', function () {
69 let baseCorrectParams
70
71 before(function () {
72 baseCorrectParams = {
73 name: 'my super name',
74 category: 5,
75 licence: 1,
76 language: 'pt',
77 nsfw: false,
78 commentsEnabled: true,
79 downloadEnabled: true,
80 waitTranscoding: true,
81 description: 'my super description',
82 support: 'my super support text',
83 tags: [ 'tag1', 'tag2' ],
84 privacy: VideoPrivacy.PUBLIC,
85 channelId,
86 saveReplay: false,
87 permanentLive: false,
88 latencyMode: LiveVideoLatencyMode.DEFAULT
89 }
90 })
91
92 it('Should fail with nothing', async function () {
93 const fields = {}
94 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
95 })
96
97 it('Should fail with a long name', async function () {
98 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
99
100 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
101 })
102
103 it('Should fail with a bad category', async function () {
104 const fields = { ...baseCorrectParams, category: 125 }
105
106 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
107 })
108
109 it('Should fail with a bad licence', async function () {
110 const fields = { ...baseCorrectParams, licence: 125 }
111
112 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
113 })
114
115 it('Should fail with a bad language', async function () {
116 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
117
118 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
119 })
120
121 it('Should fail with a long description', async function () {
122 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
123
124 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
125 })
126
127 it('Should fail with a long support text', async function () {
128 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
129
130 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
131 })
132
133 it('Should fail without a channel', async function () {
134 const fields = omit(baseCorrectParams, 'channelId')
135
136 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
137 })
138
139 it('Should fail with a bad channel', async function () {
140 const fields = { ...baseCorrectParams, channelId: 545454 }
141
142 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
143 })
144
145 it('Should fail with another user channel', async function () {
146 const user = {
147 username: 'fake',
148 password: 'fake_password'
149 }
150 await server.users.create({ username: user.username, password: user.password })
151
152 const accessTokenUser = await server.login.getAccessToken(user)
153 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
154 const customChannelId = videoChannels[0].id
155
156 const fields = { ...baseCorrectParams, channelId: customChannelId }
157
158 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
159 })
160
161 it('Should fail with too many tags', async function () {
162 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
163
164 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
165 })
166
167 it('Should fail with a tag length too low', async function () {
168 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
169
170 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
171 })
172
173 it('Should fail with a tag length too big', async function () {
174 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
175
176 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
177 })
178
179 it('Should fail with an incorrect thumbnail file', async function () {
180 const fields = baseCorrectParams
181 const attaches = {
182 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
183 }
184
185 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
186 })
187
188 it('Should fail with a big thumbnail file', async function () {
189 const fields = baseCorrectParams
190 const attaches = {
191 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
192 }
193
194 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
195 })
196
197 it('Should fail with an incorrect preview file', async function () {
198 const fields = baseCorrectParams
199 const attaches = {
200 previewfile: buildAbsoluteFixturePath('video_short.mp4')
201 }
202
203 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
204 })
205
206 it('Should fail with a big preview file', async function () {
207 const fields = baseCorrectParams
208 const attaches = {
209 previewfile: buildAbsoluteFixturePath('preview-big.png')
210 }
211
212 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
213 })
214
215 it('Should fail with bad latency setting', async function () {
216 const fields = { ...baseCorrectParams, latencyMode: 42 }
217
218 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
219 })
220
221 it('Should fail to set latency if the server does not allow it', async function () {
222 const fields = { ...baseCorrectParams, latencyMode: LiveVideoLatencyMode.HIGH_LATENCY }
223
224 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
225 })
226
227 it('Should succeed with the correct parameters', async function () {
228 this.timeout(30000)
229
230 const res = await makePostBodyRequest({
231 url: server.url,
232 path,
233 token: server.accessToken,
234 fields: baseCorrectParams,
235 expectedStatus: HttpStatusCode.OK_200
236 })
237
238 video = res.body.video
239 })
240
241 it('Should forbid if live is disabled', async function () {
242 await server.config.updateCustomSubConfig({
243 newConfig: {
244 live: {
245 enabled: false
246 }
247 }
248 })
249
250 await makePostBodyRequest({
251 url: server.url,
252 path,
253 token: server.accessToken,
254 fields: baseCorrectParams,
255 expectedStatus: HttpStatusCode.FORBIDDEN_403
256 })
257 })
258
259 it('Should forbid to save replay if not enabled by the admin', async function () {
260 const fields = { ...baseCorrectParams, saveReplay: true }
261
262 await server.config.updateCustomSubConfig({
263 newConfig: {
264 live: {
265 enabled: true,
266 allowReplay: false
267 }
268 }
269 })
270
271 await makePostBodyRequest({
272 url: server.url,
273 path,
274 token: server.accessToken,
275 fields,
276 expectedStatus: HttpStatusCode.FORBIDDEN_403
277 })
278 })
279
280 it('Should allow to save replay if enabled by the admin', async function () {
281 const fields = { ...baseCorrectParams, saveReplay: true }
282
283 await server.config.updateCustomSubConfig({
284 newConfig: {
285 live: {
286 enabled: true,
287 allowReplay: true
288 }
289 }
290 })
291
292 await makePostBodyRequest({
293 url: server.url,
294 path,
295 token: server.accessToken,
296 fields,
297 expectedStatus: HttpStatusCode.OK_200
298 })
299 })
300
301 it('Should not allow live if max instance lives is reached', async function () {
302 await server.config.updateCustomSubConfig({
303 newConfig: {
304 live: {
305 enabled: true,
306 maxInstanceLives: 1
307 }
308 }
309 })
310
311 await makePostBodyRequest({
312 url: server.url,
313 path,
314 token: server.accessToken,
315 fields: baseCorrectParams,
316 expectedStatus: HttpStatusCode.FORBIDDEN_403
317 })
318 })
319
320 it('Should not allow live if max user lives is reached', async function () {
321 await server.config.updateCustomSubConfig({
322 newConfig: {
323 live: {
324 enabled: true,
325 maxInstanceLives: 20,
326 maxUserLives: 1
327 }
328 }
329 })
330
331 await makePostBodyRequest({
332 url: server.url,
333 path,
334 token: server.accessToken,
335 fields: baseCorrectParams,
336 expectedStatus: HttpStatusCode.FORBIDDEN_403
337 })
338 })
339 })
340
341 describe('When getting live information', function () {
342
343 it('Should fail without access token', async function () {
344 await command.get({ token: '', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
345 })
346
347 it('Should fail with a bad access token', async function () {
348 await command.get({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
349 })
350
351 it('Should fail with access token of another user', async function () {
352 await command.get({ token: userAccessToken, videoId: video.id, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
353 })
354
355 it('Should fail with a bad video id', async function () {
356 await command.get({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
357 })
358
359 it('Should fail with an unknown video id', async function () {
360 await command.get({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
361 })
362
363 it('Should fail with a non live video', async function () {
364 await command.get({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
365 })
366
367 it('Should succeed with the correct params', async function () {
368 await command.get({ videoId: video.id })
369 await command.get({ videoId: video.uuid })
370 await command.get({ videoId: video.shortUUID })
371 })
372 })
373
374 describe('When updating live information', async function () {
375
376 it('Should fail without access token', async function () {
377 await command.update({ token: '', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
378 })
379
380 it('Should fail with a bad access token', async function () {
381 await command.update({ token: 'toto', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
382 })
383
384 it('Should fail with access token of another user', async function () {
385 await command.update({ token: userAccessToken, videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
386 })
387
388 it('Should fail with a bad video id', async function () {
389 await command.update({ videoId: 'toto', fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
390 })
391
392 it('Should fail with an unknown video id', async function () {
393 await command.update({ videoId: 454555, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
394 })
395
396 it('Should fail with a non live video', async function () {
397 await command.update({ videoId: videoIdNotLive, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
398 })
399
400 it('Should fail with save replay and permanent live set to true', async function () {
401 const fields = { saveReplay: true, permanentLive: true }
402
403 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
404 })
405
406 it('Should fail with bad latency setting', async function () {
407 const fields = { latencyMode: 42 }
408
409 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
410 })
411
412 it('Should fail to set latency if the server does not allow it', async function () {
413 const fields = { latencyMode: LiveVideoLatencyMode.HIGH_LATENCY }
414
415 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
416 })
417
418 it('Should succeed with the correct params', async function () {
419 await command.update({ videoId: video.id, fields: { saveReplay: false } })
420 await command.update({ videoId: video.uuid, fields: { saveReplay: false } })
421 await command.update({ videoId: video.shortUUID, fields: { saveReplay: false } })
422 })
423
424 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
425 await server.config.updateCustomSubConfig({
426 newConfig: {
427 live: {
428 enabled: true,
429 allowReplay: false
430 }
431 }
432 })
433
434 await command.update({ videoId: video.id, fields: { saveReplay: true }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
435 })
436
437 it('Should fail to update a live if it has already started', async function () {
438 this.timeout(40000)
439
440 const live = await command.get({ videoId: video.id })
441
442 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
443
444 await command.waitUntilPublished({ videoId: video.id })
445 await command.update({ videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
446
447 await stopFfmpeg(ffmpegCommand)
448 })
449
450 it('Should fail to stream twice in the save live', async function () {
451 this.timeout(40000)
452
453 const live = await command.get({ videoId: video.id })
454
455 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
456
457 await command.waitUntilPublished({ videoId: video.id })
458
459 await command.runAndTestStreamError({ videoId: video.id, shouldHaveError: true })
460
461 await stopFfmpeg(ffmpegCommand)
462 })
463 })
464
465 after(async function () {
466 await cleanupTests([ server ])
467 })
468 })