2023-08-29 13:07:05 +03:00
|
|
|
import type { INestApplication } from '@nestjs/common';
|
|
|
|
import { Test } from '@nestjs/testing';
|
|
|
|
import { PrismaClient } from '@prisma/client';
|
2023-09-01 22:41:29 +03:00
|
|
|
import test from 'ava';
|
2023-08-29 13:07:05 +03:00
|
|
|
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
|
|
|
|
import request from 'supertest';
|
|
|
|
|
2023-09-15 10:34:14 +03:00
|
|
|
import { AppModule } from '../src/app';
|
2023-12-14 12:50:41 +03:00
|
|
|
import { RevertCommand, RunCommand } from '../src/data/commands/run';
|
2023-12-14 12:50:36 +03:00
|
|
|
import { QuotaService, QuotaType } from '../src/modules/quota';
|
2023-08-31 11:39:19 +03:00
|
|
|
import {
|
2023-09-08 00:32:41 +03:00
|
|
|
checkBlobSize,
|
2023-09-01 15:56:27 +03:00
|
|
|
collectAllBlobSizes,
|
2023-08-31 11:39:19 +03:00
|
|
|
createWorkspace,
|
2023-12-14 12:50:36 +03:00
|
|
|
getWorkspaceBlobsSize,
|
2023-12-14 12:50:41 +03:00
|
|
|
initFeatureConfigs,
|
2023-08-31 11:39:19 +03:00
|
|
|
listBlobs,
|
|
|
|
setBlob,
|
|
|
|
signUp,
|
|
|
|
} from './utils';
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
let app: INestApplication;
|
2023-12-14 12:50:36 +03:00
|
|
|
let quota: QuotaService;
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
const client = new PrismaClient();
|
|
|
|
|
|
|
|
// cleanup database before each test
|
|
|
|
test.beforeEach(async () => {
|
|
|
|
await client.$connect();
|
|
|
|
await client.user.deleteMany({});
|
|
|
|
await client.snapshot.deleteMany({});
|
|
|
|
await client.update.deleteMany({});
|
|
|
|
await client.workspace.deleteMany({});
|
|
|
|
await client.$disconnect();
|
|
|
|
});
|
|
|
|
|
|
|
|
test.beforeEach(async () => {
|
|
|
|
const module = await Test.createTestingModule({
|
|
|
|
imports: [AppModule],
|
2023-12-14 12:50:36 +03:00
|
|
|
providers: [RevertCommand, RunCommand],
|
2023-09-01 22:41:29 +03:00
|
|
|
}).compile();
|
|
|
|
app = module.createNestApplication();
|
|
|
|
app.use(
|
|
|
|
graphqlUploadExpress({
|
|
|
|
maxFileSize: 10 * 1024 * 1024,
|
|
|
|
maxFiles: 5,
|
|
|
|
})
|
|
|
|
);
|
2023-12-14 12:50:36 +03:00
|
|
|
quota = module.get(QuotaService);
|
|
|
|
|
|
|
|
// init features
|
2023-12-14 12:50:41 +03:00
|
|
|
await initFeatureConfigs(module);
|
2023-12-14 12:50:36 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
await app.init();
|
|
|
|
});
|
|
|
|
|
2023-09-11 12:30:39 +03:00
|
|
|
test.afterEach.always(async () => {
|
2023-09-01 22:41:29 +03:00
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should set blobs', async t => {
|
|
|
|
const u1 = await signUp(app, 'u1', 'u1@affine.pro', '1');
|
|
|
|
|
|
|
|
const workspace = await createWorkspace(app, u1.token.token);
|
|
|
|
|
|
|
|
const buffer1 = Buffer.from([0, 0]);
|
|
|
|
const hash1 = await setBlob(app, u1.token.token, workspace.id, buffer1);
|
|
|
|
const buffer2 = Buffer.from([0, 1]);
|
|
|
|
const hash2 = await setBlob(app, u1.token.token, workspace.id, buffer2);
|
|
|
|
|
|
|
|
const server = app.getHttpServer();
|
|
|
|
|
|
|
|
const response1 = await request(server)
|
|
|
|
.get(`/api/workspaces/${workspace.id}/blobs/${hash1}`)
|
|
|
|
.auth(u1.token.token, { type: 'bearer' })
|
|
|
|
.buffer();
|
|
|
|
|
2023-09-05 11:01:45 +03:00
|
|
|
t.deepEqual(response1.body, buffer1, 'failed to get blob');
|
2023-09-01 22:41:29 +03:00
|
|
|
|
|
|
|
const response2 = await request(server)
|
|
|
|
.get(`/api/workspaces/${workspace.id}/blobs/${hash2}`)
|
|
|
|
.auth(u1.token.token, { type: 'bearer' })
|
|
|
|
.buffer();
|
|
|
|
|
2023-09-05 11:01:45 +03:00
|
|
|
t.deepEqual(response2.body, buffer2, 'failed to get blob');
|
2023-09-01 22:41:29 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should list blobs', async t => {
|
|
|
|
const u1 = await signUp(app, 'u1', 'u1@affine.pro', '1');
|
|
|
|
|
|
|
|
const workspace = await createWorkspace(app, u1.token.token);
|
|
|
|
const blobs = await listBlobs(app, u1.token.token, workspace.id);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(blobs.length, 0, 'failed to list blobs');
|
2023-09-01 22:41:29 +03:00
|
|
|
|
|
|
|
const buffer1 = Buffer.from([0, 0]);
|
|
|
|
const hash1 = await setBlob(app, u1.token.token, workspace.id, buffer1);
|
|
|
|
const buffer2 = Buffer.from([0, 1]);
|
|
|
|
const hash2 = await setBlob(app, u1.token.token, workspace.id, buffer2);
|
|
|
|
|
|
|
|
const ret = await listBlobs(app, u1.token.token, workspace.id);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(ret.length, 2, 'failed to list blobs');
|
|
|
|
t.is(ret[0], hash1, 'failed to list blobs');
|
|
|
|
t.is(ret[1], hash2, 'failed to list blobs');
|
2023-09-01 22:41:29 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should calc blobs size', async t => {
|
|
|
|
const u1 = await signUp(app, 'u1', 'u1@affine.pro', '1');
|
|
|
|
|
|
|
|
const workspace = await createWorkspace(app, u1.token.token);
|
|
|
|
|
|
|
|
const buffer1 = Buffer.from([0, 0]);
|
|
|
|
await setBlob(app, u1.token.token, workspace.id, buffer1);
|
|
|
|
const buffer2 = Buffer.from([0, 1]);
|
|
|
|
await setBlob(app, u1.token.token, workspace.id, buffer2);
|
|
|
|
|
2023-12-14 12:50:36 +03:00
|
|
|
const size = await getWorkspaceBlobsSize(app, u1.token.token, workspace.id);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(size, 4, 'failed to collect blob sizes');
|
2023-09-01 22:41:29 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should calc all blobs size', async t => {
|
|
|
|
const u1 = await signUp(app, 'u1', 'u1@affine.pro', '1');
|
|
|
|
|
|
|
|
const workspace1 = await createWorkspace(app, u1.token.token);
|
|
|
|
|
|
|
|
const buffer1 = Buffer.from([0, 0]);
|
|
|
|
await setBlob(app, u1.token.token, workspace1.id, buffer1);
|
|
|
|
const buffer2 = Buffer.from([0, 1]);
|
|
|
|
await setBlob(app, u1.token.token, workspace1.id, buffer2);
|
|
|
|
|
|
|
|
const workspace2 = await createWorkspace(app, u1.token.token);
|
|
|
|
|
|
|
|
const buffer3 = Buffer.from([0, 0]);
|
|
|
|
await setBlob(app, u1.token.token, workspace2.id, buffer3);
|
|
|
|
const buffer4 = Buffer.from([0, 1]);
|
|
|
|
await setBlob(app, u1.token.token, workspace2.id, buffer4);
|
|
|
|
|
|
|
|
const size = await collectAllBlobSizes(app, u1.token.token);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(size, 8, 'failed to collect all blob sizes');
|
2023-09-08 00:32:41 +03:00
|
|
|
|
|
|
|
const size1 = await checkBlobSize(
|
|
|
|
app,
|
|
|
|
u1.token.token,
|
|
|
|
workspace1.id,
|
|
|
|
10 * 1024 * 1024 * 1024 - 8
|
|
|
|
);
|
|
|
|
t.is(size1, 0, 'failed to check blob size');
|
|
|
|
|
|
|
|
const size2 = await checkBlobSize(
|
|
|
|
app,
|
|
|
|
u1.token.token,
|
|
|
|
workspace1.id,
|
|
|
|
10 * 1024 * 1024 * 1024 - 7
|
|
|
|
);
|
|
|
|
t.is(size2, -1, 'failed to check blob size');
|
2023-08-29 13:07:05 +03:00
|
|
|
});
|
2023-12-14 12:50:36 +03:00
|
|
|
|
|
|
|
test('should be able calc quota after switch plan', async t => {
|
|
|
|
const u1 = await signUp(app, 'darksky', 'darksky@affine.pro', '1');
|
|
|
|
|
|
|
|
const workspace1 = await createWorkspace(app, u1.token.token);
|
|
|
|
|
|
|
|
const buffer1 = Buffer.from([0, 0]);
|
|
|
|
await setBlob(app, u1.token.token, workspace1.id, buffer1);
|
|
|
|
const buffer2 = Buffer.from([0, 1]);
|
|
|
|
await setBlob(app, u1.token.token, workspace1.id, buffer2);
|
|
|
|
|
|
|
|
const workspace2 = await createWorkspace(app, u1.token.token);
|
|
|
|
|
|
|
|
const buffer3 = Buffer.from([0, 0]);
|
|
|
|
await setBlob(app, u1.token.token, workspace2.id, buffer3);
|
|
|
|
const buffer4 = Buffer.from([0, 1]);
|
|
|
|
await setBlob(app, u1.token.token, workspace2.id, buffer4);
|
|
|
|
|
|
|
|
const size1 = await checkBlobSize(
|
|
|
|
app,
|
|
|
|
u1.token.token,
|
|
|
|
workspace1.id,
|
|
|
|
10 * 1024 * 1024 * 1024 - 8
|
|
|
|
);
|
|
|
|
t.is(size1, 0, 'failed to check free plan blob size');
|
|
|
|
|
|
|
|
quota.switchUserQuota(u1.id, QuotaType.Quota_ProPlanV1);
|
|
|
|
|
|
|
|
const size2 = await checkBlobSize(
|
|
|
|
app,
|
|
|
|
u1.token.token,
|
|
|
|
workspace1.id,
|
|
|
|
100 * 1024 * 1024 * 1024 - 8
|
|
|
|
);
|
|
|
|
t.is(size2, 0, 'failed to check pro plan blob size');
|
|
|
|
});
|