Upping automation coverage by adding webhook testing and increasing screen coverage.

This commit is contained in:
mike12345567 2021-03-10 11:47:39 +00:00
parent 929db83e99
commit dd16c84ecd
8 changed files with 171 additions and 4 deletions

View File

@ -16,7 +16,7 @@ async function activate() {
// this was an issue as NODE_ENV = 'cypress' on the server, // this was an issue as NODE_ENV = 'cypress' on the server,
// but 'production' on the client // but 'production' on the client
const response = await api.get("/api/analytics") const response = await api.get("/api/analytics")
analyticsEnabled = (await response.json()) === true analyticsEnabled = (await response.json()).enabled === true
} }
if (!analyticsEnabled) return if (!analyticsEnabled) return
if (sentryConfigured) Sentry.init({ dsn: process.env.SENTRY_DSN }) if (sentryConfigured) Sentry.init({ dsn: process.env.SENTRY_DSN })

View File

@ -1,5 +1,7 @@
const env = require("../../environment") const env = require("../../environment")
exports.isEnabled = async function(ctx) { exports.isEnabled = async function(ctx) {
ctx.body = JSON.stringify(env.ENABLE_ANALYTICS === "true") ctx.body = {
enabled: env.ENABLE_ANALYTICS === "true",
}
} }

View File

@ -98,6 +98,11 @@ exports.create = async function(ctx) {
let automation = ctx.request.body let automation = ctx.request.body
automation.appId = ctx.user.appId automation.appId = ctx.user.appId
// call through to update if already exists
if (automation._id && automation._rev) {
return exports.update(ctx)
}
automation._id = generateAutomationID() automation._id = generateAutomationID()
automation.type = "automation" automation.type = "automation"

View File

@ -41,6 +41,8 @@ exports.save = async ctx => {
exports.destroy = async ctx => { exports.destroy = async ctx => {
const db = new CouchDB(ctx.user.appId) const db = new CouchDB(ctx.user.appId)
await db.remove(ctx.params.screenId, ctx.params.screenRev) await db.remove(ctx.params.screenId, ctx.params.screenRev)
ctx.message = "Screen deleted successfully" ctx.body = {
message: "Screen deleted successfully",
}
ctx.status = 200 ctx.status = 200
} }

View File

@ -109,6 +109,35 @@ describe("/automations", () => {
automation = res.body.automation automation = res.body.automation
}) })
it("should be able to create an automation with a webhook trigger", async () => {
const autoConfig = basicAutomation()
autoConfig.definition.trigger = TRIGGER_DEFINITIONS["WEBHOOK"]
autoConfig.definition.trigger.id = "webhook_trigger_id"
const res = await request
.post(`/api/automations`)
.set(config.defaultHeaders())
.send(autoConfig)
.expect('Content-Type', /json/)
.expect(200)
const originalAuto = res.body.automation
expect(originalAuto._id).toBeDefined()
expect(originalAuto._rev).toBeDefined()
// try removing the webhook trigger
const newConfig = originalAuto
newConfig.definition.trigger = TRIGGER_DEFINITIONS["ROW_SAVED"]
newConfig.definition.trigger.id = "row_saved_id"
const newRes = await request
.post(`/api/automations`)
.set(config.defaultHeaders())
.send(newConfig)
.expect('Content-Type', /json/)
.expect(200)
const newAuto = newRes.body.automation
expect(newAuto._id).toEqual(originalAuto._id)
expect(newAuto._rev).toBeDefined()
expect(newAuto._rev).not.toEqual(originalAuto._rev)
})
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
await checkBuilderEndpoint({ await checkBuilderEndpoint({
config, config,
@ -119,6 +148,19 @@ describe("/automations", () => {
}) })
}) })
describe("find", () => {
it("should be able to find the automation", async () => {
const automation = await config.createAutomation()
const res = await request
.get(`/api/automations/${automation._id}`)
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(res.body._id).toEqual(automation._id)
expect(res.body._rev).toEqual(automation._rev)
})
})
describe("trigger", () => { describe("trigger", () => {
it("trigger the automation successfully", async () => { it("trigger the automation successfully", async () => {
let table = await config.createTable() let table = await config.createTable()

View File

@ -0,0 +1,38 @@
const setup = require("./utilities")
describe("/analytics", () => {
let request = setup.getRequest()
let config = setup.getConfig()
afterAll(setup.afterAll)
beforeEach(async () => {
await config.init()
})
describe("isEnabled", () => {
it("check if analytics enabled", async () => {
const res = await request
.get(`/api/analytics`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(typeof res.body.enabled).toEqual("boolean")
})
})
})
describe("/health", () => {
it("should confirm healthy", async () => {
let config = setup.getConfig()
await config.getRequest().get("/health").expect(200)
})
})
describe("/version", () => {
it("should confirm version", async () => {
const config = setup.getConfig()
const res = await config.getRequest().get("/version").expect(200)
expect(res.text.split(".").length).toEqual(3)
})
})

View File

@ -0,0 +1,77 @@
const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const setup = require("./utilities")
const { basicScreen } = require("./utilities/structures")
describe("/screens", () => {
let request = setup.getRequest()
let config = setup.getConfig()
let screen
afterAll(setup.afterAll)
beforeEach(async () => {
await config.init()
screen = await config.createScreen()
})
describe("fetch", () => {
it("should be able to create a layout", async () => {
const res = await request
.get(`/api/screens`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.length).toEqual(1)
expect(res.body[0]._id).toEqual(screen._id)
})
it("should apply authorization to endpoint", async () => {
await checkBuilderEndpoint({
config,
method: "GET",
url: `/api/screens`,
})
})
})
describe("save", () => {
it("should be able to save a screen", async () => {
const screenCfg = basicScreen()
const res = await request
.post(`/api/screens`)
.send(screenCfg)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body._rev).toBeDefined()
expect(res.body.name).toEqual(screenCfg.name)
})
it("should apply authorization to endpoint", async () => {
await checkBuilderEndpoint({
config,
method: "POST",
url: `/api/screens`,
})
})
})
describe("destroy", () => {
it("should be able to delete the screen", async () => {
const res = await request
.delete(`/api/screens/${screen._id}/${screen._rev}`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.message).toBeDefined()
})
it("should apply authorization to endpoint", async () => {
await checkBuilderEndpoint({
config,
method: "DELETE",
url: `/api/screens/${screen._id}/${screen._rev}`,
})
})
})
})

View File

@ -205,7 +205,8 @@ class AccessController {
tryingRoleId == null || tryingRoleId == null ||
tryingRoleId === "" || tryingRoleId === "" ||
tryingRoleId === userRoleId || tryingRoleId === userRoleId ||
tryingRoleId === BUILTIN_IDS.BUILDER tryingRoleId === BUILTIN_IDS.BUILDER ||
userRoleId === BUILTIN_IDS.BUILDER
) { ) {
return true return true
} }