1
0
Fork 0
mirror of synced 2024-09-19 18:59:06 +12:00

Test passing maybe?

This commit is contained in:
Sam Rose 2024-06-27 15:46:09 +01:00
parent 892c64ca3b
commit 238bab93f9
No known key found for this signature in database
27 changed files with 255 additions and 194 deletions

View file

@ -3,13 +3,16 @@ import TestConfiguration from "../../../../tests/utilities/TestConfiguration"
import { importToRows } from "../utils"
describe("utils", () => {
const config = new TestConfiguration()
let config: TestConfiguration
beforeEach(async () => {
config = new TestConfiguration()
await config.init()
})
afterAll(config.end)
afterEach(() => {
config.end()
})
describe("importToRows", () => {
it("consecutive row have consecutive auto ids", async () => {

View file

@ -1,9 +1,47 @@
import TestConfiguration from "../../../../../src/tests/utilities/TestConfiguration"
import { generateMakeRequest, MakeRequestResponse } from "./utils"
import { User } from "@budibase/types"
import { mocks } from "@budibase/backend-core/tests"
import * as workerRequests from "../../../../utilities/workerRequests"
import TestConfiguration from "../../../../../src/tests/utilities/TestConfiguration"
function user() {
return {
_id: "user",
_rev: "rev",
createdAt: Date.now(),
email: "test@example.com",
roles: {},
tenantId: "default",
status: "active",
}
}
jest.mock("../../../../utilities/workerRequests", () => ({
getGlobalUsers: jest.fn(() => {
return {
_id: "us_uuid1",
}
}),
getGlobalSelf: jest.fn(() => {
return {
_id: "us_uuid1",
}
}),
allGlobalUsers: jest.fn(() => {
return [user()]
}),
readGlobalUser: jest.fn(() => {
return user()
}),
saveGlobalUser: jest.fn(() => {
return { _id: "user", _rev: "rev" }
}),
deleteGlobalUser: jest.fn(() => {
return { message: "deleted user" }
}),
removeAppFromUserRoles: jest.fn(),
}))
const mockedWorkerReq = jest.mocked(workerRequests)

View file

@ -14,7 +14,7 @@ import sdk from "../../../sdk"
import { Automation } from "@budibase/types"
import { mocks } from "@budibase/backend-core/tests"
import { FilterConditions } from "../../../automations/steps/filter"
import TestConfiguration from "src/tests/utilities/TestConfiguration"
import TestConfiguration from "../../../../src/tests/utilities/TestConfiguration"
const MAX_RETRIES = 4
let {

View file

@ -17,7 +17,7 @@ import {
} from "@budibase/types"
import { structures } from "./utilities"
import { generator, mocks } from "@budibase/backend-core/tests"
import TestConfiguration from "src/tests/utilities/TestConfiguration"
import TestConfiguration from "../../../../src/tests/utilities/TestConfiguration"
const { basicRow } = structures
const { BUILTIN_ROLE_IDS } = roles

View file

@ -20,14 +20,15 @@ const mockUploadDirectory = objectStore.uploadDirectory as jest.Mock
const mockDeleteFolder = objectStore.deleteFolder as jest.Mock
describe("/plugins", () => {
const config = new TestConfiguration()
let config: TestConfiguration
beforeEach(async () => {
config = new TestConfiguration()
await config.init()
jest.clearAllMocks()
})
afterAll(() => {
afterEach(() => {
config.end()
})

View file

@ -7,11 +7,12 @@ import TestConfiguration from "../../../../src/tests/utilities/TestConfiguration
const { basicWebhook, basicAutomation, collectAutomation } = structures
describe("/webhooks", () => {
const config = new TestConfiguration()
let config: TestConfiguration
let webhook: Webhook
let cleanupEnv: () => void
const setupTest = async () => {
beforeEach(async () => {
config = new TestConfiguration()
cleanupEnv = config.setEnv({ SELF_HOSTED: "true" })
await config.init()
const autoConfig = basicAutomation()
@ -22,11 +23,9 @@ describe("/webhooks", () => {
autoConfig.definition.trigger.inputs = {}
await config.createAutomation(autoConfig)
webhook = await config.createWebhook()
}
})
beforeAll(setupTest)
afterAll(() => {
afterEach(() => {
config.end()
cleanupEnv()
})
@ -55,8 +54,6 @@ describe("/webhooks", () => {
})
describe("fetch", () => {
beforeAll(setupTest)
it("returns the correct routing for basic user", async () => {
const res = await config
.request!.get(`/api/webhooks`)
@ -77,8 +74,6 @@ describe("/webhooks", () => {
})
describe("delete", () => {
beforeAll(setupTest)
it("should successfully delete", async () => {
const res = await config
.request!.delete(`/api/webhooks/${webhook._id}/${webhook._rev}`)
@ -99,8 +94,6 @@ describe("/webhooks", () => {
})
describe("build schema", () => {
beforeAll(setupTest)
it("should allow building a schema", async () => {
const res = await config
.request!.post(

View file

@ -7,14 +7,10 @@ import TestConfiguration from "../../../src/tests/utilities/TestConfiguration"
const futureTimestamp = `20500101174029`
describe("migrationsProcessor", () => {
const config = new TestConfiguration()
beforeAll(async () => {
await config.init()
})
let config: TestConfiguration | undefined = undefined
afterAll(() => {
config.end()
config?.end()
})
it("running migrations will update the latest applied migration", async () => {
@ -24,6 +20,9 @@ describe("migrationsProcessor", () => {
{ id: `${futureTimestamp}_125`, func: async () => {} },
]
config = new TestConfiguration()
await config.init()
const appId = config.getAppId()
await config.doInContext(appId, () =>
@ -45,6 +44,9 @@ describe("migrationsProcessor", () => {
},
]
config = new TestConfiguration()
await config.init()
const appId = config.getAppId()
await expect(

View file

@ -51,6 +51,7 @@ const ACTION_IMPLS: Record<
integromat: make.run,
n8n: n8n.run,
}
export const BUILTIN_ACTION_DEFINITIONS: Record<string, AutomationStepSchema> =
{
SEND_EMAIL_SMTP: sendSmtpEmail.definition,

View file

@ -1,6 +1,8 @@
import * as setup from "./utilities"
import { runStep } from "./utilities"
import { basicTableWithAttachmentField } from "../../tests/utilities/structures"
import { objectStore } from "@budibase/backend-core"
import TestConfiguration from "../../../src/tests/utilities/TestConfiguration"
import { AutomationActionStepId } from "@budibase/types"
async function uploadTestFile(filename: string) {
let bucket = "testbucket"
@ -13,12 +15,15 @@ async function uploadTestFile(filename: string) {
return presignedUrl
}
describe("test the create row action", () => {
let config: TestConfiguration
let table: any
let row: any
let config = setup.getConfig()
beforeEach(async () => {
config = new TestConfiguration()
await config.init()
table = await config.createTable()
row = {
@ -28,10 +33,12 @@ describe("test the create row action", () => {
}
})
afterAll(setup.afterAll)
afterAll(() => {
config.end()
})
it("should be able to run the action", async () => {
const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, {
const res = await runStep(config, AutomationActionStepId.CREATE_ROW, {
row,
})
expect(res.id).toBeDefined()
@ -43,7 +50,7 @@ describe("test the create row action", () => {
})
it("should return an error (not throw) when bad info provided", async () => {
const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, {
const res = await runStep(config, AutomationActionStepId.CREATE_ROW, {
row: {
tableId: "invalid",
invalid: "invalid",
@ -53,7 +60,7 @@ describe("test the create row action", () => {
})
it("should check invalid inputs return an error", async () => {
const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, {})
const res = await runStep(config, AutomationActionStepId.CREATE_ROW, {})
expect(res.success).toEqual(false)
})
@ -76,7 +83,7 @@ describe("test the create row action", () => {
]
attachmentRow.file_attachment = attachmentObject
const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, {
const res = await runStep(config, AutomationActionStepId.CREATE_ROW, {
row: attachmentRow,
})
@ -111,7 +118,7 @@ describe("test the create row action", () => {
}
attachmentRow.single_file_attachment = attachmentObject
const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, {
const res = await runStep(config, AutomationActionStepId.CREATE_ROW, {
row: attachmentRow,
})
@ -146,7 +153,7 @@ describe("test the create row action", () => {
}
attachmentRow.single_file_attachment = attachmentObject
const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, {
const res = await runStep(config, AutomationActionStepId.CREATE_ROW, {
row: attachmentRow,
})

View file

@ -1,5 +1,6 @@
import { AutomationActionStepId } from "@budibase/types"
import TestConfiguration from "../../tests/utilities/TestConfiguration"
import { runStep, actions } from "./utilities"
import { runStep } from "./utilities"
describe("test the delay logic", () => {
const config = new TestConfiguration()
@ -14,7 +15,7 @@ describe("test the delay logic", () => {
it("should be able to run the delay", async () => {
const start = performance.now()
await runStep(config, actions.DELAY.stepId, { time: 100 })
await runStep(config, AutomationActionStepId.DELAY, { time: 100 })
const end = performance.now()
expect(end - start).toBeGreaterThanOrEqual(95)

View file

@ -1,5 +1,6 @@
import { AutomationActionStepId } from "@budibase/types"
import TestConfiguration from "../../../src/tests/utilities/TestConfiguration"
import { actions, runStep } from "./utilities"
import { runStep } from "./utilities"
describe("test the delete row action", () => {
let table: any
@ -24,11 +25,17 @@ describe("test the delete row action", () => {
})
it("should be able to run the action", async () => {
const res = await runStep(config, actions.DELETE_ROW.stepId, inputs)
expect(res).toEqual({ success: true })
expect(res.success).toEqual(true)
expect(res.response).toBeDefined()
expect(res.row._id).toEqual(row._id)
const res = await runStep(config, AutomationActionStepId.DELETE_ROW, inputs)
expect(res).toEqual(
expect.objectContaining({
success: true,
response: expect.any(Object),
row: expect.objectContaining({
_id: row._id,
}),
})
)
await config.api.row.get(table._id, res.row._id, {
status: 404,
})
@ -36,21 +43,21 @@ describe("test the delete row action", () => {
it("check usage quota attempts", async () => {
await config.withEnv({ NODE_ENV: "production" }, async () => {
await runStep(config, actions.DELETE_ROW.stepId, inputs)
await runStep(config, AutomationActionStepId.DELETE_ROW, inputs)
})
})
it("should check invalid inputs return an error", async () => {
const res = await runStep(config, actions.DELETE_ROW.stepId, {})
expect(res.success).toEqual(false)
const res = await runStep(config, AutomationActionStepId.DELETE_ROW, {})
expect(res).toEqual(expect.objectContaining({ success: false }))
})
it("should return an error when table doesn't exist", async () => {
const res = await runStep(config, actions.DELETE_ROW.stepId, {
const res = await runStep(config, AutomationActionStepId.DELETE_ROW, {
tableId: "invalid",
id: "invalid",
revision: "invalid",
})
expect(res.success).toEqual(false)
expect(res).toEqual(expect.objectContaining({ success: false }))
})
})

View file

@ -1,5 +1,6 @@
import { AutomationActionStepId } from "@budibase/types"
import TestConfiguration from "../../tests/utilities/TestConfiguration"
import { runStep, actions } from "./utilities"
import { runStep } from "./utilities"
jest.mock("node-fetch")
@ -15,7 +16,7 @@ describe("test the outgoing webhook action", () => {
})
it("should be able to run the action", async () => {
const res = await runStep(config, actions.discord.stepId, {
const res = await runStep(config, AutomationActionStepId.discord, {
username: "joe_bloggs",
url: "http://www.example.com",
})

View file

@ -1,20 +1,13 @@
import { Datasource, Query, SourceName } from "@budibase/types"
import * as setup from "./utilities"
import { DatabaseName, getDatasource } from "../../integrations/tests/utils"
import knex, { Knex } from "knex"
import { AutomationActionStepId, Datasource, Query } from "@budibase/types"
import { runStep } from "./utilities"
import {
DatabaseName,
getDatasource,
knexClient,
} from "../../integrations/tests/utils"
import { Knex } from "knex"
import { generator } from "@budibase/backend-core/tests"
function getKnexClientName(source: SourceName) {
switch (source) {
case SourceName.MYSQL:
return "mysql2"
case SourceName.SQL_SERVER:
return "mssql"
case SourceName.POSTGRES:
return "pg"
}
throw new Error(`Unsupported source: ${source}`)
}
import TestConfiguration from "../../../src/tests/utilities/TestConfiguration"
describe.each(
[
@ -24,21 +17,19 @@ describe.each(
DatabaseName.MARIADB,
].map(name => [name, getDatasource(name)])
)("execute query action (%s)", (_, dsProvider) => {
const config = new TestConfiguration()
let tableName: string
let client: Knex
let datasource: Datasource
let query: Query
let config = setup.getConfig()
beforeAll(async () => {
await config.init()
const ds = await dsProvider
datasource = await config.api.datasource.create(ds)
client = knex({
client: getKnexClientName(ds.source),
connection: ds.config,
})
client = await knexClient(ds)
})
beforeEach(async () => {
@ -66,10 +57,12 @@ describe.each(
await client.schema.dropTable(tableName)
})
afterAll(setup.afterAll)
afterAll(() => {
config.end()
})
it("should be able to execute a query", async () => {
let res = await setup.runStep(config, setup.actions.EXECUTE_QUERY.stepId, {
let res = await runStep(config, AutomationActionStepId.EXECUTE_QUERY, {
query: { queryId: query._id },
})
expect(res.response).toEqual([{ a: "string", b: 1 }])
@ -77,7 +70,7 @@ describe.each(
})
it("should handle a null query value", async () => {
let res = await setup.runStep(config, setup.actions.EXECUTE_QUERY.stepId, {
let res = await runStep(config, AutomationActionStepId.EXECUTE_QUERY, {
query: null,
})
expect(res.response.message).toEqual("Invalid inputs")
@ -85,7 +78,7 @@ describe.each(
})
it("should handle an error executing a query", async () => {
let res = await setup.runStep(config, setup.actions.EXECUTE_QUERY.stepId, {
let res = await runStep(config, AutomationActionStepId.EXECUTE_QUERY, {
query: { queryId: "wrong_id" },
})
expect(res.response).toBeDefined()

View file

@ -1,5 +1,6 @@
import { AutomationActionStepId } from "@budibase/types"
import TestConfiguration from "../../tests/utilities/TestConfiguration"
import { runStep, actions } from "./utilities"
import { runStep } from "./utilities"
describe("test the execute script action", () => {
const config = new TestConfiguration()
@ -13,7 +14,7 @@ describe("test the execute script action", () => {
})
it("should be able to execute a script", async () => {
const res = await runStep(config, actions.EXECUTE_SCRIPT.stepId, {
const res = await runStep(config, AutomationActionStepId.EXECUTE_SCRIPT, {
code: "return 1 + 1",
})
expect(res.value).toEqual(2)
@ -21,7 +22,7 @@ describe("test the execute script action", () => {
})
it("should handle a null value", async () => {
const res = await runStep(config, actions.EXECUTE_SCRIPT.stepId, {
const res = await runStep(config, AutomationActionStepId.EXECUTE_SCRIPT, {
code: null,
})
expect(res.response.message).toEqual("Invalid inputs")
@ -31,7 +32,7 @@ describe("test the execute script action", () => {
it("should be able to get a value from context", async () => {
const res = await runStep(
config,
actions.EXECUTE_SCRIPT.stepId,
AutomationActionStepId.EXECUTE_SCRIPT,
{
code: "return steps.map(d => d.value)",
},
@ -45,7 +46,7 @@ describe("test the execute script action", () => {
})
it("should be able to handle an error gracefully", async () => {
const res = await runStep(config, actions.EXECUTE_SCRIPT.stepId, {
const res = await runStep(config, AutomationActionStepId.EXECUTE_SCRIPT, {
code: "return something.map(x => x.name)",
})
expect(res.response).toEqual("ReferenceError: something is not defined")

View file

@ -1,6 +1,7 @@
import * as setup from "./utilities"
import { FilterConditions } from "../steps/filter"
import TestConfiguration from "src/tests/utilities/TestConfiguration"
import TestConfiguration from "../../../src/tests/utilities/TestConfiguration"
import { AutomationActionStepId } from "@budibase/types"
describe("test the filter logic", () => {
const config = new TestConfiguration()
@ -19,7 +20,7 @@ describe("test the filter logic", () => {
value: any,
pass = true
) {
let res = await setup.runStep(config, setup.actions.FILTER.stepId, {
let res = await setup.runStep(config, AutomationActionStepId.FILTER, {
field,
condition,
value,

View file

@ -1,16 +1,20 @@
import { getConfig, afterAll, runStep, actions } from "./utilities"
import { AutomationActionStepId } from "@budibase/types"
import TestConfiguration from "../../../src/tests/utilities/TestConfiguration"
import { runStep } from "./utilities"
describe("test the outgoing webhook action", () => {
let config = getConfig()
const config = new TestConfiguration()
beforeAll(async () => {
await config.init()
})
afterAll()
afterAll(() => {
config.end()
})
it("should be able to run the action", async () => {
const res = await runStep(config, actions.integromat.stepId, {
const res = await runStep(config, AutomationActionStepId.integromat, {
value1: "test",
url: "http://www.example.com",
})
@ -21,7 +25,7 @@ describe("test the outgoing webhook action", () => {
it("should add the payload props when a JSON string is provided", async () => {
const payload = `{"value1":1,"value2":2,"value3":3,"value4":4,"value5":5,"name":"Adam","age":9}`
const res = await runStep(config, actions.integromat.stepId, {
const res = await runStep(config, AutomationActionStepId.integromat, {
value1: "ONE",
value2: "TWO",
value3: "THREE",
@ -40,7 +44,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(config, actions.integromat.stepId, {
const res = await runStep(config, AutomationActionStepId.integromat, {
value1: "ONE",
body: {
value: payload,

View file

@ -1,16 +1,20 @@
import { getConfig, afterAll, runStep, actions } from "./utilities"
import { AutomationActionStepId } from "@budibase/types"
import TestConfiguration from "../../../src/tests/utilities/TestConfiguration"
import { runStep } from "./utilities"
describe("test the outgoing webhook action", () => {
let config = getConfig()
const config = new TestConfiguration()
beforeAll(async () => {
await config.init()
})
afterAll()
afterAll(() => {
config.end()
})
it("should be able to run the action and default to 'get'", async () => {
const res = await runStep(config, actions.n8n.stepId, {
const res = await runStep(config, AutomationActionStepId.n8n, {
url: "http://www.example.com",
body: {
test: "IGNORE_ME",
@ -24,7 +28,7 @@ describe("test the outgoing webhook action", () => {
it("should add the payload props when a JSON string is provided", async () => {
const payload = `{ "name": "Adam", "age": 9 }`
const res = await runStep(config, actions.n8n.stepId, {
const res = await runStep(config, AutomationActionStepId.n8n, {
body: {
value: payload,
},
@ -39,7 +43,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(config, actions.n8n.stepId, {
const res = await runStep(config, AutomationActionStepId.n8n, {
value1: "ONE",
body: {
value: payload,
@ -53,7 +57,7 @@ describe("test the outgoing webhook action", () => {
})
it("should not append the body if the method is HEAD", async () => {
const res = await runStep(config, actions.n8n.stepId, {
const res = await runStep(config, AutomationActionStepId.n8n, {
url: "http://www.example.com",
method: "HEAD",
body: {

View file

@ -1,7 +1,9 @@
const setup = require("./utilities")
import { runStep } from "./utilities"
import environment from "../../environment"
import openai from "openai"
import TestConfiguration from "../../../src/tests/utilities/TestConfiguration"
import { AutomationActionStepId } from "@budibase/types"
jest.mock(
"openai",
@ -30,7 +32,7 @@ const mockedOpenAIApi = openai.OpenAIApi as jest.MockedClass<
const OPENAI_PROMPT = "What is the meaning of life?"
describe("test the openai action", () => {
let config = setup.getConfig()
const config = new TestConfiguration()
beforeAll(async () => {
await config.init()
@ -40,12 +42,14 @@ describe("test the openai action", () => {
environment.OPENAI_API_KEY = "abc123"
})
afterAll(setup.afterAll)
afterAll(() => {
config.end()
})
it("should present the correct error message when the OPENAI_API_KEY variable isn't set", async () => {
delete environment.OPENAI_API_KEY
let res = await setup.runStep(config, "OPENAI", {
let res = await runStep(config, AutomationActionStepId.OPENAI, {
prompt: OPENAI_PROMPT,
})
expect(res.response).toEqual(
@ -55,7 +59,7 @@ describe("test the openai action", () => {
})
it("should be able to receive a response from ChatGPT given a prompt", async () => {
const res = await setup.runStep(config, "OPENAI", {
const res = await runStep(config, AutomationActionStepId.OPENAI, {
prompt: OPENAI_PROMPT,
})
expect(res.response).toEqual("This is a test")
@ -63,7 +67,7 @@ describe("test the openai action", () => {
})
it("should present the correct error message when a prompt is not provided", async () => {
const res = await setup.runStep(config, "OPENAI", {
const res = await runStep(config, AutomationActionStepId.OPENAI, {
prompt: null,
})
expect(res.response).toEqual(
@ -84,7 +88,7 @@ describe("test the openai action", () => {
} as any)
)
const res = await setup.runStep(config, "OPENAI", {
const res = await runStep(config, AutomationActionStepId.OPENAI, {
prompt: OPENAI_PROMPT,
})

View file

@ -1,5 +1,6 @@
import { AutomationActionStepId } from "@budibase/types"
import TestConfiguration from "../../tests/utilities/TestConfiguration"
import { runStep, actions } from "./utilities"
import { runStep } from "./utilities"
describe("test the outgoing webhook action", () => {
const config = new TestConfiguration()
@ -13,7 +14,7 @@ describe("test the outgoing webhook action", () => {
})
it("should be able to run the action", async () => {
const res = await runStep(config, actions.OUTGOING_WEBHOOK.stepId, {
const res = await runStep(config, AutomationActionStepId.OUTGOING_WEBHOOK, {
requestMethod: "POST",
url: "www.example.com",
requestBody: JSON.stringify({
@ -27,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(config, actions.OUTGOING_WEBHOOK.stepId, {
const res = await runStep(config, AutomationActionStepId.OUTGOING_WEBHOOK, {
requestMethod: "GET",
url: "www.invalid.com",
})

View file

@ -1,6 +1,6 @@
import { Table } from "@budibase/types"
import { runStep, actions } from "./utilities"
import TestConfiguration from "src/tests/utilities/TestConfiguration"
import { AutomationActionStepId, Table } from "@budibase/types"
import { runStep } from "./utilities"
import TestConfiguration from "../../../src/tests/utilities/TestConfiguration"
const NAME = "Test"
@ -37,7 +37,7 @@ describe("Test a query step automation", () => {
sortOrder: "ascending",
limit: 10,
}
const res = await runStep(config, actions.QUERY_ROWS.stepId, inputs)
const res = await runStep(config, AutomationActionStepId.QUERY_ROWS, inputs)
expect(res.success).toBe(true)
expect(res.rows).toBeDefined()
expect(res.rows.length).toBe(2)
@ -52,7 +52,7 @@ describe("Test a query step automation", () => {
sortOrder: "ascending",
limit: 10,
}
const res = await runStep(config, actions.QUERY_ROWS.stepId, inputs)
const res = await runStep(config, AutomationActionStepId.QUERY_ROWS, inputs)
expect(res.success).toBe(true)
expect(res.rows).toBeDefined()
expect(res.rows.length).toBe(2)
@ -69,7 +69,7 @@ describe("Test a query step automation", () => {
limit: 10,
onEmptyFilter: "none",
}
const res = await runStep(config, actions.QUERY_ROWS.stepId, inputs)
const res = await runStep(config, AutomationActionStepId.QUERY_ROWS, inputs)
expect(res.success).toBe(false)
expect(res.rows).toBeDefined()
expect(res.rows.length).toBe(0)
@ -89,7 +89,7 @@ describe("Test a query step automation", () => {
sortOrder: "ascending",
limit: 10,
}
const res = await runStep(config, actions.QUERY_ROWS.stepId, inputs)
const res = await runStep(config, AutomationActionStepId.QUERY_ROWS, inputs)
expect(res.success).toBe(false)
expect(res.rows).toBeDefined()
expect(res.rows.length).toBe(0)
@ -104,7 +104,7 @@ describe("Test a query step automation", () => {
sortOrder: "ascending",
limit: 10,
}
const res = await runStep(config, actions.QUERY_ROWS.stepId, inputs)
const res = await runStep(config, AutomationActionStepId.QUERY_ROWS, inputs)
expect(res.success).toBe(true)
expect(res.rows).toBeDefined()
expect(res.rows.length).toBe(2)

View file

@ -1,4 +1,7 @@
import TestConfiguration from "../../../src/tests/utilities/TestConfiguration"
import * as workerRequests from "../../utilities/workerRequests"
import { runStep } from "./utilities"
import { AutomationActionStepId } from "@budibase/types"
jest.mock("../../utilities/workerRequests", () => ({
sendSmtpEmail: jest.fn(),
@ -18,16 +21,16 @@ function generateResponse(to: string, from: string) {
}
}
const setup = require("./utilities")
describe("test the outgoing webhook action", () => {
let inputs
let config = setup.getConfig()
const config = new TestConfiguration()
beforeAll(async () => {
await config.init()
})
afterAll(setup.afterAll)
afterAll(() => {
config.end()
})
it("should be able to run the action", async () => {
jest
@ -42,7 +45,7 @@ describe("test the outgoing webhook action", () => {
location: "location",
url: "url",
}
inputs = {
const inputs = {
to: "user1@example.com",
from: "admin@example.com",
subject: "hello",
@ -57,9 +60,9 @@ describe("test the outgoing webhook action", () => {
...invite,
}
let resp = generateResponse(inputs.to, inputs.from)
const res = await setup.runStep(
const res = await runStep(
config,
setup.actions.SEND_EMAIL_SMTP.stepId,
AutomationActionStepId.SEND_EMAIL_SMTP,
inputs
)
expect(res.response).toEqual(resp)

View file

@ -1,5 +1,6 @@
import { AutomationActionStepId } from "@budibase/types"
import TestConfiguration from "../../tests/utilities/TestConfiguration"
import { runStep, actions } from "./utilities"
import { runStep } from "./utilities"
describe("test the server log action", () => {
const config = new TestConfiguration()
@ -13,7 +14,7 @@ describe("test the server log action", () => {
})
it("should be able to log the text", async () => {
let res = await runStep(config, actions.SERVER_LOG.stepId, {
let res = await runStep(config, AutomationActionStepId.SERVER_LOG, {
text: "log message",
})
expect(res.message).toEqual(`App ${config.getAppId()} - log message`)

View file

@ -1,21 +1,23 @@
jest.spyOn(global.console, "error")
import * as setup from "./utilities"
import * as automation from "../index"
import { runStep } from "./utilities"
import { init, shutdown } from "../index"
import { serverLogAutomation } from "../../tests/utilities/structures"
import env from "../../environment"
import TestConfiguration from "../../../src/tests/utilities/TestConfiguration"
import { AutomationActionStepId } from "@budibase/types"
describe("Test triggering an automation from another automation", () => {
let config = setup.getConfig()
const config = new TestConfiguration()
beforeAll(async () => {
await automation.init()
await init()
await config.init()
})
afterAll(async () => {
await automation.shutdown()
setup.afterAll()
await shutdown()
config.end()
})
it("should trigger an other server log automation", async () => {
@ -28,9 +30,9 @@ describe("Test triggering an automation from another automation", () => {
timeout: env.getDefaults().AUTOMATION_THREAD_TIMEOUT,
},
}
const res = await setup.runStep(
const res = await runStep(
config,
setup.actions.TRIGGER_AUTOMATION_RUN.stepId,
AutomationActionStepId.TRIGGER_AUTOMATION_RUN,
inputs
)
// Check if the SERVER_LOG step was successful
@ -44,9 +46,9 @@ describe("Test triggering an automation from another automation", () => {
timeout: env.getDefaults().AUTOMATION_THREAD_TIMEOUT,
},
}
const res = await setup.runStep(
const res = await runStep(
config,
setup.actions.TRIGGER_AUTOMATION_RUN.stepId,
AutomationActionStepId.TRIGGER_AUTOMATION_RUN,
inputs
)
expect(res.success).toBe(false)

View file

@ -1,4 +1,5 @@
import {
AutomationActionStepId,
FieldSchema,
FieldType,
INTERNAL_TABLE_SOURCE_ID,
@ -9,7 +10,7 @@ import {
TableSourceType,
} from "@budibase/types"
import { runStep, actions } from "./utilities"
import { runStep } from "./utilities"
import * as uuid from "uuid"
import TestConfiguration from "../../../src/tests/utilities/TestConfiguration"
@ -37,7 +38,7 @@ describe("test the update row action", () => {
})
it("should be able to run the action", async () => {
const res = await runStep(config, actions.UPDATE_ROW.stepId, inputs)
const res = await runStep(config, AutomationActionStepId.UPDATE_ROW, inputs)
expect(res.success).toEqual(true)
const updatedRow = await config.api.row.get(table._id!, res.id)
expect(updatedRow.name).toEqual("Updated name")
@ -45,12 +46,12 @@ describe("test the update row action", () => {
})
it("should check invalid inputs return an error", async () => {
const res = await runStep(config, actions.UPDATE_ROW.stepId, {})
const res = await runStep(config, AutomationActionStepId.UPDATE_ROW, {})
expect(res.success).toEqual(false)
})
it("should return an error when table doesn't exist", async () => {
const res = await runStep(config, actions.UPDATE_ROW.stepId, {
const res = await runStep(config, AutomationActionStepId.UPDATE_ROW, {
row: { _id: "invalid" },
rowId: "invalid",
})
@ -93,7 +94,7 @@ 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 runStep(config, actions.UPDATE_ROW.stepId, {
let stepResp = await runStep(config, AutomationActionStepId.UPDATE_ROW, {
rowId: row._id,
row: {
_id: row._id,
@ -146,7 +147,7 @@ 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 runStep(config, actions.UPDATE_ROW.stepId, {
let stepResp = await runStep(config, AutomationActionStepId.UPDATE_ROW, {
rowId: row._id,
row: {
_id: row._id,

View file

@ -1,42 +1,29 @@
import TestConfig from "../../../tests/utilities/TestConfiguration"
import { BUILTIN_ACTION_DEFINITIONS, getAction } from "../../actions"
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
import { getAction } from "../../actions"
import emitter from "../../../events/index"
let config: TestConfig
export function getConfig(): TestConfig {
if (!config) {
config = new TestConfig(true)
}
return config
}
export function afterAll() {
config.end()
}
import { AutomationActionStepId, AutomationStepInput } from "@budibase/types"
export async function runStep(
config: TestConfig,
stepId: string,
inputs: any,
stepContext?: any
config: TestConfiguration,
stepId: AutomationActionStepId,
inputs: AutomationStepInput["inputs"],
stepContext?: AutomationStepInput["context"]
) {
async function run() {
let step = await getAction(stepId)
expect(step).toBeDefined()
if (!step) {
throw new Error("No step found")
}
return step({
context: stepContext || {},
inputs,
appId: config.getAppId(),
// don't really need an API key, mocked out usage quota, not being tested here
apiKey: "test",
emitter,
return await config.doInContext(config.getAppId(), async () => {
let step = await getAction(stepId)
if (!step) {
throw new Error("No step found")
}
return step({
context: stepContext || {},
inputs,
appId: config.getAppId(),
// don't really need an API key, mocked out usage quota, not being tested here
apiKey: "test",
emitter,
})
})
}
return run()
}
export const actions = BUILTIN_ACTION_DEFINITIONS

View file

@ -1,16 +1,20 @@
import { getConfig, afterAll, runStep, actions } from "./utilities"
import { AutomationActionStepId } from "@budibase/types"
import TestConfiguration from "../../../src/tests/utilities/TestConfiguration"
import { runStep } from "./utilities"
describe("test the outgoing webhook action", () => {
let config = getConfig()
const config = new TestConfiguration()
beforeAll(async () => {
await config.init()
})
afterAll()
afterAll(() => {
config.end()
})
it("should be able to run the action", async () => {
const res = await runStep(config, actions.zapier.stepId, {
const res = await runStep(config, AutomationActionStepId.zapier, {
value1: "test",
url: "http://www.example.com",
})
@ -21,7 +25,7 @@ describe("test the outgoing webhook action", () => {
it("should add the payload props when a JSON string is provided", async () => {
const payload = `{ "value1": 1, "value2": 2, "value3": 3, "value4": 4, "value5": 5, "name": "Adam", "age": 9 }`
const res = await runStep(config, actions.zapier.stepId, {
const res = await runStep(config, AutomationActionStepId.zapier, {
value1: "ONE",
value2: "TWO",
value3: "THREE",
@ -42,7 +46,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(config, actions.zapier.stepId, {
const res = await runStep(config, AutomationActionStepId.zapier, {
value1: "ONE",
body: {
value: payload,

View file

@ -5,8 +5,12 @@ import {
isErrorInOutput,
isRecurring,
} from "../automations/utils"
import * as actions from "../automations/actions"
import * as automationUtils from "../automations/automationUtils"
import { BUILTIN_ACTION_DEFINITIONS, getAction } from "../automations/actions"
import {
stringSplit,
typecastForLooping,
cleanInputValues,
} from "../automations/automationUtils"
import { replaceFakeBindings } from "../automations/loopUtils"
import { default as AutomationEmitter } from "../events/AutomationEmitter"
@ -39,9 +43,6 @@ import env from "../environment"
import tracer from "dd-trace"
threadUtils.threadSetup()
const FILTER_STEP_ID = actions.BUILTIN_ACTION_DEFINITIONS.FILTER.stepId
const LOOP_STEP_ID = actions.BUILTIN_ACTION_DEFINITIONS.LOOP.stepId
const CRON_STEP_ID = triggerDefs.CRON.stepId
const STOPPED_STATUS = { success: true, status: AutomationStatus.STOPPED }
function getLoopIterations(loopStep: LoopStep) {
@ -58,7 +59,7 @@ function getLoopIterations(loopStep: LoopStep) {
// ignore error - wasn't able to parse
}
if (typeof binding === "string") {
return automationUtils.stringSplit(binding).length
return stringSplit(binding).length
}
return 0
}
@ -102,14 +103,14 @@ class Orchestrator {
}
cleanupTriggerOutputs(stepId: string, triggerOutput: TriggerOutput) {
if (stepId === CRON_STEP_ID && !triggerOutput.timestamp) {
if (stepId === triggerDefs.CRON.stepId && !triggerOutput.timestamp) {
triggerOutput.timestamp = Date.now()
}
return triggerOutput
}
async getStepFunctionality(stepId: string) {
let step = await actions.getAction(stepId)
let step = await getAction(stepId)
if (step == null) {
throw `Cannot find automation step by name ${stepId}`
}
@ -198,7 +199,7 @@ class Orchestrator {
const stepObj = { id, stepId, inputs, outputs }
// replacing trigger when disabling CRON
if (
stepId === CRON_STEP_ID &&
stepId === triggerDefs.CRON.stepId &&
outputs.status === AutomationStatus.STOPPED_ERROR
) {
this.executionOutput.trigger = stepObj
@ -309,7 +310,7 @@ class Orchestrator {
}
stepCount++
if (step.stepId === LOOP_STEP_ID) {
if (step.stepId === BUILTIN_ACTION_DEFINITIONS.LOOP.stepId) {
loopStep = step as LoopStep
currentLoopStepIndex = stepCount
continue
@ -329,7 +330,7 @@ class Orchestrator {
iterations: iterationCount,
}
try {
loopStep.inputs.binding = automationUtils.typecastForLooping(
loopStep.inputs.binding = typecastForLooping(
loopStep.inputs as LoopInput
)
} catch (err) {
@ -351,7 +352,7 @@ class Orchestrator {
typeof loopStep.inputs.binding === "string" &&
loopStep.inputs.option === "String"
) {
item = automationUtils.stringSplit(loopStep.inputs.binding)
item = stringSplit(loopStep.inputs.binding)
} else if (Array.isArray(loopStep.inputs.binding)) {
item = loopStep.inputs.binding
}
@ -424,10 +425,7 @@ class Orchestrator {
let stepFn = await this.getStepFunctionality(step.stepId)
let inputs = await processObject(originalStepInput, this._context)
inputs = automationUtils.cleanInputValues(
inputs,
step.schema.inputs
)
inputs = cleanInputValues(inputs, step.schema.inputs)
try {
// appId is always passed
const outputs = await stepFn({
@ -440,7 +438,10 @@ class Orchestrator {
this._context.steps[stepCount] = outputs
// if filter causes us to stop execution don't break the loop, set a var
// so that we can finish iterating through the steps and record that it stopped
if (step.stepId === FILTER_STEP_ID && !outputs.result) {
if (
step.stepId === BUILTIN_ACTION_DEFINITIONS.FILTER.stepId &&
!outputs.result
) {
stopped = true
this.updateExecutionOutput(
step.id,