]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/live.ts
Fix getting live by anonymous user
[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'
89d241a7
C
54 await server.users.create({ username: username, password: password })
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
C
344
345 it('Should fail with a bad access token', async function () {
04aed767 346 await command.get({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
77e9f859
C
347 })
348
961cbe42
C
349 it('Should not display private information without access token', async function () {
350 const live = await command.get({ token: '', videoId: video.id })
351
352 expect(live.rtmpUrl).to.not.exist
353 expect(live.streamKey).to.not.exist
354 expect(live.latencyMode).to.exist
355 })
356
357 it('Should not display private information with token of another user', async function () {
358 const live = await command.get({ token: userAccessToken, videoId: video.id })
359
360 expect(live.rtmpUrl).to.not.exist
361 expect(live.streamKey).to.not.exist
362 expect(live.latencyMode).to.exist
363 })
364
365 it('Should display private information with appropriate token', async function () {
366 const live = await command.get({ videoId: video.id })
367
368 expect(live.rtmpUrl).to.exist
369 expect(live.streamKey).to.exist
370 expect(live.latencyMode).to.exist
77e9f859
C
371 })
372
373 it('Should fail with a bad video id', async function () {
04aed767 374 await command.get({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
77e9f859
C
375 })
376
377 it('Should fail with an unknown video id', async function () {
04aed767 378 await command.get({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
77e9f859
C
379 })
380
381 it('Should fail with a non live video', async function () {
04aed767 382 await command.get({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
77e9f859
C
383 })
384
385 it('Should succeed with the correct params', async function () {
04aed767
C
386 await command.get({ videoId: video.id })
387 await command.get({ videoId: video.uuid })
388 await command.get({ videoId: video.shortUUID })
77e9f859
C
389 })
390 })
391
392 describe('When updating live information', async function () {
393
394 it('Should fail without access token', async function () {
04aed767 395 await command.update({ token: '', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
77e9f859
C
396 })
397
398 it('Should fail with a bad access token', async function () {
04aed767 399 await command.update({ token: 'toto', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
77e9f859
C
400 })
401
402 it('Should fail with access token of another user', async function () {
04aed767 403 await command.update({ token: userAccessToken, videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
77e9f859
C
404 })
405
406 it('Should fail with a bad video id', async function () {
04aed767 407 await command.update({ videoId: 'toto', fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
77e9f859
C
408 })
409
410 it('Should fail with an unknown video id', async function () {
04aed767 411 await command.update({ videoId: 454555, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
77e9f859
C
412 })
413
414 it('Should fail with a non live video', async function () {
04aed767 415 await command.update({ videoId: videoIdNotLive, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
77e9f859
C
416 })
417
bb4ba6d9
C
418 it('Should fail with save replay and permanent live set to true', async function () {
419 const fields = { saveReplay: true, permanentLive: true }
420
04aed767 421 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
bb4ba6d9
C
422 })
423
f443a746
C
424 it('Should fail with bad latency setting', async function () {
425 const fields = { latencyMode: 42 }
426
427 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
428 })
429
430 it('Should fail to set latency if the server does not allow it', async function () {
431 const fields = { latencyMode: LiveVideoLatencyMode.HIGH_LATENCY }
432
433 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
434 })
435
77e9f859 436 it('Should succeed with the correct params', async function () {
04aed767
C
437 await command.update({ videoId: video.id, fields: { saveReplay: false } })
438 await command.update({ videoId: video.uuid, fields: { saveReplay: false } })
439 await command.update({ videoId: video.shortUUID, fields: { saveReplay: false } })
77e9f859
C
440 })
441
442 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
89d241a7 443 await server.config.updateCustomSubConfig({
65e6e260
C
444 newConfig: {
445 live: {
446 enabled: true,
447 allowReplay: false
448 }
77e9f859
C
449 }
450 })
451
04aed767 452 await command.update({ videoId: video.id, fields: { saveReplay: true }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
77e9f859
C
453 })
454
455 it('Should fail to update a live if it has already started', async function () {
59fd824c 456 this.timeout(40000)
77e9f859 457
04aed767 458 const live = await command.get({ videoId: video.id })
77e9f859 459
c826f34a 460 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
77e9f859 461
04aed767
C
462 await command.waitUntilPublished({ videoId: video.id })
463 await command.update({ videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
77e9f859 464
4f219914 465 await stopFfmpeg(ffmpegCommand)
77e9f859 466 })
97969c4e
C
467
468 it('Should fail to stream twice in the save live', async function () {
59fd824c 469 this.timeout(40000)
97969c4e 470
04aed767 471 const live = await command.get({ videoId: video.id })
97969c4e 472
c826f34a 473 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
97969c4e 474
04aed767 475 await command.waitUntilPublished({ videoId: video.id })
97969c4e 476
04aed767 477 await command.runAndTestStreamError({ videoId: video.id, shouldHaveError: true })
97969c4e 478
4f219914 479 await stopFfmpeg(ffmpegCommand)
97969c4e 480 })
77e9f859
C
481 })
482
483 after(async function () {
484 await cleanupTests([ server ])
485 })
486})