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