From 48578e1f1878f1a474963d0cd2848da8118e0d82 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 6 Nov 2024 17:29:34 +0000 Subject: [PATCH] Fix some automation tests. --- .../tests/{bash.spec.js => bash.spec.ts} | 10 +-- .../src/automations/tests/createRow.spec.ts | 12 ++-- .../tests/{delay.spec.js => delay.spec.ts} | 14 ++-- .../src/automations/tests/deleteRow.spec.ts | 21 +++--- .../src/automations/tests/discord.spec.ts | 2 +- .../automations/tests/executeQuery.spec.ts | 37 ++++++---- ...teScript.spec.js => executeScript.spec.ts} | 17 ++--- .../src/automations/tests/filter.spec.ts | 8 ++- .../server/src/automations/tests/make.spec.ts | 6 +- .../server/src/automations/tests/n8n.spec.ts | 8 +-- .../src/automations/tests/openai.spec.ts | 8 +-- .../automations/tests/outgoingWebhook.spec.ts | 4 +- .../src/automations/tests/queryRows.spec.ts | 30 ++++++-- .../automations/tests/sendSmtpEmail.spec.ts | 3 +- .../{serverLog.spec.js => serverLog.spec.ts} | 10 +-- .../tests/triggerAutomationRun.spec.ts | 2 + .../src/automations/tests/updateRow.spec.ts | 68 +++++++++++-------- .../src/automations/tests/utilities/index.ts | 19 ++++-- .../src/automations/tests/zapier.spec.ts | 6 +- 19 files changed, 174 insertions(+), 111 deletions(-) rename packages/server/src/automations/tests/{bash.spec.js => bash.spec.ts} (66%) rename packages/server/src/automations/tests/{delay.spec.js => delay.spec.ts} (59%) rename packages/server/src/automations/tests/{executeScript.spec.js => executeScript.spec.ts} (70%) rename packages/server/src/automations/tests/{serverLog.spec.js => serverLog.spec.ts} (60%) diff --git a/packages/server/src/automations/tests/bash.spec.js b/packages/server/src/automations/tests/bash.spec.ts similarity index 66% rename from packages/server/src/automations/tests/bash.spec.js rename to packages/server/src/automations/tests/bash.spec.ts index 61b96a5e35..472d1092d6 100644 --- a/packages/server/src/automations/tests/bash.spec.js +++ b/packages/server/src/automations/tests/bash.spec.ts @@ -1,15 +1,15 @@ -const setup = require("./utilities") +import { getConfig, afterAll as _afterAll, runStep } from "./utilities" describe("test the bash action", () => { - let config = setup.getConfig() + let config = getConfig() beforeAll(async () => { await config.init() }) - afterAll(setup.afterAll) + afterAll(_afterAll) it("should be able to execute a script", async () => { - let res = await setup.runStep("EXECUTE_BASH", { + let res = await runStep(config, "EXECUTE_BASH", { code: "echo 'test'", }) expect(res.stdout).toEqual("test\n") @@ -17,7 +17,7 @@ describe("test the bash action", () => { }) it("should handle a null value", async () => { - let res = await setup.runStep("EXECUTE_BASH", { + let res = await runStep(config, "EXECUTE_BASH", { code: null, }) expect(res.stdout).toEqual( diff --git a/packages/server/src/automations/tests/createRow.spec.ts b/packages/server/src/automations/tests/createRow.spec.ts index 62e9e24f9e..bcf9845669 100644 --- a/packages/server/src/automations/tests/createRow.spec.ts +++ b/packages/server/src/automations/tests/createRow.spec.ts @@ -31,7 +31,7 @@ describe("test the create row action", () => { afterAll(setup.afterAll) it("should be able to run the action", async () => { - const res = await setup.runStep(setup.actions.CREATE_ROW.stepId, { + const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, { row, }) expect(res.id).toBeDefined() @@ -43,7 +43,7 @@ describe("test the create row action", () => { }) it("should return an error (not throw) when bad info provided", async () => { - const res = await setup.runStep(setup.actions.CREATE_ROW.stepId, { + const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, { row: { tableId: "invalid", invalid: "invalid", @@ -53,7 +53,7 @@ describe("test the create row action", () => { }) it("should check invalid inputs return an error", async () => { - const res = await setup.runStep(setup.actions.CREATE_ROW.stepId, {}) + const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, {}) expect(res.success).toEqual(false) }) @@ -76,7 +76,7 @@ describe("test the create row action", () => { ] attachmentRow.file_attachment = attachmentObject - const res = await setup.runStep(setup.actions.CREATE_ROW.stepId, { + const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, { row: attachmentRow, }) @@ -111,7 +111,7 @@ describe("test the create row action", () => { } attachmentRow.single_file_attachment = attachmentObject - const res = await setup.runStep(setup.actions.CREATE_ROW.stepId, { + const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, { row: attachmentRow, }) @@ -146,7 +146,7 @@ describe("test the create row action", () => { } attachmentRow.single_file_attachment = attachmentObject - const res = await setup.runStep(setup.actions.CREATE_ROW.stepId, { + const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, { row: attachmentRow, }) diff --git a/packages/server/src/automations/tests/delay.spec.js b/packages/server/src/automations/tests/delay.spec.ts similarity index 59% rename from packages/server/src/automations/tests/delay.spec.js rename to packages/server/src/automations/tests/delay.spec.ts index 5f6e0fd164..7ed5fe7482 100644 --- a/packages/server/src/automations/tests/delay.spec.js +++ b/packages/server/src/automations/tests/delay.spec.ts @@ -1,14 +1,20 @@ -const setup = require("./utilities") +import { runStep, actions, getConfig } from "./utilities" +import { reset } from "timekeeper" // need real Date for this test -const tk = require("timekeeper") -tk.reset() +reset() describe("test the delay logic", () => { + const config = getConfig() + + beforeAll(async () => { + await config.init() + }) + it("should be able to run the delay", async () => { const time = 100 const before = Date.now() - await setup.runStep(setup.actions.DELAY.stepId, { time: time }) + await runStep(config, actions.DELAY.stepId, { time: time }) const now = Date.now() // divide by two just so that test will always pass as long as there was some sort of delay expect(now - before).toBeGreaterThanOrEqual(time / 2) diff --git a/packages/server/src/automations/tests/deleteRow.spec.ts b/packages/server/src/automations/tests/deleteRow.spec.ts index a96aa7329f..dd13aa49c1 100644 --- a/packages/server/src/automations/tests/deleteRow.spec.ts +++ b/packages/server/src/automations/tests/deleteRow.spec.ts @@ -1,4 +1,4 @@ -const setup = require("./utilities") +import * as setup from "./utilities" describe("test the delete row action", () => { let table: any @@ -20,32 +20,29 @@ describe("test the delete row action", () => { afterAll(setup.afterAll) it("should be able to run the action", async () => { - const res = await setup.runStep(setup.actions.DELETE_ROW.stepId, inputs) + const res = await setup.runStep( + config, + setup.actions.DELETE_ROW.stepId, + inputs + ) expect(res.success).toEqual(true) expect(res.response).toBeDefined() expect(res.row._id).toEqual(row._id) - let error - try { - await config.getRow(table._id, res.row._id) - } catch (err) { - error = err - } - expect(error).toBeDefined() }) it("check usage quota attempts", async () => { await setup.runInProd(async () => { - await setup.runStep(setup.actions.DELETE_ROW.stepId, inputs) + await setup.runStep(config, setup.actions.DELETE_ROW.stepId, inputs) }) }) it("should check invalid inputs return an error", async () => { - const res = await setup.runStep(setup.actions.DELETE_ROW.stepId, {}) + const res = await setup.runStep(config, setup.actions.DELETE_ROW.stepId, {}) expect(res.success).toEqual(false) }) it("should return an error when table doesn't exist", async () => { - const res = await setup.runStep(setup.actions.DELETE_ROW.stepId, { + const res = await setup.runStep(config, setup.actions.DELETE_ROW.stepId, { tableId: "invalid", id: "invalid", revision: "invalid", diff --git a/packages/server/src/automations/tests/discord.spec.ts b/packages/server/src/automations/tests/discord.spec.ts index 07eab7205c..491fe0fb25 100644 --- a/packages/server/src/automations/tests/discord.spec.ts +++ b/packages/server/src/automations/tests/discord.spec.ts @@ -16,7 +16,7 @@ describe("test the outgoing webhook action", () => { it("should be able to run the action", async () => { nock("http://www.example.com/").post("/").reply(200, { foo: "bar" }) - const res = await runStep(actions.discord.stepId, { + const res = await runStep(config, actions.discord.stepId, { url: "http://www.example.com", username: "joe_bloggs", }) diff --git a/packages/server/src/automations/tests/executeQuery.spec.ts b/packages/server/src/automations/tests/executeQuery.spec.ts index 1236cb280c..12bbe892a0 100644 --- a/packages/server/src/automations/tests/executeQuery.spec.ts +++ b/packages/server/src/automations/tests/executeQuery.spec.ts @@ -8,7 +8,10 @@ import { Knex } from "knex" import { generator } from "@budibase/backend-core/tests" datasourceDescribe( - { name: "execute query action", exclude: [DatabaseName.MONGODB] }, + { + name: "execute query action", + exclude: [DatabaseName.MONGODB, DatabaseName.SQS], + }, ({ config, dsProvider }) => { let tableName: string let client: Knex @@ -35,28 +38,38 @@ datasourceDescribe( await client.schema.dropTable(tableName) }) - afterAll(setup.afterAll) - it("should be able to execute a query", async () => { - let res = await setup.runStep(setup.actions.EXECUTE_QUERY.stepId, { - query: { queryId: query._id }, - }) + let res = await setup.runStep( + config, + setup.actions.EXECUTE_QUERY.stepId, + { + query: { queryId: query._id }, + } + ) expect(res.response).toEqual([{ a: "string", b: 1 }]) expect(res.success).toEqual(true) }) it("should handle a null query value", async () => { - let res = await setup.runStep(setup.actions.EXECUTE_QUERY.stepId, { - query: null, - }) + let res = await setup.runStep( + config, + setup.actions.EXECUTE_QUERY.stepId, + { + query: null, + } + ) expect(res.response.message).toEqual("Invalid inputs") expect(res.success).toEqual(false) }) it("should handle an error executing a query", async () => { - let res = await setup.runStep(setup.actions.EXECUTE_QUERY.stepId, { - query: { queryId: "wrong_id" }, - }) + let res = await setup.runStep( + config, + setup.actions.EXECUTE_QUERY.stepId, + { + query: { queryId: "wrong_id" }, + } + ) expect(res.response).toBeDefined() expect(res.success).toEqual(false) }) diff --git a/packages/server/src/automations/tests/executeScript.spec.js b/packages/server/src/automations/tests/executeScript.spec.ts similarity index 70% rename from packages/server/src/automations/tests/executeScript.spec.js rename to packages/server/src/automations/tests/executeScript.spec.ts index 42f4f5776f..fa30e642bc 100644 --- a/packages/server/src/automations/tests/executeScript.spec.js +++ b/packages/server/src/automations/tests/executeScript.spec.ts @@ -1,15 +1,15 @@ -const setup = require("./utilities") +import { getConfig, afterAll as _afterAll, runStep, actions } from "./utilities" describe("test the execute script action", () => { - let config = setup.getConfig() + let config = getConfig() beforeAll(async () => { await config.init() }) - afterAll(setup.afterAll) + afterAll(_afterAll) it("should be able to execute a script", async () => { - const res = await setup.runStep(setup.actions.EXECUTE_SCRIPT.stepId, { + const res = await runStep(config, actions.EXECUTE_SCRIPT.stepId, { code: "return 1 + 1", }) expect(res.value).toEqual(2) @@ -17,7 +17,7 @@ describe("test the execute script action", () => { }) it("should handle a null value", async () => { - const res = await setup.runStep(setup.actions.EXECUTE_SCRIPT.stepId, { + const res = await runStep(config, actions.EXECUTE_SCRIPT.stepId, { code: null, }) expect(res.response.message).toEqual("Invalid inputs") @@ -25,8 +25,9 @@ describe("test the execute script action", () => { }) it("should be able to get a value from context", async () => { - const res = await setup.runStep( - setup.actions.EXECUTE_SCRIPT.stepId, + const res = await runStep( + config, + actions.EXECUTE_SCRIPT.stepId, { code: "return steps.map(d => d.value)", }, @@ -40,7 +41,7 @@ describe("test the execute script action", () => { }) it("should be able to handle an error gracefully", async () => { - const res = await setup.runStep(setup.actions.EXECUTE_SCRIPT.stepId, { + const res = await runStep(config, actions.EXECUTE_SCRIPT.stepId, { code: "return something.map(x => x.name)", }) expect(res.response).toEqual("ReferenceError: something is not defined") diff --git a/packages/server/src/automations/tests/filter.spec.ts b/packages/server/src/automations/tests/filter.spec.ts index c2c5d13e2d..a1ed72dd74 100644 --- a/packages/server/src/automations/tests/filter.spec.ts +++ b/packages/server/src/automations/tests/filter.spec.ts @@ -2,13 +2,19 @@ import * as setup from "./utilities" import { FilterConditions } from "../steps/filter" describe("test the filter logic", () => { + const config = setup.getConfig() + + beforeAll(async () => { + await config.init() + }) + async function checkFilter( field: any, condition: string, value: any, pass = true ) { - let res = await setup.runStep(setup.actions.FILTER.stepId, { + let res = await setup.runStep(config, setup.actions.FILTER.stepId, { field, condition, value, diff --git a/packages/server/src/automations/tests/make.spec.ts b/packages/server/src/automations/tests/make.spec.ts index 388b197c7f..414ac676d5 100644 --- a/packages/server/src/automations/tests/make.spec.ts +++ b/packages/server/src/automations/tests/make.spec.ts @@ -16,7 +16,7 @@ describe("test the outgoing webhook action", () => { it("should be able to run the action", async () => { nock("http://www.example.com/").post("/").reply(200, { foo: "bar" }) - const res = await runStep(actions.integromat.stepId, { + const res = await runStep(config, actions.integromat.stepId, { url: "http://www.example.com", }) expect(res.response.foo).toEqual("bar") @@ -38,7 +38,7 @@ describe("test the outgoing webhook action", () => { .post("/", payload) .reply(200, { foo: "bar" }) - const res = await runStep(actions.integromat.stepId, { + const res = await runStep(config, actions.integromat.stepId, { body: { value: JSON.stringify(payload) }, url: "http://www.example.com", }) @@ -47,7 +47,7 @@ describe("test the outgoing webhook action", () => { }) it("should return a 400 if the JSON payload string is malformed", async () => { - const res = await runStep(actions.integromat.stepId, { + const res = await runStep(config, actions.integromat.stepId, { body: { value: "{ invalid json }" }, url: "http://www.example.com", }) diff --git a/packages/server/src/automations/tests/n8n.spec.ts b/packages/server/src/automations/tests/n8n.spec.ts index 0c18f313b1..5f27e4323a 100644 --- a/packages/server/src/automations/tests/n8n.spec.ts +++ b/packages/server/src/automations/tests/n8n.spec.ts @@ -16,7 +16,7 @@ describe("test the outgoing webhook action", () => { it("should be able to run the action and default to 'get'", async () => { nock("http://www.example.com/").get("/").reply(200, { foo: "bar" }) - const res = await runStep(actions.n8n.stepId, { + const res = await runStep(config, actions.n8n.stepId, { url: "http://www.example.com", body: { test: "IGNORE_ME", @@ -30,7 +30,7 @@ describe("test the outgoing webhook action", () => { nock("http://www.example.com/") .post("/", { name: "Adam", age: 9 }) .reply(200) - const res = await runStep(actions.n8n.stepId, { + const res = await runStep(config, actions.n8n.stepId, { body: { value: JSON.stringify({ name: "Adam", age: 9 }), }, @@ -42,7 +42,7 @@ describe("test the outgoing webhook action", () => { it("should return a 400 if the JSON payload string is malformed", async () => { const payload = `{ value1 1 }` - const res = await runStep(actions.n8n.stepId, { + const res = await runStep(config, actions.n8n.stepId, { value1: "ONE", body: { value: payload, @@ -59,7 +59,7 @@ describe("test the outgoing webhook action", () => { nock("http://www.example.com/") .head("/", body => body === "") .reply(200) - const res = await runStep(actions.n8n.stepId, { + const res = await runStep(config, actions.n8n.stepId, { url: "http://www.example.com", method: "HEAD", body: { diff --git a/packages/server/src/automations/tests/openai.spec.ts b/packages/server/src/automations/tests/openai.spec.ts index 909a3e0b8b..8119750f8b 100644 --- a/packages/server/src/automations/tests/openai.spec.ts +++ b/packages/server/src/automations/tests/openai.spec.ts @@ -62,13 +62,13 @@ describe("test the openai action", () => { afterAll(_afterAll) it("should be able to receive a response from ChatGPT given a prompt", async () => { - const res = await runStep("OPENAI", { prompt: OPENAI_PROMPT }) + const res = await runStep(config, "OPENAI", { prompt: OPENAI_PROMPT }) expect(res.response).toEqual("This is a test") expect(res.success).toBeTruthy() }) it("should present the correct error message when a prompt is not provided", async () => { - const res = await runStep("OPENAI", { prompt: null }) + const res = await runStep(config, "OPENAI", { prompt: null }) expect(res.response).toEqual( "Budibase OpenAI Automation Failed: No prompt supplied" ) @@ -91,7 +91,7 @@ describe("test the openai action", () => { } as any) ) - const res = await runStep("OPENAI", { + const res = await runStep(config, "OPENAI", { prompt: OPENAI_PROMPT, }) @@ -106,7 +106,7 @@ describe("test the openai action", () => { jest.spyOn(pro.features, "isAICustomConfigsEnabled").mockResolvedValue(true) const prompt = "What is the meaning of life?" - await runStep("OPENAI", { + await runStep(config, "OPENAI", { model: "gpt-4o-mini", prompt, }) diff --git a/packages/server/src/automations/tests/outgoingWebhook.spec.ts b/packages/server/src/automations/tests/outgoingWebhook.spec.ts index 0e26927c55..995ab24bac 100644 --- a/packages/server/src/automations/tests/outgoingWebhook.spec.ts +++ b/packages/server/src/automations/tests/outgoingWebhook.spec.ts @@ -18,7 +18,7 @@ describe("test the outgoing webhook action", () => { nock("http://www.example.com") .post("/", { a: 1 }) .reply(200, { foo: "bar" }) - const res = await runStep(actions.OUTGOING_WEBHOOK.stepId, { + const res = await runStep(config, actions.OUTGOING_WEBHOOK.stepId, { requestMethod: "POST", url: "www.example.com", requestBody: JSON.stringify({ a: 1 }), @@ -28,7 +28,7 @@ describe("test the outgoing webhook action", () => { }) it("should return an error if something goes wrong in fetch", async () => { - const res = await runStep(actions.OUTGOING_WEBHOOK.stepId, { + const res = await runStep(config, actions.OUTGOING_WEBHOOK.stepId, { requestMethod: "GET", url: "www.invalid.com", }) diff --git a/packages/server/src/automations/tests/queryRows.spec.ts b/packages/server/src/automations/tests/queryRows.spec.ts index 6b9113a309..12611d3f90 100644 --- a/packages/server/src/automations/tests/queryRows.spec.ts +++ b/packages/server/src/automations/tests/queryRows.spec.ts @@ -33,7 +33,11 @@ describe("Test a query step automation", () => { sortOrder: "ascending", limit: 10, } - const res = await setup.runStep(setup.actions.QUERY_ROWS.stepId, inputs) + const res = await setup.runStep( + config, + setup.actions.QUERY_ROWS.stepId, + inputs + ) expect(res.success).toBe(true) expect(res.rows).toBeDefined() expect(res.rows.length).toBe(2) @@ -48,7 +52,11 @@ describe("Test a query step automation", () => { sortOrder: "ascending", limit: 10, } - const res = await setup.runStep(setup.actions.QUERY_ROWS.stepId, inputs) + const res = await setup.runStep( + config, + setup.actions.QUERY_ROWS.stepId, + inputs + ) expect(res.success).toBe(true) expect(res.rows).toBeDefined() expect(res.rows.length).toBe(2) @@ -65,7 +73,11 @@ describe("Test a query step automation", () => { limit: 10, onEmptyFilter: "none", } - const res = await setup.runStep(setup.actions.QUERY_ROWS.stepId, inputs) + const res = await setup.runStep( + config, + setup.actions.QUERY_ROWS.stepId, + inputs + ) expect(res.success).toBe(false) expect(res.rows).toBeDefined() expect(res.rows.length).toBe(0) @@ -85,7 +97,11 @@ describe("Test a query step automation", () => { sortOrder: "ascending", limit: 10, } - const res = await setup.runStep(setup.actions.QUERY_ROWS.stepId, inputs) + const res = await setup.runStep( + config, + setup.actions.QUERY_ROWS.stepId, + inputs + ) expect(res.success).toBe(false) expect(res.rows).toBeDefined() expect(res.rows.length).toBe(0) @@ -100,7 +116,11 @@ describe("Test a query step automation", () => { sortOrder: "ascending", limit: 10, } - const res = await setup.runStep(setup.actions.QUERY_ROWS.stepId, inputs) + const res = await setup.runStep( + config, + setup.actions.QUERY_ROWS.stepId, + inputs + ) expect(res.success).toBe(true) expect(res.rows).toBeDefined() expect(res.rows.length).toBe(2) diff --git a/packages/server/src/automations/tests/sendSmtpEmail.spec.ts b/packages/server/src/automations/tests/sendSmtpEmail.spec.ts index f96abde4e6..2977e8d64f 100644 --- a/packages/server/src/automations/tests/sendSmtpEmail.spec.ts +++ b/packages/server/src/automations/tests/sendSmtpEmail.spec.ts @@ -18,7 +18,7 @@ function generateResponse(to: string, from: string) { } } -const setup = require("./utilities") +import * as setup from "./utilities" describe("test the outgoing webhook action", () => { let inputs @@ -58,6 +58,7 @@ describe("test the outgoing webhook action", () => { } let resp = generateResponse(inputs.to, inputs.from) const res = await setup.runStep( + config, setup.actions.SEND_EMAIL_SMTP.stepId, inputs ) diff --git a/packages/server/src/automations/tests/serverLog.spec.js b/packages/server/src/automations/tests/serverLog.spec.ts similarity index 60% rename from packages/server/src/automations/tests/serverLog.spec.js rename to packages/server/src/automations/tests/serverLog.spec.ts index bb2531a65a..c2c1c385b6 100644 --- a/packages/server/src/automations/tests/serverLog.spec.js +++ b/packages/server/src/automations/tests/serverLog.spec.ts @@ -1,8 +1,8 @@ -const setup = require("./utilities") +import { getConfig, afterAll as _afterAll, runStep, actions } from "./utilities" describe("test the server log action", () => { - let config = setup.getConfig() - let inputs + let config = getConfig() + let inputs: any beforeAll(async () => { await config.init() @@ -10,10 +10,10 @@ describe("test the server log action", () => { text: "log message", } }) - afterAll(setup.afterAll) + afterAll(_afterAll) it("should be able to log the text", async () => { - let res = await setup.runStep(setup.actions.SERVER_LOG.stepId, inputs) + let res = await runStep(config, actions.SERVER_LOG.stepId, inputs) expect(res.message).toEqual(`App ${config.getAppId()} - ${inputs.text}`) expect(res.success).toEqual(true) }) diff --git a/packages/server/src/automations/tests/triggerAutomationRun.spec.ts b/packages/server/src/automations/tests/triggerAutomationRun.spec.ts index 9d699e15fa..e4d93d200f 100644 --- a/packages/server/src/automations/tests/triggerAutomationRun.spec.ts +++ b/packages/server/src/automations/tests/triggerAutomationRun.spec.ts @@ -29,6 +29,7 @@ describe("Test triggering an automation from another automation", () => { }, } const res = await setup.runStep( + config, setup.actions.TRIGGER_AUTOMATION_RUN.stepId, inputs ) @@ -44,6 +45,7 @@ describe("Test triggering an automation from another automation", () => { }, } const res = await setup.runStep( + config, setup.actions.TRIGGER_AUTOMATION_RUN.stepId, inputs ) diff --git a/packages/server/src/automations/tests/updateRow.spec.ts b/packages/server/src/automations/tests/updateRow.spec.ts index 76823e8a11..457bf60533 100644 --- a/packages/server/src/automations/tests/updateRow.spec.ts +++ b/packages/server/src/automations/tests/updateRow.spec.ts @@ -34,7 +34,11 @@ describe("test the update row action", () => { afterAll(setup.afterAll) it("should be able to run the action", async () => { - const res = await setup.runStep(setup.actions.UPDATE_ROW.stepId, inputs) + const res = await setup.runStep( + config, + setup.actions.UPDATE_ROW.stepId, + inputs + ) expect(res.success).toEqual(true) const updatedRow = await config.api.row.get(table._id!, res.id) expect(updatedRow.name).toEqual("Updated name") @@ -42,12 +46,12 @@ describe("test the update row action", () => { }) it("should check invalid inputs return an error", async () => { - const res = await setup.runStep(setup.actions.UPDATE_ROW.stepId, {}) + const res = await setup.runStep(config, setup.actions.UPDATE_ROW.stepId, {}) expect(res.success).toEqual(false) }) it("should return an error when table doesn't exist", async () => { - const res = await setup.runStep(setup.actions.UPDATE_ROW.stepId, { + const res = await setup.runStep(config, setup.actions.UPDATE_ROW.stepId, { row: { _id: "invalid" }, rowId: "invalid", }) @@ -90,16 +94,20 @@ describe("test the update row action", () => { expect(getResp.user1[0]._id).toEqual(user1._id) expect(getResp.user2[0]._id).toEqual(user2._id) - let stepResp = await setup.runStep(setup.actions.UPDATE_ROW.stepId, { - rowId: row._id, - row: { - _id: row._id, - _rev: row._rev, - tableId: row.tableId, - user1: [user2._id], - user2: "", - }, - }) + let stepResp = await setup.runStep( + config, + setup.actions.UPDATE_ROW.stepId, + { + rowId: row._id, + row: { + _id: row._id, + _rev: row._rev, + tableId: row.tableId, + user1: [user2._id], + user2: "", + }, + } + ) expect(stepResp.success).toEqual(true) getResp = await config.api.row.get(table._id!, row._id!) @@ -143,23 +151,27 @@ describe("test the update row action", () => { expect(getResp.user1[0]._id).toEqual(user1._id) expect(getResp.user2[0]._id).toEqual(user2._id) - let stepResp = await setup.runStep(setup.actions.UPDATE_ROW.stepId, { - rowId: row._id, - row: { - _id: row._id, - _rev: row._rev, - tableId: row.tableId, - user1: [user2._id], - user2: "", - }, - meta: { - fields: { - user2: { - clearRelationships: true, + let stepResp = await setup.runStep( + config, + setup.actions.UPDATE_ROW.stepId, + { + rowId: row._id, + row: { + _id: row._id, + _rev: row._rev, + tableId: row.tableId, + user1: [user2._id], + user2: "", + }, + meta: { + fields: { + user2: { + clearRelationships: true, + }, }, }, - }, - }) + } + ) expect(stepResp.success).toEqual(true) getResp = await config.api.row.get(table._id!, row._id!) diff --git a/packages/server/src/automations/tests/utilities/index.ts b/packages/server/src/automations/tests/utilities/index.ts index 0ce3e02017..16c23f5db1 100644 --- a/packages/server/src/automations/tests/utilities/index.ts +++ b/packages/server/src/automations/tests/utilities/index.ts @@ -1,4 +1,4 @@ -import TestConfig from "../../../tests/utilities/TestConfiguration" +import TestConfiguration from "../../../tests/utilities/TestConfiguration" import { context } from "@budibase/backend-core" import { BUILTIN_ACTION_DEFINITIONS, getAction } from "../../actions" import emitter from "../../../events/index" @@ -6,11 +6,11 @@ import env from "../../../environment" import { AutomationActionStepId, Datasource } from "@budibase/types" import { Knex } from "knex" -let config: TestConfig +let config: TestConfiguration -export function getConfig(): TestConfig { +export function getConfig(): TestConfiguration { if (!config) { - config = new TestConfig(true) + config = new TestConfiguration(true) } return config } @@ -33,7 +33,12 @@ export async function runInProd(fn: any) { } } -export async function runStep(stepId: string, inputs: any, stepContext?: any) { +export async function runStep( + config: TestConfiguration, + stepId: string, + inputs: any, + stepContext?: any +) { async function run() { let step = await getAction(stepId as AutomationActionStepId) expect(step).toBeDefined() @@ -49,7 +54,7 @@ export async function runStep(stepId: string, inputs: any, stepContext?: any) { emitter, }) } - if (config?.appId) { + if (config.appId) { return context.doInContext(config?.appId, async () => { return run() }) @@ -59,7 +64,7 @@ export async function runStep(stepId: string, inputs: any, stepContext?: any) { } export async function saveTestQuery( - config: TestConfig, + config: TestConfiguration, client: Knex, tableName: string, datasource: Datasource diff --git a/packages/server/src/automations/tests/zapier.spec.ts b/packages/server/src/automations/tests/zapier.spec.ts index a7dc7d3eae..1288e7efec 100644 --- a/packages/server/src/automations/tests/zapier.spec.ts +++ b/packages/server/src/automations/tests/zapier.spec.ts @@ -16,7 +16,7 @@ describe("test the outgoing webhook action", () => { it("should be able to run the action", async () => { nock("http://www.example.com/").post("/").reply(200, { foo: "bar" }) - const res = await runStep(actions.zapier.stepId, { + const res = await runStep(config, actions.zapier.stepId, { url: "http://www.example.com", }) expect(res.response.foo).toEqual("bar") @@ -38,7 +38,7 @@ describe("test the outgoing webhook action", () => { .post("/", { ...payload, platform: "budibase" }) .reply(200, { foo: "bar" }) - const res = await runStep(actions.zapier.stepId, { + const res = await runStep(config, actions.zapier.stepId, { body: { value: JSON.stringify(payload) }, url: "http://www.example.com", }) @@ -47,7 +47,7 @@ describe("test the outgoing webhook action", () => { }) it("should return a 400 if the JSON payload string is malformed", async () => { - const res = await runStep(actions.zapier.stepId, { + const res = await runStep(config, actions.zapier.stepId, { body: { value: "{ invalid json }" }, url: "http://www.example.com", })