tachybase_todo/packages/test/src/mockServer.ts
ChengLei Shao 15950ece05
Application (#175)
* feat: getRepository

* getRepository return type

* export action

* add: acl

* feat: setResourceAction

* feat: action alias

* chore: code struct

* feat: removeResourceAction

* chore: file name

* ignorecase

* remove ACL

* feat: ACL

* feat: role toJSON

* using emit

* chore: test

* feat: plugin-acl

* feat: acl with predicate

* grant universal action test

* grant action test

* update resource action test

* revoke resource action

* usingActionsConfig switch

* plugin-ui-schema-storage

* remove global acl instance

* fix: collection manager with sqlite

* add own action listener

* add acl middleware

* add acl allowConfigure strategy option

* add plugin-acl allowConfigure

* change acl resourceName

* add acl middleware merge params

* bugfix

* append fields on acl action params

* acl middleware parse template

* fix: collection-manager migrate

* add acl association field test

* feat(plugin-acl): grant association field actions

* chore(plugin-acl): type name

* feat(plugin-acl): regrant actions on resource action update

* feat(plugin-acl): regrant action on field destroy

* fix(plugin-acl): test

* fix(plugin-acl): test run

* feat(plugin-acl): set default role

* feat(plugin-users): set user default role

* test(plugin-users): create user with role

* feat(plugin-users): create user with role

* feat(application): application hook

* feat(database): reconnect

* feat(database): application life cycle

* feat(database): sync with option

* feat(database): hook position

* feat(database): hook position

* feat(database): remove load in start

* fix(application): get plugin

* feat(test): loadAndInstall

* feat: improve code

* feat: improve code

* fix: listen options

* fix: bug

* test(database): add test case

Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 11:11:36 +08:00

136 lines
3.9 KiB
TypeScript

import qs from 'qs';
import supertest, { SuperAgentTest } from 'supertest';
import Application, { ApplicationOptions } from '@nocobase/server';
import { getConfig } from './mockDatabase';
interface ActionParams {
filterByTk?: any;
fields?: string[];
filter?: any;
sort?: string[];
page?: number;
perPage?: 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() {
await this.load();
await this.install({ clean: true });
}
async cleanDb() {
await this.db.sequelize.getQueryInterface().dropAllTables();
}
agent(): 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 (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, 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}`;
}
switch (method) {
case 'upload':
return agent
.post(`${url}?${qs.stringify(restParams)}`)
.attach('file', file)
.field(values);
case 'list':
case 'get':
return agent.get(`${url}?${qs.stringify(restParams)}`);
default:
return agent.post(`${url}?${qs.stringify(restParams)}`).send(values);
}
};
},
},
);
return proxy;
};
}
return (...args: any[]) => {
return agent[method](...args);
};
},
});
return proxy as any;
}
}
export function mockServer(options?: ApplicationOptions) {
return new MockServer({
...options,
database: getConfig(options?.database),
});
}
export function createMockServer() {}
export default mockServer;