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