From 378cdbe163efcc7187165888c959c2468ad75ca7 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 17 Feb 2025 11:11:08 +0000 Subject: [PATCH] Fix type errors. --- .../src/api/routes/tests/automation.spec.ts | 30 ++++++++ .../tests/utilities/AutomationTestBuilder.ts | 1 + .../server/src/sdk/app/automations/crud.ts | 6 +- .../sdk/app/automations/tests/index.spec.ts | 69 ------------------- .../server/src/tests/utilities/structures.ts | 21 +++++- 5 files changed, 53 insertions(+), 74 deletions(-) delete mode 100644 packages/server/src/sdk/app/automations/tests/index.spec.ts diff --git a/packages/server/src/api/routes/tests/automation.spec.ts b/packages/server/src/api/routes/tests/automation.spec.ts index 722aeffcc3..5108895c16 100644 --- a/packages/server/src/api/routes/tests/automation.spec.ts +++ b/packages/server/src/api/routes/tests/automation.spec.ts @@ -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", () => { diff --git a/packages/server/src/automations/tests/utilities/AutomationTestBuilder.ts b/packages/server/src/automations/tests/utilities/AutomationTestBuilder.ts index d707430a35..4eddf5875f 100644 --- a/packages/server/src/automations/tests/utilities/AutomationTestBuilder.ts +++ b/packages/server/src/automations/tests/utilities/AutomationTestBuilder.ts @@ -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 { diff --git a/packages/server/src/sdk/app/automations/crud.ts b/packages/server/src/sdk/app/automations/crud.ts index cd8af1e548..764b1df784 100644 --- a/packages/server/src/sdk/app/automations/crud.ts +++ b/packages/server/src/sdk/app/automations/crud.ts @@ -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( diff --git a/packages/server/src/sdk/app/automations/tests/index.spec.ts b/packages/server/src/sdk/app/automations/tests/index.spec.ts deleted file mode 100644 index 6c70392300..0000000000 --- a/packages/server/src/sdk/app/automations/tests/index.spec.ts +++ /dev/null @@ -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" - ) - }) - }) - }) -}) diff --git a/packages/server/src/tests/utilities/structures.ts b/packages/server/src/tests/utilities/structures.ts index a78a2c6c9e..38d60e1c11 100644 --- a/packages/server/src/tests/utilities/structures.ts +++ b/packages/server/src/tests/utilities/structures.ts @@ -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 { 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, + }, }, ], }