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