From bee0abffff73804d816b90c7fd599e0a51c09d61 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 28 Aug 2018 02:01:35 -0500 Subject: Implement daily upload limit (#956) * Implement daily upload limit (ref #652) * remove duplicate code * review fixes * fix tests? * whitespace fixes, finish leftover todo * fix tests * added some new tests * use different config value for tests * remove todo --- server/tests/api/check-params/config.ts | 3 +- server/tests/api/check-params/users.ts | 56 +++++++++++++++++++++++++++++++-- server/tests/api/server/config.ts | 5 ++- server/tests/utils/server/config.ts | 3 +- server/tests/utils/users/users.ts | 6 +++- 5 files changed, 67 insertions(+), 6 deletions(-) (limited to 'server/tests') diff --git a/server/tests/api/check-params/config.ts b/server/tests/api/check-params/config.ts index b26dfa252..ecfb76d47 100644 --- a/server/tests/api/check-params/config.ts +++ b/server/tests/api/check-params/config.ts @@ -48,7 +48,8 @@ describe('Test config API validators', function () { email: 'superadmin1@example.com' }, user: { - videoQuota: 5242881 + videoQuota: 5242881, + videoQuotaDaily: 318742 }, transcoding: { enabled: true, diff --git a/server/tests/api/check-params/users.ts b/server/tests/api/check-params/users.ts index b3fb61f6c..8b2ed1b04 100644 --- a/server/tests/api/check-params/users.ts +++ b/server/tests/api/check-params/users.ts @@ -94,6 +94,7 @@ describe('Test users API validators', function () { email: 'test@example.com', password: 'my super password', videoQuota: -1, + videoQuotaDaily: -1, role: UserRole.USER } @@ -173,12 +174,24 @@ describe('Test users API validators', function () { await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) }) + it('Should fail without a videoQuotaDaily', async function () { + const fields = omit(baseCorrectParams, 'videoQuotaDaily') + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + it('Should fail with an invalid videoQuota', async function () { const fields = immutableAssign(baseCorrectParams, { videoQuota: -5 }) await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) }) + it('Should fail with an invalid videoQuotaDaily', async function () { + const fields = immutableAssign(baseCorrectParams, { videoQuotaDaily: -7 }) + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + it('Should fail without a user role', async function () { const fields = omit(baseCorrectParams, 'role') @@ -607,7 +620,7 @@ describe('Test users API validators', function () { }) describe('When having a video quota', function () { - it('Should fail with a user having too many video', async function () { + it('Should fail with a user having too many videos', async function () { await updateUser({ url: server.url, userId: rootId, @@ -618,7 +631,7 @@ describe('Test users API validators', function () { await uploadVideo(server.url, server.accessToken, {}, 403) }) - it('Should fail with a registered user having too many video', async function () { + it('Should fail with a registered user having too many videos', async function () { this.timeout(30000) const user = { @@ -663,6 +676,45 @@ describe('Test users API validators', function () { }) }) + describe('When having a daily video quota', function () { + it('Should fail with a user having too many videos', async function () { + await updateUser({ + url: server.url, + userId: rootId, + accessToken: server.accessToken, + videoQuotaDaily: 42 + }) + + await uploadVideo(server.url, server.accessToken, {}, 403) + }) + }) + + describe('When having an absolute and daily video quota', function () { + it('Should fail if exceeding total quota', async function () { + await updateUser({ + url: server.url, + userId: rootId, + accessToken: server.accessToken, + videoQuota: 42, + videoQuotaDaily: 1024 * 1024 * 1024 + }) + + await uploadVideo(server.url, server.accessToken, {}, 403) + }) + + it('Should fail if exceeding daily quota', async function () { + await updateUser({ + url: server.url, + userId: rootId, + accessToken: server.accessToken, + videoQuota: 1024 * 1024 * 1024, + videoQuotaDaily: 42 + }) + + await uploadVideo(server.url, server.accessToken, {}, 403) + }) + }) + describe('When asking a password reset', function () { const path = '/api/v1/users/ask-reset-password' diff --git a/server/tests/api/server/config.ts b/server/tests/api/server/config.ts index f9805b6ea..8a5f27c34 100644 --- a/server/tests/api/server/config.ts +++ b/server/tests/api/server/config.ts @@ -37,6 +37,7 @@ function checkInitialConfig (data: CustomConfig) { expect(data.signup.limit).to.equal(4) expect(data.admin.email).to.equal('admin1@example.com') expect(data.user.videoQuota).to.equal(5242880) + expect(data.user.videoQuotaDaily).to.equal(318742) expect(data.transcoding.enabled).to.be.false expect(data.transcoding.threads).to.equal(2) expect(data.transcoding.resolutions['240p']).to.be.true @@ -65,6 +66,7 @@ function checkUpdatedConfig (data: CustomConfig) { expect(data.signup.limit).to.equal(5) expect(data.admin.email).to.equal('superadmin1@example.com') expect(data.user.videoQuota).to.equal(5242881) + expect(data.user.videoQuotaDaily).to.equal(318742) expect(data.transcoding.enabled).to.be.true expect(data.transcoding.threads).to.equal(1) expect(data.transcoding.resolutions['240p']).to.be.false @@ -152,7 +154,8 @@ describe('Test config', function () { email: 'superadmin1@example.com' }, user: { - videoQuota: 5242881 + videoQuota: 5242881, + videoQuotaDaily: 318742 }, transcoding: { enabled: true, diff --git a/server/tests/utils/server/config.ts b/server/tests/utils/server/config.ts index d6ac3ef8a..799c31ae5 100644 --- a/server/tests/utils/server/config.ts +++ b/server/tests/utils/server/config.ts @@ -80,7 +80,8 @@ function updateCustomSubConfig (url: string, token: string, newConfig: any) { email: 'superadmin1@example.com' }, user: { - videoQuota: 5242881 + videoQuota: 5242881, + videoQuotaDaily: 318742 }, transcoding: { enabled: true, diff --git a/server/tests/utils/users/users.ts b/server/tests/utils/users/users.ts index f786de6e3..5dba34b69 100644 --- a/server/tests/utils/users/users.ts +++ b/server/tests/utils/users/users.ts @@ -10,6 +10,7 @@ function createUser ( username: string, password: string, videoQuota = 1000000, + videoQuotaDaily = -1, role: UserRole = UserRole.USER, specialStatus = 200 ) { @@ -19,7 +20,8 @@ function createUser ( password, role, email: username + '@example.com', - videoQuota + videoQuota, + videoQuotaDaily } return request(url) @@ -202,6 +204,7 @@ function updateUser (options: { accessToken: string, email?: string, videoQuota?: number, + videoQuotaDaily?: number, role?: UserRole }) { const path = '/api/v1/users/' + options.userId @@ -209,6 +212,7 @@ function updateUser (options: { const toSend = {} if (options.email !== undefined && options.email !== null) toSend['email'] = options.email if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota + if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily if (options.role !== undefined && options.role !== null) toSend['role'] = options.role return makePutBodyRequest({ -- cgit v1.2.3