budibase/packages/server/tests/testApp.js

76 lines
1.8 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 () => {
2019-06-19 23:05:53 +02:00
try {
await reInitialize();
server = await app(config);
} catch(e) {
console.log(e.message);
}
2019-06-14 11:05:46 +02:00
enableDestroy(server);
},
config,
server:() => server,
post: (url, body) => postRequest(server,url,body),
2019-06-15 00:03:01 +02:00
get: (url) => getRequest(server, url),
2019-06-14 11:05:46 +02:00
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');
2019-06-15 00:03:01 +02:00
const getRequest = (server, url) =>
request(server)
.get(url)
.set('Accept', 'application/json');
2019-06-14 11:05:46 +02:00
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
);
}