]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/live.ts
Reimplement a typed omit function
[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
961cbe42 3import { expect } from 'chai'
bbd5aa7e 4import { buildAbsoluteFixturePath, omit } from '@shared/core-utils'
f443a746 5import { HttpStatusCode, LiveVideoLatencyMode, VideoCreateResult, VideoPrivacy } from '@shared/models'
77e9f859
C
6import {
7 cleanupTests,
254d3579 8 createSingleServer,
4f219914 9 LiveCommand,
77e9f859
C
10 makePostBodyRequest,
11 makeUploadRequest,
254d3579 12 PeerTubeServer,
4c7e60bc 13 sendRTMPStream,
77e9f859 14 setAccessTokensToServers,
d23dd9fb 15 stopFfmpeg
bf54587a 16} from '@shared/server-commands'
77e9f859
C
17
18describe('Test video lives API validator', function () {
19 const path = '/api/v1/videos/live'
254d3579 20 let server: PeerTubeServer
77e9f859 21 let userAccessToken = ''
77e9f859 22 let channelId: number
d4a8e7a6 23 let video: VideoCreateResult
77e9f859 24 let videoIdNotLive: number
4f219914 25 let command: LiveCommand
77e9f859
C
26
27 // ---------------------------------------------------------------
28
29 before(async function () {
30 this.timeout(30000)
31
254d3579 32 server = await createSingleServer(1)
77e9f859
C
33
34 await setAccessTokensToServers([ server ])
35
89d241a7 36 await server.config.updateCustomSubConfig({
65e6e260
C
37 newConfig: {
38 live: {
39 enabled: true,
f443a746
C
40 latencySetting: {
41 enabled: false
42 },
65e6e260
C
43 maxInstanceLives: 20,
44 maxUserLives: 20,
45 allowReplay: true
46 }
77e9f859
C
47 }
48 })
49
50 const username = 'user1'
51 const password = 'my super password'
ba2684ce 52 await server.users.create({ username, password })
89d241a7 53 userAccessToken = await server.login.getAccessToken({ username, password })
77e9f859
C
54
55 {
89d241a7 56 const { videoChannels } = await server.users.getMyInfo()
7926c5f9 57 channelId = videoChannels[0].id
77e9f859
C
58 }
59
60 {
89d241a7 61 videoIdNotLive = (await server.videos.quickUpload({ name: 'not live' })).id
77e9f859 62 }
4f219914 63
89d241a7 64 command = server.live
77e9f859
C
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,
bb4ba6d9 85 saveReplay: false,
f443a746
C
86 permanentLive: false,
87 latencyMode: LiveVideoLatencyMode.DEFAULT
77e9f859
C
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 () {
6c5065a0 97 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
77e9f859
C
98
99 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
100 })
101
102 it('Should fail with a bad category', async function () {
6c5065a0 103 const fields = { ...baseCorrectParams, category: 125 }
77e9f859
C
104
105 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
106 })
107
108 it('Should fail with a bad licence', async function () {
6c5065a0 109 const fields = { ...baseCorrectParams, licence: 125 }
77e9f859
C
110
111 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
112 })
113
114 it('Should fail with a bad language', async function () {
6c5065a0 115 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
77e9f859
C
116
117 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
118 })
119
120 it('Should fail with a long description', async function () {
6c5065a0 121 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
77e9f859
C
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 () {
6c5065a0 127 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
77e9f859
C
128
129 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
130 })
131
132 it('Should fail without a channel', async function () {
bbd5aa7e 133 const fields = omit(baseCorrectParams, [ 'channelId' ])
77e9f859
C
134
135 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
136 })
137
138 it('Should fail with a bad channel', async function () {
6c5065a0 139 const fields = { ...baseCorrectParams, channelId: 545454 }
77e9f859
C
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 }
89d241a7 149 await server.users.create({ username: user.username, password: user.password })
77e9f859 150
89d241a7
C
151 const accessTokenUser = await server.login.getAccessToken(user)
152 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
7926c5f9 153 const customChannelId = videoChannels[0].id
77e9f859 154
6c5065a0 155 const fields = { ...baseCorrectParams, channelId: customChannelId }
77e9f859
C
156
157 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
158 })
159
160 it('Should fail with too many tags', async function () {
6c5065a0 161 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
77e9f859
C
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 () {
6c5065a0 167 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
77e9f859
C
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 () {
6c5065a0 173 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
77e9f859
C
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 = {
3d470a53 181 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
77e9f859
C
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 = {
3d470a53 190 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
77e9f859
C
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 = {
3d470a53 199 previewfile: buildAbsoluteFixturePath('video_short.mp4')
77e9f859
C
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 = {
3d470a53 208 previewfile: buildAbsoluteFixturePath('preview-big.png')
77e9f859
C
209 }
210
211 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
212 })
213
f443a746
C
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
77e9f859
C
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,
c0e8b12e 234 expectedStatus: HttpStatusCode.OK_200
77e9f859
C
235 })
236
d4a8e7a6 237 video = res.body.video
77e9f859
C
238 })
239
240 it('Should forbid if live is disabled', async function () {
89d241a7 241 await server.config.updateCustomSubConfig({
65e6e260
C
242 newConfig: {
243 live: {
244 enabled: false
245 }
77e9f859
C
246 }
247 })
248
249 await makePostBodyRequest({
250 url: server.url,
251 path,
252 token: server.accessToken,
253 fields: baseCorrectParams,
c0e8b12e 254 expectedStatus: HttpStatusCode.FORBIDDEN_403
77e9f859
C
255 })
256 })
257
258 it('Should forbid to save replay if not enabled by the admin', async function () {
6c5065a0 259 const fields = { ...baseCorrectParams, saveReplay: true }
77e9f859 260
89d241a7 261 await server.config.updateCustomSubConfig({
65e6e260
C
262 newConfig: {
263 live: {
264 enabled: true,
265 allowReplay: false
266 }
77e9f859
C
267 }
268 })
269
270 await makePostBodyRequest({
271 url: server.url,
272 path,
273 token: server.accessToken,
274 fields,
c0e8b12e 275 expectedStatus: HttpStatusCode.FORBIDDEN_403
77e9f859
C
276 })
277 })
278
279 it('Should allow to save replay if enabled by the admin', async function () {
6c5065a0 280 const fields = { ...baseCorrectParams, saveReplay: true }
77e9f859 281
89d241a7 282 await server.config.updateCustomSubConfig({
65e6e260
C
283 newConfig: {
284 live: {
285 enabled: true,
286 allowReplay: true
287 }
77e9f859
C
288 }
289 })
290
291 await makePostBodyRequest({
292 url: server.url,
293 path,
294 token: server.accessToken,
295 fields,
c0e8b12e 296 expectedStatus: HttpStatusCode.OK_200
77e9f859
C
297 })
298 })
299
300 it('Should not allow live if max instance lives is reached', async function () {
89d241a7 301 await server.config.updateCustomSubConfig({
65e6e260
C
302 newConfig: {
303 live: {
304 enabled: true,
305 maxInstanceLives: 1
306 }
77e9f859
C
307 }
308 })
309
310 await makePostBodyRequest({
311 url: server.url,
312 path,
313 token: server.accessToken,
314 fields: baseCorrectParams,
c0e8b12e 315 expectedStatus: HttpStatusCode.FORBIDDEN_403
77e9f859
C
316 })
317 })
318
319 it('Should not allow live if max user lives is reached', async function () {
89d241a7 320 await server.config.updateCustomSubConfig({
65e6e260
C
321 newConfig: {
322 live: {
323 enabled: true,
324 maxInstanceLives: 20,
325 maxUserLives: 1
326 }
77e9f859
C
327 }
328 })
329
330 await makePostBodyRequest({
331 url: server.url,
332 path,
333 token: server.accessToken,
334 fields: baseCorrectParams,
c0e8b12e 335 expectedStatus: HttpStatusCode.FORBIDDEN_403
77e9f859
C
336 })
337 })
338 })
339
340 describe('When getting live information', function () {
341
77e9f859 342 it('Should fail with a bad access token', async function () {
04aed767 343 await command.get({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
77e9f859
C
344 })
345
961cbe42
C
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
77e9f859
C
368 })
369
370 it('Should fail with a bad video id', async function () {
04aed767 371 await command.get({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
77e9f859
C
372 })
373
374 it('Should fail with an unknown video id', async function () {
04aed767 375 await command.get({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
77e9f859
C
376 })
377
378 it('Should fail with a non live video', async function () {
04aed767 379 await command.get({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
77e9f859
C
380 })
381
382 it('Should succeed with the correct params', async function () {
04aed767
C
383 await command.get({ videoId: video.id })
384 await command.get({ videoId: video.uuid })
385 await command.get({ videoId: video.shortUUID })
77e9f859
C
386 })
387 })
388
26e3e98f
C
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
77e9f859
C
435 describe('When updating live information', async function () {
436
437 it('Should fail without access token', async function () {
04aed767 438 await command.update({ token: '', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
77e9f859
C
439 })
440
441 it('Should fail with a bad access token', async function () {
04aed767 442 await command.update({ token: 'toto', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
77e9f859
C
443 })
444
445 it('Should fail with access token of another user', async function () {
04aed767 446 await command.update({ token: userAccessToken, videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
77e9f859
C
447 })
448
449 it('Should fail with a bad video id', async function () {
04aed767 450 await command.update({ videoId: 'toto', fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
77e9f859
C
451 })
452
453 it('Should fail with an unknown video id', async function () {
04aed767 454 await command.update({ videoId: 454555, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
77e9f859
C
455 })
456
457 it('Should fail with a non live video', async function () {
04aed767 458 await command.update({ videoId: videoIdNotLive, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
77e9f859
C
459 })
460
f443a746
C
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
77e9f859 473 it('Should succeed with the correct params', async function () {
04aed767
C
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 } })
77e9f859
C
477 })
478
479 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
89d241a7 480 await server.config.updateCustomSubConfig({
65e6e260
C
481 newConfig: {
482 live: {
483 enabled: true,
484 allowReplay: false
485 }
77e9f859
C
486 }
487 })
488
04aed767 489 await command.update({ videoId: video.id, fields: { saveReplay: true }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
77e9f859
C
490 })
491
492 it('Should fail to update a live if it has already started', async function () {
59fd824c 493 this.timeout(40000)
77e9f859 494
04aed767 495 const live = await command.get({ videoId: video.id })
77e9f859 496
c826f34a 497 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
77e9f859 498
04aed767
C
499 await command.waitUntilPublished({ videoId: video.id })
500 await command.update({ videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
77e9f859 501
4f219914 502 await stopFfmpeg(ffmpegCommand)
77e9f859 503 })
97969c4e
C
504
505 it('Should fail to stream twice in the save live', async function () {
59fd824c 506 this.timeout(40000)
97969c4e 507
04aed767 508 const live = await command.get({ videoId: video.id })
97969c4e 509
c826f34a 510 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
97969c4e 511
04aed767 512 await command.waitUntilPublished({ videoId: video.id })
97969c4e 513
04aed767 514 await command.runAndTestStreamError({ videoId: video.id, shouldHaveError: true })
97969c4e 515
4f219914 516 await stopFfmpeg(ffmpegCommand)
97969c4e 517 })
77e9f859
C
518 })
519
520 after(async function () {
521 await cleanupTests([ server ])
522 })
523})