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