]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/live.ts
replace numbers with typed http status codes (#3409)
[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 { omit } from 'lodash'
5 import { join } from 'path'
6 import { LiveVideo, VideoPrivacy } from '@shared/models'
7 import {
8 cleanupTests,
9 createUser,
10 flushAndRunServer,
11 getLive,
12 getMyUserInformation,
13 immutableAssign,
14 makePostBodyRequest,
15 makeUploadRequest,
16 runAndTestFfmpegStreamError,
17 sendRTMPStream,
18 ServerInfo,
19 setAccessTokensToServers,
20 stopFfmpeg,
21 updateCustomSubConfig,
22 updateLive,
23 uploadVideoAndGetId,
24 userLogin,
25 waitUntilLiveStarts
26 } from '../../../../shared/extra-utils'
27 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
28
29 describe('Test video lives API validator', function () {
30 const path = '/api/v1/videos/live'
31 let server: ServerInfo
32 let userAccessToken = ''
33 let channelId: number
34 let videoId: number
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,
88 saveReplay: false,
89 permanentLive: false
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 = {
183 thumbnailfile: join(__dirname, '..', '..', 'fixtures', 'avatar.png')
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: join(__dirname, '..', '..', 'fixtures', 'avatar-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: join(__dirname, '..', '..', 'fixtures', 'avatar.png')
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: join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
211 }
212
213 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
214 })
215
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
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,
230 statusCodeExpected: HttpStatusCode.OK_200
231 })
232
233 videoId = res.body.video.id
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,
248 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
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,
267 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
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,
286 statusCodeExpected: HttpStatusCode.OK_200
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,
303 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
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,
321 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
322 })
323 })
324 })
325
326 describe('When getting live information', function () {
327
328 it('Should fail without access token', async function () {
329 await getLive(server.url, '', videoId, HttpStatusCode.UNAUTHORIZED_401)
330 })
331
332 it('Should fail with a bad access token', async function () {
333 await getLive(server.url, 'toto', videoId, HttpStatusCode.UNAUTHORIZED_401)
334 })
335
336 it('Should fail with access token of another user', async function () {
337 await getLive(server.url, userAccessToken, videoId, HttpStatusCode.FORBIDDEN_403)
338 })
339
340 it('Should fail with a bad video id', async function () {
341 await getLive(server.url, server.accessToken, 'toto', HttpStatusCode.BAD_REQUEST_400)
342 })
343
344 it('Should fail with an unknown video id', async function () {
345 await getLive(server.url, server.accessToken, 454555, HttpStatusCode.NOT_FOUND_404)
346 })
347
348 it('Should fail with a non live video', async function () {
349 await getLive(server.url, server.accessToken, videoIdNotLive, HttpStatusCode.NOT_FOUND_404)
350 })
351
352 it('Should succeed with the correct params', async function () {
353 await getLive(server.url, server.accessToken, videoId)
354 })
355 })
356
357 describe('When updating live information', async function () {
358
359 it('Should fail without access token', async function () {
360 await updateLive(server.url, '', videoId, {}, HttpStatusCode.UNAUTHORIZED_401)
361 })
362
363 it('Should fail with a bad access token', async function () {
364 await updateLive(server.url, 'toto', videoId, {}, HttpStatusCode.UNAUTHORIZED_401)
365 })
366
367 it('Should fail with access token of another user', async function () {
368 await updateLive(server.url, userAccessToken, videoId, {}, HttpStatusCode.FORBIDDEN_403)
369 })
370
371 it('Should fail with a bad video id', async function () {
372 await updateLive(server.url, server.accessToken, 'toto', {}, HttpStatusCode.BAD_REQUEST_400)
373 })
374
375 it('Should fail with an unknown video id', async function () {
376 await updateLive(server.url, server.accessToken, 454555, {}, HttpStatusCode.NOT_FOUND_404)
377 })
378
379 it('Should fail with a non live video', async function () {
380 await updateLive(server.url, server.accessToken, videoIdNotLive, {}, HttpStatusCode.NOT_FOUND_404)
381 })
382
383 it('Should fail with save replay and permanent live set to true', async function () {
384 const fields = { saveReplay: true, permanentLive: true }
385
386 await updateLive(server.url, server.accessToken, videoId, fields, HttpStatusCode.BAD_REQUEST_400)
387 })
388
389 it('Should succeed with the correct params', async function () {
390 await updateLive(server.url, server.accessToken, videoId, { saveReplay: false })
391 })
392
393 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
394 await updateCustomSubConfig(server.url, server.accessToken, {
395 live: {
396 enabled: true,
397 allowReplay: false
398 }
399 })
400
401 await updateLive(server.url, server.accessToken, videoId, { saveReplay: true }, HttpStatusCode.FORBIDDEN_403)
402 })
403
404 it('Should fail to update a live if it has already started', async function () {
405 this.timeout(20000)
406
407 const resLive = await getLive(server.url, server.accessToken, videoId)
408 const live: LiveVideo = resLive.body
409
410 const command = sendRTMPStream(live.rtmpUrl, live.streamKey)
411
412 await waitUntilLiveStarts(server.url, server.accessToken, videoId)
413 await updateLive(server.url, server.accessToken, videoId, {}, HttpStatusCode.BAD_REQUEST_400)
414
415 await stopFfmpeg(command)
416 })
417
418 it('Should fail to stream twice in the save live', async function () {
419 this.timeout(30000)
420
421 const resLive = await getLive(server.url, server.accessToken, videoId)
422 const live: LiveVideo = resLive.body
423
424 const command = sendRTMPStream(live.rtmpUrl, live.streamKey)
425
426 await waitUntilLiveStarts(server.url, server.accessToken, videoId)
427
428 await runAndTestFfmpegStreamError(server.url, server.accessToken, videoId, true)
429
430 await stopFfmpeg(command)
431 })
432 })
433
434 after(async function () {
435 await cleanupTests([ server ])
436 })
437 })