feat: blob size limit with quota (#5524)

fix AFF-506 TOV-342
This commit is contained in:
DarkSky
2024-01-11 10:21:40 +00:00
parent d1c2b2a7b0
commit d6f65ea414
8 changed files with 150 additions and 26 deletions

View File

@@ -3,6 +3,7 @@ import test from 'ava';
import request from 'supertest';
import { AppModule } from '../src/app';
import { FeatureManagementService, FeatureType } from '../src/modules/features';
import { QuotaService, QuotaType } from '../src/modules/quota';
import {
checkBlobSize,
@@ -15,8 +16,11 @@ import {
signUp,
} from './utils';
const OneMB = 1024 * 1024;
let app: INestApplication;
let quota: QuotaService;
let feature: FeatureManagementService;
test.beforeEach(async () => {
const { app: testApp } = await createTestingApp({
@@ -25,6 +29,7 @@ test.beforeEach(async () => {
app = testApp;
quota = app.get(QuotaService);
feature = app.get(FeatureManagementService);
});
test.afterEach.always(async () => {
@@ -163,12 +168,51 @@ test('should be able calc quota after switch plan', async t => {
t.is(size2, 0, 'failed to check pro plan blob size');
});
test('should reject blob exceeded limit', t => {
// TODO
t.true(true);
test('should reject blob exceeded limit', async t => {
const u1 = await signUp(app, 'darksky', 'darksky@affine.pro', '1');
const workspace1 = await createWorkspace(app, u1.token.token);
await quota.switchUserQuota(u1.id, QuotaType.RestrictedPlanV1);
const buffer1 = Buffer.from(Array.from({ length: OneMB + 1 }, () => 0));
await t.throwsAsync(setBlob(app, u1.token.token, workspace1.id, buffer1));
await quota.switchUserQuota(u1.id, QuotaType.FreePlanV1);
const buffer2 = Buffer.from(Array.from({ length: OneMB + 1 }, () => 0));
await t.notThrowsAsync(setBlob(app, u1.token.token, workspace1.id, buffer2));
const buffer3 = Buffer.from(Array.from({ length: 10 * OneMB + 1 }, () => 0));
await t.throwsAsync(setBlob(app, u1.token.token, workspace1.id, buffer3));
});
test('should reject blob exceeded quota', t => {
// TODO
t.true(true);
test('should reject blob exceeded quota', async t => {
const u1 = await signUp(app, 'darksky', 'darksky@affine.pro', '1');
const workspace = await createWorkspace(app, u1.token.token);
await quota.switchUserQuota(u1.id, QuotaType.RestrictedPlanV1);
const buffer = Buffer.from(Array.from({ length: OneMB }, () => 0));
for (let i = 0; i < 10; i++) {
await t.notThrowsAsync(setBlob(app, u1.token.token, workspace.id, buffer));
}
await t.throwsAsync(setBlob(app, u1.token.token, workspace.id, buffer));
});
test('should accept blob even storage out of quota if workspace has unlimited feature', async t => {
const u1 = await signUp(app, 'darksky', 'darksky@affine.pro', '1');
const workspace = await createWorkspace(app, u1.token.token);
await quota.switchUserQuota(u1.id, QuotaType.RestrictedPlanV1);
feature.addWorkspaceFeatures(workspace.id, FeatureType.UnlimitedWorkspace);
const buffer = Buffer.from(Array.from({ length: OneMB }, () => 0));
for (let i = 0; i < 10; i++) {
await t.notThrowsAsync(setBlob(app, u1.token.token, workspace.id, buffer));
}
await t.notThrowsAsync(setBlob(app, u1.token.token, workspace.id, buffer));
});