2020-12-19 08:45:19 +08:00
|
|
|
import path from 'path';
|
|
|
|
import qs from 'qs';
|
|
|
|
import supertest from 'supertest';
|
|
|
|
import bodyParser from 'koa-bodyparser';
|
|
|
|
import { Dialect } from 'sequelize';
|
|
|
|
import Database from '@nocobase/database';
|
2021-01-13 16:23:15 +08:00
|
|
|
import { actions, middlewares } from '@nocobase/actions/src';
|
|
|
|
import { Application } from '@nocobase/server/src';
|
2020-12-23 12:46:13 +08:00
|
|
|
import middleware from '@nocobase/server/src/middleware'
|
2020-12-19 08:45:19 +08:00
|
|
|
import plugin from '../server';
|
2020-12-23 12:46:13 +08:00
|
|
|
import { FILE_FIELD_NAME } from '../constants';
|
2020-12-19 08:45:19 +08:00
|
|
|
|
|
|
|
function getTestKey() {
|
|
|
|
const { id } = require.main;
|
|
|
|
const key = id
|
|
|
|
.replace(`${process.env.PWD}/packages`, '')
|
|
|
|
.replace(/src\/__tests__/g, '')
|
|
|
|
.replace('.test.ts', '')
|
|
|
|
.replace(/[^\w]/g, '_')
|
|
|
|
.replace(/_+/g, '_');
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
username: process.env.DB_USER,
|
|
|
|
password: process.env.DB_PASSWORD,
|
|
|
|
database: process.env.DB_DATABASE,
|
|
|
|
host: process.env.DB_HOST,
|
|
|
|
port: Number.parseInt(process.env.DB_PORT, 10),
|
|
|
|
dialect: process.env.DB_DIALECT as Dialect,
|
|
|
|
define: {
|
|
|
|
hooks: {
|
|
|
|
beforeCreate(model, options) {
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
logging: process.env.DB_LOG_SQL === 'on',
|
|
|
|
sync: {
|
|
|
|
force: true,
|
|
|
|
alter: {
|
|
|
|
drop: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
hooks: {
|
|
|
|
beforeDefine(columns, model) {
|
|
|
|
model.tableName = `${getTestKey()}_${model.tableName || model.name.plural}`;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export function getDatabase() {
|
|
|
|
return new Database(config);
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function getApp() {
|
|
|
|
const app = new Application({
|
|
|
|
database: config,
|
|
|
|
resourcer: {
|
|
|
|
prefix: '/api',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
app.resourcer.use(middlewares.associated);
|
|
|
|
app.resourcer.registerActionHandlers({...actions.associate, ...actions.common});
|
2021-01-13 16:23:15 +08:00
|
|
|
app.registerPlugin({
|
2020-12-19 08:45:19 +08:00
|
|
|
'collections': [path.resolve(__dirname, '../../../plugin-collections')],
|
|
|
|
'file-manager': [plugin]
|
|
|
|
});
|
|
|
|
await app.loadPlugins();
|
2020-12-23 12:46:13 +08:00
|
|
|
app.database.import({
|
|
|
|
directory: path.resolve(__dirname, './tables')
|
2020-12-19 08:45:19 +08:00
|
|
|
});
|
2020-12-23 12:46:13 +08:00
|
|
|
try {
|
|
|
|
await app.database.sync();
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
2020-12-19 08:45:19 +08:00
|
|
|
app.use(async (ctx, next) => {
|
|
|
|
ctx.db = app.database;
|
|
|
|
await next();
|
|
|
|
});
|
|
|
|
app.use(bodyParser());
|
|
|
|
app.use(middleware({
|
|
|
|
prefix: '/api',
|
|
|
|
resourcer: app.resourcer,
|
|
|
|
database: app.database,
|
|
|
|
}));
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ActionParams {
|
|
|
|
resourceKey?: string | number;
|
|
|
|
// resourceName?: string;
|
|
|
|
// associatedName?: string;
|
|
|
|
associatedKey?: string | number;
|
|
|
|
fields?: any;
|
|
|
|
filter?: any;
|
|
|
|
values?: any;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Handler {
|
|
|
|
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>;
|
|
|
|
[name: string]: (params?: ActionParams) => Promise<supertest.Response>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Agent {
|
|
|
|
resource: (name: string) => Handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAgent(app: Application) {
|
|
|
|
return supertest.agent(app.callback());
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAPI(app: Application) {
|
|
|
|
const agent = getAgent(app);
|
|
|
|
return {
|
|
|
|
resource(name: string): any {
|
|
|
|
return new Proxy({}, {
|
2020-12-23 12:46:13 +08:00
|
|
|
get(target, method: string, receiver) {
|
2020-12-19 08:45:19 +08:00
|
|
|
return (params: ActionParams = {}) => {
|
2020-12-23 12:46:13 +08:00
|
|
|
const { associatedKey, resourceKey, values = {}, filePath, ...restParams } = params;
|
2020-12-19 08:45:19 +08:00
|
|
|
let url = `/api/${name}`;
|
|
|
|
if (associatedKey) {
|
|
|
|
url = `/api/${name.split('.').join(`/${associatedKey}/`)}`;
|
|
|
|
}
|
|
|
|
url += `:${method as string}`;
|
|
|
|
if (resourceKey) {
|
|
|
|
url += `/${resourceKey}`;
|
|
|
|
}
|
2020-12-23 12:46:13 +08:00
|
|
|
|
|
|
|
switch (method) {
|
|
|
|
case 'upload':
|
|
|
|
return agent.post(`${url}?${qs.stringify(restParams)}`)
|
|
|
|
.attach(FILE_FIELD_NAME, path.resolve(__dirname, filePath))
|
|
|
|
.field(values);
|
|
|
|
|
|
|
|
case 'list':
|
|
|
|
case 'get':
|
|
|
|
return agent.get(`${url}?${qs.stringify(restParams)}`);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return agent.post(`${url}?${qs.stringify(restParams)}`).send(values);
|
2020-12-19 08:45:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|