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