budibase/packages/server/tests/createNewApp.js

135 lines
5.6 KiB
JavaScript
Raw Normal View History

2019-06-28 23:59:27 +02:00
const statusCodes = require("../utilities/statusCodes");
const constructHierarchy = require("../utilities/constructHierarchy");
const { readFile } = require("../utilities/fsawait");
const { hierarchy } = require("budibase-core");
const { take } = require("lodash/fp");
2019-06-28 23:59:27 +02:00
const masterAppDefinition = constructHierarchy(
require("../appPackages/master/appDefinition.json"));
const {getApisWithFullAccess} = require("../utilities/budibaseApi");
const { createTarGzPackage } = require("../utilities/targzAppPackage");
const { timeout } = require("./helpers");
2019-06-28 23:59:27 +02:00
module.exports = (app) => {
let _master;
const getmaster = async () => {
if(!_master)
_master = await getApisWithFullAccess({}, app.masterAppPackage);
return _master;
}
let newAppKey = "";
it("create new application should be successful for owner", async () => {
const master = await getmaster();
const newApp = master.recordApi.getNew("/applications", "application");
newApp.name = app.testAppInfo.name
newAppKey = newApp.key;
2019-07-11 10:43:47 +02:00
app.apps.testApp1.key = newAppKey;
2019-06-28 23:59:27 +02:00
await app.post(`/_master/api/record/${newApp.key}`, newApp)
2019-07-11 10:43:47 +02:00
.set("cookie", app.credentials.masterOwner.cookie)
2019-06-28 23:59:27 +02:00
.expect(statusCodes.OK);
const response = await app.get(`/_master/api/record/${newApp.key}`)
2019-07-11 10:43:47 +02:00
.set("cookie", app.credentials.masterOwner.cookie)
2019-06-28 23:59:27 +02:00
.expect(statusCodes.OK);
expect(response.body.name).toBe(newApp.name);
});
let version1Key = "";
it("should be able to upload new version including package files", async () => {
const master = await getmaster();
const version1 = master.recordApi
.getNew(`${newAppKey}/versions`, "version");
version1.name = "v1";
version1.defaultAccessLevel = "owner";
2019-06-28 23:59:27 +02:00
version1Key = version1.key;
const { path, size } = await createTarGzPackage(app.config, "testApp");
version1.package = { relativePath: "package.tar.gz", size};
await app.post(`/_master/api/record/${version1.key}`, version1)
2019-07-11 10:43:47 +02:00
.set("cookie", app.credentials.masterOwner.cookie)
2019-06-28 23:59:27 +02:00
.expect(statusCodes.OK);
await app.post(`/_master/api/files/${version1.key}`)
.attach("file", path, "package.tar.gz")
2019-07-11 10:43:47 +02:00
.set("cookie", app.credentials.masterOwner.cookie)
2019-06-28 23:59:27 +02:00
.expect(statusCodes.OK);
const savedAppResponse = await app.get(`/_master/api/record/${newAppKey}`)
.set("cookie", app.credentials.masterOwner.cookie);
const savedApp = savedAppResponse.body;
savedApp.defaultVersion = version1
await app.post(`/_master/api/record/${newAppKey}`, savedApp)
.set("cookie", app.credentials.masterOwner.cookie)
.expect(statusCodes.OK);
2019-07-11 10:43:47 +02:00
app.apps.testApp1.version1 = version1;
2019-06-28 23:59:27 +02:00
});
let instance1;
2019-06-28 23:59:27 +02:00
it("should be able to create new instance of app", async () => {
const master = await getmaster();
instance1 = master.recordApi
2019-06-28 23:59:27 +02:00
.getNew(`${newAppKey}/instances`, "instance");
instance1.name = "instance 1";
instance1.active = true;
2019-07-03 00:15:15 +02:00
instance1.version = {key:version1Key, name:"v1", defaultAccessLevel:"owner"};
2019-07-01 23:49:13 +02:00
await app.post(`/_master/api/record/${instance1.key}`, instance1)
2019-07-11 10:43:47 +02:00
.set("cookie", app.credentials.masterOwner.cookie)
2019-07-01 23:49:13 +02:00
.expect(statusCodes.OK);
2019-06-28 23:59:27 +02:00
const loadInstanceResponse = await app.get(`/_master/api/record/${instance1.key}`)
2019-07-11 10:43:47 +02:00
.set("cookie", app.credentials.masterOwner.cookie)
.expect(statusCodes.OK);
instance1 = loadInstanceResponse.body;
2019-07-11 10:43:47 +02:00
app.apps.testApp1.instance1 = instance1;
2019-07-03 00:15:15 +02:00
});
let user1_instance1;
it("should be able to create new user on instance, via master", async () => {
const master = await getmaster();
user1_instance1 = master.recordApi
.getNew(`${newAppKey}/users`, "user");
2019-07-11 10:43:47 +02:00
user1_instance1.name = app.credentials.testAppUser1.username;
2019-07-09 08:29:50 +02:00
user1_instance1.createdByMaster = true;
2019-07-11 08:35:29 +02:00
master.recordApi.setCustomId(user1_instance1, user1_instance1.name);
/*const lookupResponse = await app.get(`/_master/api/lookup_field/${user1_instance1.key}?fields=instance`)
2019-07-11 10:43:47 +02:00
.set("cookie", app.credentials.masterOwner.cookie)
.expect(statusCodes.OK);
*/
user1_instance1.instance = instance1;
user1_instance1.active = true;
//await timeout(100);
await app.post(`/_master/api/record/${user1_instance1.key}`, user1_instance1)
2019-07-11 10:43:47 +02:00
.set("cookie", app.credentials.masterOwner.cookie)
.expect(statusCodes.OK);
2019-06-28 23:59:27 +02:00
});
it("should be able to set password for new user using temporary code", async () => {
const testUserTempCode = await readFile(`./tests/.data/tempaccess${user1_instance1.name}`, "utf8");
2019-07-11 10:43:47 +02:00
user1_instance1.password = app.credentials.testAppUser1.password;
await app.post("/testApp/api/setPasswordFromTemporaryCode", {
2019-07-11 10:43:47 +02:00
username: app.credentials.testAppUser1.username,
tempCode:testUserTempCode,
2019-07-11 10:43:47 +02:00
newPassword:app.credentials.testAppUser1.password
})
.expect(statusCodes.OK);
2019-07-11 10:43:47 +02:00
const response = await app.post("/testApp/api/authenticate", {
username: app.credentials.testAppUser1.username,
password: app.credentials.testAppUser1.password
})
.expect(statusCodes.OK);
2019-07-07 10:03:37 +02:00
2019-07-11 10:43:47 +02:00
app.credentials.testAppUser1.cookie = response.header['set-cookie'];
})
2019-06-28 23:59:27 +02:00
}