]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/live.ts
Fix tests
[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
C
4import { omit } from 'lodash'
5import { join } from 'path'
6import { LiveVideo, VideoPrivacy } from '@shared/models'
7import {
8 cleanupTests,
9 createUser,
10 flushAndRunServer,
11 getLive,
12 getMyUserInformation,
13 immutableAssign,
14 makePostBodyRequest,
15 makeUploadRequest,
68e70a74 16 runAndTestFfmpegStreamError,
77e9f859
C
17 sendRTMPStream,
18 ServerInfo,
19 setAccessTokensToServers,
20 stopFfmpeg,
21 updateCustomSubConfig,
22 updateLive,
23 uploadVideoAndGetId,
24 userLogin,
0d8de275 25 waitUntilLivePublished
77e9f859 26} from '../../../../shared/extra-utils'
2d53be02 27import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
77e9f859
C
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
77e9f859
C
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,
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 = {
e9cb361c 183 thumbnailfile: join(__dirname, '..', '..', 'fixtures', '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 = {
e9cb361c 192 thumbnailfile: join(__dirname, '..', '..', 'fixtures', '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 = {
e9cb361c 201 previewfile: join(__dirname, '..', '..', 'fixtures', '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 = {
e9cb361c 210 previewfile: join(__dirname, '..', '..', 'fixtures', '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
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,
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 () {
2d53be02 329 await getLive(server.url, '', videoId, HttpStatusCode.UNAUTHORIZED_401)
77e9f859
C
330 })
331
332 it('Should fail with a bad access token', async function () {
2d53be02 333 await getLive(server.url, 'toto', videoId, HttpStatusCode.UNAUTHORIZED_401)
77e9f859
C
334 })
335
336 it('Should fail with access token of another user', async function () {
2d53be02 337 await getLive(server.url, userAccessToken, videoId, 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 () {
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 () {
2d53be02 360 await updateLive(server.url, '', videoId, {}, HttpStatusCode.UNAUTHORIZED_401)
77e9f859
C
361 })
362
363 it('Should fail with a bad access token', async function () {
2d53be02 364 await updateLive(server.url, 'toto', videoId, {}, HttpStatusCode.UNAUTHORIZED_401)
77e9f859
C
365 })
366
367 it('Should fail with access token of another user', async function () {
2d53be02 368 await updateLive(server.url, userAccessToken, videoId, {}, HttpStatusCode.FORBIDDEN_403)
77e9f859
C
369 })
370
371 it('Should fail with a bad video id', async function () {
2d53be02 372 await updateLive(server.url, server.accessToken, 'toto', {}, HttpStatusCode.BAD_REQUEST_400)
77e9f859
C
373 })
374
375 it('Should fail with an unknown video id', async function () {
2d53be02 376 await updateLive(server.url, server.accessToken, 454555, {}, HttpStatusCode.NOT_FOUND_404)
77e9f859
C
377 })
378
379 it('Should fail with a non live video', async function () {
2d53be02 380 await updateLive(server.url, server.accessToken, videoIdNotLive, {}, HttpStatusCode.NOT_FOUND_404)
77e9f859
C
381 })
382
bb4ba6d9
C
383 it('Should fail with save replay and permanent live set to true', async function () {
384 const fields = { saveReplay: true, permanentLive: true }
385
2d53be02 386 await updateLive(server.url, server.accessToken, videoId, fields, HttpStatusCode.BAD_REQUEST_400)
bb4ba6d9
C
387 })
388
77e9f859
C
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
2d53be02 401 await updateLive(server.url, server.accessToken, videoId, { saveReplay: true }, HttpStatusCode.FORBIDDEN_403)
77e9f859
C
402 })
403
404 it('Should fail to update a live if it has already started', async function () {
59fd824c 405 this.timeout(40000)
77e9f859
C
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
0d8de275 412 await waitUntilLivePublished(server.url, server.accessToken, videoId)
2d53be02 413 await updateLive(server.url, server.accessToken, videoId, {}, HttpStatusCode.BAD_REQUEST_400)
77e9f859
C
414
415 await stopFfmpeg(command)
416 })
97969c4e
C
417
418 it('Should fail to stream twice in the save live', async function () {
59fd824c 419 this.timeout(40000)
97969c4e
C
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
0d8de275 426 await waitUntilLivePublished(server.url, server.accessToken, videoId)
97969c4e 427
68e70a74 428 await runAndTestFfmpegStreamError(server.url, server.accessToken, videoId, true)
97969c4e
C
429
430 await stopFfmpeg(command)
431 })
77e9f859
C
432 })
433
434 after(async function () {
435 await cleanupTests([ server ])
436 })
437})