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