]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/live/live-constraints.ts
We don't need to import mocha
[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
68e70a74 3import * as chai from 'chai'
c55e3d72 4import { wait } from '@shared/core-utils'
26e3e98f 5import { LiveVideoError, VideoPrivacy } from '@shared/models'
68e70a74 6import {
68e70a74 7 cleanupTests,
65e6e260 8 ConfigCommand,
254d3579 9 createMultipleServers,
4c7e60bc 10 doubleFollow,
254d3579 11 PeerTubeServer,
68e70a74
C
12 setAccessTokensToServers,
13 setDefaultVideoChannel,
9a82ce24 14 stopFfmpeg,
26e3e98f 15 waitJobs,
9295c68b 16 waitUntilLiveReplacedByReplayOnAllServers,
26e3e98f 17 waitUntilLiveWaitingOnAllServers
c55e3d72 18} from '@shared/server-commands'
4ec52d04 19import { checkLiveCleanup } from '../../shared'
68e70a74
C
20
21const expect = chai.expect
22
23describe('Test live constraints', function () {
254d3579 24 let servers: PeerTubeServer[] = []
68e70a74
C
25 let userId: number
26 let userAccessToken: string
27 let userChannelId: number
28
26e3e98f
C
29 async function createLiveWrapper (options: {
30 replay: boolean
31 permanent: boolean
32 }) {
33 const { replay, permanent } = options
34
68e70a74
C
35 const liveAttributes = {
36 name: 'user live',
37 channelId: userChannelId,
38 privacy: VideoPrivacy.PUBLIC,
26e3e98f
C
39 saveReplay: replay,
40 permanentLive: permanent
68e70a74
C
41 }
42
89d241a7 43 const { uuid } = await servers[0].live.create({ token: userAccessToken, fields: liveAttributes })
4f219914 44 return uuid
68e70a74
C
45 }
46
47 async function checkSaveReplay (videoId: string, resolutions = [ 720 ]) {
48 for (const server of servers) {
89d241a7 49 const video = await server.videos.get({ id: videoId })
68e70a74
C
50 expect(video.isLive).to.be.false
51 expect(video.duration).to.be.greaterThan(0)
52 }
53
4ec52d04 54 await checkLiveCleanup(servers[0], videoId, resolutions)
68e70a74
C
55 }
56
a1bb73f9 57 function updateQuota (options: { total: number, daily: number }) {
89d241a7 58 return servers[0].users.update({
a1bb73f9
C
59 userId,
60 videoQuota: options.total,
61 videoQuotaDaily: options.daily
62 })
63 }
64
68e70a74
C
65 before(async function () {
66 this.timeout(120000)
67
254d3579 68 servers = await createMultipleServers(2)
68e70a74
C
69
70 // Get the access tokens
71 await setAccessTokensToServers(servers)
72 await setDefaultVideoChannel(servers)
73
89d241a7 74 await servers[0].config.updateCustomSubConfig({
65e6e260
C
75 newConfig: {
76 live: {
77 enabled: true,
78 allowReplay: true,
79 transcoding: {
80 enabled: false
81 }
68e70a74
C
82 }
83 }
84 })
85
86 {
89d241a7 87 const res = await servers[0].users.generate('user1')
a1bb73f9
C
88 userId = res.userId
89 userChannelId = res.userChannelId
90 userAccessToken = res.token
91
92 await updateQuota({ total: 1, daily: -1 })
68e70a74
C
93 }
94
95 // Server 1 and server 2 follow each other
96 await doubleFollow(servers[0], servers[1])
97 })
98
99 it('Should not have size limit if save replay is disabled', async function () {
100 this.timeout(60000)
101
26e3e98f 102 const userVideoLiveoId = await createLiveWrapper({ replay: false, permanent: false })
89d241a7 103 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
68e70a74
C
104 })
105
26e3e98f 106 it('Should have size limit depending on user global quota if save replay is enabled on non permanent live', async function () {
68e70a74
C
107 this.timeout(60000)
108
109 // Wait for user quota memoize cache invalidation
110 await wait(5000)
111
26e3e98f 112 const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
89d241a7 113 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
68e70a74 114
9295c68b 115 await waitUntilLiveReplacedByReplayOnAllServers(servers, userVideoLiveoId)
68e70a74
C
116 await waitJobs(servers)
117
118 await checkSaveReplay(userVideoLiveoId)
26e3e98f
C
119
120 const session = await servers[0].live.getReplaySession({ videoId: userVideoLiveoId })
121 expect(session.error).to.equal(LiveVideoError.QUOTA_EXCEEDED)
122 })
123
124 it('Should have size limit depending on user global quota if save replay is enabled on a permanent live', async function () {
125 this.timeout(60000)
126
127 // Wait for user quota memoize cache invalidation
128 await wait(5000)
129
130 const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: true })
131 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
132
133 await waitJobs(servers)
134 await waitUntilLiveWaitingOnAllServers(servers, userVideoLiveoId)
135
136 const session = await servers[0].live.findLatestSession({ videoId: userVideoLiveoId })
137 expect(session.error).to.equal(LiveVideoError.QUOTA_EXCEEDED)
68e70a74
C
138 })
139
140 it('Should have size limit depending on user daily quota if save replay is enabled', async function () {
141 this.timeout(60000)
142
143 // Wait for user quota memoize cache invalidation
144 await wait(5000)
145
a1bb73f9 146 await updateQuota({ total: -1, daily: 1 })
68e70a74 147
26e3e98f 148 const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
89d241a7 149 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
68e70a74 150
9295c68b 151 await waitUntilLiveReplacedByReplayOnAllServers(servers, userVideoLiveoId)
68e70a74
C
152 await waitJobs(servers)
153
154 await checkSaveReplay(userVideoLiveoId)
26e3e98f
C
155
156 const session = await servers[0].live.getReplaySession({ videoId: userVideoLiveoId })
157 expect(session.error).to.equal(LiveVideoError.QUOTA_EXCEEDED)
68e70a74
C
158 })
159
160 it('Should succeed without quota limit', async function () {
161 this.timeout(60000)
162
163 // Wait for user quota memoize cache invalidation
164 await wait(5000)
165
a1bb73f9 166 await updateQuota({ total: 10 * 1000 * 1000, daily: -1 })
68e70a74 167
26e3e98f 168 const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
89d241a7 169 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
68e70a74
C
170 })
171
9a82ce24
C
172 it('Should have the same quota in admin and as a user', async function () {
173 this.timeout(120000)
174
175 const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
176 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ token: userAccessToken, videoId: userVideoLiveoId })
177
178 await servers[0].live.waitUntilPublished({ videoId: userVideoLiveoId })
179
180 await wait(3000)
181
182 const quotaUser = await servers[0].users.getMyQuotaUsed({ token: userAccessToken })
183
184 const { data } = await servers[0].users.list()
185 const quotaAdmin = data.find(u => u.username === 'user1')
186
187 expect(quotaUser.videoQuotaUsed).to.equal(quotaAdmin.videoQuotaUsed)
188 expect(quotaUser.videoQuotaUsedDaily).to.equal(quotaAdmin.videoQuotaUsedDaily)
189
190 expect(quotaUser.videoQuotaUsed).to.be.above(10)
191 expect(quotaUser.videoQuotaUsedDaily).to.be.above(10)
192
193 await stopFfmpeg(ffmpegCommand)
194 })
195
68e70a74 196 it('Should have max duration limit', async function () {
ffc12d3a 197 this.timeout(60000)
68e70a74 198
89d241a7 199 await servers[0].config.updateCustomSubConfig({
65e6e260
C
200 newConfig: {
201 live: {
68e70a74 202 enabled: true,
65e6e260
C
203 allowReplay: true,
204 maxDuration: 1,
205 transcoding: {
206 enabled: true,
207 resolutions: ConfigCommand.getCustomConfigResolutions(true)
208 }
68e70a74
C
209 }
210 }
211 })
212
26e3e98f 213 const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
89d241a7 214 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
68e70a74 215
9295c68b 216 await waitUntilLiveReplacedByReplayOnAllServers(servers, userVideoLiveoId)
68e70a74
C
217 await waitJobs(servers)
218
8dd754c7 219 await checkSaveReplay(userVideoLiveoId, [ 720, 480, 360, 240, 144 ])
26e3e98f
C
220
221 const session = await servers[0].live.getReplaySession({ videoId: userVideoLiveoId })
222 expect(session.error).to.equal(LiveVideoError.DURATION_EXCEEDED)
68e70a74
C
223 })
224
225 after(async function () {
226 await cleanupTests(servers)
227 })
228})