]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/live/live-constraints.ts
Introduce live command
[github/Chocobozzz/PeerTube.git] / server / tests / api / live / live-constraints.ts
CommitLineData
68e70a74
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import * as chai from 'chai'
a1bb73f9 5import { VideoDetails, VideoPrivacy } from '@shared/models'
68e70a74
C
6import {
7 checkLiveCleanup,
8 cleanupTests,
65e6e260 9 ConfigCommand,
68e70a74
C
10 doubleFollow,
11 flushAndRunMultipleServers,
a1bb73f9 12 generateUser,
68e70a74 13 getVideo,
68e70a74
C
14 ServerInfo,
15 setAccessTokensToServers,
16 setDefaultVideoChannel,
68e70a74 17 updateUser,
68e70a74 18 wait,
4f219914 19 waitJobs
68e70a74
C
20} from '../../../../shared/extra-utils'
21
22const expect = chai.expect
23
24describe('Test live constraints', function () {
25 let servers: ServerInfo[] = []
26 let userId: number
27 let userAccessToken: string
28 let userChannelId: number
29
30 async function createLiveWrapper (saveReplay: boolean) {
31 const liveAttributes = {
32 name: 'user live',
33 channelId: userChannelId,
34 privacy: VideoPrivacy.PUBLIC,
35 saveReplay
36 }
37
4f219914
C
38 const { uuid } = await servers[0].liveCommand.createLive({ token: userAccessToken, fields: liveAttributes })
39 return uuid
68e70a74
C
40 }
41
42 async function checkSaveReplay (videoId: string, resolutions = [ 720 ]) {
43 for (const server of servers) {
44 const res = await getVideo(server.url, videoId)
45
46 const video: VideoDetails = res.body
47 expect(video.isLive).to.be.false
48 expect(video.duration).to.be.greaterThan(0)
49 }
50
51 await checkLiveCleanup(servers[0], videoId, resolutions)
52 }
53
300cb723
C
54 async function waitUntilLivePublishedOnAllServers (videoId: string) {
55 for (const server of servers) {
4f219914 56 await server.liveCommand.waitUntilLivePublished({ videoId })
300cb723
C
57 }
58 }
59
a1bb73f9
C
60 function updateQuota (options: { total: number, daily: number }) {
61 return updateUser({
62 url: servers[0].url,
63 accessToken: servers[0].accessToken,
64 userId,
65 videoQuota: options.total,
66 videoQuotaDaily: options.daily
67 })
68 }
69
68e70a74
C
70 before(async function () {
71 this.timeout(120000)
72
73 servers = await flushAndRunMultipleServers(2)
74
75 // Get the access tokens
76 await setAccessTokensToServers(servers)
77 await setDefaultVideoChannel(servers)
78
65e6e260
C
79 await servers[0].configCommand.updateCustomSubConfig({
80 newConfig: {
81 live: {
82 enabled: true,
83 allowReplay: true,
84 transcoding: {
85 enabled: false
86 }
68e70a74
C
87 }
88 }
89 })
90
91 {
a1bb73f9
C
92 const res = await generateUser(servers[0], 'user1')
93 userId = res.userId
94 userChannelId = res.userChannelId
95 userAccessToken = res.token
96
97 await updateQuota({ total: 1, daily: -1 })
68e70a74
C
98 }
99
100 // Server 1 and server 2 follow each other
101 await doubleFollow(servers[0], servers[1])
102 })
103
104 it('Should not have size limit if save replay is disabled', async function () {
105 this.timeout(60000)
106
107 const userVideoLiveoId = await createLiveWrapper(false)
4f219914 108 await servers[0].liveCommand.runAndTestFfmpegStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
68e70a74
C
109 })
110
111 it('Should have size limit depending on user global quota if save replay is enabled', async function () {
112 this.timeout(60000)
113
114 // Wait for user quota memoize cache invalidation
115 await wait(5000)
116
117 const userVideoLiveoId = await createLiveWrapper(true)
4f219914 118 await servers[0].liveCommand.runAndTestFfmpegStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
68e70a74 119
300cb723 120 await waitUntilLivePublishedOnAllServers(userVideoLiveoId)
68e70a74
C
121 await waitJobs(servers)
122
123 await checkSaveReplay(userVideoLiveoId)
124 })
125
126 it('Should have size limit depending on user daily quota if save replay is enabled', async function () {
127 this.timeout(60000)
128
129 // Wait for user quota memoize cache invalidation
130 await wait(5000)
131
a1bb73f9 132 await updateQuota({ total: -1, daily: 1 })
68e70a74
C
133
134 const userVideoLiveoId = await createLiveWrapper(true)
4f219914 135 await servers[0].liveCommand.runAndTestFfmpegStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
68e70a74 136
300cb723 137 await waitUntilLivePublishedOnAllServers(userVideoLiveoId)
68e70a74
C
138 await waitJobs(servers)
139
140 await checkSaveReplay(userVideoLiveoId)
141 })
142
143 it('Should succeed without quota limit', async function () {
144 this.timeout(60000)
145
146 // Wait for user quota memoize cache invalidation
147 await wait(5000)
148
a1bb73f9 149 await updateQuota({ total: 10 * 1000 * 1000, daily: -1 })
68e70a74
C
150
151 const userVideoLiveoId = await createLiveWrapper(true)
4f219914 152 await servers[0].liveCommand.runAndTestFfmpegStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
68e70a74
C
153 })
154
155 it('Should have max duration limit', async function () {
ffc12d3a 156 this.timeout(60000)
68e70a74 157
65e6e260
C
158 await servers[0].configCommand.updateCustomSubConfig({
159 newConfig: {
160 live: {
68e70a74 161 enabled: true,
65e6e260
C
162 allowReplay: true,
163 maxDuration: 1,
164 transcoding: {
165 enabled: true,
166 resolutions: ConfigCommand.getCustomConfigResolutions(true)
167 }
68e70a74
C
168 }
169 }
170 })
171
172 const userVideoLiveoId = await createLiveWrapper(true)
4f219914 173 await servers[0].liveCommand.runAndTestFfmpegStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
68e70a74 174
300cb723 175 await waitUntilLivePublishedOnAllServers(userVideoLiveoId)
68e70a74
C
176 await waitJobs(servers)
177
178 await checkSaveReplay(userVideoLiveoId, [ 720, 480, 360, 240 ])
179 })
180
181 after(async function () {
182 await cleanupTests(servers)
183 })
184})