2021-03-11 19:29:48 +01:00
|
|
|
const { BUILTIN_ROLE_IDS } = require("../../utilities/security/roles")
|
|
|
|
const env = require("../../environment")
|
2021-03-03 19:41:49 +01:00
|
|
|
const {
|
|
|
|
basicTable,
|
|
|
|
basicRow,
|
|
|
|
basicRole,
|
|
|
|
basicAutomation,
|
2021-03-04 11:05:50 +01:00
|
|
|
basicDatasource,
|
|
|
|
basicQuery,
|
2021-03-08 15:49:19 +01:00
|
|
|
basicScreen,
|
2021-03-09 12:56:32 +01:00
|
|
|
basicLayout,
|
2021-03-08 16:57:19 +01:00
|
|
|
basicWebhook,
|
2021-03-03 19:41:49 +01:00
|
|
|
} = require("./structures")
|
2021-03-04 11:05:50 +01:00
|
|
|
const controllers = require("./controllers")
|
2021-03-04 13:32:31 +01:00
|
|
|
const supertest = require("supertest")
|
2021-03-25 15:46:32 +01:00
|
|
|
const { cleanup } = require("../../utilities/fileSystem")
|
2021-04-21 17:42:44 +02:00
|
|
|
const { Cookies } = require("@budibase/auth").constants
|
2021-04-23 15:58:06 +02:00
|
|
|
const { jwt } = require("@budibase/auth").auth
|
2021-03-03 18:52:41 +01:00
|
|
|
|
|
|
|
const EMAIL = "babs@babs.com"
|
|
|
|
const PASSWORD = "babs_password"
|
|
|
|
|
|
|
|
class TestConfiguration {
|
2021-03-11 19:29:48 +01:00
|
|
|
constructor(openServer = true) {
|
|
|
|
if (openServer) {
|
|
|
|
env.PORT = 4002
|
|
|
|
this.server = require("../../app")
|
|
|
|
// we need the request for logging in, involves cookies, hard to fake
|
|
|
|
this.request = supertest(this.server)
|
|
|
|
}
|
2021-03-03 18:52:41 +01:00
|
|
|
this.appId = null
|
2021-03-11 13:09:47 +01:00
|
|
|
this.allApps = []
|
2021-03-03 18:52:41 +01:00
|
|
|
}
|
|
|
|
|
2021-03-05 13:11:44 +01:00
|
|
|
getRequest() {
|
|
|
|
return this.request
|
|
|
|
}
|
|
|
|
|
|
|
|
getAppId() {
|
|
|
|
return this.appId
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:52:41 +01:00
|
|
|
async _req(config, params, controlFunc) {
|
|
|
|
const request = {}
|
|
|
|
// fake cookies, we don't need them
|
|
|
|
request.cookies = { set: () => {}, get: () => {} }
|
|
|
|
request.config = { jwtSecret: env.JWT_SECRET }
|
|
|
|
request.appId = this.appId
|
|
|
|
request.user = { appId: this.appId }
|
|
|
|
request.request = {
|
|
|
|
body: config,
|
|
|
|
}
|
|
|
|
if (params) {
|
|
|
|
request.params = params
|
|
|
|
}
|
|
|
|
await controlFunc(request)
|
|
|
|
return request.body
|
|
|
|
}
|
|
|
|
|
|
|
|
async init(appName = "test_application") {
|
|
|
|
return this.createApp(appName)
|
|
|
|
}
|
|
|
|
|
2021-03-04 13:32:31 +01:00
|
|
|
end() {
|
2021-03-11 19:29:48 +01:00
|
|
|
if (this.server) {
|
|
|
|
this.server.close()
|
|
|
|
}
|
2021-03-25 15:46:32 +01:00
|
|
|
cleanup(this.allApps.map(app => app._id))
|
2021-03-04 13:32:31 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 11:40:27 +01:00
|
|
|
defaultHeaders() {
|
2021-04-13 19:12:35 +02:00
|
|
|
const user = {
|
2021-04-19 17:26:33 +02:00
|
|
|
userId: "ro_ta_user_us_uuid1",
|
2021-04-13 19:12:35 +02:00
|
|
|
builder: {
|
|
|
|
global: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
const app = {
|
|
|
|
roleId: BUILTIN_ROLE_IDS.BUILDER,
|
|
|
|
appId: this.appId,
|
2021-03-04 11:40:27 +01:00
|
|
|
}
|
2021-04-13 19:12:35 +02:00
|
|
|
const authToken = jwt.sign(user, env.JWT_SECRET)
|
|
|
|
const appToken = jwt.sign(app, env.JWT_SECRET)
|
2021-03-04 11:40:27 +01:00
|
|
|
const headers = {
|
|
|
|
Accept: "application/json",
|
2021-04-13 19:12:35 +02:00
|
|
|
Cookie: [
|
|
|
|
`${Cookies.Auth}=${authToken}`,
|
|
|
|
`${Cookies.CurrentApp}=${appToken}`,
|
|
|
|
],
|
2021-03-04 11:40:27 +01:00
|
|
|
}
|
|
|
|
if (this.appId) {
|
|
|
|
headers["x-budibase-app-id"] = this.appId
|
|
|
|
}
|
|
|
|
return headers
|
|
|
|
}
|
|
|
|
|
|
|
|
publicHeaders() {
|
|
|
|
const headers = {
|
|
|
|
Accept: "application/json",
|
|
|
|
}
|
|
|
|
if (this.appId) {
|
|
|
|
headers["x-budibase-app-id"] = this.appId
|
|
|
|
}
|
|
|
|
return headers
|
|
|
|
}
|
|
|
|
|
2021-03-08 19:03:26 +01:00
|
|
|
async roleHeaders(email = EMAIL, roleId = BUILTIN_ROLE_IDS.ADMIN) {
|
2021-04-19 17:26:33 +02:00
|
|
|
let user
|
2021-03-08 15:49:19 +01:00
|
|
|
try {
|
2021-04-19 17:26:33 +02:00
|
|
|
user = await this.createUser(email, PASSWORD, roleId)
|
2021-03-08 15:49:19 +01:00
|
|
|
} catch (err) {
|
|
|
|
// allow errors here
|
|
|
|
}
|
2021-04-19 17:26:33 +02:00
|
|
|
return this.login(email, PASSWORD, { roleId, userId: user._id })
|
2021-03-08 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2021-03-03 18:52:41 +01:00
|
|
|
async createApp(appName) {
|
2021-03-04 11:05:50 +01:00
|
|
|
this.app = await this._req({ name: appName }, null, controllers.app.create)
|
2021-03-03 18:52:41 +01:00
|
|
|
this.appId = this.app._id
|
2021-03-11 13:09:47 +01:00
|
|
|
this.allApps.push(this.app)
|
2021-03-03 18:52:41 +01:00
|
|
|
return this.app
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateTable(config = null) {
|
|
|
|
config = config || basicTable()
|
2021-03-04 11:05:50 +01:00
|
|
|
this.table = await this._req(config, null, controllers.table.save)
|
2021-03-03 18:52:41 +01:00
|
|
|
return this.table
|
|
|
|
}
|
|
|
|
|
|
|
|
async createTable(config = null) {
|
|
|
|
if (config != null && config._id) {
|
|
|
|
delete config._id
|
|
|
|
}
|
|
|
|
return this.updateTable(config)
|
|
|
|
}
|
|
|
|
|
2021-03-04 15:36:59 +01:00
|
|
|
async getTable(tableId = null) {
|
|
|
|
tableId = tableId || this.table._id
|
|
|
|
return this._req(null, { id: tableId }, controllers.table.find)
|
|
|
|
}
|
|
|
|
|
2021-03-17 00:25:18 +01:00
|
|
|
async createLinkedTable(relationshipType = null, links = ["link"]) {
|
2021-03-04 14:07:33 +01:00
|
|
|
if (!this.table) {
|
|
|
|
throw "Must have created a table first."
|
|
|
|
}
|
|
|
|
const tableConfig = basicTable()
|
|
|
|
tableConfig.primaryDisplay = "name"
|
2021-03-16 19:13:00 +01:00
|
|
|
for (let link of links) {
|
|
|
|
tableConfig.schema[link] = {
|
|
|
|
type: "link",
|
|
|
|
fieldName: link,
|
|
|
|
tableId: this.table._id,
|
|
|
|
name: link,
|
|
|
|
}
|
|
|
|
if (relationshipType) {
|
|
|
|
tableConfig.schema[link].relationshipType = relationshipType
|
|
|
|
}
|
2021-03-03 18:52:41 +01:00
|
|
|
}
|
2021-03-04 14:07:33 +01:00
|
|
|
const linkedTable = await this.createTable(tableConfig)
|
2021-03-03 18:52:41 +01:00
|
|
|
this.linkedTable = linkedTable
|
|
|
|
return linkedTable
|
|
|
|
}
|
|
|
|
|
|
|
|
async createAttachmentTable() {
|
|
|
|
const table = basicTable()
|
|
|
|
table.schema.attachment = {
|
|
|
|
type: "attachment",
|
|
|
|
}
|
|
|
|
return this.createTable(table)
|
|
|
|
}
|
|
|
|
|
|
|
|
async createRow(config = null) {
|
|
|
|
if (!this.table) {
|
|
|
|
throw "Test requires table to be configured."
|
|
|
|
}
|
2021-03-16 19:13:00 +01:00
|
|
|
const tableId = (config && config.tableId) || this.table._id
|
|
|
|
config = config || basicRow(tableId)
|
|
|
|
return this._req(config, { tableId }, controllers.row.save)
|
2021-03-03 18:52:41 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 19:29:48 +01:00
|
|
|
async getRow(tableId, rowId) {
|
|
|
|
return this._req(null, { tableId, rowId }, controllers.row.find)
|
|
|
|
}
|
|
|
|
|
2021-03-15 17:36:38 +01:00
|
|
|
async getRows(tableId) {
|
|
|
|
if (!tableId && this.table) {
|
|
|
|
tableId = this.table._id
|
|
|
|
}
|
|
|
|
return this._req(null, { tableId }, controllers.row.fetchTableRows)
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:52:41 +01:00
|
|
|
async createRole(config = null) {
|
|
|
|
config = config || basicRole()
|
2021-03-04 11:05:50 +01:00
|
|
|
return this._req(config, null, controllers.role.save)
|
2021-03-03 18:52:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async addPermission(roleId, resourceId, level = "read") {
|
|
|
|
return this._req(
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
roleId,
|
|
|
|
resourceId,
|
|
|
|
level,
|
|
|
|
},
|
2021-03-04 11:05:50 +01:00
|
|
|
controllers.perms.addPermission
|
2021-03-03 18:52:41 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async createView(config) {
|
|
|
|
if (!this.table) {
|
|
|
|
throw "Test requires table to be configured."
|
|
|
|
}
|
|
|
|
const view = config || {
|
|
|
|
map: "function(doc) { emit(doc[doc.key], doc._id); } ",
|
|
|
|
tableId: this.table._id,
|
2021-03-15 17:36:38 +01:00
|
|
|
name: "ViewTest",
|
2021-03-03 18:52:41 +01:00
|
|
|
}
|
2021-03-04 11:05:50 +01:00
|
|
|
return this._req(view, null, controllers.view.save)
|
2021-03-03 18:52:41 +01:00
|
|
|
}
|
|
|
|
|
2021-03-03 19:41:49 +01:00
|
|
|
async createAutomation(config) {
|
|
|
|
config = config || basicAutomation()
|
|
|
|
if (config._rev) {
|
|
|
|
delete config._rev
|
|
|
|
}
|
|
|
|
this.automation = (
|
2021-03-04 11:05:50 +01:00
|
|
|
await this._req(config, null, controllers.automation.create)
|
2021-03-03 19:41:49 +01:00
|
|
|
).automation
|
|
|
|
return this.automation
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAllAutomations() {
|
2021-03-04 11:05:50 +01:00
|
|
|
return this._req(null, null, controllers.automation.fetch)
|
2021-03-03 19:41:49 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 11:05:50 +01:00
|
|
|
async deleteAutomation(automation = null) {
|
2021-03-03 19:41:49 +01:00
|
|
|
automation = automation || this.automation
|
|
|
|
if (!automation) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return this._req(
|
|
|
|
null,
|
|
|
|
{ id: automation._id, rev: automation._rev },
|
2021-03-04 11:05:50 +01:00
|
|
|
controllers.automation.destroy
|
2021-03-03 19:41:49 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-04 11:05:50 +01:00
|
|
|
async createDatasource(config = null) {
|
|
|
|
config = config || basicDatasource()
|
|
|
|
this.datasource = await this._req(config, null, controllers.datasource.save)
|
|
|
|
return this.datasource
|
|
|
|
}
|
|
|
|
|
|
|
|
async createQuery(config = null) {
|
|
|
|
if (!this.datasource && !config) {
|
|
|
|
throw "No data source created for query."
|
|
|
|
}
|
|
|
|
config = config || basicQuery(this.datasource._id)
|
|
|
|
return this._req(config, null, controllers.query.save)
|
|
|
|
}
|
|
|
|
|
2021-03-08 15:49:19 +01:00
|
|
|
async createScreen(config = null) {
|
|
|
|
config = config || basicScreen()
|
|
|
|
return this._req(config, null, controllers.screen.save)
|
|
|
|
}
|
|
|
|
|
2021-03-08 16:57:19 +01:00
|
|
|
async createWebhook(config = null) {
|
|
|
|
if (!this.automation) {
|
|
|
|
throw "Must create an automation before creating webhook."
|
|
|
|
}
|
|
|
|
config = config || basicWebhook(this.automation._id)
|
|
|
|
return (await this._req(config, null, controllers.webhook.save)).webhook
|
|
|
|
}
|
|
|
|
|
2021-03-09 12:56:32 +01:00
|
|
|
async createLayout(config = null) {
|
|
|
|
config = config || basicLayout()
|
|
|
|
return await this._req(config, null, controllers.layout.save)
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:52:41 +01:00
|
|
|
async createUser(
|
|
|
|
email = EMAIL,
|
|
|
|
password = PASSWORD,
|
2021-03-10 13:56:30 +01:00
|
|
|
roleId = BUILTIN_ROLE_IDS.POWER
|
2021-03-03 18:52:41 +01:00
|
|
|
) {
|
|
|
|
return this._req(
|
|
|
|
{
|
|
|
|
email,
|
|
|
|
password,
|
|
|
|
roleId,
|
|
|
|
},
|
|
|
|
null,
|
2021-04-08 17:58:33 +02:00
|
|
|
controllers.user.createMetadata
|
2021-03-03 18:52:41 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:26:33 +02:00
|
|
|
async login(email, password, { roleId, userId } = {}) {
|
|
|
|
if (!roleId) {
|
|
|
|
roleId = BUILTIN_ROLE_IDS.BUILDER
|
|
|
|
}
|
2021-03-11 19:29:48 +01:00
|
|
|
if (!this.request) {
|
|
|
|
throw "Server has not been opened, cannot login."
|
|
|
|
}
|
2021-03-03 18:52:41 +01:00
|
|
|
if (!email || !password) {
|
|
|
|
await this.createUser()
|
|
|
|
}
|
2021-04-19 17:26:33 +02:00
|
|
|
// have to fake this
|
2021-04-13 19:12:35 +02:00
|
|
|
const user = {
|
2021-04-20 18:27:23 +02:00
|
|
|
userId: userId || `us_uuid1`,
|
2021-04-13 21:25:43 +02:00
|
|
|
email: email || EMAIL,
|
|
|
|
}
|
|
|
|
const app = {
|
|
|
|
roleId: roleId,
|
|
|
|
appId: this.appId,
|
2021-04-13 19:12:35 +02:00
|
|
|
}
|
2021-04-13 21:25:43 +02:00
|
|
|
const authToken = jwt.sign(user, env.JWT_SECRET)
|
|
|
|
const appToken = jwt.sign(app, env.JWT_SECRET)
|
2021-03-03 18:52:41 +01:00
|
|
|
|
|
|
|
// returning necessary request headers
|
|
|
|
return {
|
|
|
|
Accept: "application/json",
|
2021-04-13 21:25:43 +02:00
|
|
|
Cookie: [
|
|
|
|
`${Cookies.Auth}=${authToken}`,
|
|
|
|
`${Cookies.CurrentApp}=${appToken}`,
|
|
|
|
],
|
2021-03-08 15:49:19 +01:00
|
|
|
"x-budibase-app-id": this.appId,
|
2021-03-03 18:52:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = TestConfiguration
|