]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/live.ts
Fix getting live by anonymous user
[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 { expect } from 'chai'
5 import { omit } from 'lodash'
6 import { buildAbsoluteFixturePath } from '@shared/core-utils'
7 import { HttpStatusCode, LiveVideoLatencyMode, VideoCreateResult, VideoPrivacy } from '@shared/models'
8 import {
9 cleanupTests,
10 createSingleServer,
11 LiveCommand,
12 makePostBodyRequest,
13 makeUploadRequest,
14 PeerTubeServer,
15 sendRTMPStream,
16 setAccessTokensToServers,
17 stopFfmpeg
18 } from '@shared/server-commands'
19
20 describe('Test video lives API validator', function () {
21 const path = '/api/v1/videos/live'
22 let server: PeerTubeServer
23 let userAccessToken = ''
24 let channelId: number
25 let video: VideoCreateResult
26 let videoIdNotLive: number
27 let command: LiveCommand
28
29 // ---------------------------------------------------------------
30
31 before(async function () {
32 this.timeout(30000)
33
34 server = await createSingleServer(1)
35
36 await setAccessTokensToServers([ server ])
37
38 await server.config.updateCustomSubConfig({
39 newConfig: {
40 live: {
41 enabled: true,
42 latencySetting: {
43 enabled: false
44 },
45 maxInstanceLives: 20,
46 maxUserLives: 20,
47 allowReplay: true
48 }
49 }
50 })
51
52 const username = 'user1'
53 const password = 'my super password'
54 await server.users.create({ username: username, password: password })
55 userAccessToken = await server.login.getAccessToken({ username, password })
56
57 {
58 const { videoChannels } = await server.users.getMyInfo()
59 channelId = videoChannels[0].id
60 }
61
62 {
63 videoIdNotLive = (await server.videos.quickUpload({ name: 'not live' })).id
64 }
65
66 command = server.live
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,
87 saveReplay: false,
88 permanentLive: false,
89 latencyMode: LiveVideoLatencyMode.DEFAULT
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 () {
99 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
100
101 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
102 })
103
104 it('Should fail with a bad category', async function () {
105 const fields = { ...baseCorrectParams, category: 125 }
106
107 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
108 })
109
110 it('Should fail with a bad licence', async function () {
111 const fields = { ...baseCorrectParams, licence: 125 }
112
113 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
114 })
115
116 it('Should fail with a bad language', async function () {
117 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
118
119 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
120 })
121
122 it('Should fail with a long description', async function () {
123 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
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 () {
129 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
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 () {
141 const fields = { ...baseCorrectParams, channelId: 545454 }
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 }
151 await server.users.create({ username: user.username, password: user.password })
152
153 const accessTokenUser = await server.login.getAccessToken(user)
154 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
155 const customChannelId = videoChannels[0].id
156
157 const fields = { ...baseCorrectParams, channelId: customChannelId }
158
159 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
160 })
161
162 it('Should fail with too many tags', async function () {
163 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
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 () {
169 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
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 () {
175 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
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 = {
183 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
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 = {
192 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
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 = {
201 previewfile: buildAbsoluteFixturePath('video_short.mp4')
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 = {
210 previewfile: buildAbsoluteFixturePath('preview-big.png')
211 }
212
213 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
214 })
215
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
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,
236 expectedStatus: HttpStatusCode.OK_200
237 })
238
239 video = res.body.video
240 })
241
242 it('Should forbid if live is disabled', async function () {
243 await server.config.updateCustomSubConfig({
244 newConfig: {
245 live: {
246 enabled: false
247 }
248 }
249 })
250
251 await makePostBodyRequest({
252 url: server.url,
253 path,
254 token: server.accessToken,
255 fields: baseCorrectParams,
256 expectedStatus: HttpStatusCode.FORBIDDEN_403
257 })
258 })
259
260 it('Should forbid to save replay if not enabled by the admin', async function () {
261 const fields = { ...baseCorrectParams, saveReplay: true }
262
263 await server.config.updateCustomSubConfig({
264 newConfig: {
265 live: {
266 enabled: true,
267 allowReplay: false
268 }
269 }
270 })
271
272 await makePostBodyRequest({
273 url: server.url,
274 path,
275 token: server.accessToken,
276 fields,
277 expectedStatus: HttpStatusCode.FORBIDDEN_403
278 })
279 })
280
281 it('Should allow to save replay if enabled by the admin', async function () {
282 const fields = { ...baseCorrectParams, saveReplay: true }
283
284 await server.config.updateCustomSubConfig({
285 newConfig: {
286 live: {
287 enabled: true,
288 allowReplay: true
289 }
290 }
291 })
292
293 await makePostBodyRequest({
294 url: server.url,
295 path,
296 token: server.accessToken,
297 fields,
298 expectedStatus: HttpStatusCode.OK_200
299 })
300 })
301
302 it('Should not allow live if max instance lives is reached', async function () {
303 await server.config.updateCustomSubConfig({
304 newConfig: {
305 live: {
306 enabled: true,
307 maxInstanceLives: 1
308 }
309 }
310 })
311
312 await makePostBodyRequest({
313 url: server.url,
314 path,
315 token: server.accessToken,
316 fields: baseCorrectParams,
317 expectedStatus: HttpStatusCode.FORBIDDEN_403
318 })
319 })
320
321 it('Should not allow live if max user lives is reached', async function () {
322 await server.config.updateCustomSubConfig({
323 newConfig: {
324 live: {
325 enabled: true,
326 maxInstanceLives: 20,
327 maxUserLives: 1
328 }
329 }
330 })
331
332 await makePostBodyRequest({
333 url: server.url,
334 path,
335 token: server.accessToken,
336 fields: baseCorrectParams,
337 expectedStatus: HttpStatusCode.FORBIDDEN_403
338 })
339 })
340 })
341
342 describe('When getting live information', function () {
343
344
345 it('Should fail with a bad access token', async function () {
346 await command.get({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
347 })
348
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
371 })
372
373 it('Should fail with a bad video id', async function () {
374 await command.get({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
375 })
376
377 it('Should fail with an unknown video id', async function () {
378 await command.get({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
379 })
380
381 it('Should fail with a non live video', async function () {
382 await command.get({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
383 })
384
385 it('Should succeed with the correct params', async function () {
386 await command.get({ videoId: video.id })
387 await command.get({ videoId: video.uuid })
388 await command.get({ videoId: video.shortUUID })
389 })
390 })
391
392 describe('When updating live information', async function () {
393
394 it('Should fail without access token', async function () {
395 await command.update({ token: '', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
396 })
397
398 it('Should fail with a bad access token', async function () {
399 await command.update({ token: 'toto', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
400 })
401
402 it('Should fail with access token of another user', async function () {
403 await command.update({ token: userAccessToken, videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
404 })
405
406 it('Should fail with a bad video id', async function () {
407 await command.update({ videoId: 'toto', fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
408 })
409
410 it('Should fail with an unknown video id', async function () {
411 await command.update({ videoId: 454555, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
412 })
413
414 it('Should fail with a non live video', async function () {
415 await command.update({ videoId: videoIdNotLive, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
416 })
417
418 it('Should fail with save replay and permanent live set to true', async function () {
419 const fields = { saveReplay: true, permanentLive: true }
420
421 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
422 })
423
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
436 it('Should succeed with the correct params', async function () {
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 } })
440 })
441
442 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
443 await server.config.updateCustomSubConfig({
444 newConfig: {
445 live: {
446 enabled: true,
447 allowReplay: false
448 }
449 }
450 })
451
452 await command.update({ videoId: video.id, fields: { saveReplay: true }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
453 })
454
455 it('Should fail to update a live if it has already started', async function () {
456 this.timeout(40000)
457
458 const live = await command.get({ videoId: video.id })
459
460 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
461
462 await command.waitUntilPublished({ videoId: video.id })
463 await command.update({ videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
464
465 await stopFfmpeg(ffmpegCommand)
466 })
467
468 it('Should fail to stream twice in the save live', async function () {
469 this.timeout(40000)
470
471 const live = await command.get({ videoId: video.id })
472
473 const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
474
475 await command.waitUntilPublished({ videoId: video.id })
476
477 await command.runAndTestStreamError({ videoId: video.id, shouldHaveError: true })
478
479 await stopFfmpeg(ffmpegCommand)
480 })
481 })
482
483 after(async function () {
484 await cleanupTests([ server ])
485 })
486 })