2022-03-28 22:01:10 +08:00
|
|
|
import { Database, mockDatabase } from '@nocobase/database';
|
2022-02-15 00:20:25 +08:00
|
|
|
import Application, { ApplicationOptions } from '@nocobase/server';
|
2021-09-09 13:09:25 +08:00
|
|
|
import qs from 'qs';
|
2021-09-16 00:38:48 +08:00
|
|
|
import supertest, { SuperAgentTest } from 'supertest';
|
2021-09-09 13:09:25 +08:00
|
|
|
|
2021-09-09 22:38:39 +08:00
|
|
|
interface ActionParams {
|
2022-01-23 15:04:50 +08:00
|
|
|
filterByTk?: any;
|
|
|
|
fields?: string[];
|
2021-09-09 22:38:39 +08:00
|
|
|
filter?: any;
|
|
|
|
sort?: string[];
|
|
|
|
page?: number;
|
2022-02-18 09:46:10 +08:00
|
|
|
pageSize?: number;
|
2021-09-09 22:38:39 +08:00
|
|
|
values?: any;
|
2022-01-23 15:04:50 +08:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2021-09-09 22:38:39 +08:00
|
|
|
resourceName?: string;
|
2022-01-23 15:04:50 +08:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2021-12-04 16:28:52 +08:00
|
|
|
resourceIndex?: string;
|
2022-01-23 15:04:50 +08:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2021-09-09 22:38:39 +08:00
|
|
|
associatedName?: string;
|
2022-01-23 15:04:50 +08:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2021-12-04 16:28:52 +08:00
|
|
|
associatedIndex?: string;
|
2021-09-09 22:38:39 +08:00
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SortActionParams {
|
|
|
|
resourceName?: string;
|
2021-12-04 16:28:52 +08:00
|
|
|
resourceIndex?: any;
|
2021-09-09 22:38:39 +08:00
|
|
|
associatedName?: string;
|
2021-12-04 16:28:52 +08:00
|
|
|
associatedIndex?: any;
|
2021-09-09 22:38:39 +08:00
|
|
|
sourceId?: any;
|
|
|
|
targetId?: any;
|
|
|
|
sortField?: string;
|
|
|
|
method?: string;
|
|
|
|
target?: any;
|
|
|
|
sticky?: boolean;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
2021-09-09 13:09:25 +08:00
|
|
|
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>;
|
2021-09-09 22:38:39 +08:00
|
|
|
sort: (params?: SortActionParams) => Promise<supertest.Response>;
|
2021-09-09 13:09:25 +08:00
|
|
|
[name: string]: (params?: ActionParams) => Promise<supertest.Response>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class MockServer extends Application {
|
2022-01-30 11:11:36 +08:00
|
|
|
async loadAndInstall() {
|
2022-01-23 15:04:50 +08:00
|
|
|
await this.load();
|
2022-02-15 22:32:02 +08:00
|
|
|
await this.install({
|
|
|
|
sync: {
|
|
|
|
force: true,
|
|
|
|
alter: {
|
|
|
|
drop: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-01-23 15:04:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async cleanDb() {
|
|
|
|
await this.db.sequelize.getQueryInterface().dropAllTables();
|
|
|
|
}
|
|
|
|
|
|
|
|
agent(): SuperAgentTest & { resource: (name: string, resourceOf?: any) => Resource } {
|
2022-03-28 22:01:10 +08:00
|
|
|
const agent = supertest.agent(this.appManager.callback());
|
2021-09-09 13:09:25 +08:00
|
|
|
const prefix = this.resourcer.options.prefix;
|
2021-12-03 07:31:22 +08:00
|
|
|
const proxy = new Proxy(agent, {
|
2021-09-09 13:09:25 +08:00
|
|
|
get(target, method: string, receiver) {
|
2021-09-16 00:38:48 +08:00
|
|
|
if (method === 'resource') {
|
2022-01-23 15:04:50 +08:00
|
|
|
return (name: string, resourceOf?: any) => {
|
2021-09-16 00:38:48 +08:00
|
|
|
const keys = name.split('.');
|
2021-12-06 21:23:34 +08:00
|
|
|
const proxy = new Proxy(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
get(target, method: string, receiver) {
|
|
|
|
return (params: ActionParams = {}) => {
|
2022-01-23 15:04:50 +08:00
|
|
|
let { filterByTk, values = {}, file, ...restParams } = params;
|
|
|
|
if (params.associatedIndex) {
|
|
|
|
resourceOf = params.associatedIndex;
|
|
|
|
}
|
|
|
|
if (params.resourceIndex) {
|
|
|
|
filterByTk = params.resourceIndex;
|
|
|
|
}
|
2022-01-24 14:10:35 +08:00
|
|
|
let url = prefix || '';
|
2021-12-06 21:23:34 +08:00
|
|
|
if (keys.length > 1) {
|
2022-01-23 15:04:50 +08:00
|
|
|
url += `/${keys[0]}/${resourceOf}/${keys[1]}`;
|
2021-12-06 21:23:34 +08:00
|
|
|
} else {
|
2022-01-23 15:04:50 +08:00
|
|
|
url += `/${name}`;
|
2021-12-06 21:23:34 +08:00
|
|
|
}
|
|
|
|
url += `:${method as string}`;
|
2022-01-23 15:04:50 +08:00
|
|
|
if (filterByTk) {
|
|
|
|
url += `/${filterByTk}`;
|
2021-12-06 21:23:34 +08:00
|
|
|
}
|
2021-12-03 07:31:22 +08:00
|
|
|
|
2022-02-15 22:32:02 +08:00
|
|
|
const queryString = qs.stringify(restParams, { arrayFormat: 'brackets' });
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
switch (method) {
|
|
|
|
case 'upload':
|
2022-02-15 22:32:02 +08:00
|
|
|
return agent.post(`${url}?${queryString}`).attach('file', file).field(values);
|
2021-12-06 21:23:34 +08:00
|
|
|
case 'list':
|
|
|
|
case 'get':
|
2022-02-15 22:32:02 +08:00
|
|
|
return agent.get(`${url}?${queryString}`);
|
2021-12-06 21:23:34 +08:00
|
|
|
default:
|
2022-02-15 22:32:02 +08:00
|
|
|
return agent.post(`${url}?${queryString}`).send(values);
|
2021-12-06 21:23:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2021-09-16 00:38:48 +08:00
|
|
|
},
|
2021-12-06 21:23:34 +08:00
|
|
|
);
|
2021-09-16 00:38:48 +08:00
|
|
|
return proxy;
|
2021-12-06 21:23:34 +08:00
|
|
|
};
|
2021-09-16 00:38:48 +08:00
|
|
|
}
|
|
|
|
return (...args: any[]) => {
|
|
|
|
return agent[method](...args);
|
2021-09-09 13:09:25 +08:00
|
|
|
};
|
2021-12-06 21:23:34 +08:00
|
|
|
},
|
2021-09-09 13:09:25 +08:00
|
|
|
});
|
2021-09-16 00:38:48 +08:00
|
|
|
return proxy as any;
|
2021-09-09 13:09:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 00:20:25 +08:00
|
|
|
export function mockServer(options: ApplicationOptions = {}) {
|
2022-03-28 22:01:10 +08:00
|
|
|
let database;
|
|
|
|
if (options?.database instanceof Database) {
|
|
|
|
database = options.database;
|
|
|
|
} else {
|
|
|
|
database = mockDatabase(<any>options?.database || {});
|
|
|
|
}
|
|
|
|
|
2021-09-09 13:09:25 +08:00
|
|
|
return new MockServer({
|
|
|
|
...options,
|
2022-02-15 00:20:25 +08:00
|
|
|
database,
|
2021-09-09 13:09:25 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
export function createMockServer() {}
|
|
|
|
|
2021-09-09 13:09:25 +08:00
|
|
|
export default mockServer;
|