Fix type errors.

This commit is contained in:
Sam Rose 2025-02-17 11:11:08 +00:00
parent d4a55b743d
commit 378cdbe163
No known key found for this signature in database
5 changed files with 53 additions and 74 deletions

View File

@ -485,6 +485,36 @@ describe("/automations", () => {
expect(events.automation.created).not.toHaveBeenCalled()
expect(events.automation.triggerUpdated).not.toHaveBeenCalled()
})
it("can update an input field", async () => {
const { automation } = await createAutomationBuilder(config)
.onRowDeleted({ tableId: "tableId" })
.serverLog({ text: "test" })
.save()
automation.definition.trigger.inputs.tableId = "newTableId"
const { automation: updatedAutomation } =
await config.api.automation.update(automation)
expect(updatedAutomation.definition.trigger.inputs.tableId).toEqual(
"newTableId"
)
})
it("cannot update a readonly field", async () => {
const { automation } = await createAutomationBuilder(config)
.onRowAction({ tableId: "tableId" })
.serverLog({ text: "test" })
.save()
automation.definition.trigger.inputs.tableId = "newTableId"
await config.api.automation.update(automation, {
status: 400,
body: {
message: "Field tableId is readonly and it cannot be modified",
},
})
})
})
describe("fetch", () => {

View File

@ -63,6 +63,7 @@ class TriggerBuilder {
onRowDeleted = this.trigger(AutomationTriggerStepId.ROW_DELETED)
onWebhook = this.trigger(AutomationTriggerStepId.WEBHOOK)
onCron = this.trigger(AutomationTriggerStepId.CRON)
onRowAction = this.trigger(AutomationTriggerStepId.ROW_ACTION)
}
class BranchStepBuilder<TStep extends AutomationTriggerStepId> {

View File

@ -40,7 +40,8 @@ function cleanAutomationInputs(automation: Automation) {
if (step == null) {
continue
}
for (let inputName of Object.keys(step.inputs)) {
for (const key of Object.keys(step.inputs)) {
const inputName = key as keyof typeof step.inputs
if (!step.inputs[inputName] || step.inputs[inputName] === "") {
delete step.inputs[inputName]
}
@ -281,7 +282,8 @@ function guardInvalidUpdatesAndThrow(
const readonlyFields = Object.keys(
step.schema.inputs.properties || {}
).filter(k => step.schema.inputs.properties[k].readonly)
readonlyFields.forEach(readonlyField => {
readonlyFields.forEach(key => {
const readonlyField = key as keyof typeof step.inputs
const oldStep = oldStepDefinitions.find(i => i.id === step.id)
if (step.inputs[readonlyField] !== oldStep?.inputs[readonlyField]) {
throw new HTTPError(

View File

@ -1,69 +0,0 @@
import { sample } from "lodash/fp"
import { Automation } from "@budibase/types"
import { generator } from "@budibase/backend-core/tests"
import TestConfiguration from "../../../../tests/utilities/TestConfiguration"
import automationSdk from "../"
import { structures } from "../../../../api/routes/tests/utilities"
describe("automation sdk", () => {
const config = new TestConfiguration()
beforeAll(async () => {
await config.init()
})
describe("update", () => {
it("can rename existing automations", async () => {
await config.doInContext(config.getAppId(), async () => {
const automation = structures.newAutomation()
const response = await automationSdk.create(automation)
const newName = generator.guid()
const update = { ...response, name: newName }
const result = await automationSdk.update(update)
expect(result.name).toEqual(newName)
})
})
it.each([
["trigger", (a: Automation) => a.definition.trigger],
["step", (a: Automation) => a.definition.steps[0]],
])("can update input fields (for a %s)", async (_, getStep) => {
await config.doInContext(config.getAppId(), async () => {
const automation = structures.newAutomation()
const keyToUse = sample(Object.keys(getStep(automation).inputs))!
getStep(automation).inputs[keyToUse] = "anyValue"
const response = await automationSdk.create(automation)
const update = { ...response }
getStep(update).inputs[keyToUse] = "anyUpdatedValue"
const result = await automationSdk.update(update)
expect(getStep(result).inputs[keyToUse]).toEqual("anyUpdatedValue")
})
})
it.each([
["trigger", (a: Automation) => a.definition.trigger],
["step", (a: Automation) => a.definition.steps[0]],
])("cannot update readonly fields (for a %s)", async (_, getStep) => {
await config.doInContext(config.getAppId(), async () => {
const automation = structures.newAutomation()
getStep(automation).schema.inputs.properties["readonlyProperty"] = {
readonly: true,
}
getStep(automation).inputs["readonlyProperty"] = "anyValue"
const response = await automationSdk.create(automation)
const update = { ...response }
getStep(update).inputs["readonlyProperty"] = "anyUpdatedValue"
await expect(automationSdk.update(update)).rejects.toThrow(
"Field readonlyProperty is readonly and it cannot be modified"
)
})
})
})
})

View File

@ -35,6 +35,8 @@ import {
WebhookActionType,
BuiltinPermissionID,
DeepPartial,
FilterCondition,
AutomationTriggerResult,
} from "@budibase/types"
import { LoopInput } from "../../definitions/automations"
import { merge } from "lodash"
@ -372,7 +374,11 @@ export function filterAutomation(opts?: DeepPartial<Automation>): Automation {
type: AutomationStepType.ACTION,
internal: true,
stepId: AutomationActionStepId.FILTER,
inputs: { field: "name", value: "test", condition: "EQ" },
inputs: {
field: "name",
value: "test",
condition: FilterCondition.EQUAL,
},
schema: BUILTIN_ACTION_DEFINITIONS.EXECUTE_SCRIPT.schema,
},
],
@ -437,15 +443,24 @@ export function updateRowAutomationWithFilters(
export function basicAutomationResults(
automationId: string
): AutomationResults {
const trigger: AutomationTriggerResult = {
id: "trigger",
stepId: AutomationTriggerStepId.APP,
outputs: {},
}
return {
automationId,
status: AutomationStatus.SUCCESS,
trigger: "trigger" as any,
trigger,
steps: [
trigger,
{
id: "step1",
stepId: AutomationActionStepId.SERVER_LOG,
inputs: {},
outputs: {},
outputs: {
success: true,
},
},
],
}