Do a typing pass on automation.spec.ts

This commit is contained in:
Sam Rose 2025-01-28 17:43:03 +00:00
parent 0b9eb4a8d5
commit 8ca5cb5599
No known key found for this signature in database
7 changed files with 363 additions and 296 deletions

View File

@ -1,7 +1,6 @@
import { import {
checkBuilderEndpoint, checkBuilderEndpoint,
getAllTableRows, getAllTableRows,
clearAllAutomations,
testAutomation, testAutomation,
} from "./utilities/TestFunctions" } from "./utilities/TestFunctions"
import * as setup from "./utilities" import * as setup from "./utilities"
@ -12,9 +11,9 @@ import {
import { configs, context, events } from "@budibase/backend-core" import { configs, context, events } from "@budibase/backend-core"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import { import {
Automation,
ConfigType, ConfigType,
FieldType, FieldType,
isDidNotTriggerResponse,
SettingsConfig, SettingsConfig,
Table, Table,
} from "@budibase/types" } from "@budibase/types"
@ -22,11 +21,13 @@ import { mocks } from "@budibase/backend-core/tests"
import { removeDeprecated } from "../../../automations/utils" import { removeDeprecated } from "../../../automations/utils"
import { createAutomationBuilder } from "../../../automations/tests/utilities/AutomationTestBuilder" import { createAutomationBuilder } from "../../../automations/tests/utilities/AutomationTestBuilder"
import { automations } from "@budibase/shared-core" import { automations } from "@budibase/shared-core"
import { basicTable } from "../../../tests/utilities/structures"
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
const FilterConditions = automations.steps.filter.FilterConditions const FilterConditions = automations.steps.filter.FilterConditions
const MAX_RETRIES = 4 const MAX_RETRIES = 4
let { const {
basicAutomation, basicAutomation,
newAutomation, newAutomation,
automationTrigger, automationTrigger,
@ -37,10 +38,11 @@ let {
} = setup.structures } = setup.structures
describe("/automations", () => { describe("/automations", () => {
let request = setup.getRequest() const config = new TestConfiguration()
let config = setup.getConfig()
afterAll(setup.afterAll) afterAll(() => {
config.end()
})
beforeAll(async () => { beforeAll(async () => {
await config.init() await config.init()
@ -52,40 +54,26 @@ describe("/automations", () => {
describe("get definitions", () => { describe("get definitions", () => {
it("returns a list of definitions for actions", async () => { it("returns a list of definitions for actions", async () => {
const res = await request const res = await config.api.automation.getActions()
.get(`/api/automations/action/list`) expect(Object.keys(res).length).not.toEqual(0)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(Object.keys(res.body).length).not.toEqual(0)
}) })
it("returns a list of definitions for triggerInfo", async () => { it("returns a list of definitions for triggerInfo", async () => {
const res = await request const res = await config.api.automation.getTriggers()
.get(`/api/automations/trigger/list`) expect(Object.keys(res).length).not.toEqual(0)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(Object.keys(res.body).length).not.toEqual(0)
}) })
it("returns all of the definitions in one", async () => { it("returns all of the definitions in one", async () => {
const res = await request const { action, trigger } = await config.api.automation.getDefinitions()
.get(`/api/automations/definitions/list`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
let definitionsLength = Object.keys( let definitionsLength = Object.keys(
removeDeprecated(BUILTIN_ACTION_DEFINITIONS) removeDeprecated(BUILTIN_ACTION_DEFINITIONS)
).length ).length
expect(Object.keys(res.body.action).length).toBeGreaterThanOrEqual( expect(Object.keys(action).length).toBeGreaterThanOrEqual(
definitionsLength definitionsLength
) )
expect(Object.keys(res.body.trigger).length).toEqual( expect(Object.keys(trigger).length).toEqual(
Object.keys(removeDeprecated(TRIGGER_DEFINITIONS)).length Object.keys(removeDeprecated(TRIGGER_DEFINITIONS)).length
) )
}) })
@ -93,38 +81,27 @@ describe("/automations", () => {
describe("create", () => { describe("create", () => {
it("creates an automation with no steps", async () => { it("creates an automation with no steps", async () => {
const automation = newAutomation() const { message, automation } = await config.api.automation.post(
automation.definition.steps = [] newAutomation({ steps: [] })
)
const res = await request expect(message).toEqual("Automation created successfully")
.post(`/api/automations`) expect(automation.name).toEqual("My Automation")
.set(config.defaultHeaders()) expect(automation._id).not.toEqual(null)
.send(automation)
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.message).toEqual("Automation created successfully")
expect(res.body.automation.name).toEqual("My Automation")
expect(res.body.automation._id).not.toEqual(null)
expect(events.automation.created).toHaveBeenCalledTimes(1) expect(events.automation.created).toHaveBeenCalledTimes(1)
expect(events.automation.stepCreated).not.toHaveBeenCalled() expect(events.automation.stepCreated).not.toHaveBeenCalled()
}) })
it("creates an automation with steps", async () => { it("creates an automation with steps", async () => {
const automation = newAutomation()
automation.definition.steps.push(automationStep())
jest.clearAllMocks() jest.clearAllMocks()
const res = await request const { message, automation } = await config.api.automation.post(
.post(`/api/automations`) newAutomation({ steps: [automationStep(), automationStep()] })
.set(config.defaultHeaders()) )
.send(automation)
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.message).toEqual("Automation created successfully") expect(message).toEqual("Automation created successfully")
expect(res.body.automation.name).toEqual("My Automation") expect(automation.name).toEqual("My Automation")
expect(res.body.automation._id).not.toEqual(null) expect(automation._id).not.toEqual(null)
expect(events.automation.created).toHaveBeenCalledTimes(1) expect(events.automation.created).toHaveBeenCalledTimes(1)
expect(events.automation.stepCreated).toHaveBeenCalledTimes(2) expect(events.automation.stepCreated).toHaveBeenCalledTimes(2)
}) })
@ -241,13 +218,9 @@ describe("/automations", () => {
describe("find", () => { describe("find", () => {
it("should be able to find the automation", async () => { it("should be able to find the automation", async () => {
const automation = await config.createAutomation() const automation = await config.createAutomation()
const res = await request const { _id, _rev } = await config.api.automation.get(automation._id!)
.get(`/api/automations/${automation._id}`) expect(_id).toEqual(automation._id)
.set(config.defaultHeaders()) expect(_rev).toEqual(automation._rev)
.expect("Content-Type", /json/)
.expect(200)
expect(res.body._id).toEqual(automation._id)
expect(res.body._rev).toEqual(automation._rev)
}) })
}) })
@ -348,106 +321,104 @@ describe("/automations", () => {
describe("trigger", () => { describe("trigger", () => {
it("does not trigger an automation when not synchronous and in dev", async () => { it("does not trigger an automation when not synchronous and in dev", async () => {
let automation = newAutomation() const { automation } = await config.api.automation.post(newAutomation())
automation = await config.createAutomation(automation) await config.api.automation.trigger(
const res = await request automation._id!,
.post(`/api/automations/${automation._id}/trigger`) {
.set(config.defaultHeaders()) fields: {},
.expect("Content-Type", /json/) timeout: 1000,
.expect(400) },
{
expect(res.body.message).toEqual( status: 400,
"Only apps in production support this endpoint" body: {
message: "Only apps in production support this endpoint",
},
}
) )
}) })
it("triggers a synchronous automation", async () => { it("triggers a synchronous automation", async () => {
mocks.licenses.useSyncAutomations() mocks.licenses.useSyncAutomations()
let automation = collectAutomation() const { automation } = await config.api.automation.post(
automation = await config.createAutomation(automation) collectAutomation()
const res = await request )
.post(`/api/automations/${automation._id}/trigger`) await config.api.automation.trigger(
.set(config.defaultHeaders()) automation._id!,
.expect("Content-Type", /json/) {
.expect(200) fields: {},
timeout: 1000,
expect(res.body.success).toEqual(true) },
expect(res.body.value).toEqual([1, 2, 3]) {
status: 200,
body: {
success: true,
value: [1, 2, 3],
},
}
)
}) })
it("should throw an error when attempting to trigger a disabled automation", async () => { it("should throw an error when attempting to trigger a disabled automation", async () => {
mocks.licenses.useSyncAutomations() mocks.licenses.useSyncAutomations()
let automation = collectAutomation() const { automation } = await config.api.automation.post(
automation = await config.createAutomation({ collectAutomation({ disabled: true })
...automation, )
disabled: true,
})
const res = await request await config.api.automation.trigger(
.post(`/api/automations/${automation._id}/trigger`) automation._id!,
.set(config.defaultHeaders()) {
.expect("Content-Type", /json/) fields: {},
.expect(400) timeout: 1000,
},
expect(res.body.message).toEqual("Automation is disabled") {
status: 400,
body: {
message: "Automation is disabled",
},
}
)
}) })
it("triggers an asynchronous automation", async () => { it("triggers an asynchronous automation", async () => {
let automation = newAutomation() const { automation } = await config.api.automation.post(newAutomation())
automation = await config.createAutomation(automation)
await config.publish() await config.publish()
const res = await request await config.withProdApp(() =>
.post(`/api/automations/${automation._id}/trigger`) config.api.automation.trigger(
.set(config.defaultHeaders({}, true)) automation._id!,
.expect("Content-Type", /json/) {
.expect(200) fields: {},
timeout: 1000,
expect(res.body.message).toEqual( },
`Automation ${automation._id} has been triggered.` {
status: 200,
body: {
message: `Automation ${automation._id} has been triggered.`,
},
}
)
) )
}) })
}) })
describe("update", () => { describe("update", () => {
const update = async (automation: Automation) => {
return request
.put(`/api/automations`)
.set(config.defaultHeaders())
.send(automation)
.expect("Content-Type", /json/)
.expect(200)
}
const updateWithPost = async (automation: Automation) => {
return request
.post(`/api/automations`)
.set(config.defaultHeaders())
.send(automation)
.expect("Content-Type", /json/)
.expect(200)
}
it("updates a automations name", async () => { it("updates a automations name", async () => {
const automation = await config.createAutomation(newAutomation()) const { automation } = await config.api.automation.post(basicAutomation())
automation.name = "Updated Name" automation.name = "Updated Name"
jest.clearAllMocks() jest.clearAllMocks()
const res = await update(automation) const { automation: updatedAutomation, message } =
await config.api.automation.update(automation)
const automationRes = res.body.automation expect(updatedAutomation._id).toEqual(automation._id)
const message = res.body.message expect(updatedAutomation._rev).toBeDefined()
expect(updatedAutomation._rev).not.toEqual(automation._rev)
// doc attributes expect(updatedAutomation.name).toEqual("Updated Name")
expect(automationRes._id).toEqual(automation._id)
expect(automationRes._rev).toBeDefined()
expect(automationRes._rev).not.toEqual(automation._rev)
// content updates
expect(automationRes.name).toEqual("Updated Name")
expect(message).toEqual( expect(message).toEqual(
`Automation ${automation._id} updated successfully.` `Automation ${automation._id} updated successfully.`
) )
// events
expect(events.automation.created).not.toHaveBeenCalled() expect(events.automation.created).not.toHaveBeenCalled()
expect(events.automation.stepCreated).not.toHaveBeenCalled() expect(events.automation.stepCreated).not.toHaveBeenCalled()
expect(events.automation.stepDeleted).not.toHaveBeenCalled() expect(events.automation.stepDeleted).not.toHaveBeenCalled()
@ -455,26 +426,23 @@ describe("/automations", () => {
}) })
it("updates a automations name using POST request", async () => { it("updates a automations name using POST request", async () => {
const automation = await config.createAutomation(newAutomation()) const { automation } = await config.api.automation.post(basicAutomation())
automation.name = "Updated Name" automation.name = "Updated Name"
jest.clearAllMocks() jest.clearAllMocks()
// the POST request will defer to the update // the POST request will defer to the update when an id has been supplied.
// when an id has been supplied. const { automation: updatedAutomation, message } =
const res = await updateWithPost(automation) await config.api.automation.post(automation)
const automationRes = res.body.automation expect(updatedAutomation._id).toEqual(automation._id)
const message = res.body.message expect(updatedAutomation._rev).toBeDefined()
// doc attributes expect(updatedAutomation._rev).not.toEqual(automation._rev)
expect(automationRes._id).toEqual(automation._id)
expect(automationRes._rev).toBeDefined() expect(updatedAutomation.name).toEqual("Updated Name")
expect(automationRes._rev).not.toEqual(automation._rev)
// content updates
expect(automationRes.name).toEqual("Updated Name")
expect(message).toEqual( expect(message).toEqual(
`Automation ${automation._id} updated successfully.` `Automation ${automation._id} updated successfully.`
) )
// events
expect(events.automation.created).not.toHaveBeenCalled() expect(events.automation.created).not.toHaveBeenCalled()
expect(events.automation.stepCreated).not.toHaveBeenCalled() expect(events.automation.stepCreated).not.toHaveBeenCalled()
expect(events.automation.stepDeleted).not.toHaveBeenCalled() expect(events.automation.stepDeleted).not.toHaveBeenCalled()
@ -482,16 +450,14 @@ describe("/automations", () => {
}) })
it("updates an automation trigger", async () => { it("updates an automation trigger", async () => {
let automation = newAutomation() const { automation } = await config.api.automation.post(newAutomation())
automation = await config.createAutomation(automation)
automation.definition.trigger = automationTrigger( automation.definition.trigger = automationTrigger(
TRIGGER_DEFINITIONS.WEBHOOK TRIGGER_DEFINITIONS.WEBHOOK
) )
jest.clearAllMocks() jest.clearAllMocks()
await update(automation) await config.api.automation.update(automation)
// events
expect(events.automation.created).not.toHaveBeenCalled() expect(events.automation.created).not.toHaveBeenCalled()
expect(events.automation.stepCreated).not.toHaveBeenCalled() expect(events.automation.stepCreated).not.toHaveBeenCalled()
expect(events.automation.stepDeleted).not.toHaveBeenCalled() expect(events.automation.stepDeleted).not.toHaveBeenCalled()
@ -499,16 +465,13 @@ describe("/automations", () => {
}) })
it("adds automation steps", async () => { it("adds automation steps", async () => {
let automation = newAutomation() const { automation } = await config.api.automation.post(newAutomation())
automation = await config.createAutomation(automation)
automation.definition.steps.push(automationStep()) automation.definition.steps.push(automationStep())
automation.definition.steps.push(automationStep()) automation.definition.steps.push(automationStep())
jest.clearAllMocks() jest.clearAllMocks()
// check the post request honours updates with same id await config.api.automation.update(automation)
await update(automation)
// events
expect(events.automation.stepCreated).toHaveBeenCalledTimes(2) expect(events.automation.stepCreated).toHaveBeenCalledTimes(2)
expect(events.automation.created).not.toHaveBeenCalled() expect(events.automation.created).not.toHaveBeenCalled()
expect(events.automation.stepDeleted).not.toHaveBeenCalled() expect(events.automation.stepDeleted).not.toHaveBeenCalled()
@ -516,32 +479,25 @@ describe("/automations", () => {
}) })
it("removes automation steps", async () => { it("removes automation steps", async () => {
let automation = newAutomation() const { automation } = await config.api.automation.post(newAutomation())
automation.definition.steps.push(automationStep())
automation = await config.createAutomation(automation)
automation.definition.steps = [] automation.definition.steps = []
jest.clearAllMocks() jest.clearAllMocks()
// check the post request honours updates with same id await config.api.automation.update(automation)
await update(automation)
// events expect(events.automation.stepDeleted).toHaveBeenCalledTimes(1)
expect(events.automation.stepDeleted).toHaveBeenCalledTimes(2)
expect(events.automation.stepCreated).not.toHaveBeenCalled() expect(events.automation.stepCreated).not.toHaveBeenCalled()
expect(events.automation.created).not.toHaveBeenCalled() expect(events.automation.created).not.toHaveBeenCalled()
expect(events.automation.triggerUpdated).not.toHaveBeenCalled() expect(events.automation.triggerUpdated).not.toHaveBeenCalled()
}) })
it("adds and removes automation steps", async () => { it("adds and removes automation steps", async () => {
let automation = newAutomation() const { automation } = await config.api.automation.post(newAutomation())
automation = await config.createAutomation(automation)
automation.definition.steps = [automationStep(), automationStep()] automation.definition.steps = [automationStep(), automationStep()]
jest.clearAllMocks() jest.clearAllMocks()
// check the post request honours updates with same id await config.api.automation.update(automation)
await update(automation)
// events
expect(events.automation.stepCreated).toHaveBeenCalledTimes(2) expect(events.automation.stepCreated).toHaveBeenCalledTimes(2)
expect(events.automation.stepDeleted).toHaveBeenCalledTimes(1) expect(events.automation.stepDeleted).toHaveBeenCalledTimes(1)
expect(events.automation.created).not.toHaveBeenCalled() expect(events.automation.created).not.toHaveBeenCalled()
@ -551,16 +507,24 @@ describe("/automations", () => {
describe("fetch", () => { describe("fetch", () => {
it("return all the automations for an instance", async () => { it("return all the automations for an instance", async () => {
await clearAllAutomations(config) const fetchResponse = await config.api.automation.fetch()
const autoConfig = await config.createAutomation(basicAutomation()) for (const auto of fetchResponse.automations) {
const res = await request await config.api.automation.delete(auto)
.get(`/api/automations`) }
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.automations[0]).toEqual( const { automation: automation1 } = await config.api.automation.post(
expect.objectContaining(autoConfig) newAutomation()
)
const { automation: automation2 } = await config.api.automation.post(
newAutomation()
)
const { automation: automation3 } = await config.api.automation.post(
newAutomation()
)
const { automations } = await config.api.automation.fetch()
expect(automations).toEqual(
expect.arrayContaining([automation1, automation2, automation3])
) )
}) })
@ -575,29 +539,25 @@ describe("/automations", () => {
describe("destroy", () => { describe("destroy", () => {
it("deletes a automation by its ID", async () => { it("deletes a automation by its ID", async () => {
const automation = await config.createAutomation() const { automation } = await config.api.automation.post(newAutomation())
const res = await request const { id } = await config.api.automation.delete(automation)
.delete(`/api/automations/${automation._id}/${automation._rev}`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.id).toEqual(automation._id) expect(id).toEqual(automation._id)
expect(events.automation.deleted).toHaveBeenCalledTimes(1) expect(events.automation.deleted).toHaveBeenCalledTimes(1)
}) })
it("cannot delete a row action automation", async () => { it("cannot delete a row action automation", async () => {
const automation = await config.createAutomation( const { automation } = await config.api.automation.post(
setup.structures.rowActionAutomation() setup.structures.rowActionAutomation()
) )
await request
.delete(`/api/automations/${automation._id}/${automation._rev}`) await config.api.automation.delete(automation, {
.set(config.defaultHeaders()) status: 422,
.expect("Content-Type", /json/) body: {
.expect(422, {
message: "Row actions automations cannot be deleted", message: "Row actions automations cannot be deleted",
status: 422, status: 422,
}) },
})
expect(events.automation.deleted).not.toHaveBeenCalled() expect(events.automation.deleted).not.toHaveBeenCalled()
}) })
@ -614,10 +574,19 @@ describe("/automations", () => {
describe("checkForCollectStep", () => { describe("checkForCollectStep", () => {
it("should return true if a collect step exists in an automation", async () => { it("should return true if a collect step exists in an automation", async () => {
let automation = collectAutomation() const { automation } = await config.api.automation.post(
await config.createAutomation(automation) collectAutomation()
let res = await sdk.automations.utils.checkForCollectStep(automation) )
expect(res).toEqual(true) expect(sdk.automations.utils.checkForCollectStep(automation)).toEqual(
true
)
})
it("should return false if a collect step does not exist in an automation", async () => {
const { automation } = await config.api.automation.post(newAutomation())
expect(sdk.automations.utils.checkForCollectStep(automation)).toEqual(
false
)
}) })
}) })
@ -628,28 +597,45 @@ describe("/automations", () => {
])( ])(
"triggers an update row automation and compares new to old rows with old city '%s' and new city '%s'", "triggers an update row automation and compares new to old rows with old city '%s' and new city '%s'",
async ({ oldCity, newCity }) => { async ({ oldCity, newCity }) => {
const expectedResult = oldCity === newCity let table = await config.api.table.save(basicTable())
let table = await config.createTable() const { automation } = await config.api.automation.post(
filterAutomation({
definition: {
trigger: {
inputs: {
tableId: table._id,
},
},
steps: [
{
inputs: {
condition: FilterConditions.EQUAL,
field: "{{ trigger.row.City }}",
value: "{{ trigger.oldRow.City }}",
},
},
],
},
})
)
let automation = await filterAutomation(config.getAppId()) const res = await config.api.automation.test(automation._id!, {
automation.definition.trigger.inputs.tableId = table._id fields: {},
automation.definition.steps[0].inputs = {
condition: FilterConditions.EQUAL,
field: "{{ trigger.row.City }}",
value: "{{ trigger.oldRow.City }}",
}
automation = await config.createAutomation(automation)
let triggerInputs = {
oldRow: { oldRow: {
City: oldCity, City: oldCity,
}, },
row: { row: {
City: newCity, City: newCity,
}, },
})
if (isDidNotTriggerResponse(res)) {
throw new Error("Automation did not trigger")
} }
const res = await testAutomation(config, automation, triggerInputs)
expect(res.body.steps[1].outputs.result).toEqual(expectedResult) const expectedResult = oldCity === newCity
expect(res.steps[1].outputs.result).toEqual(expectedResult)
} }
) )
}) })
@ -657,16 +643,18 @@ describe("/automations", () => {
let table: Table let table: Table
beforeAll(async () => { beforeAll(async () => {
table = await config.createTable({ table = await config.api.table.save(
name: "table", basicTable(undefined, {
type: "table", name: "table",
schema: { type: "table",
Approved: { schema: {
name: "Approved", Approved: {
type: FieldType.BOOLEAN, name: "Approved",
type: FieldType.BOOLEAN,
},
}, },
}, })
}) )
}) })
const testCases = [ const testCases = [
@ -712,33 +700,29 @@ describe("/automations", () => {
it.each(testCases)( it.each(testCases)(
"$description", "$description",
async ({ filters, row, oldRow, expectToRun }) => { async ({ filters, row, oldRow, expectToRun }) => {
let automation = await updateRowAutomationWithFilters( let req = updateRowAutomationWithFilters(config.getAppId(), table._id!)
config.getAppId(), req.definition.trigger.inputs = {
table._id!
)
automation.definition.trigger.inputs = {
tableId: table._id, tableId: table._id,
filters, filters,
} }
automation = await config.createAutomation(automation)
const inputs = { const { automation } = await config.api.automation.post(req)
row: { const res = await config.api.automation.test(automation._id!, {
tableId: table._id, fields: {},
...row,
},
oldRow: { oldRow: {
tableId: table._id, tableId: table._id,
...oldRow, ...oldRow,
}, },
} row: {
tableId: table._id,
...row,
},
})
const res = await testAutomation(config, automation, inputs) if (isDidNotTriggerResponse(res)) {
expect(expectToRun).toEqual(false)
if (expectToRun) {
expect(res.body.steps[1].outputs.success).toEqual(true)
} else { } else {
expect(res.body.outputs.success).toEqual(false) expect(res.steps[1].outputs.success).toEqual(expectToRun)
} }
} }
) )

View File

@ -53,15 +53,6 @@ export const clearAllApps = async (
}) })
} }
export const clearAllAutomations = async (config: TestConfiguration) => {
const { automations } = await config.getAllAutomations()
for (let auto of automations) {
await context.doInAppContext(config.getAppId(), async () => {
await config.deleteAutomation(auto)
})
}
}
export const wipeDb = async () => { export const wipeDb = async () => {
const couchInfo = db.getCouchInfo() const couchInfo = db.getCouchInfo()
const nano = Nano({ const nano = Nano({

View File

@ -258,7 +258,7 @@ export default class TestConfiguration {
} }
} }
async withApp(app: App | string, f: () => Promise<void>) { async withApp<R>(app: App | string, f: () => Promise<R>) {
const oldAppId = this.appId const oldAppId = this.appId
this.appId = typeof app === "string" ? app : app.appId this.appId = typeof app === "string" ? app : app.appId
try { try {
@ -268,6 +268,10 @@ export default class TestConfiguration {
} }
} }
async withProdApp<R>(f: () => Promise<R>) {
return await this.withApp(this.getProdAppId(), f)
}
// UTILS // UTILS
_req<Req extends Record<string, any> | void, Res>( _req<Req extends Record<string, any> | void, Res>(

View File

@ -1,8 +1,17 @@
import { import {
Automation, Automation,
CreateAutomationResponse,
DeleteAutomationResponse,
FetchAutomationResponse, FetchAutomationResponse,
GetAutomationActionDefinitionsResponse,
GetAutomationStepDefinitionsResponse,
GetAutomationTriggerDefinitionsResponse,
TestAutomationRequest, TestAutomationRequest,
TestAutomationResponse, TestAutomationResponse,
TriggerAutomationRequest,
TriggerAutomationResponse,
UpdateAutomationRequest,
UpdateAutomationResponse,
} from "@budibase/types" } from "@budibase/types"
import { Expectations, TestAPI } from "./base" import { Expectations, TestAPI } from "./base"
@ -20,6 +29,39 @@ export class AutomationAPI extends TestAPI {
return result return result
} }
getActions = async (
expectations?: Expectations
): Promise<GetAutomationActionDefinitionsResponse> => {
return await this._get<GetAutomationActionDefinitionsResponse>(
`/api/automations/actions/list`,
{
expectations,
}
)
}
getTriggers = async (
expectations?: Expectations
): Promise<GetAutomationTriggerDefinitionsResponse> => {
return await this._get<GetAutomationTriggerDefinitionsResponse>(
`/api/automations/triggers/list`,
{
expectations,
}
)
}
getDefinitions = async (
expectations?: Expectations
): Promise<GetAutomationStepDefinitionsResponse> => {
return await this._get<GetAutomationStepDefinitionsResponse>(
`/api/automations/definitions/list`,
{
expectations,
}
)
}
fetch = async ( fetch = async (
expectations?: Expectations expectations?: Expectations
): Promise<FetchAutomationResponse> => { ): Promise<FetchAutomationResponse> => {
@ -31,11 +73,14 @@ export class AutomationAPI extends TestAPI {
post = async ( post = async (
body: Automation, body: Automation,
expectations?: Expectations expectations?: Expectations
): Promise<Automation> => { ): Promise<CreateAutomationResponse> => {
const result = await this._post<Automation>(`/api/automations`, { const result = await this._post<CreateAutomationResponse>(
body, `/api/automations`,
expectations, {
}) body,
expectations,
}
)
return result return result
} }
@ -52,4 +97,40 @@ export class AutomationAPI extends TestAPI {
} }
) )
} }
trigger = async (
id: string,
body: TriggerAutomationRequest,
expectations?: Expectations
): Promise<TriggerAutomationResponse> => {
return await this._post<TriggerAutomationResponse>(
`/api/automations/${id}/trigger`,
{
expectations,
body,
}
)
}
update = async (
body: UpdateAutomationRequest,
expectations?: Expectations
): Promise<UpdateAutomationResponse> => {
return await this._put<UpdateAutomationResponse>(`/api/automations`, {
body,
expectations,
})
}
delete = async (
automation: Automation,
expectations?: Expectations
): Promise<DeleteAutomationResponse> => {
return await this._delete<DeleteAutomationResponse>(
`/api/automations/${automation._id!}/${automation._rev!}`,
{
expectations,
}
)
}
} }

View File

@ -19,43 +19,43 @@ import { PluginAPI } from "./plugin"
import { WebhookAPI } from "./webhook" import { WebhookAPI } from "./webhook"
export default class API { export default class API {
table: TableAPI
legacyView: LegacyViewAPI
viewV2: ViewV2API
row: RowAPI
permission: PermissionAPI
datasource: DatasourceAPI
screen: ScreenAPI
application: ApplicationAPI application: ApplicationAPI
backup: BackupAPI
attachment: AttachmentAPI attachment: AttachmentAPI
user: UserAPI automation: AutomationAPI
backup: BackupAPI
datasource: DatasourceAPI
legacyView: LegacyViewAPI
permission: PermissionAPI
plugin: PluginAPI
query: QueryAPI query: QueryAPI
roles: RoleAPI roles: RoleAPI
templates: TemplateAPI row: RowAPI
rowAction: RowActionAPI rowAction: RowActionAPI
automation: AutomationAPI screen: ScreenAPI
plugin: PluginAPI table: TableAPI
templates: TemplateAPI
user: UserAPI
viewV2: ViewV2API
webhook: WebhookAPI webhook: WebhookAPI
constructor(config: TestConfiguration) { constructor(config: TestConfiguration) {
this.table = new TableAPI(config)
this.legacyView = new LegacyViewAPI(config)
this.viewV2 = new ViewV2API(config)
this.row = new RowAPI(config)
this.permission = new PermissionAPI(config)
this.datasource = new DatasourceAPI(config)
this.screen = new ScreenAPI(config)
this.application = new ApplicationAPI(config) this.application = new ApplicationAPI(config)
this.backup = new BackupAPI(config)
this.attachment = new AttachmentAPI(config) this.attachment = new AttachmentAPI(config)
this.user = new UserAPI(config) this.automation = new AutomationAPI(config)
this.backup = new BackupAPI(config)
this.datasource = new DatasourceAPI(config)
this.legacyView = new LegacyViewAPI(config)
this.permission = new PermissionAPI(config)
this.plugin = new PluginAPI(config)
this.query = new QueryAPI(config) this.query = new QueryAPI(config)
this.roles = new RoleAPI(config) this.roles = new RoleAPI(config)
this.templates = new TemplateAPI(config) this.row = new RowAPI(config)
this.rowAction = new RowActionAPI(config) this.rowAction = new RowActionAPI(config)
this.automation = new AutomationAPI(config) this.screen = new ScreenAPI(config)
this.plugin = new PluginAPI(config) this.table = new TableAPI(config)
this.templates = new TemplateAPI(config)
this.user = new UserAPI(config)
this.viewV2 = new ViewV2API(config)
this.webhook = new WebhookAPI(config) this.webhook = new WebhookAPI(config)
} }
} }

View File

@ -34,6 +34,7 @@ import {
Webhook, Webhook,
WebhookActionType, WebhookActionType,
BuiltinPermissionID, BuiltinPermissionID,
DeepPartial,
} from "@budibase/types" } from "@budibase/types"
import { LoopInput } from "../../definitions/automations" import { LoopInput } from "../../definitions/automations"
import { merge } from "lodash" import { merge } from "lodash"
@ -184,21 +185,12 @@ export function newAutomation({
steps, steps,
trigger, trigger,
}: { steps?: AutomationStep[]; trigger?: AutomationTrigger } = {}) { }: { steps?: AutomationStep[]; trigger?: AutomationTrigger } = {}) {
const automation = basicAutomation() return basicAutomation({
definition: {
if (trigger) { steps: steps || [automationStep()],
automation.definition.trigger = trigger trigger: trigger || automationTrigger(),
} else { },
automation.definition.trigger = automationTrigger() })
}
if (steps) {
automation.definition.steps = steps
} else {
automation.definition.steps = [automationStep()]
}
return automation
} }
export function rowActionAutomation() { export function rowActionAutomation() {
@ -211,8 +203,8 @@ export function rowActionAutomation() {
return automation return automation
} }
export function basicAutomation(appId?: string): Automation { export function basicAutomation(opts?: DeepPartial<Automation>): Automation {
return { const baseAutomation: Automation = {
name: "My Automation", name: "My Automation",
screenId: "kasdkfldsafkl", screenId: "kasdkfldsafkl",
live: true, live: true,
@ -241,8 +233,9 @@ export function basicAutomation(appId?: string): Automation {
steps: [], steps: [],
}, },
type: "automation", type: "automation",
appId: appId!, appId: "appId",
} }
return merge(baseAutomation, opts)
} }
export function basicCronAutomation(appId: string, cron: string): Automation { export function basicCronAutomation(appId: string, cron: string): Automation {
@ -387,16 +380,21 @@ export function loopAutomation(
return automation as Automation return automation as Automation
} }
export function collectAutomation(tableId?: string): Automation { export function collectAutomation(opts?: DeepPartial<Automation>): Automation {
const automation: any = { const baseAutomation: Automation = {
appId: "appId",
name: "looping", name: "looping",
type: "automation", type: "automation",
definition: { definition: {
steps: [ steps: [
{ {
id: "b", id: "b",
type: "ACTION", name: "b",
tagline: "An automation action step",
icon: "Icon",
type: AutomationStepType.ACTION,
internal: true, internal: true,
description: "Execute script",
stepId: AutomationActionStepId.EXECUTE_SCRIPT, stepId: AutomationActionStepId.EXECUTE_SCRIPT,
inputs: { inputs: {
code: "return [1,2,3]", code: "return [1,2,3]",
@ -405,8 +403,12 @@ export function collectAutomation(tableId?: string): Automation {
}, },
{ {
id: "c", id: "c",
type: "ACTION", name: "c",
type: AutomationStepType.ACTION,
tagline: "An automation action step",
icon: "Icon",
internal: true, internal: true,
description: "Collect",
stepId: AutomationActionStepId.COLLECT, stepId: AutomationActionStepId.COLLECT,
inputs: { inputs: {
collection: "{{ literal steps.1.value }}", collection: "{{ literal steps.1.value }}",
@ -416,24 +418,28 @@ export function collectAutomation(tableId?: string): Automation {
], ],
trigger: { trigger: {
id: "a", id: "a",
type: "TRIGGER", type: AutomationStepType.TRIGGER,
event: AutomationEventType.ROW_SAVE, event: AutomationEventType.ROW_SAVE,
stepId: AutomationTriggerStepId.ROW_SAVED, stepId: AutomationTriggerStepId.ROW_SAVED,
name: "trigger Step",
tagline: "An automation trigger",
description: "A trigger",
icon: "Icon",
inputs: { inputs: {
tableId, tableId: "tableId",
}, },
schema: TRIGGER_DEFINITIONS.ROW_SAVED.schema, schema: TRIGGER_DEFINITIONS.ROW_SAVED.schema,
}, },
}, },
} }
return automation return merge(baseAutomation, opts)
} }
export function filterAutomation(appId: string, tableId?: string): Automation { export function filterAutomation(opts?: DeepPartial<Automation>): Automation {
const automation: Automation = { const automation: Automation = {
name: "looping", name: "looping",
type: "automation", type: "automation",
appId, appId: "appId",
definition: { definition: {
steps: [ steps: [
{ {
@ -459,13 +465,13 @@ export function filterAutomation(appId: string, tableId?: string): Automation {
event: AutomationEventType.ROW_SAVE, event: AutomationEventType.ROW_SAVE,
stepId: AutomationTriggerStepId.ROW_SAVED, stepId: AutomationTriggerStepId.ROW_SAVED,
inputs: { inputs: {
tableId: tableId!, tableId: "tableId",
}, },
schema: TRIGGER_DEFINITIONS.ROW_SAVED.schema, schema: TRIGGER_DEFINITIONS.ROW_SAVED.schema,
}, },
}, },
} }
return automation return merge(automation, opts)
} }
export function updateRowAutomationWithFilters( export function updateRowAutomationWithFilters(

View File

@ -75,6 +75,7 @@ export interface TestAutomationRequest {
revision?: string revision?: string
fields: Record<string, any> fields: Record<string, any>
row?: Row row?: Row
oldRow?: Row
} }
export type TestAutomationResponse = AutomationResults | DidNotTriggerResponse export type TestAutomationResponse = AutomationResults | DidNotTriggerResponse