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