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