]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/live.ts
3e97dffdc670c41d2090e581363e2bc9a96d470f
[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 * as chai from 'chai'
5 import { omit } from 'lodash'
6 import { join } from 'path'
7 import { LiveVideo, VideoPrivacy } from '@shared/models'
8 import {
9 cleanupTests,
10 createUser,
11 flushAndRunServer,
12 getLive,
13 getMyUserInformation,
14 immutableAssign,
15 makePostBodyRequest,
16 makeUploadRequest,
17 sendRTMPStream,
18 ServerInfo,
19 setAccessTokensToServers,
20 stopFfmpeg,
21 testFfmpegStreamError,
22 updateCustomSubConfig,
23 updateLive,
24 uploadVideoAndGetId,
25 userLogin,
26 waitUntilLiveStarts
27 } from '../../../../shared/extra-utils'
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 accountName: string
34 let channelId: number
35 let channelName: string
36 let videoId: number
37 let videoIdNotLive: number
38
39 // ---------------------------------------------------------------
40
41 before(async function () {
42 this.timeout(30000)
43
44 server = await flushAndRunServer(1)
45
46 await setAccessTokensToServers([ server ])
47
48 await updateCustomSubConfig(server.url, server.accessToken, {
49 live: {
50 enabled: true,
51 maxInstanceLives: 20,
52 maxUserLives: 20,
53 allowReplay: true
54 }
55 })
56
57 const username = 'user1'
58 const password = 'my super password'
59 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
60 userAccessToken = await userLogin(server, { username, password })
61
62 {
63 const res = await getMyUserInformation(server.url, server.accessToken)
64 channelId = res.body.videoChannels[0].id
65 }
66
67 {
68 videoIdNotLive = (await uploadVideoAndGetId({ server, videoName: 'not live' })).id
69 }
70 })
71
72 describe('When creating a live', function () {
73 let baseCorrectParams
74
75 before(function () {
76 baseCorrectParams = {
77 name: 'my super name',
78 category: 5,
79 licence: 1,
80 language: 'pt',
81 nsfw: false,
82 commentsEnabled: true,
83 downloadEnabled: true,
84 waitTranscoding: true,
85 description: 'my super description',
86 support: 'my super support text',
87 tags: [ 'tag1', 'tag2' ],
88 privacy: VideoPrivacy.PUBLIC,
89 channelId,
90 saveReplay: false
91 }
92 })
93
94 it('Should fail with nothing', async function () {
95 const fields = {}
96 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
97 })
98
99 it('Should fail with a long name', async function () {
100 const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) })
101
102 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
103 })
104
105 it('Should fail with a bad category', async function () {
106 const fields = immutableAssign(baseCorrectParams, { category: 125 })
107
108 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
109 })
110
111 it('Should fail with a bad licence', async function () {
112 const fields = immutableAssign(baseCorrectParams, { licence: 125 })
113
114 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
115 })
116
117 it('Should fail with a bad language', async function () {
118 const fields = immutableAssign(baseCorrectParams, { language: 'a'.repeat(15) })
119
120 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
121 })
122
123 it('Should fail with a long description', async function () {
124 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(2500) })
125
126 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
127 })
128
129 it('Should fail with a long support text', async function () {
130 const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) })
131
132 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
133 })
134
135 it('Should fail without a channel', async function () {
136 const fields = omit(baseCorrectParams, 'channelId')
137
138 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
139 })
140
141 it('Should fail with a bad channel', async function () {
142 const fields = immutableAssign(baseCorrectParams, { channelId: 545454 })
143
144 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
145 })
146
147 it('Should fail with another user channel', async function () {
148 const user = {
149 username: 'fake',
150 password: 'fake_password'
151 }
152 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
153
154 const accessTokenUser = await userLogin(server, user)
155 const res = await getMyUserInformation(server.url, accessTokenUser)
156 const customChannelId = res.body.videoChannels[0].id
157
158 const fields = immutableAssign(baseCorrectParams, { channelId: customChannelId })
159
160 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
161 })
162
163 it('Should fail with too many tags', async function () {
164 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] })
165
166 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
167 })
168
169 it('Should fail with a tag length too low', async function () {
170 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] })
171
172 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
173 })
174
175 it('Should fail with a tag length too big', async function () {
176 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] })
177
178 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
179 })
180
181 it('Should fail with an incorrect thumbnail file', async function () {
182 const fields = baseCorrectParams
183 const attaches = {
184 thumbnailfile: join(__dirname, '..', '..', 'fixtures', 'avatar.png')
185 }
186
187 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
188 })
189
190 it('Should fail with a big thumbnail file', async function () {
191 const fields = baseCorrectParams
192 const attaches = {
193 thumbnailfile: join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
194 }
195
196 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
197 })
198
199 it('Should fail with an incorrect preview file', async function () {
200 const fields = baseCorrectParams
201 const attaches = {
202 previewfile: join(__dirname, '..', '..', 'fixtures', 'avatar.png')
203 }
204
205 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
206 })
207
208 it('Should fail with a big preview file', async function () {
209 const fields = baseCorrectParams
210 const attaches = {
211 previewfile: join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
212 }
213
214 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
215 })
216
217 it('Should succeed with the correct parameters', async function () {
218 this.timeout(30000)
219
220 const res = await makePostBodyRequest({
221 url: server.url,
222 path,
223 token: server.accessToken,
224 fields: baseCorrectParams,
225 statusCodeExpected: 200
226 })
227
228 videoId = res.body.video.id
229 })
230
231 it('Should forbid if live is disabled', async function () {
232 await updateCustomSubConfig(server.url, server.accessToken, {
233 live: {
234 enabled: false
235 }
236 })
237
238 await makePostBodyRequest({
239 url: server.url,
240 path,
241 token: server.accessToken,
242 fields: baseCorrectParams,
243 statusCodeExpected: 403
244 })
245 })
246
247 it('Should forbid to save replay if not enabled by the admin', async function () {
248 const fields = immutableAssign(baseCorrectParams, { saveReplay: true })
249
250 await updateCustomSubConfig(server.url, server.accessToken, {
251 live: {
252 enabled: true,
253 allowReplay: false
254 }
255 })
256
257 await makePostBodyRequest({
258 url: server.url,
259 path,
260 token: server.accessToken,
261 fields,
262 statusCodeExpected: 403
263 })
264 })
265
266 it('Should allow to save replay if enabled by the admin', async function () {
267 const fields = immutableAssign(baseCorrectParams, { saveReplay: true })
268
269 await updateCustomSubConfig(server.url, server.accessToken, {
270 live: {
271 enabled: true,
272 allowReplay: true
273 }
274 })
275
276 await makePostBodyRequest({
277 url: server.url,
278 path,
279 token: server.accessToken,
280 fields,
281 statusCodeExpected: 200
282 })
283 })
284
285 it('Should not allow live if max instance lives is reached', async function () {
286 await updateCustomSubConfig(server.url, server.accessToken, {
287 live: {
288 enabled: true,
289 maxInstanceLives: 1
290 }
291 })
292
293 await makePostBodyRequest({
294 url: server.url,
295 path,
296 token: server.accessToken,
297 fields: baseCorrectParams,
298 statusCodeExpected: 403
299 })
300 })
301
302 it('Should not allow live if max user lives is reached', async function () {
303 await updateCustomSubConfig(server.url, server.accessToken, {
304 live: {
305 enabled: true,
306 maxInstanceLives: 20,
307 maxUserLives: 1
308 }
309 })
310
311 await makePostBodyRequest({
312 url: server.url,
313 path,
314 token: server.accessToken,
315 fields: baseCorrectParams,
316 statusCodeExpected: 403
317 })
318 })
319 })
320
321 describe('When getting live information', function () {
322
323 it('Should fail without access token', async function () {
324 await getLive(server.url, '', videoId, 401)
325 })
326
327 it('Should fail with a bad access token', async function () {
328 await getLive(server.url, 'toto', videoId, 401)
329 })
330
331 it('Should fail with access token of another user', async function () {
332 await getLive(server.url, userAccessToken, videoId, 403)
333 })
334
335 it('Should fail with a bad video id', async function () {
336 await getLive(server.url, server.accessToken, 'toto', 400)
337 })
338
339 it('Should fail with an unknown video id', async function () {
340 await getLive(server.url, server.accessToken, 454555, 404)
341 })
342
343 it('Should fail with a non live video', async function () {
344 await getLive(server.url, server.accessToken, videoIdNotLive, 404)
345 })
346
347 it('Should succeed with the correct params', async function () {
348 await getLive(server.url, server.accessToken, videoId)
349 })
350 })
351
352 describe('When updating live information', async function () {
353
354 it('Should fail without access token', async function () {
355 await updateLive(server.url, '', videoId, {}, 401)
356 })
357
358 it('Should fail with a bad access token', async function () {
359 await updateLive(server.url, 'toto', videoId, {}, 401)
360 })
361
362 it('Should fail with access token of another user', async function () {
363 await updateLive(server.url, userAccessToken, videoId, {}, 403)
364 })
365
366 it('Should fail with a bad video id', async function () {
367 await updateLive(server.url, server.accessToken, 'toto', {}, 400)
368 })
369
370 it('Should fail with an unknown video id', async function () {
371 await updateLive(server.url, server.accessToken, 454555, {}, 404)
372 })
373
374 it('Should fail with a non live video', async function () {
375 await updateLive(server.url, server.accessToken, videoIdNotLive, {}, 404)
376 })
377
378 it('Should succeed with the correct params', async function () {
379 await updateLive(server.url, server.accessToken, videoId, { saveReplay: false })
380 })
381
382 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
383 await updateCustomSubConfig(server.url, server.accessToken, {
384 live: {
385 enabled: true,
386 allowReplay: false
387 }
388 })
389
390 await updateLive(server.url, server.accessToken, videoId, { saveReplay: true }, 403)
391 })
392
393 it('Should fail to update a live if it has already started', async function () {
394 this.timeout(20000)
395
396 const resLive = await getLive(server.url, server.accessToken, videoId)
397 const live: LiveVideo = resLive.body
398
399 const command = sendRTMPStream(live.rtmpUrl, live.streamKey)
400
401 await waitUntilLiveStarts(server.url, server.accessToken, videoId)
402 await updateLive(server.url, server.accessToken, videoId, {}, 400)
403
404 await stopFfmpeg(command)
405 })
406
407 it('Should fail to stream twice in the save live', async function () {
408 this.timeout(30000)
409
410 const resLive = await getLive(server.url, server.accessToken, videoId)
411 const live: LiveVideo = resLive.body
412
413 const command = sendRTMPStream(live.rtmpUrl, live.streamKey)
414
415 await waitUntilLiveStarts(server.url, server.accessToken, videoId)
416
417 await testFfmpegStreamError(server.url, server.accessToken, videoId, true)
418
419 await stopFfmpeg(command)
420 })
421 })
422
423 after(async function () {
424 await cleanupTests([ server ])
425 })
426 })