budibase/packages/server/tests/testApp.js

67 lines
1.6 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
2019-06-14 18:01:01 +02:00
},
destroy: () => server.destroy()
2019-06-14 11:05:46 +02:00
})
};
const postRequest = (server, url, body) =>
2019-06-14 18:01:01 +02:00
request(server)
2019-06-14 11:05:46 +02:00
.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
);
}