Update to improve test cases and get JOI to work as expected.

This commit is contained in:
mike12345567 2020-09-16 19:25:52 +01:00
parent 5997550fbb
commit 31939e3dc9
12 changed files with 115 additions and 26 deletions

View File

@ -23,6 +23,7 @@ const workflowActions = store => ({
create: async ({ name }) => {
const workflow = {
name,
type: "workflow",
definition: {
steps: [],
},

View File

@ -2,6 +2,7 @@ const {
createClientDatabase,
createApplication,
createInstance,
createModel,
defaultHeaders,
supertest,
insertDocument,
@ -19,24 +20,24 @@ const TEST_WORKFLOW = {
},
definition: {
triggers: [
trigger: {},
steps: [
],
next: {
stepId: "abc123",
type: "SERVER",
conditions: {
}
}
}
},
type: "workflow",
}
let ACTION_DEFINITIONS = {}
let TRIGGER_DEFINITIONS = {}
let LOGIC_DEFINITIONS = {}
describe("/workflows", () => {
let request
let server
let app
let instance
let workflow
let model
beforeAll(async () => {
({ request, server } = await supertest())
@ -45,8 +46,9 @@ describe("/workflows", () => {
})
beforeEach(async () => {
if (workflow) await destroyDocument(workflow.id)
instance = await createInstance(request, app._id)
if (workflow) await destroyDocument(workflow.id);
model = await createModel(request, app._id, instance._id)
})
afterAll(async () => {
@ -58,9 +60,77 @@ describe("/workflows", () => {
type: "workflow",
...TEST_WORKFLOW
});
workflow = { ...workflow, ...TEST_WORKFLOW }
}
describe("get definitions", () => {
it("returns a list of definitions for actions", async () => {
const res = await request
.get(`/api/workflows/action/list`)
.set(defaultHeaders(app._id, instance._id))
.expect('Content-Type', /json/)
.expect(200)
expect(Object.keys(res.body).length).not.toEqual(0)
ACTION_DEFINITIONS = res.body
})
it("returns a list of definitions for triggers", async () => {
const res = await request
.get(`/api/workflows/trigger/list`)
.set(defaultHeaders(app._id, instance._id))
.expect('Content-Type', /json/)
.expect(200)
expect(Object.keys(res.body).length).not.toEqual(0)
TRIGGER_DEFINITIONS = res.body
})
it("returns a list of definitions for actions", async () => {
const res = await request
.get(`/api/workflows/logic/list`)
.set(defaultHeaders(app._id, instance._id))
.expect('Content-Type', /json/)
.expect(200)
expect(Object.keys(res.body).length).not.toEqual(0)
LOGIC_DEFINITIONS = res.body
})
it("returns all of the definitions in one", async () => {
const res = await request
.get(`/api/workflows/definitions/list`)
.set(defaultHeaders(app._id, instance._id))
.expect('Content-Type', /json/)
.expect(200)
expect(Object.keys(res.body.action).length).toEqual(Object.keys(ACTION_DEFINITIONS).length)
expect(Object.keys(res.body.trigger).length).toEqual(Object.keys(TRIGGER_DEFINITIONS).length)
expect(Object.keys(res.body.logic).length).toEqual(Object.keys(LOGIC_DEFINITIONS).length)
})
})
describe("create", () => {
it("should setup the workflow fully", () => {
let trigger = TRIGGER_DEFINITIONS["RECORD_SAVED"]
trigger.inputs.modelId = model._id
trigger.id = "wadiawdo34"
let saveAction = ACTION_DEFINITIONS["SAVE_RECORD"]
saveAction.inputs.record = {
modelId: model._id,
name: "Testing",
}
saveAction.id = "awde444wk"
let deleteAction = ACTION_DEFINITIONS["DELETE_RECORD"]
deleteAction.inputs.id = "{{blocks[1].id}}"
deleteAction.inputs.revision = "{{blocks[1].revision}}"
deleteAction.id = "78MOt8nQO"
TEST_WORKFLOW.definition.steps.push(saveAction)
TEST_WORKFLOW.definition.steps.push(deleteAction)
TEST_WORKFLOW.definition.trigger = trigger
})
it("returns a success message when the workflow is successfully created", async () => {
const res = await request
.post(`/api/workflows`)
@ -91,6 +161,7 @@ describe("/workflows", () => {
workflow._id = workflow.id
workflow._rev = workflow.rev
workflow.name = "Updated Name";
workflow.type = "workflow"
const res = await request
.put(`/api/workflows`)

View File

@ -23,18 +23,20 @@ function generateStepSchema(allowStepTypes) {
}).unknown(true)
}
// prettier-ignore
const workflowValidator = joiValidator.body(Joi.object({
live: Joi.bool(),
id: Joi.string().required(),
rev: Joi.string().required(),
name: Joi.string().required(),
type: Joi.string().valid("workflow").required(),
definition: Joi.object({
steps: Joi.array().required().items(generateStepSchema(["ACTION", "LOGIC"])),
trigger: generateStepSchema(["TRIGGER"]).required(),
}).required().unknown(true),
}).unknown(true))
function generateValidator(existing = false) {
// prettier-ignore
return joiValidator.body(Joi.object({
live: Joi.bool(),
id: existing ? Joi.string().required() : Joi.string(),
rev: existing ? Joi.string().required() : Joi.string(),
name: Joi.string().required(),
type: Joi.string().valid("workflow").required(),
definition: Joi.object({
steps: Joi.array().required().items(generateStepSchema(["ACTION", "LOGIC"])),
trigger: generateStepSchema(["TRIGGER"]),
}).required().unknown(true),
}).unknown(true))
}
router
.get(
@ -62,13 +64,13 @@ router
.put(
"/api/workflows",
authorized(BUILDER),
workflowValidator,
generateValidator(true),
controller.update
)
.post(
"/api/workflows",
authorized(BUILDER),
workflowValidator,
generateValidator(false),
controller.create
)
.post("/api/workflows/:id/trigger", controller.trigger)

View File

@ -2,9 +2,16 @@ function validate(schema, property) {
// Return a Koa middleware function
return (ctx, next) => {
if (schema) {
const { error } = schema.validate(ctx[property])
let params =
ctx[property] != null
? ctx[property]
: ctx.request[property] != null
? ctx.request[property]
: null
const { error } = schema.validate(params)
if (error) {
ctx.throw(400, `Invalid ${property} - ${error.message}`)
return
}
}
return next()

View File

@ -7,6 +7,7 @@ module.exports.definition = {
icon: "ri-user-add-fill",
name: "Create User",
type: "ACTION",
stepId: "CREATE_USER",
inputs: {},
schema: {
inputs: {

View File

@ -5,6 +5,7 @@ module.exports.definition = {
icon: "ri-time-fill",
tagline: "Delay for {{inputs.time}} milliseconds",
description: "Delay the workflow until an amount of time has passed",
stepId: "DELAY",
inputs: {},
schema: {
inputs: {

View File

@ -6,6 +6,7 @@ module.exports.definition = {
name: "Delete Record",
tagline: "Delete a {{inputs.enriched.model.name}} record",
type: "ACTION",
stepId: "DELETE_RECORD",
inputs: {},
schema: {
inputs: {

View File

@ -11,6 +11,7 @@ module.exports.definition = {
icon: "ri-git-branch-line",
description: "Filter any workflows which do not meet certain conditions",
type: "LOGIC",
stepId: "FILTER",
inputs: {},
schema: {
inputs: {

View File

@ -6,6 +6,7 @@ module.exports.definition = {
icon: "ri-save-3-fill",
description: "Save a record to your database",
type: "ACTION",
stepId: "SAVE_RECORD",
inputs: {},
schema: {
inputs: {

View File

@ -7,6 +7,7 @@ module.exports.definition = {
icon: "ri-mail-open-fill",
name: "Send Email",
type: "ACTION",
stepId: "SEND_EMAIL",
inputs: {},
schema: {
inputs: {

View File

@ -25,7 +25,7 @@ class Orchestrator {
constructor(workflow, triggerOutput) {
this._instanceId = triggerOutput.instanceId
// block zero is never used as the mustache is zero indexed for customer facing
this._context = { blocks: [{}, triggerOutput] }
this._context = { blocks: [{}], trigger: triggerOutput }
this._workflow = workflow
}

View File

@ -11,6 +11,7 @@ const BUILTIN_DEFINITIONS = {
icon: "ri-save-line",
tagline: "Record is added to {{inputs.enriched.model.name}}",
description: "Fired when a record is saved to your database",
stepId: "RECORD_SAVED",
inputs: {},
schema: {
inputs: {
@ -42,6 +43,7 @@ const BUILTIN_DEFINITIONS = {
icon: "ri-delete-bin-line",
tagline: "Record is deleted from {{inputs.enriched.model.name}}",
description: "Fired when a record is deleted from your database",
stepId: "RECORD_DELETED",
inputs: {},
schema: {
inputs: {