sync automation tests
This commit is contained in:
parent
a8e40d711b
commit
986348f511
|
@ -1,15 +1,26 @@
|
||||||
const {
|
import {
|
||||||
checkBuilderEndpoint,
|
checkBuilderEndpoint,
|
||||||
getAllTableRows,
|
getAllTableRows,
|
||||||
clearAllAutomations,
|
clearAllAutomations,
|
||||||
testAutomation,
|
testAutomation,
|
||||||
} = require("./utilities/TestFunctions")
|
} from "./utilities/TestFunctions"
|
||||||
const setup = require("./utilities")
|
import * as setup from "./utilities"
|
||||||
const { basicAutomation, newAutomation, automationTrigger, automationStep } = setup.structures
|
import {
|
||||||
const MAX_RETRIES = 4
|
TRIGGER_DEFINITIONS,
|
||||||
const { TRIGGER_DEFINITIONS, BUILTIN_ACTION_DEFINITIONS } = require("../../../automations")
|
BUILTIN_ACTION_DEFINITIONS,
|
||||||
const { events } = require("@budibase/backend-core")
|
} from "../../../automations"
|
||||||
|
import { events } from "@budibase/backend-core"
|
||||||
|
import sdk from "../../../sdk"
|
||||||
|
import { Automation } from "@budibase/types"
|
||||||
|
|
||||||
|
const MAX_RETRIES = 4
|
||||||
|
let {
|
||||||
|
basicAutomation,
|
||||||
|
newAutomation,
|
||||||
|
automationTrigger,
|
||||||
|
automationStep,
|
||||||
|
collectAutomation,
|
||||||
|
} = setup.structures
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
jest.setTimeout(30000)
|
||||||
|
|
||||||
|
@ -24,6 +35,7 @@ describe("/automations", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
// @ts-ignore
|
||||||
events.automation.deleted.mockClear()
|
events.automation.deleted.mockClear()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -32,7 +44,7 @@ describe("/automations", () => {
|
||||||
const res = await request
|
const res = await request
|
||||||
.get(`/api/automations/action/list`)
|
.get(`/api/automations/action/list`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.expect('Content-Type', /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
|
|
||||||
expect(Object.keys(res.body).length).not.toEqual(0)
|
expect(Object.keys(res.body).length).not.toEqual(0)
|
||||||
|
@ -42,7 +54,7 @@ describe("/automations", () => {
|
||||||
const res = await request
|
const res = await request
|
||||||
.get(`/api/automations/trigger/list`)
|
.get(`/api/automations/trigger/list`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.expect('Content-Type', /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
|
|
||||||
expect(Object.keys(res.body).length).not.toEqual(0)
|
expect(Object.keys(res.body).length).not.toEqual(0)
|
||||||
|
@ -52,14 +64,18 @@ describe("/automations", () => {
|
||||||
const res = await request
|
const res = await request
|
||||||
.get(`/api/automations/definitions/list`)
|
.get(`/api/automations/definitions/list`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.expect('Content-Type', /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
|
|
||||||
let definitionsLength = Object.keys(BUILTIN_ACTION_DEFINITIONS).length
|
let definitionsLength = Object.keys(BUILTIN_ACTION_DEFINITIONS).length
|
||||||
definitionsLength-- // OUTGOING_WEBHOOK is deprecated
|
definitionsLength-- // OUTGOING_WEBHOOK is deprecated
|
||||||
|
|
||||||
expect(Object.keys(res.body.action).length).toBeGreaterThanOrEqual(definitionsLength)
|
expect(Object.keys(res.body.action).length).toBeGreaterThanOrEqual(
|
||||||
expect(Object.keys(res.body.trigger).length).toEqual(Object.keys(TRIGGER_DEFINITIONS).length)
|
definitionsLength
|
||||||
|
)
|
||||||
|
expect(Object.keys(res.body.trigger).length).toEqual(
|
||||||
|
Object.keys(TRIGGER_DEFINITIONS).length
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -72,7 +88,7 @@ describe("/automations", () => {
|
||||||
.post(`/api/automations`)
|
.post(`/api/automations`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.send(automation)
|
.send(automation)
|
||||||
.expect('Content-Type', /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
|
|
||||||
expect(res.body.message).toEqual("Automation created successfully")
|
expect(res.body.message).toEqual("Automation created successfully")
|
||||||
|
@ -91,7 +107,7 @@ describe("/automations", () => {
|
||||||
.post(`/api/automations`)
|
.post(`/api/automations`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.send(automation)
|
.send(automation)
|
||||||
.expect('Content-Type', /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
|
|
||||||
expect(res.body.message).toEqual("Automation created successfully")
|
expect(res.body.message).toEqual("Automation created successfully")
|
||||||
|
@ -107,7 +123,7 @@ describe("/automations", () => {
|
||||||
config,
|
config,
|
||||||
method: "POST",
|
method: "POST",
|
||||||
url: `/api/automations`,
|
url: `/api/automations`,
|
||||||
body: automation
|
body: automation,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -118,7 +134,7 @@ describe("/automations", () => {
|
||||||
const res = await request
|
const res = await request
|
||||||
.get(`/api/automations/${automation._id}`)
|
.get(`/api/automations/${automation._id}`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.expect('Content-Type', /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
expect(res.body._id).toEqual(automation._id)
|
expect(res.body._id).toEqual(automation._id)
|
||||||
expect(res.body._rev).toEqual(automation._rev)
|
expect(res.body._rev).toEqual(automation._rev)
|
||||||
|
@ -134,8 +150,8 @@ describe("/automations", () => {
|
||||||
row: {
|
row: {
|
||||||
name: "{{trigger.row.name}}",
|
name: "{{trigger.row.name}}",
|
||||||
description: "{{trigger.row.description}}",
|
description: "{{trigger.row.description}}",
|
||||||
tableId: table._id
|
tableId: table._id,
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
automation.appId = config.appId
|
automation.appId = config.appId
|
||||||
automation = await config.createAutomation(automation)
|
automation = await config.createAutomation(automation)
|
||||||
|
@ -162,23 +178,67 @@ describe("/automations", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("update", () => {
|
describe("trigger", () => {
|
||||||
|
it("does not trigger an automation when not synchronous and in dev", async () => {
|
||||||
|
let automation = newAutomation()
|
||||||
|
automation = await config.createAutomation(automation)
|
||||||
|
const res = await request
|
||||||
|
.post(`/api/automations/${automation._id}/trigger`)
|
||||||
|
.set(config.defaultHeaders())
|
||||||
|
.expect("Content-Type", /json/)
|
||||||
|
.expect(400)
|
||||||
|
|
||||||
const update = async (automation) => {
|
expect(res.body.message).toEqual(
|
||||||
|
"Only apps in production support this endpoint"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("triggers a synchronous automation", async () => {
|
||||||
|
let automation = collectAutomation()
|
||||||
|
automation = await config.createAutomation(automation)
|
||||||
|
const res = await request
|
||||||
|
.post(`/api/automations/${automation._id}/trigger`)
|
||||||
|
.set(config.defaultHeaders())
|
||||||
|
.expect("Content-Type", /json/)
|
||||||
|
.expect(200)
|
||||||
|
|
||||||
|
expect(res.body.success).toEqual(true)
|
||||||
|
expect(res.body.value).toEqual([1, 2, 3])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("triggers an asynchronous automation", async () => {
|
||||||
|
let automation = newAutomation()
|
||||||
|
automation = await config.createAutomation(automation)
|
||||||
|
await config.publish()
|
||||||
|
|
||||||
|
const res = await request
|
||||||
|
.post(`/api/automations/${automation._id}/trigger`)
|
||||||
|
.set(config.defaultHeaders({}, true))
|
||||||
|
.expect("Content-Type", /json/)
|
||||||
|
.expect(200)
|
||||||
|
|
||||||
|
expect(res.body.message).toEqual(
|
||||||
|
`Automation ${automation._id} has been triggered.`
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("update", () => {
|
||||||
|
const update = async (automation: Automation) => {
|
||||||
return request
|
return request
|
||||||
.put(`/api/automations`)
|
.put(`/api/automations`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.send(automation)
|
.send(automation)
|
||||||
.expect('Content-Type', /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateWithPost = async (automation) => {
|
const updateWithPost = async (automation: Automation) => {
|
||||||
return request
|
return request
|
||||||
.post(`/api/automations`)
|
.post(`/api/automations`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.send(automation)
|
.send(automation)
|
||||||
.expect('Content-Type', /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +259,9 @@ describe("/automations", () => {
|
||||||
expect(automationRes._rev).not.toEqual(automation._rev)
|
expect(automationRes._rev).not.toEqual(automation._rev)
|
||||||
// content updates
|
// content updates
|
||||||
expect(automationRes.name).toEqual("Updated Name")
|
expect(automationRes.name).toEqual("Updated Name")
|
||||||
expect(message).toEqual(`Automation ${automation._id} updated successfully.`)
|
expect(message).toEqual(
|
||||||
|
`Automation ${automation._id} updated successfully.`
|
||||||
|
)
|
||||||
// events
|
// events
|
||||||
expect(events.automation.created).not.toBeCalled()
|
expect(events.automation.created).not.toBeCalled()
|
||||||
expect(events.automation.stepCreated).not.toBeCalled()
|
expect(events.automation.stepCreated).not.toBeCalled()
|
||||||
|
@ -207,7 +269,6 @@ describe("/automations", () => {
|
||||||
expect(events.automation.triggerUpdated).not.toBeCalled()
|
expect(events.automation.triggerUpdated).not.toBeCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
it("updates a automations name using POST request", async () => {
|
it("updates a automations name using POST request", async () => {
|
||||||
let automation = newAutomation()
|
let automation = newAutomation()
|
||||||
await config.createAutomation(automation)
|
await config.createAutomation(automation)
|
||||||
|
@ -226,7 +287,9 @@ describe("/automations", () => {
|
||||||
expect(automationRes._rev).not.toEqual(automation._rev)
|
expect(automationRes._rev).not.toEqual(automation._rev)
|
||||||
// content updates
|
// content updates
|
||||||
expect(automationRes.name).toEqual("Updated Name")
|
expect(automationRes.name).toEqual("Updated Name")
|
||||||
expect(message).toEqual(`Automation ${automation._id} updated successfully.`)
|
expect(message).toEqual(
|
||||||
|
`Automation ${automation._id} updated successfully.`
|
||||||
|
)
|
||||||
// events
|
// events
|
||||||
expect(events.automation.created).not.toBeCalled()
|
expect(events.automation.created).not.toBeCalled()
|
||||||
expect(events.automation.stepCreated).not.toBeCalled()
|
expect(events.automation.stepCreated).not.toBeCalled()
|
||||||
|
@ -237,7 +300,9 @@ describe("/automations", () => {
|
||||||
it("updates an automation trigger", async () => {
|
it("updates an automation trigger", async () => {
|
||||||
let automation = newAutomation()
|
let automation = newAutomation()
|
||||||
automation = await config.createAutomation(automation)
|
automation = await config.createAutomation(automation)
|
||||||
automation.definition.trigger = automationTrigger(TRIGGER_DEFINITIONS.WEBHOOK)
|
automation.definition.trigger = automationTrigger(
|
||||||
|
TRIGGER_DEFINITIONS.WEBHOOK
|
||||||
|
)
|
||||||
jest.clearAllMocks()
|
jest.clearAllMocks()
|
||||||
|
|
||||||
await update(automation)
|
await update(automation)
|
||||||
|
@ -266,7 +331,6 @@ describe("/automations", () => {
|
||||||
expect(events.automation.triggerUpdated).not.toBeCalled()
|
expect(events.automation.triggerUpdated).not.toBeCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
it("removes automation steps", async () => {
|
it("removes automation steps", async () => {
|
||||||
let automation = newAutomation()
|
let automation = newAutomation()
|
||||||
automation.definition.steps.push(automationStep())
|
automation.definition.steps.push(automationStep())
|
||||||
|
@ -305,11 +369,11 @@ describe("/automations", () => {
|
||||||
it("return all the automations for an instance", async () => {
|
it("return all the automations for an instance", async () => {
|
||||||
await clearAllAutomations(config)
|
await clearAllAutomations(config)
|
||||||
const autoConfig = basicAutomation()
|
const autoConfig = basicAutomation()
|
||||||
automation = await config.createAutomation(autoConfig)
|
await config.createAutomation(autoConfig)
|
||||||
const res = await request
|
const res = await request
|
||||||
.get(`/api/automations`)
|
.get(`/api/automations`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.expect('Content-Type', /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
|
|
||||||
expect(res.body[0]).toEqual(expect.objectContaining(autoConfig))
|
expect(res.body[0]).toEqual(expect.objectContaining(autoConfig))
|
||||||
|
@ -330,7 +394,7 @@ describe("/automations", () => {
|
||||||
const res = await request
|
const res = await request
|
||||||
.delete(`/api/automations/${automation.id}/${automation.rev}`)
|
.delete(`/api/automations/${automation.id}/${automation.rev}`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.expect('Content-Type', /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
|
|
||||||
expect(res.body.id).toEqual(automation._id)
|
expect(res.body.id).toEqual(automation._id)
|
||||||
|
@ -346,4 +410,13 @@ describe("/automations", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("checkForCollectStep", () => {
|
||||||
|
it("should return true if a collect step exists in an automation", async () => {
|
||||||
|
let automation = collectAutomation()
|
||||||
|
await config.createAutomation(automation)
|
||||||
|
let res = await sdk.automations.utils.checkForCollectStep(automation)
|
||||||
|
expect(res).toEqual(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
|
@ -373,7 +373,7 @@ class TestConfiguration {
|
||||||
|
|
||||||
// HEADERS
|
// HEADERS
|
||||||
|
|
||||||
defaultHeaders(extras = {}) {
|
defaultHeaders(extras = {}, prodApp = false) {
|
||||||
const tenantId = this.getTenantId()
|
const tenantId = this.getTenantId()
|
||||||
const authObj: AuthToken = {
|
const authObj: AuthToken = {
|
||||||
userId: this.defaultUserValues.globalUserId,
|
userId: this.defaultUserValues.globalUserId,
|
||||||
|
@ -390,7 +390,9 @@ class TestConfiguration {
|
||||||
...extras,
|
...extras,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.appId) {
|
if (prodApp) {
|
||||||
|
headers[constants.Header.APP_ID] = this.prodAppId
|
||||||
|
} else if (this.appId) {
|
||||||
headers[constants.Header.APP_ID] = this.appId
|
headers[constants.Header.APP_ID] = this.appId
|
||||||
}
|
}
|
||||||
return headers
|
return headers
|
||||||
|
|
|
@ -199,6 +199,48 @@ export function loopAutomation(tableId: string, loopOpts?: any): Automation {
|
||||||
return automation as Automation
|
return automation as Automation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function collectAutomation(tableId?: string): Automation {
|
||||||
|
const automation: any = {
|
||||||
|
name: "looping",
|
||||||
|
type: "automation",
|
||||||
|
definition: {
|
||||||
|
steps: [
|
||||||
|
{
|
||||||
|
id: "b",
|
||||||
|
type: "ACTION",
|
||||||
|
internal: true,
|
||||||
|
stepId: AutomationActionStepId.EXECUTE_SCRIPT,
|
||||||
|
inputs: {
|
||||||
|
code: "return [1,2,3]",
|
||||||
|
},
|
||||||
|
schema: BUILTIN_ACTION_DEFINITIONS.EXECUTE_SCRIPT.schema,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "c",
|
||||||
|
type: "ACTION",
|
||||||
|
internal: true,
|
||||||
|
stepId: AutomationActionStepId.COLLECT,
|
||||||
|
inputs: {
|
||||||
|
collection: "{{ literal steps.1.value }}",
|
||||||
|
},
|
||||||
|
schema: BUILTIN_ACTION_DEFINITIONS.SERVER_LOG.schema,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
trigger: {
|
||||||
|
id: "a",
|
||||||
|
type: "TRIGGER",
|
||||||
|
event: "row:save",
|
||||||
|
stepId: AutomationTriggerStepId.ROW_SAVED,
|
||||||
|
inputs: {
|
||||||
|
tableId,
|
||||||
|
},
|
||||||
|
schema: TRIGGER_DEFINITIONS.ROW_SAVED.schema,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return automation as Automation
|
||||||
|
}
|
||||||
|
|
||||||
export function basicRow(tableId: string) {
|
export function basicRow(tableId: string) {
|
||||||
return {
|
return {
|
||||||
name: "Test Contact",
|
name: "Test Contact",
|
||||||
|
|
Loading…
Reference in New Issue