PeerTube/server/tests/api/live/live-constraints.ts

238 lines
8.0 KiB
TypeScript
Raw Normal View History

2020-11-04 14:16:57 +01:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2022-08-17 15:44:32 +02:00
import { expect } from 'chai'
import { wait } from '@shared/core-utils'
import { LiveVideoError, UserVideoQuota, VideoPrivacy } from '@shared/models'
2020-11-04 14:16:57 +01:00
import {
cleanupTests,
2021-07-07 11:51:09 +02:00
ConfigCommand,
2021-07-16 09:47:51 +02:00
createMultipleServers,
2021-07-16 14:27:30 +02:00
doubleFollow,
2021-07-16 09:47:51 +02:00
PeerTubeServer,
2020-11-04 14:16:57 +01:00
setAccessTokensToServers,
setDefaultVideoChannel,
2022-05-04 10:07:06 +02:00
stopFfmpeg,
2022-05-03 11:38:07 +02:00
waitJobs,
2022-05-03 16:12:10 +02:00
waitUntilLiveReplacedByReplayOnAllServers,
2022-05-03 11:38:07 +02:00
waitUntilLiveWaitingOnAllServers
} from '@shared/server-commands'
import { checkLiveCleanup } from '../../shared'
2020-11-04 14:16:57 +01:00
describe('Test live constraints', function () {
2021-07-16 09:47:51 +02:00
let servers: PeerTubeServer[] = []
2020-11-04 14:16:57 +01:00
let userId: number
let userAccessToken: string
let userChannelId: number
async function createLiveWrapper (options: { replay: boolean, permanent: boolean }) {
2022-05-03 11:38:07 +02:00
const { replay, permanent } = options
2020-11-04 14:16:57 +01:00
const liveAttributes = {
name: 'user live',
channelId: userChannelId,
privacy: VideoPrivacy.PUBLIC,
2022-05-03 11:38:07 +02:00
saveReplay: replay,
replaySettings: options.replay ? { privacy: VideoPrivacy.PUBLIC } : undefined,
2022-05-03 11:38:07 +02:00
permanentLive: permanent
2020-11-04 14:16:57 +01:00
}
2021-07-16 09:04:35 +02:00
const { uuid } = await servers[0].live.create({ token: userAccessToken, fields: liveAttributes })
2021-07-08 10:18:40 +02:00
return uuid
2020-11-04 14:16:57 +01:00
}
async function checkSaveReplay (videoId: string, resolutions = [ 720 ]) {
for (const server of servers) {
2021-07-16 09:04:35 +02:00
const video = await server.videos.get({ id: videoId })
2020-11-04 14:16:57 +01:00
expect(video.isLive).to.be.false
expect(video.duration).to.be.greaterThan(0)
}
await checkLiveCleanup({ server: servers[0], permanent: false, videoUUID: videoId, savedResolutions: resolutions })
2020-11-04 14:16:57 +01:00
}
2021-05-07 11:53:46 +02:00
function updateQuota (options: { total: number, daily: number }) {
2021-07-16 09:04:35 +02:00
return servers[0].users.update({
2021-05-07 11:53:46 +02:00
userId,
videoQuota: options.total,
videoQuotaDaily: options.daily
})
}
2020-11-04 14:16:57 +01:00
before(async function () {
this.timeout(120000)
2021-07-16 09:47:51 +02:00
servers = await createMultipleServers(2)
2020-11-04 14:16:57 +01:00
// Get the access tokens
await setAccessTokensToServers(servers)
await setDefaultVideoChannel(servers)
2021-07-16 09:04:35 +02:00
await servers[0].config.updateCustomSubConfig({
2021-07-07 11:51:09 +02:00
newConfig: {
live: {
enabled: true,
allowReplay: true,
transcoding: {
enabled: false
}
2020-11-04 14:16:57 +01:00
}
}
})
{
2021-07-16 09:04:35 +02:00
const res = await servers[0].users.generate('user1')
2021-05-07 11:53:46 +02:00
userId = res.userId
userChannelId = res.userChannelId
userAccessToken = res.token
await updateQuota({ total: 1, daily: -1 })
2020-11-04 14:16:57 +01:00
}
// 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)
2022-05-03 11:38:07 +02:00
const userVideoLiveoId = await createLiveWrapper({ replay: false, permanent: false })
2021-07-16 09:04:35 +02:00
await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
2020-11-04 14:16:57 +01:00
})
2022-05-03 11:38:07 +02:00
it('Should have size limit depending on user global quota if save replay is enabled on non permanent live', async function () {
2020-11-04 14:16:57 +01:00
this.timeout(60000)
// Wait for user quota memoize cache invalidation
await wait(5000)
2022-05-03 11:38:07 +02:00
const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
2021-07-16 09:04:35 +02:00
await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
2020-11-04 14:16:57 +01:00
2022-05-03 16:12:10 +02:00
await waitUntilLiveReplacedByReplayOnAllServers(servers, userVideoLiveoId)
2020-11-04 14:16:57 +01:00
await waitJobs(servers)
await checkSaveReplay(userVideoLiveoId)
2022-05-03 11:38:07 +02:00
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)
2020-11-04 14:16:57 +01:00
})
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)
2021-05-07 11:53:46 +02:00
await updateQuota({ total: -1, daily: 1 })
2020-11-04 14:16:57 +01:00
2022-05-03 11:38:07 +02:00
const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
2021-07-16 09:04:35 +02:00
await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
2020-11-04 14:16:57 +01:00
2022-05-03 16:12:10 +02:00
await waitUntilLiveReplacedByReplayOnAllServers(servers, userVideoLiveoId)
2020-11-04 14:16:57 +01:00
await waitJobs(servers)
await checkSaveReplay(userVideoLiveoId)
2022-05-03 11:38:07 +02:00
const session = await servers[0].live.getReplaySession({ videoId: userVideoLiveoId })
expect(session.error).to.equal(LiveVideoError.QUOTA_EXCEEDED)
2020-11-04 14:16:57 +01:00
})
it('Should succeed without quota limit', async function () {
this.timeout(60000)
// Wait for user quota memoize cache invalidation
await wait(5000)
2021-05-07 11:53:46 +02:00
await updateQuota({ total: 10 * 1000 * 1000, daily: -1 })
2020-11-04 14:16:57 +01:00
2022-05-03 11:38:07 +02:00
const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
2021-07-16 09:04:35 +02:00
await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
2020-11-04 14:16:57 +01:00
})
2022-05-04 10:07:06 +02:00
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 })
// Wait previous live cleanups
await wait(3000)
2022-05-04 10:07:06 +02:00
2023-05-22 14:31:35 +02:00
const baseQuota = await servers[0].users.getMyQuotaUsed({ token: userAccessToken })
let quotaUser: UserVideoQuota
do {
await wait(500)
2022-05-04 10:07:06 +02:00
quotaUser = await servers[0].users.getMyQuotaUsed({ token: userAccessToken })
2023-05-22 16:23:28 +02:00
} while (quotaUser.videoQuotaUsed <= baseQuota.videoQuotaUsed)
2022-05-04 10:07:06 +02:00
const { data } = await servers[0].users.list()
const quotaAdmin = data.find(u => u.username === 'user1')
2023-05-22 14:31:35 +02:00
expect(quotaUser.videoQuotaUsed).to.be.above(baseQuota.videoQuotaUsed)
expect(quotaUser.videoQuotaUsedDaily).to.be.above(baseQuota.videoQuotaUsedDaily)
expect(quotaAdmin.videoQuotaUsed).to.be.above(baseQuota.videoQuotaUsed)
expect(quotaAdmin.videoQuotaUsedDaily).to.be.above(baseQuota.videoQuotaUsedDaily)
2022-05-04 10:07:06 +02:00
expect(quotaUser.videoQuotaUsed).to.be.above(10)
expect(quotaUser.videoQuotaUsedDaily).to.be.above(10)
2023-05-22 14:31:35 +02:00
expect(quotaAdmin.videoQuotaUsed).to.be.above(10)
expect(quotaAdmin.videoQuotaUsedDaily).to.be.above(10)
2022-05-04 10:07:06 +02:00
await stopFfmpeg(ffmpegCommand)
})
2020-11-04 14:16:57 +01:00
it('Should have max duration limit', async function () {
2023-05-05 14:35:59 +02:00
this.timeout(240000)
2020-11-04 14:16:57 +01:00
2021-07-16 09:04:35 +02:00
await servers[0].config.updateCustomSubConfig({
2021-07-07 11:51:09 +02:00
newConfig: {
live: {
2020-11-04 14:16:57 +01:00
enabled: true,
2021-07-07 11:51:09 +02:00
allowReplay: true,
2023-05-22 13:44:22 +02:00
maxDuration: 3,
2021-07-07 11:51:09 +02:00
transcoding: {
enabled: true,
resolutions: ConfigCommand.getCustomConfigResolutions(true)
}
2020-11-04 14:16:57 +01:00
}
}
})
2022-05-03 11:38:07 +02:00
const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
2021-07-16 09:04:35 +02:00
await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
2020-11-04 14:16:57 +01:00
2022-05-03 16:12:10 +02:00
await waitUntilLiveReplacedByReplayOnAllServers(servers, userVideoLiveoId)
2020-11-04 14:16:57 +01:00
await waitJobs(servers)
await checkSaveReplay(userVideoLiveoId, [ 720, 480, 360, 240, 144 ])
2022-05-03 11:38:07 +02:00
const session = await servers[0].live.getReplaySession({ videoId: userVideoLiveoId })
expect(session.error).to.equal(LiveVideoError.DURATION_EXCEEDED)
2020-11-04 14:16:57 +01:00
})
after(async function () {
await cleanupTests(servers)
})
})