]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/live.ts
Adapt CLI to new commands
[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'
4f219914 5import { VideoCreateResult, VideoPrivacy } from '@shared/models'
3d470a53 6import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
77e9f859 7import {
3d470a53 8 buildAbsoluteFixturePath,
77e9f859
C
9 cleanupTests,
10 createUser,
11 flushAndRunServer,
77e9f859 12 getMyUserInformation,
4f219914 13 LiveCommand,
77e9f859
C
14 makePostBodyRequest,
15 makeUploadRequest,
16 sendRTMPStream,
17 ServerInfo,
18 setAccessTokensToServers,
19 stopFfmpeg,
41d1d075 20 uploadVideoAndGetId
77e9f859
C
21} from '../../../../shared/extra-utils'
22
23describe('Test video lives API validator', function () {
24 const path = '/api/v1/videos/live'
25 let server: ServerInfo
26 let userAccessToken = ''
77e9f859 27 let channelId: number
d4a8e7a6 28 let video: VideoCreateResult
77e9f859 29 let videoIdNotLive: number
4f219914 30 let command: LiveCommand
77e9f859
C
31
32 // ---------------------------------------------------------------
33
34 before(async function () {
35 this.timeout(30000)
36
37 server = await flushAndRunServer(1)
38
39 await setAccessTokensToServers([ server ])
40
65e6e260
C
41 await server.configCommand.updateCustomSubConfig({
42 newConfig: {
43 live: {
44 enabled: true,
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'
54 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
41d1d075 55 userAccessToken = await server.loginCommand.getAccessToken({ username, password })
77e9f859
C
56
57 {
58 const res = await getMyUserInformation(server.url, server.accessToken)
59 channelId = res.body.videoChannels[0].id
60 }
61
62 {
63 videoIdNotLive = (await uploadVideoAndGetId({ server, videoName: 'not live' })).id
64 }
4f219914
C
65
66 command = server.liveCommand
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
C
87 saveReplay: false,
88 permanentLive: false
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 }
150 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
151
41d1d075 152 const accessTokenUser = await server.loginCommand.getAccessToken(user)
77e9f859
C
153 const res = await getMyUserInformation(server.url, accessTokenUser)
154 const customChannelId = res.body.videoChannels[0].id
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
77e9f859
C
221 it('Should succeed with the correct parameters', async function () {
222 this.timeout(30000)
223
224 const res = await makePostBodyRequest({
225 url: server.url,
226 path,
227 token: server.accessToken,
228 fields: baseCorrectParams,
2d53be02 229 statusCodeExpected: HttpStatusCode.OK_200
77e9f859
C
230 })
231
d4a8e7a6 232 video = res.body.video
77e9f859
C
233 })
234
235 it('Should forbid if live is disabled', async function () {
65e6e260
C
236 await server.configCommand.updateCustomSubConfig({
237 newConfig: {
238 live: {
239 enabled: false
240 }
77e9f859
C
241 }
242 })
243
244 await makePostBodyRequest({
245 url: server.url,
246 path,
247 token: server.accessToken,
248 fields: baseCorrectParams,
2d53be02 249 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
77e9f859
C
250 })
251 })
252
253 it('Should forbid to save replay if not enabled by the admin', async function () {
6c5065a0 254 const fields = { ...baseCorrectParams, saveReplay: true }
77e9f859 255
65e6e260
C
256 await server.configCommand.updateCustomSubConfig({
257 newConfig: {
258 live: {
259 enabled: true,
260 allowReplay: false
261 }
77e9f859
C
262 }
263 })
264
265 await makePostBodyRequest({
266 url: server.url,
267 path,
268 token: server.accessToken,
269 fields,
2d53be02 270 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
77e9f859
C
271 })
272 })
273
274 it('Should allow to save replay if enabled by the admin', async function () {
6c5065a0 275 const fields = { ...baseCorrectParams, saveReplay: true }
77e9f859 276
65e6e260
C
277 await server.configCommand.updateCustomSubConfig({
278 newConfig: {
279 live: {
280 enabled: true,
281 allowReplay: true
282 }
77e9f859
C
283 }
284 })
285
286 await makePostBodyRequest({
287 url: server.url,
288 path,
289 token: server.accessToken,
290 fields,
2d53be02 291 statusCodeExpected: HttpStatusCode.OK_200
77e9f859
C
292 })
293 })
294
295 it('Should not allow live if max instance lives is reached', async function () {
65e6e260
C
296 await server.configCommand.updateCustomSubConfig({
297 newConfig: {
298 live: {
299 enabled: true,
300 maxInstanceLives: 1
301 }
77e9f859
C
302 }
303 })
304
305 await makePostBodyRequest({
306 url: server.url,
307 path,
308 token: server.accessToken,
309 fields: baseCorrectParams,
2d53be02 310 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
77e9f859
C
311 })
312 })
313
314 it('Should not allow live if max user lives is reached', async function () {
65e6e260
C
315 await server.configCommand.updateCustomSubConfig({
316 newConfig: {
317 live: {
318 enabled: true,
319 maxInstanceLives: 20,
320 maxUserLives: 1
321 }
77e9f859
C
322 }
323 })
324
325 await makePostBodyRequest({
326 url: server.url,
327 path,
328 token: server.accessToken,
329 fields: baseCorrectParams,
2d53be02 330 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
77e9f859
C
331 })
332 })
333 })
334
335 describe('When getting live information', function () {
336
337 it('Should fail without access token', async function () {
04aed767 338 await command.get({ token: '', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
77e9f859
C
339 })
340
341 it('Should fail with a bad access token', async function () {
04aed767 342 await command.get({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
77e9f859
C
343 })
344
345 it('Should fail with access token of another user', async function () {
04aed767 346 await command.get({ token: userAccessToken, videoId: video.id, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
77e9f859
C
347 })
348
349 it('Should fail with a bad video id', async function () {
04aed767 350 await command.get({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
77e9f859
C
351 })
352
353 it('Should fail with an unknown video id', async function () {
04aed767 354 await command.get({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
77e9f859
C
355 })
356
357 it('Should fail with a non live video', async function () {
04aed767 358 await command.get({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
77e9f859
C
359 })
360
361 it('Should succeed with the correct params', async function () {
04aed767
C
362 await command.get({ videoId: video.id })
363 await command.get({ videoId: video.uuid })
364 await command.get({ videoId: video.shortUUID })
77e9f859
C
365 })
366 })
367
368 describe('When updating live information', async function () {
369
370 it('Should fail without access token', async function () {
04aed767 371 await command.update({ token: '', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
77e9f859
C
372 })
373
374 it('Should fail with a bad access token', async function () {
04aed767 375 await command.update({ token: 'toto', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
77e9f859
C
376 })
377
378 it('Should fail with access token of another user', async function () {
04aed767 379 await command.update({ token: userAccessToken, videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
77e9f859
C
380 })
381
382 it('Should fail with a bad video id', async function () {
04aed767 383 await command.update({ videoId: 'toto', fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
77e9f859
C
384 })
385
386 it('Should fail with an unknown video id', async function () {
04aed767 387 await command.update({ videoId: 454555, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
77e9f859
C
388 })
389
390 it('Should fail with a non live video', async function () {
04aed767 391 await command.update({ videoId: videoIdNotLive, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
77e9f859
C
392 })
393
bb4ba6d9
C
394 it('Should fail with save replay and permanent live set to true', async function () {
395 const fields = { saveReplay: true, permanentLive: true }
396
04aed767 397 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
bb4ba6d9
C
398 })
399
77e9f859 400 it('Should succeed with the correct params', async function () {
04aed767
C
401 await command.update({ videoId: video.id, fields: { saveReplay: false } })
402 await command.update({ videoId: video.uuid, fields: { saveReplay: false } })
403 await command.update({ videoId: video.shortUUID, fields: { saveReplay: false } })
77e9f859
C
404 })
405
406 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
65e6e260
C
407 await server.configCommand.updateCustomSubConfig({
408 newConfig: {
409 live: {
410 enabled: true,
411 allowReplay: false
412 }
77e9f859
C
413 }
414 })
415
04aed767 416 await command.update({ videoId: video.id, fields: { saveReplay: true }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
77e9f859
C
417 })
418
419 it('Should fail to update a live if it has already started', async function () {
59fd824c 420 this.timeout(40000)
77e9f859 421
04aed767 422 const live = await command.get({ videoId: video.id })
77e9f859 423
4f219914 424 const ffmpegCommand = sendRTMPStream(live.rtmpUrl, live.streamKey)
77e9f859 425
04aed767
C
426 await command.waitUntilPublished({ videoId: video.id })
427 await command.update({ videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
77e9f859 428
4f219914 429 await stopFfmpeg(ffmpegCommand)
77e9f859 430 })
97969c4e
C
431
432 it('Should fail to stream twice in the save live', async function () {
59fd824c 433 this.timeout(40000)
97969c4e 434
04aed767 435 const live = await command.get({ videoId: video.id })
97969c4e 436
4f219914 437 const ffmpegCommand = sendRTMPStream(live.rtmpUrl, live.streamKey)
97969c4e 438
04aed767 439 await command.waitUntilPublished({ videoId: video.id })
97969c4e 440
04aed767 441 await command.runAndTestStreamError({ videoId: video.id, shouldHaveError: true })
97969c4e 442
4f219914 443 await stopFfmpeg(ffmpegCommand)
97969c4e 444 })
77e9f859
C
445 })
446
447 after(async function () {
448 await cleanupTests([ server ])
449 })
450})