]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/live/live-constraints.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / api / live / live-constraints.ts
index 46153f7b1b1a035c074e8eafd283cd5accc21a21..cf43c262ad0b3fb135efc6a0035696bc0d1ffdcb 100644 (file)
@@ -2,48 +2,52 @@
 
 import 'mocha'
 import * as chai from 'chai'
-import { VideoDetails, VideoPrivacy } from '@shared/models'
+import { wait } from '@shared/core-utils'
+import { LiveVideoError, VideoPrivacy } from '@shared/models'
 import {
-  checkLiveCleanup,
   cleanupTests,
   ConfigCommand,
+  createMultipleServers,
   doubleFollow,
-  flushAndRunMultipleServers,
-  generateUser,
-  getVideo,
-  ServerInfo,
+  PeerTubeServer,
   setAccessTokensToServers,
   setDefaultVideoChannel,
-  updateUser,
-  wait,
-  waitJobs
-} from '../../../../shared/extra-utils'
+  stopFfmpeg,
+  waitJobs,
+  waitUntilLiveReplacedByReplayOnAllServers,
+  waitUntilLiveWaitingOnAllServers
+} from '@shared/server-commands'
+import { checkLiveCleanup } from '../../shared'
 
 const expect = chai.expect
 
 describe('Test live constraints', function () {
-  let servers: ServerInfo[] = []
+  let servers: PeerTubeServer[] = []
   let userId: number
   let userAccessToken: string
   let userChannelId: number
 
-  async function createLiveWrapper (saveReplay: boolean) {
+  async function createLiveWrapper (options: {
+    replay: boolean
+    permanent: boolean
+  }) {
+    const { replay, permanent } = options
+
     const liveAttributes = {
       name: 'user live',
       channelId: userChannelId,
       privacy: VideoPrivacy.PUBLIC,
-      saveReplay
+      saveReplay: replay,
+      permanentLive: permanent
     }
 
-    const { uuid } = await servers[0].liveCommand.create({ token: userAccessToken, fields: liveAttributes })
+    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 res = await getVideo(server.url, videoId)
-
-      const video: VideoDetails = res.body
+      const video = await server.videos.get({ id: videoId })
       expect(video.isLive).to.be.false
       expect(video.duration).to.be.greaterThan(0)
     }
@@ -51,16 +55,8 @@ describe('Test live constraints', function () {
     await checkLiveCleanup(servers[0], videoId, resolutions)
   }
 
-  async function waitUntilLivePublishedOnAllServers (videoId: string) {
-    for (const server of servers) {
-      await server.liveCommand.waitUntilPublished({ videoId })
-    }
-  }
-
   function updateQuota (options: { total: number, daily: number }) {
-    return updateUser({
-      url: servers[0].url,
-      accessToken: servers[0].accessToken,
+    return servers[0].users.update({
       userId,
       videoQuota: options.total,
       videoQuotaDaily: options.daily
@@ -70,13 +66,13 @@ describe('Test live constraints', function () {
   before(async function () {
     this.timeout(120000)
 
-    servers = await flushAndRunMultipleServers(2)
+    servers = await createMultipleServers(2)
 
     // Get the access tokens
     await setAccessTokensToServers(servers)
     await setDefaultVideoChannel(servers)
 
-    await servers[0].configCommand.updateCustomSubConfig({
+    await servers[0].config.updateCustomSubConfig({
       newConfig: {
         live: {
           enabled: true,
@@ -89,7 +85,7 @@ describe('Test live constraints', function () {
     })
 
     {
-      const res = await generateUser(servers[0], 'user1')
+      const res = await servers[0].users.generate('user1')
       userId = res.userId
       userChannelId = res.userChannelId
       userAccessToken = res.token
@@ -104,23 +100,42 @@ describe('Test live constraints', function () {
   it('Should not have size limit if save replay is disabled', async function () {
     this.timeout(60000)
 
-    const userVideoLiveoId = await createLiveWrapper(false)
-    await servers[0].liveCommand.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
+    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', async function () {
+  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(true)
-    await servers[0].liveCommand.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
+    const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
+    await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
 
-    await waitUntilLivePublishedOnAllServers(userVideoLiveoId)
+    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 () {
@@ -131,13 +146,16 @@ describe('Test live constraints', function () {
 
     await updateQuota({ total: -1, daily: 1 })
 
-    const userVideoLiveoId = await createLiveWrapper(true)
-    await servers[0].liveCommand.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
+    const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
+    await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
 
-    await waitUntilLivePublishedOnAllServers(userVideoLiveoId)
+    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 () {
@@ -148,14 +166,38 @@ describe('Test live constraints', function () {
 
     await updateQuota({ total: 10 * 1000 * 1000, daily: -1 })
 
-    const userVideoLiveoId = await createLiveWrapper(true)
-    await servers[0].liveCommand.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
+    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].configCommand.updateCustomSubConfig({
+    await servers[0].config.updateCustomSubConfig({
       newConfig: {
         live: {
           enabled: true,
@@ -169,13 +211,16 @@ describe('Test live constraints', function () {
       }
     })
 
-    const userVideoLiveoId = await createLiveWrapper(true)
-    await servers[0].liveCommand.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
+    const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false })
+    await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
 
-    await waitUntilLivePublishedOnAllServers(userVideoLiveoId)
+    await waitUntilLiveReplacedByReplayOnAllServers(servers, userVideoLiveoId)
     await waitJobs(servers)
 
-    await checkSaveReplay(userVideoLiveoId, [ 720, 480, 360, 240 ])
+    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 () {