2022-03-29 17:03:44 +02:00
|
|
|
require("../../db").init()
|
2022-01-10 20:33:00 +01:00
|
|
|
const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles")
|
2021-03-11 19:29:48 +01:00
|
|
|
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-08-05 10:59:08 +02:00
|
|
|
TENANT_ID,
|
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")
|
2022-01-12 12:32:14 +01:00
|
|
|
const { Cookies, Headers } = require("@budibase/backend-core/constants")
|
|
|
|
const { jwt } = require("@budibase/backend-core/auth")
|
2022-04-21 15:56:14 +02:00
|
|
|
const { doInTenant, doWithGlobalDB } = require("@budibase/backend-core/tenancy")
|
2022-01-10 20:33:00 +01:00
|
|
|
const { createASession } = require("@budibase/backend-core/sessions")
|
|
|
|
const { user: userCache } = require("@budibase/backend-core/cache")
|
2022-01-21 18:31:18 +01:00
|
|
|
const newid = require("../../db/newid")
|
2022-01-27 19:18:31 +01:00
|
|
|
const context = require("@budibase/backend-core/context")
|
2022-02-25 00:21:10 +01:00
|
|
|
const { generateDevInfoID, SEPARATOR } = require("@budibase/backend-core/db")
|
|
|
|
const { encrypt } = require("@budibase/backend-core/encryption")
|
2022-08-11 14:50:05 +02:00
|
|
|
const { DocumentType } = require("../../db/utils")
|
2021-03-03 18:52:41 +01:00
|
|
|
|
2021-04-23 19:07:39 +02:00
|
|
|
const GLOBAL_USER_ID = "us_uuid1"
|
2021-03-03 18:52:41 +01:00
|
|
|
const EMAIL = "babs@babs.com"
|
2022-06-17 13:00:42 +02:00
|
|
|
const FIRSTNAME = "Barbara"
|
|
|
|
const LASTNAME = "Barbington"
|
2022-01-25 23:54:50 +01:00
|
|
|
const CSRF_TOKEN = "e3727778-7af0-4226-b5eb-f43cbe60a306"
|
2021-03-03 18:52:41 +01:00
|
|
|
|
|
|
|
class TestConfiguration {
|
2021-03-11 19:29:48 +01:00
|
|
|
constructor(openServer = true) {
|
|
|
|
if (openServer) {
|
2021-06-25 17:14:06 +02:00
|
|
|
// use a random port because it doesn't matter
|
|
|
|
env.PORT = 0
|
2021-03-11 19:29:48 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-25 16:55:19 +01:00
|
|
|
getApp() {
|
|
|
|
return this.app
|
|
|
|
}
|
|
|
|
|
2022-08-10 12:01:54 +02:00
|
|
|
getProdApp() {
|
|
|
|
return this.prodApp
|
|
|
|
}
|
|
|
|
|
2021-03-05 13:11:44 +01:00
|
|
|
getAppId() {
|
|
|
|
return this.appId
|
|
|
|
}
|
|
|
|
|
2022-04-04 16:59:00 +02:00
|
|
|
getProdAppId() {
|
|
|
|
return this.prodAppId
|
|
|
|
}
|
|
|
|
|
2022-06-17 13:00:42 +02:00
|
|
|
getUserDetails() {
|
|
|
|
return {
|
|
|
|
globalId: GLOBAL_USER_ID,
|
|
|
|
email: EMAIL,
|
|
|
|
firstName: FIRSTNAME,
|
|
|
|
lastName: LASTNAME,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 15:56:14 +02:00
|
|
|
async doInContext(appId, task) {
|
|
|
|
if (!appId) {
|
|
|
|
appId = this.appId
|
|
|
|
}
|
|
|
|
return doInTenant(TENANT_ID, () => {
|
|
|
|
// check if already in a context
|
|
|
|
if (context.getAppId() == null && appId !== null) {
|
|
|
|
return context.doInAppContext(appId, async () => {
|
|
|
|
return task()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return task()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:56 +02:00
|
|
|
// SETUP / TEARDOWN
|
|
|
|
|
|
|
|
// use a new id as the name to avoid name collisions
|
|
|
|
async init(appName = newid()) {
|
|
|
|
await this.globalUser()
|
|
|
|
return this.createApp(appName)
|
|
|
|
}
|
|
|
|
|
|
|
|
end() {
|
|
|
|
if (!this) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (this.server) {
|
|
|
|
this.server.close()
|
|
|
|
}
|
|
|
|
cleanup(this.allApps.map(app => app.appId))
|
|
|
|
}
|
|
|
|
|
|
|
|
// UTILS
|
|
|
|
|
2022-08-10 12:01:54 +02:00
|
|
|
async _req(body, params, controlFunc) {
|
2022-07-13 14:22:21 +02:00
|
|
|
// create a fake request ctx
|
2021-03-03 18:52:41 +01:00
|
|
|
const request = {}
|
2022-08-10 12:01:54 +02:00
|
|
|
const appId = this.appId
|
2022-07-13 14:22:21 +02:00
|
|
|
request.appId = appId
|
2021-03-03 18:52:41 +01:00
|
|
|
// fake cookies, we don't need them
|
|
|
|
request.cookies = { set: () => {}, get: () => {} }
|
|
|
|
request.config = { jwtSecret: env.JWT_SECRET }
|
2022-07-13 14:22:21 +02:00
|
|
|
request.user = { appId, tenantId: TENANT_ID }
|
2021-04-26 15:14:51 +02:00
|
|
|
request.query = {}
|
2021-03-03 18:52:41 +01:00
|
|
|
request.request = {
|
2022-07-13 14:22:21 +02:00
|
|
|
body,
|
2021-03-03 18:52:41 +01:00
|
|
|
}
|
2022-07-13 14:22:21 +02:00
|
|
|
if (params) {
|
|
|
|
request.params = params
|
|
|
|
}
|
|
|
|
return this.doInContext(appId, async () => {
|
2022-01-28 16:43:51 +01:00
|
|
|
await controlFunc(request)
|
|
|
|
return request.body
|
2022-04-21 15:56:14 +02:00
|
|
|
})
|
2021-03-03 18:52:41 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:56 +02:00
|
|
|
// USER / AUTH
|
2021-10-25 17:59:09 +02:00
|
|
|
async globalUser({
|
|
|
|
id = GLOBAL_USER_ID,
|
2022-06-17 13:00:42 +02:00
|
|
|
firstName = FIRSTNAME,
|
|
|
|
lastName = LASTNAME,
|
2021-10-25 17:59:09 +02:00
|
|
|
builder = true,
|
2022-05-20 22:16:29 +02:00
|
|
|
admin = false,
|
2021-10-25 17:59:09 +02:00
|
|
|
email = EMAIL,
|
|
|
|
roles,
|
|
|
|
} = {}) {
|
2022-04-19 20:42:52 +02:00
|
|
|
return doWithGlobalDB(TENANT_ID, async db => {
|
|
|
|
let existing
|
|
|
|
try {
|
|
|
|
existing = await db.get(id)
|
|
|
|
} catch (err) {
|
|
|
|
existing = { email }
|
|
|
|
}
|
|
|
|
const user = {
|
|
|
|
_id: id,
|
|
|
|
...existing,
|
|
|
|
roles: roles || {},
|
|
|
|
tenantId: TENANT_ID,
|
2022-06-17 13:00:42 +02:00
|
|
|
firstName,
|
|
|
|
lastName,
|
2022-04-19 20:42:52 +02:00
|
|
|
}
|
|
|
|
await createASession(id, {
|
|
|
|
sessionId: "sessionid",
|
|
|
|
tenantId: TENANT_ID,
|
|
|
|
csrfToken: CSRF_TOKEN,
|
|
|
|
})
|
|
|
|
if (builder) {
|
|
|
|
user.builder = { global: true }
|
|
|
|
} else {
|
|
|
|
user.builder = { global: false }
|
|
|
|
}
|
2022-05-20 22:16:29 +02:00
|
|
|
if (admin) {
|
|
|
|
user.admin = { global: true }
|
|
|
|
} else {
|
|
|
|
user.admin = { global: false }
|
|
|
|
}
|
2022-04-19 20:42:52 +02:00
|
|
|
const resp = await db.put(user)
|
|
|
|
return {
|
|
|
|
_rev: resp._rev,
|
|
|
|
...user,
|
|
|
|
}
|
2022-01-25 23:54:50 +01:00
|
|
|
})
|
2021-04-23 19:07:39 +02:00
|
|
|
}
|
|
|
|
|
2022-05-23 11:29:33 +02:00
|
|
|
async createUser(
|
|
|
|
id = null,
|
2022-06-17 13:00:42 +02:00
|
|
|
firstName = FIRSTNAME,
|
|
|
|
lastName = LASTNAME,
|
2022-05-23 11:29:33 +02:00
|
|
|
email = EMAIL,
|
|
|
|
builder = true,
|
|
|
|
admin = false,
|
|
|
|
roles = {}
|
|
|
|
) {
|
2022-04-06 17:57:56 +02:00
|
|
|
const globalId = !id ? `us_${Math.random()}` : `us_${id}`
|
2022-05-20 22:16:29 +02:00
|
|
|
const resp = await this.globalUser({
|
|
|
|
id: globalId,
|
2022-06-17 13:00:42 +02:00
|
|
|
firstName,
|
|
|
|
lastName,
|
2022-05-20 22:16:29 +02:00
|
|
|
email,
|
|
|
|
builder,
|
|
|
|
admin,
|
|
|
|
roles,
|
|
|
|
})
|
2022-04-06 17:57:56 +02:00
|
|
|
await userCache.invalidateUser(globalId)
|
|
|
|
return {
|
|
|
|
...resp,
|
|
|
|
globalId,
|
|
|
|
}
|
2021-03-03 18:52:41 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:56 +02:00
|
|
|
async login({ roleId, userId, builder, prodApp = false } = {}) {
|
|
|
|
const appId = prodApp ? this.prodAppId : this.appId
|
|
|
|
return context.doInAppContext(appId, async () => {
|
|
|
|
userId = !userId ? `us_uuid1` : userId
|
|
|
|
if (!this.request) {
|
|
|
|
throw "Server has not been opened, cannot login."
|
|
|
|
}
|
|
|
|
// make sure the user exists in the global DB
|
|
|
|
if (roleId !== BUILTIN_ROLE_IDS.PUBLIC) {
|
|
|
|
await this.globalUser({
|
|
|
|
id: userId,
|
|
|
|
builder,
|
|
|
|
roles: { [this.prodAppId]: roleId },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
await createASession(userId, {
|
|
|
|
sessionId: "sessionid",
|
|
|
|
tenantId: TENANT_ID,
|
|
|
|
})
|
|
|
|
// have to fake this
|
|
|
|
const auth = {
|
|
|
|
userId,
|
|
|
|
sessionId: "sessionid",
|
|
|
|
tenantId: TENANT_ID,
|
|
|
|
}
|
|
|
|
const app = {
|
|
|
|
roleId: roleId,
|
|
|
|
appId,
|
|
|
|
}
|
|
|
|
const authToken = jwt.sign(auth, env.JWT_SECRET)
|
|
|
|
const appToken = jwt.sign(app, env.JWT_SECRET)
|
|
|
|
|
|
|
|
// returning necessary request headers
|
|
|
|
await userCache.invalidateUser(userId)
|
|
|
|
return {
|
|
|
|
Accept: "application/json",
|
|
|
|
Cookie: [
|
|
|
|
`${Cookies.Auth}=${authToken}`,
|
|
|
|
`${Cookies.CurrentApp}=${appToken}`,
|
|
|
|
],
|
|
|
|
[Headers.APP_ID]: appId,
|
|
|
|
}
|
|
|
|
})
|
2021-03-04 13:32:31 +01:00
|
|
|
}
|
|
|
|
|
2022-02-25 00:21:10 +01:00
|
|
|
defaultHeaders(extras = {}) {
|
2021-04-23 19:07:39 +02:00
|
|
|
const auth = {
|
|
|
|
userId: GLOBAL_USER_ID,
|
2021-07-08 01:30:55 +02:00
|
|
|
sessionId: "sessionid",
|
2021-08-05 10:59:08 +02:00
|
|
|
tenantId: TENANT_ID,
|
2021-04-13 19:12:35 +02:00
|
|
|
}
|
|
|
|
const app = {
|
2021-06-04 13:13:29 +02:00
|
|
|
roleId: BUILTIN_ROLE_IDS.ADMIN,
|
2021-04-13 19:12:35 +02:00
|
|
|
appId: this.appId,
|
2021-03-04 11:40:27 +01:00
|
|
|
}
|
2021-04-23 19:07:39 +02:00
|
|
|
const authToken = jwt.sign(auth, env.JWT_SECRET)
|
2021-04-13 19:12:35 +02:00
|
|
|
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}`,
|
|
|
|
],
|
2022-01-25 23:54:50 +01:00
|
|
|
[Headers.CSRF_TOKEN]: CSRF_TOKEN,
|
2022-02-25 00:21:10 +01:00
|
|
|
...extras,
|
2021-03-04 11:40:27 +01:00
|
|
|
}
|
|
|
|
if (this.appId) {
|
2021-07-23 16:29:14 +02:00
|
|
|
headers[Headers.APP_ID] = this.appId
|
2021-03-04 11:40:27 +01:00
|
|
|
}
|
|
|
|
return headers
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:59:09 +02:00
|
|
|
publicHeaders({ prodApp = true } = {}) {
|
|
|
|
const appId = prodApp ? this.prodAppId : this.appId
|
|
|
|
|
2021-03-04 11:40:27 +01:00
|
|
|
const headers = {
|
|
|
|
Accept: "application/json",
|
|
|
|
}
|
2021-10-26 17:21:26 +02:00
|
|
|
if (appId) {
|
2021-10-25 17:59:09 +02:00
|
|
|
headers[Headers.APP_ID] = appId
|
2021-03-04 11:40:27 +01:00
|
|
|
}
|
|
|
|
return headers
|
|
|
|
}
|
|
|
|
|
2021-04-23 19:07:39 +02:00
|
|
|
async roleHeaders({
|
|
|
|
email = EMAIL,
|
|
|
|
roleId = BUILTIN_ROLE_IDS.ADMIN,
|
|
|
|
builder = false,
|
2021-10-25 17:59:09 +02:00
|
|
|
prodApp = true,
|
|
|
|
} = {}) {
|
|
|
|
return this.login({ email, roleId, builder, prodApp })
|
2021-03-08 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:56 +02:00
|
|
|
// API
|
|
|
|
|
|
|
|
async generateApiKey(userId = GLOBAL_USER_ID) {
|
2022-04-27 17:32:00 +02:00
|
|
|
return doWithGlobalDB(TENANT_ID, async db => {
|
|
|
|
const id = generateDevInfoID(userId)
|
|
|
|
let devInfo
|
|
|
|
try {
|
|
|
|
devInfo = await db.get(id)
|
|
|
|
} catch (err) {
|
|
|
|
devInfo = { _id: id, userId }
|
|
|
|
}
|
|
|
|
devInfo.apiKey = encrypt(`${TENANT_ID}${SEPARATOR}${newid()}`)
|
|
|
|
await db.put(devInfo)
|
|
|
|
return devInfo.apiKey
|
|
|
|
})
|
2022-04-06 17:57:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// APP
|
|
|
|
|
2022-05-20 22:16:29 +02:00
|
|
|
async createApp(appName) {
|
2021-10-25 17:59:09 +02:00
|
|
|
// create dev app
|
2022-04-21 15:56:14 +02:00
|
|
|
// clear any old app
|
|
|
|
this.appId = null
|
|
|
|
await context.updateAppId(null)
|
2021-03-04 11:05:50 +01:00
|
|
|
this.app = await this._req({ name: appName }, null, controllers.app.create)
|
2021-05-16 22:25:37 +02:00
|
|
|
this.appId = this.app.appId
|
2022-04-21 00:10:39 +02:00
|
|
|
await context.updateAppId(this.appId)
|
2021-10-25 17:59:09 +02:00
|
|
|
|
|
|
|
// create production app
|
2022-05-20 22:16:29 +02:00
|
|
|
this.prodApp = await this.deploy()
|
2021-10-25 17:59:09 +02:00
|
|
|
|
|
|
|
this.allApps.push(this.prodApp)
|
2021-03-11 13:09:47 +01:00
|
|
|
this.allApps.push(this.app)
|
2021-10-25 17:59:09 +02:00
|
|
|
|
2021-03-03 18:52:41 +01:00
|
|
|
return this.app
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:59:09 +02:00
|
|
|
async deploy() {
|
2022-01-28 01:05:39 +01:00
|
|
|
await this._req(null, null, controllers.deploy.deployApp)
|
|
|
|
const prodAppId = this.getAppId().replace("_dev", "")
|
2022-07-13 14:22:21 +02:00
|
|
|
this.prodAppId = prodAppId
|
2022-08-10 12:01:54 +02:00
|
|
|
|
2022-01-28 16:43:51 +01:00
|
|
|
return context.doInAppContext(prodAppId, async () => {
|
2022-08-10 12:01:54 +02:00
|
|
|
const db = context.getProdAppDB()
|
2022-08-11 14:50:05 +02:00
|
|
|
return await db.get(DocumentType.APP_METADATA)
|
2022-01-28 16:43:51 +01:00
|
|
|
})
|
2021-10-25 17:59:09 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:56 +02:00
|
|
|
// TABLE
|
|
|
|
|
2021-03-03 18:52:41 +01:00
|
|
|
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
|
2022-02-28 19:53:03 +01:00
|
|
|
return this._req(null, { tableId }, controllers.table.find)
|
2021-03-04 15:36:59 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:56 +02:00
|
|
|
// ROW
|
|
|
|
|
2021-03-03 18:52:41 +01:00
|
|
|
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
|
|
|
|
}
|
2021-06-17 17:35:58 +02:00
|
|
|
return this._req(null, { tableId }, controllers.row.fetch)
|
2021-03-15 17:36:38 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:56 +02:00
|
|
|
// ROLE
|
|
|
|
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:56 +02:00
|
|
|
// VIEW
|
|
|
|
|
2021-03-03 18:52:41 +01:00
|
|
|
async createView(config) {
|
|
|
|
if (!this.table) {
|
|
|
|
throw "Test requires table to be configured."
|
|
|
|
}
|
|
|
|
const view = config || {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:56 +02:00
|
|
|
// AUTOMATION
|
|
|
|
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:56 +02: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
|
|
|
|
}
|
|
|
|
|
|
|
|
// DATASOURCE
|
|
|
|
|
2021-03-04 11:05:50 +01:00
|
|
|
async createDatasource(config = null) {
|
|
|
|
config = config || basicDatasource()
|
2021-10-27 14:10:46 +02:00
|
|
|
const response = await this._req(config, null, controllers.datasource.save)
|
|
|
|
this.datasource = response.datasource
|
2021-03-04 11:05:50 +01:00
|
|
|
return this.datasource
|
|
|
|
}
|
|
|
|
|
2022-01-04 19:23:45 +01:00
|
|
|
async updateDatasource(datasource) {
|
|
|
|
const response = await this._req(
|
|
|
|
datasource,
|
|
|
|
{ datasourceId: datasource._id },
|
|
|
|
controllers.datasource.update
|
|
|
|
)
|
|
|
|
this.datasource = response.datasource
|
|
|
|
return this.datasource
|
|
|
|
}
|
|
|
|
|
2022-01-11 11:35:53 +01:00
|
|
|
async restDatasource(cfg) {
|
|
|
|
return this.createDatasource({
|
|
|
|
datasource: {
|
|
|
|
...basicDatasource().datasource,
|
|
|
|
source: "REST",
|
|
|
|
config: cfg || {},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async dynamicVariableDatasource() {
|
|
|
|
let datasource = await this.restDatasource()
|
|
|
|
const basedOnQuery = await this.createQuery({
|
|
|
|
...basicQuery(datasource._id),
|
|
|
|
fields: {
|
|
|
|
path: "www.google.com",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
datasource = await this.updateDatasource({
|
|
|
|
...datasource,
|
|
|
|
config: {
|
|
|
|
dynamicVariables: [
|
|
|
|
{
|
|
|
|
queryId: basedOnQuery._id,
|
|
|
|
name: "variable3",
|
|
|
|
value: "{{ data.0.[value] }}",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return { datasource, query: basedOnQuery }
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:56 +02:00
|
|
|
// QUERY
|
|
|
|
|
2022-06-17 13:00:42 +02:00
|
|
|
async previewQuery(request, config, datasource, fields, params, verb) {
|
2022-01-11 11:35:53 +01:00
|
|
|
return request
|
|
|
|
.post(`/api/queries/preview`)
|
|
|
|
.send({
|
|
|
|
datasourceId: datasource._id,
|
2022-06-17 13:00:42 +02:00
|
|
|
parameters: params || {},
|
2022-01-11 11:35:53 +01:00
|
|
|
fields,
|
2022-06-17 13:00:42 +02:00
|
|
|
queryVerb: verb || "read",
|
2022-02-11 17:28:19 +01:00
|
|
|
name: datasource.name,
|
2022-01-11 11:35:53 +01:00
|
|
|
})
|
|
|
|
.set(config.defaultHeaders())
|
|
|
|
.expect("Content-Type", /json/)
|
|
|
|
.expect(200)
|
|
|
|
}
|
|
|
|
|
2021-03-04 11:05:50 +01:00
|
|
|
async createQuery(config = null) {
|
|
|
|
if (!this.datasource && !config) {
|
2022-09-14 13:11:20 +02:00
|
|
|
throw "No datasource created for query."
|
2021-03-04 11:05:50 +01:00
|
|
|
}
|
|
|
|
config = config || basicQuery(this.datasource._id)
|
|
|
|
return this._req(config, null, controllers.query.save)
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:56 +02:00
|
|
|
// SCREEN
|
|
|
|
|
2021-03-08 15:49:19 +01:00
|
|
|
async createScreen(config = null) {
|
|
|
|
config = config || basicScreen()
|
|
|
|
return this._req(config, null, controllers.screen.save)
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:56 +02:00
|
|
|
// LAYOUT
|
2021-03-08 16:57:19 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = TestConfiguration
|