budibase/packages/server/tests/testApp.js

66 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-06-14 11:05:46 +02:00
const app = require("../app");
const { promisify } = require("util");
const rimraf = promisify(require("rimraf"));
const createMasterDb = require("../initialise/createMasterDb");
const request = require("supertest");
const fs = require("fs");
var enableDestroy = require('server-destroy');
const mkdir = promisify(fs.mkdir);
const masterOwnerName = "test_master";
const masterOwnerPassword = "test_master_pass";
const config = {
datastore: "local",
datastoreConfig: {
rootPath: "./tests/.data"
},
keys: ["secret1", "secret2"],
port: 4002
}
module.exports = () => {
let server;
return ({
start: async () => {
await reInitialize();
server = await app(config);
enableDestroy(server);
},
config,
server:() => server,
post: (url, body) => postRequest(server,url,body),
masterAuth: {
username: masterOwnerName,
password: masterOwnerPassword
}
})
};
const postRequest = (server, url, body) =>
request(app)
.post(url)
.send(body)
.set('Accept', 'application/json');
const reInitialize = async () => {
try {
await rimraf(config.datastoreConfig.rootPath);
} catch(_){}
await mkdir(config.datastoreConfig.rootPath);
const datastoreModule = require("../../datastores/datastores/" + config.datastore);
await createMasterDb(
datastoreModule,
config.datastoreConfig,
masterOwnerName,
masterOwnerPassword
);
}