aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/check-params/upload-quota.ts
blob: 00853752402649ca1ccff9f9e91c32679aac4fca (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
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import { FIXTURE_URLS } from '@server/tests/shared'
import { randomInt } from '@shared/core-utils'
import { HttpStatusCode, VideoImportState, VideoPrivacy } from '@shared/models'
import {
  cleanupTests,
  createSingleServer,
  PeerTubeServer,
  setAccessTokensToServers,
  setDefaultVideoChannel,
  VideosCommand,
  waitJobs
} from '@shared/server-commands'

describe('Test upload quota', function () {
  let server: PeerTubeServer
  let rootId: number
  let command: VideosCommand

  // ---------------------------------------------------------------

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

    server = await createSingleServer(1)
    await setAccessTokensToServers([ server ])
    await setDefaultVideoChannel([ server ])

    const user = await server.users.getMyInfo()
    rootId = user.id

    await server.users.update({ userId: rootId, videoQuota: 42 })

    command = server.videos
  })

  describe('When having a video quota', function () {

    it('Should fail with a registered user having too many videos with legacy upload', async function () {
      this.timeout(120000)

      const user = { username: 'registered' + randomInt(1, 1500), password: 'password' }
      await server.registrations.register(user)
      const userToken = await server.login.getAccessToken(user)

      const attributes = { fixture: 'video_short2.webm' }
      for (let i = 0; i < 5; i++) {
        await command.upload({ token: userToken, attributes })
      }

      await command.upload({ token: userToken, attributes, expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'legacy' })
    })

    it('Should fail with a registered user having too many videos with resumable upload', async function () {
      this.timeout(30000)

      const user = { username: 'registered' + randomInt(1, 1500), password: 'password' }
      await server.registrations.register(user)
      const userToken = await server.login.getAccessToken(user)

      const attributes = { fixture: 'video_short2.webm' }
      for (let i = 0; i < 5; i++) {
        await command.upload({ token: userToken, attributes })
      }

      await command.upload({ token: userToken, attributes, expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'resumable' })
    })

    it('Should fail to import with HTTP/Torrent/magnet', async function () {
      this.timeout(120_000)

      const baseAttributes = {
        channelId: server.store.channel.id,
        privacy: VideoPrivacy.PUBLIC
      }
      await server.imports.importVideo({ attributes: { ...baseAttributes, targetUrl: FIXTURE_URLS.goodVideo } })
      await server.imports.importVideo({ attributes: { ...baseAttributes, magnetUri: FIXTURE_URLS.magnet } })
      await server.imports.importVideo({ attributes: { ...baseAttributes, torrentfile: 'video-720p.torrent' as any } })

      await waitJobs([ server ])

      const { total, data: videoImports } = await server.imports.getMyVideoImports()
      expect(total).to.equal(3)

      expect(videoImports).to.have.lengthOf(3)

      for (const videoImport of videoImports) {
        expect(videoImport.state.id).to.equal(VideoImportState.FAILED)
        expect(videoImport.error).not.to.be.undefined
        expect(videoImport.error).to.contain('user video quota is exceeded')
      }
    })
  })

  describe('When having a daily video quota', function () {

    it('Should fail with a user having too many videos daily', async function () {
      await server.users.update({ userId: rootId, videoQuotaDaily: 42 })

      await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'legacy' })
      await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'resumable' })
    })
  })

  describe('When having an absolute and daily video quota', function () {
    it('Should fail if exceeding total quota', async function () {
      await server.users.update({
        userId: rootId,
        videoQuota: 42,
        videoQuotaDaily: 1024 * 1024 * 1024
      })

      await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'legacy' })
      await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'resumable' })
    })

    it('Should fail if exceeding daily quota', async function () {
      await server.users.update({
        userId: rootId,
        videoQuota: 1024 * 1024 * 1024,
        videoQuotaDaily: 42
      })

      await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'legacy' })
      await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'resumable' })
    })
  })

  after(async function () {
    await cleanupTests([ server ])
  })
})