]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/upload-quota.ts
164c581e3f4e4aef0d19dfaf02fde1ed7c8bb729
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / upload-quota.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { expect } from 'chai'
5 import { HttpStatusCode, randomInt } from '@shared/core-utils'
6 import {
7 cleanupTests,
8 flushAndRunServer,
9 ImportsCommand,
10 ServerInfo,
11 setAccessTokensToServers,
12 setDefaultVideoChannel,
13 VideosCommand,
14 waitJobs
15 } from '@shared/extra-utils'
16 import { VideoImportState, VideoPrivacy } from '@shared/models'
17
18 describe('Test upload quota', function () {
19 let server: ServerInfo
20 let rootId: number
21 let command: VideosCommand
22
23 // ---------------------------------------------------------------
24
25 before(async function () {
26 this.timeout(30000)
27
28 server = await flushAndRunServer(1)
29 await setAccessTokensToServers([ server ])
30 await setDefaultVideoChannel([ server ])
31
32 const user = await server.usersCommand.getMyInfo()
33 rootId = user.id
34
35 await server.usersCommand.update({ userId: rootId, videoQuota: 42 })
36
37 command = server.videosCommand
38 })
39
40 describe('When having a video quota', function () {
41
42 it('Should fail with a registered user having too many videos with legacy upload', async function () {
43 this.timeout(30000)
44
45 const user = { username: 'registered' + randomInt(1, 1500), password: 'password' }
46 await server.usersCommand.register(user)
47 const userToken = await server.loginCommand.getAccessToken(user)
48
49 const attributes = { fixture: 'video_short2.webm' }
50 for (let i = 0; i < 5; i++) {
51 await command.upload({ token: userToken, attributes })
52 }
53
54 await command.upload({ token: userToken, attributes, expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'legacy' })
55 })
56
57 it('Should fail with a registered user having too many videos with resumable upload', async function () {
58 this.timeout(30000)
59
60 const user = { username: 'registered' + randomInt(1, 1500), password: 'password' }
61 await server.usersCommand.register(user)
62 const userToken = await server.loginCommand.getAccessToken(user)
63
64 const attributes = { fixture: 'video_short2.webm' }
65 for (let i = 0; i < 5; i++) {
66 await command.upload({ token: userToken, attributes })
67 }
68
69 await command.upload({ token: userToken, attributes, expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'resumable' })
70 })
71
72 it('Should fail to import with HTTP/Torrent/magnet', async function () {
73 this.timeout(120000)
74
75 const baseAttributes = {
76 channelId: server.videoChannel.id,
77 privacy: VideoPrivacy.PUBLIC
78 }
79 await server.importsCommand.importVideo({ attributes: { ...baseAttributes, targetUrl: ImportsCommand.getGoodVideoUrl() } })
80 await server.importsCommand.importVideo({ attributes: { ...baseAttributes, magnetUri: ImportsCommand.getMagnetURI() } })
81 await server.importsCommand.importVideo({ attributes: { ...baseAttributes, torrentfile: 'video-720p.torrent' as any } })
82
83 await waitJobs([ server ])
84
85 const { total, data: videoImports } = await server.importsCommand.getMyVideoImports()
86 expect(total).to.equal(3)
87
88 expect(videoImports).to.have.lengthOf(3)
89
90 for (const videoImport of videoImports) {
91 expect(videoImport.state.id).to.equal(VideoImportState.FAILED)
92 expect(videoImport.error).not.to.be.undefined
93 expect(videoImport.error).to.contain('user video quota is exceeded')
94 }
95 })
96 })
97
98 describe('When having a daily video quota', function () {
99
100 it('Should fail with a user having too many videos daily', async function () {
101 await server.usersCommand.update({ userId: rootId, videoQuotaDaily: 42 })
102
103 await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'legacy' })
104 await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'resumable' })
105 })
106 })
107
108 describe('When having an absolute and daily video quota', function () {
109 it('Should fail if exceeding total quota', async function () {
110 await server.usersCommand.update({
111 userId: rootId,
112 videoQuota: 42,
113 videoQuotaDaily: 1024 * 1024 * 1024
114 })
115
116 await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'legacy' })
117 await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'resumable' })
118 })
119
120 it('Should fail if exceeding daily quota', async function () {
121 await server.usersCommand.update({
122 userId: rootId,
123 videoQuota: 1024 * 1024 * 1024,
124 videoQuotaDaily: 42
125 })
126
127 await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'legacy' })
128 await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'resumable' })
129 })
130 })
131
132 after(async function () {
133 await cleanupTests([ server ])
134 })
135 })