aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/live/live-constraints.ts
blob: cf43c262ad0b3fb135efc6a0035696bc0d1ffdcb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import * as chai from 'chai'
import { wait } from '@shared/core-utils'
import { LiveVideoError, VideoPrivacy } from '@shared/models'
import {
  cleanupTests,
  ConfigCommand,
  createMultipleServers,
  doubleFollow,
  PeerTubeServer,
  setAccessTokensToServers,
  setDefaultVideoChannel,
  stopFfmpeg,
  waitJobs,
  waitUntilLiveReplacedByReplayOnAllServers,
  waitUntilLiveWaitingOnAllServers
} from '@shared/server-commands'
import { checkLiveCleanup } from '../../shared'

const expect = chai.expect

describe('Test live constraints', function () {
  let servers: PeerTubeServer[] = []
  let userId: number
  let userAccessToken: string
  let userChannelId: number

  async function createLiveWrapper (options: {
    replay: boolean
    permanent: boolean
  }) {
    const { replay, permanent } = options

    const liveAttributes = {
      name: 'user live',
      channelId: userChannelId,
      privacy: VideoPrivacy.PUBLIC,
      saveReplay: replay,
      permanentLive: permanent
    }

    const { uuid } = await servers[0].live.create({ token: userAccessToken, fields: liveAttributes })
    return uuid
  }

  async function checkSaveReplay (videoId: string, resolutions = [ 720 ]) {
    for (const server of servers) {
      const video = await server.videos.get({ id: videoId })
      expect(video.isLive).to.be.false
      expect(video.duration).to.be.greaterThan(0)
    }

    await checkLiveCleanup(servers[0], videoId, resolutions)
  }

  function updateQuota (options: { total: number, daily: number }) {
    return servers[0].users.update({
      userId,
      videoQuota: options.total,
      videoQuotaDaily: options.daily
    })
  }

  before(async function () {
    this.timeout(120000)

    servers = await createMultipleServers(2)

    // Get the access tokens
    await setAccessTokensToServers(servers)
    await setDefaultVideoChannel(servers)

    await servers[0].config.updateCustomSubConfig({
      newConfig: {
        live: {
          enabled: true,
          allowReplay: true,
          transcoding: {
            enabled: false
          }
        }
      }
    })

    {
      const res = await servers[0].users.generate('user1')
      userId = res.userId
      userChannelId = res.userChannelId
      userAccessToken = res.token

      await updateQuota({ total: 1, daily: -1 })
    }

    // Server 1 and server 2 follow each other
    await doubleFollow(servers[0], servers[1])
  })

  it('Should not have size limit if save replay is disabled', async function () {
    this.timeout(60000)

    const userVideoLiveoId = await createLiveWrapper({ replay: false, permanent: false })
    await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
  })

  it('Should have size limit depending on user global quota if save replay is enabled on non permanent live', async function () {
    this.timeout(60000)

    // Wait for user quota memoize cache invalidation
    await wait(5000)

    const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
    await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })

    await waitUntilLiveReplacedByReplayOnAllServers(servers, userVideoLiveoId)
    await waitJobs(servers)

    await checkSaveReplay(userVideoLiveoId)

    const session = await servers[0].live.getReplaySession({ videoId: userVideoLiveoId })
    expect(session.error).to.equal(LiveVideoError.QUOTA_EXCEEDED)
  })

  it('Should have size limit depending on user global quota if save replay is enabled on a permanent live', async function () {
    this.timeout(60000)

    // Wait for user quota memoize cache invalidation
    await wait(5000)

    const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: true })
    await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })

    await waitJobs(servers)
    await waitUntilLiveWaitingOnAllServers(servers, userVideoLiveoId)

    const session = await servers[0].live.findLatestSession({ videoId: userVideoLiveoId })
    expect(session.error).to.equal(LiveVideoError.QUOTA_EXCEEDED)
  })

  it('Should have size limit depending on user daily quota if save replay is enabled', async function () {
    this.timeout(60000)

    // Wait for user quota memoize cache invalidation
    await wait(5000)

    await updateQuota({ total: -1, daily: 1 })

    const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
    await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })

    await waitUntilLiveReplacedByReplayOnAllServers(servers, userVideoLiveoId)
    await waitJobs(servers)

    await checkSaveReplay(userVideoLiveoId)

    const session = await servers[0].live.getReplaySession({ videoId: userVideoLiveoId })
    expect(session.error).to.equal(LiveVideoError.QUOTA_EXCEEDED)
  })

  it('Should succeed without quota limit', async function () {
    this.timeout(60000)

    // Wait for user quota memoize cache invalidation
    await wait(5000)

    await updateQuota({ total: 10 * 1000 * 1000, daily: -1 })

    const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
    await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
  })

  it('Should have the same quota in admin and as a user', async function () {
    this.timeout(120000)

    const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
    const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ token: userAccessToken, videoId: userVideoLiveoId })

    await servers[0].live.waitUntilPublished({ videoId: userVideoLiveoId })

    await wait(3000)

    const quotaUser = await servers[0].users.getMyQuotaUsed({ token: userAccessToken })

    const { data } = await servers[0].users.list()
    const quotaAdmin = data.find(u => u.username === 'user1')

    expect(quotaUser.videoQuotaUsed).to.equal(quotaAdmin.videoQuotaUsed)
    expect(quotaUser.videoQuotaUsedDaily).to.equal(quotaAdmin.videoQuotaUsedDaily)

    expect(quotaUser.videoQuotaUsed).to.be.above(10)
    expect(quotaUser.videoQuotaUsedDaily).to.be.above(10)

    await stopFfmpeg(ffmpegCommand)
  })

  it('Should have max duration limit', async function () {
    this.timeout(60000)

    await servers[0].config.updateCustomSubConfig({
      newConfig: {
        live: {
          enabled: true,
          allowReplay: true,
          maxDuration: 1,
          transcoding: {
            enabled: true,
            resolutions: ConfigCommand.getCustomConfigResolutions(true)
          }
        }
      }
    })

    const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
    await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })

    await waitUntilLiveReplacedByReplayOnAllServers(servers, userVideoLiveoId)
    await waitJobs(servers)

    await checkSaveReplay(userVideoLiveoId, [ 720, 480, 360, 240, 144 ])

    const session = await servers[0].live.getReplaySession({ videoId: userVideoLiveoId })
    expect(session.error).to.equal(LiveVideoError.DURATION_EXCEEDED)
  })

  after(async function () {
    await cleanupTests(servers)
  })
})