forked from tsai/budibase
14 changed files with 262 additions and 20 deletions
@ -0,0 +1,18 @@ |
|||||
|
class Email { |
||||
|
constructor() { |
||||
|
this.apiKey = null |
||||
|
} |
||||
|
|
||||
|
setApiKey(apiKey) { |
||||
|
this.apiKey = apiKey |
||||
|
} |
||||
|
|
||||
|
async send(msg) { |
||||
|
if (msg.to === "invalid@test.com") { |
||||
|
throw "Invalid" |
||||
|
} |
||||
|
return msg |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
module.exports = new Email() |
||||
@ -1,17 +1,35 @@ |
|||||
const fetch = jest.requireActual("node-fetch") |
const fetch = jest.requireActual("node-fetch") |
||||
|
|
||||
module.exports = async (url, opts) => { |
module.exports = async (url, opts) => { |
||||
// mocked data based on url
|
function json(body, status = 200) { |
||||
if (url.includes("api/apps")) { |
|
||||
return { |
return { |
||||
|
status, |
||||
json: async () => { |
json: async () => { |
||||
return { |
return body |
||||
app1: { |
|
||||
url: "/app1", |
|
||||
}, |
|
||||
} |
|
||||
}, |
}, |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
|
// mocked data based on url
|
||||
|
if (url.includes("api/apps")) { |
||||
|
return json({ |
||||
|
app1: { |
||||
|
url: "/app1", |
||||
|
}, |
||||
|
}) |
||||
|
} else if (url.includes("test.com")) { |
||||
|
return json({ |
||||
|
body: opts.body, |
||||
|
url, |
||||
|
method: opts.method, |
||||
|
}) |
||||
|
} else if (url.includes("invalid.com")) { |
||||
|
return json( |
||||
|
{ |
||||
|
invalid: true, |
||||
|
}, |
||||
|
404 |
||||
|
) |
||||
|
} |
||||
return fetch(url, opts) |
return fetch(url, opts) |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,14 @@ |
|||||
|
const automation = require("../index") |
||||
|
const usageQuota = require("../../utilities/usageQuota") |
||||
|
|
||||
|
jest.mock("../../utilities/usageQuota") |
||||
|
|
||||
|
describe("Check the primary input functions to automations", () => { |
||||
|
it("should be able to init in builder", async () => { |
||||
|
|
||||
|
}) |
||||
|
|
||||
|
it("should be able to init in cloud", async () => { |
||||
|
|
||||
|
}) |
||||
|
}) |
||||
@ -0,0 +1,58 @@ |
|||||
|
const usageQuota = require("../../utilities/usageQuota") |
||||
|
const env = require("../../environment") |
||||
|
const setup = require("./utilities") |
||||
|
|
||||
|
jest.mock("../../utilities/usageQuota") |
||||
|
|
||||
|
describe("test the delete row action", () => { |
||||
|
let table, row, inputs |
||||
|
let config = setup.getConfig() |
||||
|
|
||||
|
beforeEach(async () => { |
||||
|
await config.init() |
||||
|
table = await config.createTable() |
||||
|
row = await config.createRow() |
||||
|
inputs = { |
||||
|
tableId: table._id, |
||||
|
id: row._id, |
||||
|
revision: row._rev, |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
afterAll(setup.afterAll) |
||||
|
|
||||
|
it("should be able to run the action", async () => { |
||||
|
const res = await setup.runStep(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.id) |
||||
|
} catch (err) { |
||||
|
error = err |
||||
|
} |
||||
|
expect(error).toBeDefined() |
||||
|
}) |
||||
|
|
||||
|
it("check usage quota attempts", async () => { |
||||
|
env.CLOUD = true |
||||
|
await setup.runStep(setup.actions.DELETE_ROW.stepId, inputs) |
||||
|
expect(usageQuota.update).toHaveBeenCalledWith(setup.apiKey, "rows", -1) |
||||
|
env.CLOUD = false |
||||
|
}) |
||||
|
|
||||
|
it("should check invalid inputs return an error", async () => { |
||||
|
const res = await setup.runStep(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, { |
||||
|
tableId: "invalid", |
||||
|
id: "invalid", |
||||
|
revision: "invalid", |
||||
|
}) |
||||
|
expect(res.success).toEqual(false) |
||||
|
}) |
||||
|
}) |
||||
@ -0,0 +1,39 @@ |
|||||
|
const setup = require("./utilities") |
||||
|
const fetch = require("node-fetch") |
||||
|
|
||||
|
jest.mock("node-fetch") |
||||
|
|
||||
|
describe("test the outgoing webhook action", () => { |
||||
|
let inputs |
||||
|
let config = setup.getConfig() |
||||
|
|
||||
|
beforeEach(async () => { |
||||
|
await config.init() |
||||
|
inputs = { |
||||
|
requestMethod: "POST", |
||||
|
url: "www.test.com", |
||||
|
requestBody: JSON.stringify({ |
||||
|
a: 1, |
||||
|
}), |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
afterAll(setup.afterAll) |
||||
|
|
||||
|
it("should be able to run the action", async () => { |
||||
|
const res = await setup.runStep(setup.actions.OUTGOING_WEBHOOK.stepId, inputs) |
||||
|
expect(res.success).toEqual(true) |
||||
|
expect(res.response.url).toEqual("http://www.test.com") |
||||
|
expect(res.response.method).toEqual("POST") |
||||
|
expect(res.response.body.a).toEqual(1) |
||||
|
}) |
||||
|
|
||||
|
it("should return an error if something goes wrong in fetch", async () => { |
||||
|
const res = await setup.runStep(setup.actions.OUTGOING_WEBHOOK.stepId, { |
||||
|
requestMethod: "GET", |
||||
|
url: "www.invalid.com" |
||||
|
}) |
||||
|
expect(res.success).toEqual(false) |
||||
|
}) |
||||
|
|
||||
|
}) |
||||
@ -0,0 +1,36 @@ |
|||||
|
const setup = require("./utilities") |
||||
|
|
||||
|
jest.mock("@sendgrid/mail") |
||||
|
|
||||
|
describe("test the send email action", () => { |
||||
|
let inputs |
||||
|
let config = setup.getConfig() |
||||
|
|
||||
|
beforeEach(async () => { |
||||
|
await config.init() |
||||
|
inputs = { |
||||
|
to: "me@test.com", |
||||
|
from: "budibase@test.com", |
||||
|
subject: "Testing", |
||||
|
text: "Email contents", |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
afterAll(setup.afterAll) |
||||
|
|
||||
|
it("should be able to run the action", async () => { |
||||
|
const res = await setup.runStep(setup.actions.SEND_EMAIL.stepId, inputs) |
||||
|
expect(res.success).toEqual(true) |
||||
|
// the mocked module throws back the input
|
||||
|
expect(res.response.to).toEqual("me@test.com") |
||||
|
}) |
||||
|
|
||||
|
it("should return an error if input an invalid email address", async () => { |
||||
|
const res = await setup.runStep(setup.actions.SEND_EMAIL.stepId, { |
||||
|
...inputs, |
||||
|
to: "invalid@test.com", |
||||
|
}) |
||||
|
expect(res.success).toEqual(false) |
||||
|
}) |
||||
|
|
||||
|
}) |
||||
@ -0,0 +1,45 @@ |
|||||
|
const env = require("../../environment") |
||||
|
const setup = require("./utilities") |
||||
|
|
||||
|
describe("test the update row action", () => { |
||||
|
let table, row, inputs |
||||
|
let config = setup.getConfig() |
||||
|
|
||||
|
beforeEach(async () => { |
||||
|
await config.init() |
||||
|
table = await config.createTable() |
||||
|
row = await config.createRow() |
||||
|
inputs = { |
||||
|
rowId: row._id, |
||||
|
row: { |
||||
|
...row, |
||||
|
name: "Updated name", |
||||
|
// put a falsy option in to be removed
|
||||
|
description: "", |
||||
|
} |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
afterAll(setup.afterAll) |
||||
|
|
||||
|
it("should be able to run the action", async () => { |
||||
|
const res = await setup.runStep(setup.actions.UPDATE_ROW.stepId, inputs) |
||||
|
expect(res.success).toEqual(true) |
||||
|
const updatedRow = await config.getRow(table._id, res.id) |
||||
|
expect(updatedRow.name).toEqual("Updated name") |
||||
|
expect(updatedRow.description).not.toEqual("") |
||||
|
}) |
||||
|
|
||||
|
it("should check invalid inputs return an error", async () => { |
||||
|
const res = await setup.runStep(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, { |
||||
|
row: { _id: "invalid" }, |
||||
|
rowId: "invalid", |
||||
|
}) |
||||
|
expect(res.success).toEqual(false) |
||||
|
}) |
||||
|
}) |
||||
Loading…
Reference in new issue