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