* chore: skip sync localization-management plugin enable status * chore: unSyncPlugins array * chore: remove install at dev * chore: gateway and supervisor * chore: install command * chore: remove app manager * chore: share plugin * chore: wsserver * chore: websocket server * chore: websocket connection with app status * feat: socket server at gateway * chore: loading message * chore: ready status * chore: handle app error * feat: restart app at supervisor * feat: health check endpoint * chore: test * chore: test * chore: test * chore: test * chore: build * chore: test * chore: build * chore: application ready status * chore: error message * chore: handle application error * chore: handle error in load * chore: report error in websocket * chore: ws error * chore: ws error * chore: typo * chore: switch app ready status at install * chore: test * chore: test * chore: test * chore: test * feat: add WebSocket client * chore: start gateway * chore: start command in application * chore: code * chore: pm command * chore: run start after load sub app * feat: application fsm * refactor: server life cycle (#2402) * refactor: server life cycle * fix: test error * fix: test error * fix: test error * fix: app.start * fix: cronjob stop * fix: db.sync before upgrade * fix: pm.get * fix: test error * fix: test error * fix: test error * test: add test cases * fix: remove PluginType * chore: start application after install * chore: fsm * chore: working status * chore: working command * chore: remove swith app ready status * chore: switch status after working done * chore: ws status * chore: gateway error * chore: test * chore: stopping status * chore: test * chore: test * feat: app proxy * chore: application destory * chore: application message changed with status * chore: test * chore: test timeout * test: remove listener add by plugin * test: remove listener add by plugin * chore: test * feat: app maintaining * fix: add AppMaintainingDialog * feat: off * test: gateway http response * test: gateway with errors * chore: unkown error * chore: websocket message * chore: ws message * chore: code * chore: error format * chore: delay app load * feat: improve code * chore: application initializing status * chore: supervisor with app status * chore: command status response * chore: test * chore: ws message * chore: test * fix: command running message * feat: restart * chore: code * chore: status transition * chore: test * fix: improve code * fix: error * fix: restart * fix: command * chore: reset client app tag when app selector changed * chore: error report * fix: application status * fix: build * chore: disable help command dispatch * chore: test * test: multi apps test * fix: improve code * fix: test * fix: test * fix: multi apps single running * fix: improve code * fix: app status * chore: move plugin static file to gateway * feat: static file handler * chore: test * chore: enable plugins in share collection * chore: gateway serve upload files * fix: improve client * chore: ws nginx config * fix: gateway with naonoid * fix: db sync * fix: loading * fix: ping * fix: locale load * fix: yarn start --quickstart * fix: add debug log * chore: application started event * chore: running working message payload * chore: nginx log * chore: nginxconf * chore: working message log * feat: logs * feat: compression * fix: remove koa-send and koa-static * fix: remove -e * fix: remove nginx * fix: remove -e * fix: __appName * chore: delay install sub application * chore: sync plugin status * fix: boot sub app * fix: main app reload * fix: test * fix: app status * test: field.bind block event loop * feat: newrelic * feat: debug log * chore: upgrade * fix(file-manager): test error * fix: default app selector * fix: reload after maintained * chore: boot sub app * chore: application destroy command * chore: destroy command * chore: clean code * chore: package.json * chore: maintaining message * chore: test * fix: collection.sync * feat: add test cases * chore: application * fix: test error * feat: improve codes and add test cases * fix: test error * fix: pm enable and disable * fix: pm.disable * feat: update docs * chore: update dockerfile --------- Co-authored-by: chenos <chenlinxh@gmail.com>
217 lines
5.6 KiB
TypeScript
217 lines
5.6 KiB
TypeScript
import { Database, mockDatabase } from '@nocobase/database';
|
|
import Application, { AppSupervisor, ApplicationOptions, Gateway, PluginManager } from '@nocobase/server';
|
|
import jwt from 'jsonwebtoken';
|
|
import qs from 'qs';
|
|
import supertest, { SuperAgentTest } from 'supertest';
|
|
|
|
interface ActionParams {
|
|
filterByTk?: any;
|
|
fields?: string[];
|
|
filter?: any;
|
|
sort?: string[];
|
|
page?: number;
|
|
pageSize?: number;
|
|
values?: any;
|
|
/**
|
|
* @deprecated
|
|
*/
|
|
resourceName?: string;
|
|
/**
|
|
* @deprecated
|
|
*/
|
|
resourceIndex?: string;
|
|
/**
|
|
* @deprecated
|
|
*/
|
|
associatedName?: string;
|
|
/**
|
|
* @deprecated
|
|
*/
|
|
associatedIndex?: string;
|
|
|
|
[key: string]: any;
|
|
}
|
|
|
|
interface SortActionParams {
|
|
resourceName?: string;
|
|
resourceIndex?: any;
|
|
associatedName?: string;
|
|
associatedIndex?: any;
|
|
sourceId?: any;
|
|
targetId?: any;
|
|
sortField?: string;
|
|
method?: string;
|
|
target?: any;
|
|
sticky?: boolean;
|
|
|
|
[key: string]: any;
|
|
}
|
|
|
|
interface Resource {
|
|
get: (params?: ActionParams) => Promise<supertest.Response>;
|
|
list: (params?: ActionParams) => Promise<supertest.Response>;
|
|
create: (params?: ActionParams) => Promise<supertest.Response>;
|
|
update: (params?: ActionParams) => Promise<supertest.Response>;
|
|
destroy: (params?: ActionParams) => Promise<supertest.Response>;
|
|
sort: (params?: SortActionParams) => Promise<supertest.Response>;
|
|
|
|
[name: string]: (params?: ActionParams) => Promise<supertest.Response>;
|
|
}
|
|
|
|
export class MockServer extends Application {
|
|
async loadAndInstall(options: any = {}) {
|
|
await this.load({ method: 'install' });
|
|
|
|
if (options.afterLoad) {
|
|
await options.afterLoad(this);
|
|
}
|
|
|
|
await this.install({
|
|
...options,
|
|
sync: {
|
|
force: false,
|
|
alter: {
|
|
drop: false,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async cleanDb() {
|
|
await this.db.clean({ drop: true });
|
|
}
|
|
|
|
async destroy(options: any = {}): Promise<void> {
|
|
await super.destroy(options);
|
|
|
|
Gateway.getInstance().destroy();
|
|
await AppSupervisor.getInstance().destroy();
|
|
}
|
|
|
|
agent(): SuperAgentTest & {
|
|
login: (user: any) => SuperAgentTest;
|
|
loginUsingId: (userId: number) => SuperAgentTest;
|
|
resource: (name: string, resourceOf?: any) => Resource;
|
|
} {
|
|
const agent = supertest.agent(this.callback());
|
|
const prefix = this.resourcer.options.prefix;
|
|
const proxy = new Proxy(agent, {
|
|
get(target, method: string, receiver) {
|
|
if (['login', 'loginUsingId'].includes(method)) {
|
|
return (userOrId: any) => {
|
|
return proxy
|
|
.auth(
|
|
jwt.sign(
|
|
{
|
|
userId: typeof userOrId === 'number' ? userOrId : userOrId?.id,
|
|
},
|
|
process.env.APP_KEY,
|
|
{
|
|
expiresIn: '1d',
|
|
},
|
|
),
|
|
{ type: 'bearer' },
|
|
)
|
|
.set('X-Authenticator', 'basic');
|
|
};
|
|
}
|
|
if (method === 'resource') {
|
|
return (name: string, resourceOf?: any) => {
|
|
const keys = name.split('.');
|
|
const proxy = new Proxy(
|
|
{},
|
|
{
|
|
get(target, method: string, receiver) {
|
|
return (params: ActionParams = {}) => {
|
|
let { filterByTk } = params;
|
|
const { values = {}, file, ...restParams } = params;
|
|
if (params.associatedIndex) {
|
|
resourceOf = params.associatedIndex;
|
|
}
|
|
if (params.resourceIndex) {
|
|
filterByTk = params.resourceIndex;
|
|
}
|
|
let url = prefix || '';
|
|
if (keys.length > 1) {
|
|
url += `/${keys[0]}/${resourceOf}/${keys[1]}`;
|
|
} else {
|
|
url += `/${name}`;
|
|
}
|
|
url += `:${method as string}`;
|
|
if (filterByTk) {
|
|
url += `/${filterByTk}`;
|
|
}
|
|
|
|
const queryString = qs.stringify(restParams, { arrayFormat: 'brackets' });
|
|
|
|
let request;
|
|
|
|
switch (method) {
|
|
case 'list':
|
|
case 'get':
|
|
request = agent.get(`${url}?${queryString}`);
|
|
break;
|
|
default:
|
|
request = agent.post(`${url}?${queryString}`);
|
|
break;
|
|
}
|
|
|
|
return file ? request.attach('file', file).field(values) : request.send(values);
|
|
};
|
|
},
|
|
},
|
|
);
|
|
return proxy;
|
|
};
|
|
}
|
|
return agent[method];
|
|
},
|
|
});
|
|
return proxy as any;
|
|
}
|
|
}
|
|
|
|
export function mockServer(options: ApplicationOptions = {}) {
|
|
if (typeof TextEncoder === 'undefined') {
|
|
global.TextEncoder = require('util').TextEncoder;
|
|
}
|
|
|
|
if (typeof TextDecoder === 'undefined') {
|
|
global.TextDecoder = require('util').TextDecoder;
|
|
}
|
|
|
|
Gateway.getInstance().reset();
|
|
AppSupervisor.getInstance().reset();
|
|
|
|
// @ts-ignore
|
|
if (!PluginManager.findPackagePatched) {
|
|
PluginManager.getPackageJson = () => {
|
|
return {
|
|
version: '0.0.0',
|
|
};
|
|
};
|
|
|
|
// @ts-ignore
|
|
PluginManager.findPackagePatched = true;
|
|
}
|
|
|
|
let database;
|
|
if (options?.database instanceof Database) {
|
|
database = options.database;
|
|
} else {
|
|
database = mockDatabase(<any>options?.database || {});
|
|
}
|
|
|
|
const app = new MockServer({
|
|
acl: false,
|
|
...options,
|
|
database,
|
|
});
|
|
|
|
return app;
|
|
}
|
|
|
|
export function createMockServer() {}
|
|
|
|
export default mockServer;
|