mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-08 20:57:08 +08:00
refactor(server): use new storage providers (#5433)
This commit is contained in:
@@ -22,8 +22,9 @@ import {
|
||||
import { EventModule } from '../src/event';
|
||||
import { DocManager, DocModule } from '../src/modules/doc';
|
||||
import { QuotaModule } from '../src/modules/quota';
|
||||
import { StorageModule } from '../src/modules/storage';
|
||||
import { PrismaModule, PrismaService } from '../src/prisma';
|
||||
import { FakeStorageModule, flushDB } from './utils';
|
||||
import { flushDB } from './utils';
|
||||
|
||||
const createModule = () => {
|
||||
return Test.createTestingModule({
|
||||
@@ -32,7 +33,7 @@ const createModule = () => {
|
||||
CacheModule,
|
||||
EventModule,
|
||||
QuotaModule,
|
||||
FakeStorageModule.forRoot(),
|
||||
StorageModule,
|
||||
ConfigModule.forRoot(),
|
||||
DocModule,
|
||||
RevertCommand,
|
||||
|
||||
@@ -6,11 +6,12 @@ import test from 'ava';
|
||||
import * as Sinon from 'sinon';
|
||||
|
||||
import { ConfigModule } from '../src/config';
|
||||
import type { EventPayload } from '../src/event';
|
||||
import { EventModule, type EventPayload } from '../src/event';
|
||||
import { DocHistoryManager } from '../src/modules/doc';
|
||||
import { QuotaModule } from '../src/modules/quota';
|
||||
import { StorageModule } from '../src/modules/storage';
|
||||
import { PrismaModule, PrismaService } from '../src/prisma';
|
||||
import { FakeStorageModule, flushDB } from './utils';
|
||||
import { flushDB } from './utils';
|
||||
|
||||
let app: INestApplication;
|
||||
let m: TestingModule;
|
||||
@@ -24,7 +25,8 @@ test.beforeEach(async () => {
|
||||
imports: [
|
||||
PrismaModule,
|
||||
QuotaModule,
|
||||
FakeStorageModule.forRoot(),
|
||||
EventModule,
|
||||
StorageModule,
|
||||
ScheduleModule.forRoot(),
|
||||
ConfigModule.forRoot(),
|
||||
],
|
||||
|
||||
@@ -6,6 +6,7 @@ import ava, { type TestFn } from 'ava';
|
||||
|
||||
import { ConfigModule } from '../src/config';
|
||||
import { RevertCommand, RunCommand } from '../src/data/commands/run';
|
||||
import { EventModule } from '../src/event';
|
||||
import { AuthModule } from '../src/modules/auth';
|
||||
import { AuthService } from '../src/modules/auth/service';
|
||||
import {
|
||||
@@ -15,9 +16,10 @@ import {
|
||||
QuotaService,
|
||||
QuotaType,
|
||||
} from '../src/modules/quota';
|
||||
import { StorageModule } from '../src/modules/storage';
|
||||
import { PrismaModule } from '../src/prisma';
|
||||
import { RateLimiterModule } from '../src/throttler';
|
||||
import { FakeStorageModule, initFeatureConfigs } from './utils';
|
||||
import { initFeatureConfigs } from './utils';
|
||||
|
||||
const test = ava as TestFn<{
|
||||
auth: AuthService;
|
||||
@@ -48,8 +50,9 @@ test.beforeEach(async t => {
|
||||
}),
|
||||
PrismaModule,
|
||||
AuthModule,
|
||||
EventModule,
|
||||
QuotaModule,
|
||||
FakeStorageModule.forRoot(),
|
||||
StorageModule,
|
||||
RateLimiterModule,
|
||||
RevertCommand,
|
||||
RunCommand,
|
||||
|
||||
@@ -106,7 +106,11 @@ export async function setBlob(
|
||||
})
|
||||
)
|
||||
.field('map', JSON.stringify({ '0': ['variables.blob'] }))
|
||||
.attach('0', buffer, 'blob.data')
|
||||
.attach(
|
||||
'0',
|
||||
buffer,
|
||||
`blob-${Math.random().toString(16).substring(2, 10)}.data`
|
||||
)
|
||||
.expect(200);
|
||||
return res.body.data.setBlob;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
import type { DynamicModule, FactoryProvider } from '@nestjs/common';
|
||||
import { TestingModule } from '@nestjs/testing';
|
||||
import { hashSync } from '@node-rs/argon2';
|
||||
import { PrismaClient, type User } from '@prisma/client';
|
||||
|
||||
import { RevertCommand, RunCommand } from '../../src/data/commands/run';
|
||||
import { StorageProvide } from '../../src/storage';
|
||||
|
||||
export async function flushDB() {
|
||||
const client = new PrismaClient();
|
||||
@@ -56,24 +54,6 @@ export class FakePrisma {
|
||||
}
|
||||
}
|
||||
|
||||
export class FakeStorageModule {
|
||||
static forRoot(): DynamicModule {
|
||||
const storageProvider: FactoryProvider = {
|
||||
provide: StorageProvide,
|
||||
useFactory: async () => {
|
||||
return null;
|
||||
},
|
||||
};
|
||||
|
||||
return {
|
||||
global: true,
|
||||
module: FakeStorageModule,
|
||||
providers: [storageProvider],
|
||||
exports: [storageProvider],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function initFeatureConfigs(module: TestingModule) {
|
||||
const run = module.get(RunCommand);
|
||||
const revert = module.get(RevertCommand);
|
||||
|
||||
@@ -99,8 +99,8 @@ test('should list blobs', async t => {
|
||||
|
||||
const ret = await listBlobs(app, u1.token.token, workspace.id);
|
||||
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');
|
||||
// list blob result is not ordered
|
||||
t.deepEqual(ret.sort(), [hash1, hash2].sort());
|
||||
});
|
||||
|
||||
test('should calc blobs size', async t => {
|
||||
@@ -189,3 +189,13 @@ 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 quota', t => {
|
||||
// TODO
|
||||
t.true(true);
|
||||
});
|
||||
|
||||
@@ -1,111 +0,0 @@
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import ava, { type TestFn } from 'ava';
|
||||
import { stub } from 'sinon';
|
||||
|
||||
import { AppModule } from '../src/app';
|
||||
import { FeatureManagementService } from '../src/modules/features';
|
||||
import { Quotas } from '../src/modules/quota';
|
||||
import { UsersService } from '../src/modules/users';
|
||||
import { PermissionService } from '../src/modules/workspaces/permission';
|
||||
import { WorkspaceResolver } from '../src/modules/workspaces/resolver';
|
||||
import { PrismaService } from '../src/prisma';
|
||||
import { StorageProvide } from '../src/storage';
|
||||
import { FakePrisma } from './utils';
|
||||
|
||||
class FakePermission {
|
||||
async tryCheckWorkspace() {
|
||||
return true;
|
||||
}
|
||||
async getWorkspaceOwner() {
|
||||
return {
|
||||
user: new FakePrisma().fakeUser,
|
||||
};
|
||||
}
|
||||
async getOwnedWorkspaces() {
|
||||
return [''];
|
||||
}
|
||||
}
|
||||
|
||||
const fakeUserService = {
|
||||
getStorageQuotaById: stub(),
|
||||
};
|
||||
|
||||
const test = ava as TestFn<{
|
||||
app: INestApplication;
|
||||
resolver: WorkspaceResolver;
|
||||
}>;
|
||||
|
||||
test.beforeEach(async t => {
|
||||
const module = await Test.createTestingModule({
|
||||
imports: [AppModule],
|
||||
})
|
||||
.overrideProvider(PrismaService)
|
||||
.useValue({
|
||||
workspaceUserPermission: {
|
||||
async findMany() {
|
||||
return [];
|
||||
},
|
||||
},
|
||||
userFeatures: {
|
||||
async count() {
|
||||
return 1;
|
||||
},
|
||||
async findFirst() {
|
||||
return {
|
||||
createdAt: new Date(),
|
||||
expiredAt: new Date(),
|
||||
reason: '',
|
||||
feature: Quotas[0],
|
||||
};
|
||||
},
|
||||
},
|
||||
features: {
|
||||
async findFirst() {
|
||||
return {
|
||||
id: 0,
|
||||
feature: 'free_plan_v1',
|
||||
version: 1,
|
||||
type: 1,
|
||||
configs: {
|
||||
name: 'Free',
|
||||
blobLimit: 1,
|
||||
storageQuota: 1,
|
||||
historyPeriod: 1,
|
||||
memberLimit: 3,
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
})
|
||||
.overrideProvider(PermissionService)
|
||||
.useClass(FakePermission)
|
||||
.overrideProvider(UsersService)
|
||||
.useValue(fakeUserService)
|
||||
.overrideProvider(StorageProvide)
|
||||
.useValue({
|
||||
blobsSize() {
|
||||
return 1024 * 10;
|
||||
},
|
||||
})
|
||||
.overrideProvider(FeatureManagementService)
|
||||
.useValue({})
|
||||
.compile();
|
||||
t.context.app = module.createNestApplication();
|
||||
t.context.resolver = t.context.app.get(WorkspaceResolver);
|
||||
await t.context.app.init();
|
||||
});
|
||||
|
||||
test.afterEach.always(async t => {
|
||||
await t.context.app.close();
|
||||
});
|
||||
|
||||
test('should get blob size limit', async t => {
|
||||
const { resolver } = t.context;
|
||||
fakeUserService.getStorageQuotaById.resolves(100 * 1024 * 1024 * 1024);
|
||||
const res = await resolver.checkBlobSize(new FakePrisma().fakeUser, '', 100);
|
||||
t.not(res, false);
|
||||
// @ts-expect-error
|
||||
t.is(typeof res.size, 'number');
|
||||
fakeUserService.getStorageQuotaById.reset();
|
||||
});
|
||||
Reference in New Issue
Block a user