]>
Commit | Line | Data |
---|---|---|
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, 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 | it('Should fail with a bad access token', async function () { | |
345 | await command.get({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) | |
346 | }) | |
347 | ||
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 | |
370 | }) | |
371 | ||
372 | it('Should fail with a bad video id', async function () { | |
373 | await command.get({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
374 | }) | |
375 | ||
376 | it('Should fail with an unknown video id', async function () { | |
377 | await command.get({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) | |
378 | }) | |
379 | ||
380 | it('Should fail with a non live video', async function () { | |
381 | await command.get({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) | |
382 | }) | |
383 | ||
384 | it('Should succeed with the correct params', async function () { | |
385 | await command.get({ videoId: video.id }) | |
386 | await command.get({ videoId: video.uuid }) | |
387 | await command.get({ videoId: video.shortUUID }) | |
388 | }) | |
389 | }) | |
390 | ||
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 | ||
437 | describe('When updating live information', async function () { | |
438 | ||
439 | it('Should fail without access token', async function () { | |
440 | await command.update({ token: '', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) | |
441 | }) | |
442 | ||
443 | it('Should fail with a bad access token', async function () { | |
444 | await command.update({ token: 'toto', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) | |
445 | }) | |
446 | ||
447 | it('Should fail with access token of another user', async function () { | |
448 | await command.update({ token: userAccessToken, videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) | |
449 | }) | |
450 | ||
451 | it('Should fail with a bad video id', async function () { | |
452 | await command.update({ videoId: 'toto', fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
453 | }) | |
454 | ||
455 | it('Should fail with an unknown video id', async function () { | |
456 | await command.update({ videoId: 454555, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) | |
457 | }) | |
458 | ||
459 | it('Should fail with a non live video', async function () { | |
460 | await command.update({ videoId: videoIdNotLive, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) | |
461 | }) | |
462 | ||
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 | ||
475 | it('Should succeed with the correct params', async function () { | |
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 } }) | |
479 | }) | |
480 | ||
481 | it('Should fail to update replay status if replay is not allowed on the instance', async function () { | |
482 | await server.config.updateCustomSubConfig({ | |
483 | newConfig: { | |
484 | live: { | |
485 | enabled: true, | |
486 | allowReplay: false | |
487 | } | |
488 | } | |
489 | }) | |
490 | ||
491 | await command.update({ videoId: video.id, fields: { saveReplay: true }, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) | |
492 | }) | |
493 | ||
494 | it('Should fail to update a live if it has already started', async function () { | |
495 | this.timeout(40000) | |
496 | ||
497 | const live = await command.get({ videoId: video.id }) | |
498 | ||
499 | const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey }) | |
500 | ||
501 | await command.waitUntilPublished({ videoId: video.id }) | |
502 | await command.update({ videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
503 | ||
504 | await stopFfmpeg(ffmpegCommand) | |
505 | }) | |
506 | ||
507 | it('Should fail to stream twice in the save live', async function () { | |
508 | this.timeout(40000) | |
509 | ||
510 | const live = await command.get({ videoId: video.id }) | |
511 | ||
512 | const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey }) | |
513 | ||
514 | await command.waitUntilPublished({ videoId: video.id }) | |
515 | ||
516 | await command.runAndTestStreamError({ videoId: video.id, shouldHaveError: true }) | |
517 | ||
518 | await stopFfmpeg(ffmpegCommand) | |
519 | }) | |
520 | }) | |
521 | ||
522 | after(async function () { | |
523 | await cleanupTests([ server ]) | |
524 | }) | |
525 | }) |