]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/live.ts
Introduce plugins command
[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'
d4a8e7a6 5import { LiveVideo, 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,
12 getLive,
13 getMyUserInformation,
14 immutableAssign,
15 makePostBodyRequest,
16 makeUploadRequest,
68e70a74 17 runAndTestFfmpegStreamError,
77e9f859
C
18 sendRTMPStream,
19 ServerInfo,
20 setAccessTokensToServers,
21 stopFfmpeg,
22 updateCustomSubConfig,
23 updateLive,
24 uploadVideoAndGetId,
25 userLogin,
0d8de275 26 waitUntilLivePublished
77e9f859
C
27} from '../../../../shared/extra-utils'
28
29describe('Test video lives API validator', function () {
30 const path = '/api/v1/videos/live'
31 let server: ServerInfo
32 let userAccessToken = ''
77e9f859 33 let channelId: number
d4a8e7a6 34 let video: VideoCreateResult
77e9f859
C
35 let videoIdNotLive: number
36
37 // ---------------------------------------------------------------
38
39 before(async function () {
40 this.timeout(30000)
41
42 server = await flushAndRunServer(1)
43
44 await setAccessTokensToServers([ server ])
45
46 await updateCustomSubConfig(server.url, server.accessToken, {
47 live: {
48 enabled: true,
49 maxInstanceLives: 20,
50 maxUserLives: 20,
51 allowReplay: true
52 }
53 })
54
55 const username = 'user1'
56 const password = 'my super password'
57 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
58 userAccessToken = await userLogin(server, { username, password })
59
60 {
61 const res = await getMyUserInformation(server.url, server.accessToken)
62 channelId = res.body.videoChannels[0].id
63 }
64
65 {
66 videoIdNotLive = (await uploadVideoAndGetId({ server, videoName: 'not live' })).id
67 }
68 })
69
70 describe('When creating a live', function () {
71 let baseCorrectParams
72
73 before(function () {
74 baseCorrectParams = {
75 name: 'my super name',
76 category: 5,
77 licence: 1,
78 language: 'pt',
79 nsfw: false,
80 commentsEnabled: true,
81 downloadEnabled: true,
82 waitTranscoding: true,
83 description: 'my super description',
84 support: 'my super support text',
85 tags: [ 'tag1', 'tag2' ],
86 privacy: VideoPrivacy.PUBLIC,
87 channelId,
bb4ba6d9
C
88 saveReplay: false,
89 permanentLive: false
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 () {
99 const fields = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
152
153 const accessTokenUser = await userLogin(server, user)
154 const res = await getMyUserInformation(server.url, accessTokenUser)
155 const customChannelId = res.body.videoChannels[0].id
156
157 const fields = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = {
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
bb4ba6d9
C
216 it('Should fail with save replay and permanent live set to true', async function () {
217 const fields = immutableAssign(baseCorrectParams, { saveReplay: true, permanentLive: true })
218
219 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
220 })
221
77e9f859
C
222 it('Should succeed with the correct parameters', async function () {
223 this.timeout(30000)
224
225 const res = await makePostBodyRequest({
226 url: server.url,
227 path,
228 token: server.accessToken,
229 fields: baseCorrectParams,
2d53be02 230 statusCodeExpected: HttpStatusCode.OK_200
77e9f859
C
231 })
232
d4a8e7a6 233 video = res.body.video
77e9f859
C
234 })
235
236 it('Should forbid if live is disabled', async function () {
237 await updateCustomSubConfig(server.url, server.accessToken, {
238 live: {
239 enabled: false
240 }
241 })
242
243 await makePostBodyRequest({
244 url: server.url,
245 path,
246 token: server.accessToken,
247 fields: baseCorrectParams,
2d53be02 248 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
77e9f859
C
249 })
250 })
251
252 it('Should forbid to save replay if not enabled by the admin', async function () {
253 const fields = immutableAssign(baseCorrectParams, { saveReplay: true })
254
255 await updateCustomSubConfig(server.url, server.accessToken, {
256 live: {
257 enabled: true,
258 allowReplay: false
259 }
260 })
261
262 await makePostBodyRequest({
263 url: server.url,
264 path,
265 token: server.accessToken,
266 fields,
2d53be02 267 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
77e9f859
C
268 })
269 })
270
271 it('Should allow to save replay if enabled by the admin', async function () {
272 const fields = immutableAssign(baseCorrectParams, { saveReplay: true })
273
274 await updateCustomSubConfig(server.url, server.accessToken, {
275 live: {
276 enabled: true,
277 allowReplay: true
278 }
279 })
280
281 await makePostBodyRequest({
282 url: server.url,
283 path,
284 token: server.accessToken,
285 fields,
2d53be02 286 statusCodeExpected: HttpStatusCode.OK_200
77e9f859
C
287 })
288 })
289
290 it('Should not allow live if max instance lives is reached', async function () {
291 await updateCustomSubConfig(server.url, server.accessToken, {
292 live: {
293 enabled: true,
294 maxInstanceLives: 1
295 }
296 })
297
298 await makePostBodyRequest({
299 url: server.url,
300 path,
301 token: server.accessToken,
302 fields: baseCorrectParams,
2d53be02 303 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
77e9f859
C
304 })
305 })
306
307 it('Should not allow live if max user lives is reached', async function () {
308 await updateCustomSubConfig(server.url, server.accessToken, {
309 live: {
310 enabled: true,
311 maxInstanceLives: 20,
312 maxUserLives: 1
313 }
314 })
315
316 await makePostBodyRequest({
317 url: server.url,
318 path,
319 token: server.accessToken,
320 fields: baseCorrectParams,
2d53be02 321 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
77e9f859
C
322 })
323 })
324 })
325
326 describe('When getting live information', function () {
327
328 it('Should fail without access token', async function () {
d4a8e7a6 329 await getLive(server.url, '', video.id, HttpStatusCode.UNAUTHORIZED_401)
77e9f859
C
330 })
331
332 it('Should fail with a bad access token', async function () {
d4a8e7a6 333 await getLive(server.url, 'toto', video.id, HttpStatusCode.UNAUTHORIZED_401)
77e9f859
C
334 })
335
336 it('Should fail with access token of another user', async function () {
d4a8e7a6 337 await getLive(server.url, userAccessToken, video.id, HttpStatusCode.FORBIDDEN_403)
77e9f859
C
338 })
339
340 it('Should fail with a bad video id', async function () {
2d53be02 341 await getLive(server.url, server.accessToken, 'toto', HttpStatusCode.BAD_REQUEST_400)
77e9f859
C
342 })
343
344 it('Should fail with an unknown video id', async function () {
2d53be02 345 await getLive(server.url, server.accessToken, 454555, HttpStatusCode.NOT_FOUND_404)
77e9f859
C
346 })
347
348 it('Should fail with a non live video', async function () {
2d53be02 349 await getLive(server.url, server.accessToken, videoIdNotLive, HttpStatusCode.NOT_FOUND_404)
77e9f859
C
350 })
351
352 it('Should succeed with the correct params', async function () {
d4a8e7a6
C
353 await getLive(server.url, server.accessToken, video.id)
354 await getLive(server.url, server.accessToken, video.shortUUID)
77e9f859
C
355 })
356 })
357
358 describe('When updating live information', async function () {
359
360 it('Should fail without access token', async function () {
d4a8e7a6 361 await updateLive(server.url, '', video.id, {}, HttpStatusCode.UNAUTHORIZED_401)
77e9f859
C
362 })
363
364 it('Should fail with a bad access token', async function () {
d4a8e7a6 365 await updateLive(server.url, 'toto', video.id, {}, HttpStatusCode.UNAUTHORIZED_401)
77e9f859
C
366 })
367
368 it('Should fail with access token of another user', async function () {
d4a8e7a6 369 await updateLive(server.url, userAccessToken, video.id, {}, HttpStatusCode.FORBIDDEN_403)
77e9f859
C
370 })
371
372 it('Should fail with a bad video id', async function () {
2d53be02 373 await updateLive(server.url, server.accessToken, 'toto', {}, HttpStatusCode.BAD_REQUEST_400)
77e9f859
C
374 })
375
376 it('Should fail with an unknown video id', async function () {
2d53be02 377 await updateLive(server.url, server.accessToken, 454555, {}, HttpStatusCode.NOT_FOUND_404)
77e9f859
C
378 })
379
380 it('Should fail with a non live video', async function () {
2d53be02 381 await updateLive(server.url, server.accessToken, videoIdNotLive, {}, HttpStatusCode.NOT_FOUND_404)
77e9f859
C
382 })
383
bb4ba6d9
C
384 it('Should fail with save replay and permanent live set to true', async function () {
385 const fields = { saveReplay: true, permanentLive: true }
386
d4a8e7a6 387 await updateLive(server.url, server.accessToken, video.id, fields, HttpStatusCode.BAD_REQUEST_400)
bb4ba6d9
C
388 })
389
77e9f859 390 it('Should succeed with the correct params', async function () {
d4a8e7a6
C
391 await updateLive(server.url, server.accessToken, video.id, { saveReplay: false })
392 await updateLive(server.url, server.accessToken, video.shortUUID, { saveReplay: false })
77e9f859
C
393 })
394
395 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
396 await updateCustomSubConfig(server.url, server.accessToken, {
397 live: {
398 enabled: true,
399 allowReplay: false
400 }
401 })
402
d4a8e7a6 403 await updateLive(server.url, server.accessToken, video.id, { saveReplay: true }, HttpStatusCode.FORBIDDEN_403)
77e9f859
C
404 })
405
406 it('Should fail to update a live if it has already started', async function () {
59fd824c 407 this.timeout(40000)
77e9f859 408
d4a8e7a6 409 const resLive = await getLive(server.url, server.accessToken, video.id)
77e9f859
C
410 const live: LiveVideo = resLive.body
411
412 const command = sendRTMPStream(live.rtmpUrl, live.streamKey)
413
d4a8e7a6
C
414 await waitUntilLivePublished(server.url, server.accessToken, video.id)
415 await updateLive(server.url, server.accessToken, video.id, {}, HttpStatusCode.BAD_REQUEST_400)
77e9f859
C
416
417 await stopFfmpeg(command)
418 })
97969c4e
C
419
420 it('Should fail to stream twice in the save live', async function () {
59fd824c 421 this.timeout(40000)
97969c4e 422
d4a8e7a6 423 const resLive = await getLive(server.url, server.accessToken, video.id)
97969c4e
C
424 const live: LiveVideo = resLive.body
425
426 const command = sendRTMPStream(live.rtmpUrl, live.streamKey)
427
d4a8e7a6 428 await waitUntilLivePublished(server.url, server.accessToken, video.id)
97969c4e 429
d4a8e7a6 430 await runAndTestFfmpegStreamError(server.url, server.accessToken, video.id, true)
97969c4e
C
431
432 await stopFfmpeg(command)
433 })
77e9f859
C
434 })
435
436 after(async function () {
437 await cleanupTests([ server ])
438 })
439})