budibase/packages/server/tests/testApp.js

154 lines
4.2 KiB
JavaScript
Raw Normal View History

2019-06-14 11:05:46 +02:00
const app = require("../app");
2019-07-26 18:08:59 +02:00
const { remove, mkdir } = require("fs-extra");
2019-06-14 11:05:46 +02:00
const createMasterDb = require("../initialise/createMasterDb");
const request = require("supertest");
const fs = require("fs");
2019-07-11 10:43:47 +02:00
const { masterAppPackage, applictionVersionPackage } = require("../utilities/createAppPackage");
2019-07-09 08:29:50 +02:00
const buildAppContext = require("../initialise/buildAppContext");
2019-06-14 11:05:46 +02:00
var enableDestroy = require('server-destroy');
const masterOwnerName = "test_master";
const masterOwnerPassword = "test_master_pass";
2019-06-28 23:59:27 +02:00
const extraMasterPlugins = {
2019-07-01 23:49:13 +02:00
test_plugins: {
2019-06-25 23:48:22 +02:00
outputToFile : ({filename, content}) => {
2019-06-26 23:29:28 +02:00
fs.writeFile(`./tests/.data/${filename}`, content, {encoding:"utf8"});
2019-06-25 23:48:22 +02:00
}
}
}
const customizeMaster = appDefinition => {
appDefinition.actions.outputToFile = {
name: 'outputToFile',
2019-07-01 23:49:13 +02:00
behaviourSource: 'test_plugins',
2019-06-25 23:48:22 +02:00
behaviourName: 'outputToFile',
initialOptions: {}
};
appDefinition.triggers.push({
actionName: 'outputToFile',
eventName: 'authApi:createTemporaryAccess:onComplete',
optionsCreator: 'return ({filename:"tempaccess" + context.userName, content:context.result})',
condition: ''
});
return appDefinition;
}
2019-06-14 11:05:46 +02:00
const config = {
datastore: "local",
datastoreConfig: {
rootPath: "./tests/.data"
},
keys: ["secret1", "secret2"],
2019-06-25 23:48:22 +02:00
port: 4002,
2019-07-13 11:35:57 +02:00
latestPackagesFolder: "./appPackages",
2019-06-28 23:59:27 +02:00
extraMasterPlugins,
customizeMaster,
dev:true
2019-06-14 11:05:46 +02:00
}
module.exports = () => {
let server;
return ({
start: async () => {
2019-06-19 23:05:53 +02:00
try {
await reInitialize();
2019-07-09 08:29:50 +02:00
const budibaseContext = await buildAppContext(config, true);
server = await app(budibaseContext);
2019-06-19 23:05:53 +02:00
} catch(e) {
2019-07-13 11:35:57 +02:00
console.log(e.stack);
2019-06-19 23:05:53 +02:00
}
2019-06-14 11:05:46 +02:00
enableDestroy(server);
},
config,
server:() => server,
post: (url, body) => postRequest(server,url,body),
2019-07-27 08:31:31 +02:00
patch: (url, body) => patchRequest(server,url,body),
2019-06-15 00:03:01 +02:00
get: (url) => getRequest(server, url),
2019-07-27 08:43:03 +02:00
delete: (url) => deleteRequest(server, url),
2019-07-07 10:03:37 +02:00
credentials: {
2019-07-11 10:43:47 +02:00
masterOwner: {
2019-07-07 10:03:37 +02:00
username: masterOwnerName,
password: masterOwnerPassword,
cookie: ""
},
2019-07-11 10:43:47 +02:00
testAppUser1: {
2019-07-07 10:03:37 +02:00
username: "testAppUser1",
password: "user1_instance1_password",
cookie: ""
2019-07-11 10:43:47 +02:00
},
testAppUser2: {
username: "testAppUser2",
password: "user1_instance2_password",
cookie: ""
2019-07-07 10:03:37 +02:00
}
2019-06-28 23:59:27 +02:00
},
2019-07-11 10:43:47 +02:00
apps: {
testApp1: {
key:null,
instance1:null,
instance2:null,
version1:null,
}
},
2019-06-28 23:59:27 +02:00
testAppInfo: {
name: "testApp"
2019-06-14 18:01:01 +02:00
},
2019-06-28 23:59:27 +02:00
destroy: () => server.destroy(),
2019-07-11 10:43:47 +02:00
masterAppPackage: masterAppPackage({ config }),
testAppInstance1AppPackage: async (app) => applictionVersionPackage(
await buildAppContext(config, true),
"testApp",
app.apps.testApp1.instance1.version.id,
app.apps.testApp1.instance1.key)
2019-06-14 11:05:46 +02:00
})
};
2019-07-27 08:31:31 +02:00
const patchRequest = (server, url, body) =>
request(server)
.patch(url)
.send(body)
.set('Accept', 'application/json');
2019-06-28 23:59:27 +02:00
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-07-27 08:43:03 +02:00
const deleteRequest = (server, url) =>
request(server)
.delete(url)
.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 {
2019-07-26 18:08:59 +02:00
await remove(config.datastoreConfig.rootPath);
2019-06-14 11:05:46 +02:00
} catch(_){}
await mkdir(config.datastoreConfig.rootPath);
const datastoreModule = require("../../datastores/datastores/" + config.datastore);
2019-07-09 08:29:50 +02:00
const budibaseContext = await buildAppContext(config, false);
2019-06-14 11:05:46 +02:00
await createMasterDb(
2019-07-09 08:29:50 +02:00
budibaseContext,
2019-06-14 11:05:46 +02:00
datastoreModule,
masterOwnerName,
2019-07-09 08:29:50 +02:00
masterOwnerPassword
2019-06-14 11:05:46 +02:00
);
}
2019-07-09 08:29:50 +02:00