diff --git a/packages/server/src/api/routes/tests/application.spec.js b/packages/server/src/api/routes/tests/application.spec.js index c5e9daabbd..e5100573ff 100644 --- a/packages/server/src/api/routes/tests/application.spec.js +++ b/packages/server/src/api/routes/tests/application.spec.js @@ -39,7 +39,6 @@ describe("/applications", () => { it("should apply authorization to endpoint", async () => { await checkBuilderEndpoint({ config, - request, method: "POST", url: `/api/applications`, body: { name: "My App" } @@ -65,7 +64,6 @@ describe("/applications", () => { it("should apply authorization to endpoint", async () => { await checkBuilderEndpoint({ config, - request, method: "GET", url: `/api/applications`, }) diff --git a/packages/server/src/api/routes/tests/automation.spec.js b/packages/server/src/api/routes/tests/automation.spec.js index 128d3530e9..3f3fa93192 100644 --- a/packages/server/src/api/routes/tests/automation.spec.js +++ b/packages/server/src/api/routes/tests/automation.spec.js @@ -1,34 +1,18 @@ const { - createApplication, - createTable, - getAllFromTable, defaultHeaders, supertest, - insertDocument, - destroyDocument, - builderEndpointShouldBlockNormalUsers -} = require("./couchTestUtils") -let { generateAutomationID } = require("../../../db/utils") +} = require("./utilities") +const TestConfig = require("./utilities/TestConfiguration") +const { + checkBuilderEndpoint, + getAllTableRows, + clearAllAutomations, +} = require("./utilities/TestFunctions") +const { basicAutomation } = require("./utilities/structures") const { delay } = require("./testUtils") const MAX_RETRIES = 4 -const AUTOMATION_ID = generateAutomationID() -const TEST_AUTOMATION = { - _id: AUTOMATION_ID, - name: "My Automation", - screenId: "kasdkfldsafkl", - live: true, - uiTree: { - - }, - definition: { - trigger: {}, - steps: [ - ], - }, - type: "automation", -} let ACTION_DEFINITIONS = {} let TRIGGER_DEFINITIONS = {} @@ -39,34 +23,26 @@ describe("/automations", () => { let server let app let appId + let config let automation - let automationId beforeAll(async () => { ({ request, server } = await supertest()) }) beforeEach(async () => { - app = await createApplication(request) + config = new TestConfig(request) + app = await config.init() appId = app.instance._id - if (automation) await destroyDocument(automation.id) }) afterAll(() => { server.close() }) - const createAutomation = async () => { - automation = await insertDocument(appId, { - type: "automation", - ...TEST_AUTOMATION - }) - automation = { ...automation, ...TEST_AUTOMATION } - } - - const triggerWorkflow = async (automationId) => { + const triggerWorkflow = async automation => { return await request - .post(`/api/automations/${automationId}/trigger`) + .post(`/api/automations/${automation._id}/trigger`) .send({ name: "Test", description: "TEST" }) .set(defaultHeaders(appId)) .expect('Content-Type', /json/) @@ -121,6 +97,7 @@ describe("/automations", () => { }) describe("create", () => { + const autoConfig = basicAutomation() it("should setup the automation fully", () => { let trigger = TRIGGER_DEFINITIONS["ROW_SAVED"] trigger.id = "wadiawdo34" @@ -131,52 +108,51 @@ describe("/automations", () => { } createAction.id = "awde444wk" - TEST_AUTOMATION.definition.steps.push(createAction) - TEST_AUTOMATION.definition.trigger = trigger + autoConfig.definition.steps.push(createAction) + autoConfig.definition.trigger = trigger }) it("returns a success message when the automation is successfully created", async () => { const res = await request .post(`/api/automations`) .set(defaultHeaders(appId)) - .send(TEST_AUTOMATION) + .send(autoConfig) .expect('Content-Type', /json/) .expect(200) expect(res.body.message).toEqual("Automation created successfully") expect(res.body.automation.name).toEqual("My Automation") expect(res.body.automation._id).not.toEqual(null) - automationId = res.body.automation._id + automation = res.body.automation }) it("should apply authorization to endpoint", async () => { - await builderEndpointShouldBlockNormalUsers({ - request, + await checkBuilderEndpoint({ + config, method: "POST", url: `/api/automations`, - appId: appId, - body: TEST_AUTOMATION + body: autoConfig }) }) }) describe("trigger", () => { it("trigger the automation successfully", async () => { - let table = await createTable(request, appId) - TEST_AUTOMATION.definition.trigger.inputs.tableId = table._id - TEST_AUTOMATION.definition.steps[0].inputs.row.tableId = table._id - await createAutomation() + let table = await config.createTable() + automation.definition.trigger.inputs.tableId = table._id + automation.definition.steps[0].inputs.row.tableId = table._id + automation = await config.createAutomation(automation) await delay(500) - const res = await triggerWorkflow(automation._id) + const res = await triggerWorkflow(automation) // this looks a bit mad but we don't actually have a way to wait for a response from the automation to // know that it has finished all of its actions - this is currently the best way // also when this runs in CI it is very temper-mental so for now trying to make run stable by repeating until it works // TODO: update when workflow logs are a thing for (let tries = 0; tries < MAX_RETRIES; tries++) { expect(res.body.message).toEqual(`Automation ${automation._id} has been triggered.`) - expect(res.body.automation.name).toEqual(TEST_AUTOMATION.name) + expect(res.body.automation.name).toEqual(automation.name) await delay(500) - let elements = await getAllFromTable(request, appId, table._id) + let elements = await getAllTableRows(config) // don't test it unless there are values to test if (elements.length > 1) { expect(elements.length).toEqual(5) @@ -191,9 +167,7 @@ describe("/automations", () => { describe("update", () => { it("updates a automations data", async () => { - await createAutomation() - automation._id = automation.id - automation._rev = automation.rev + automation = await config.createAutomation(automation) automation.name = "Updated Name" automation.type = "automation" @@ -204,52 +178,52 @@ describe("/automations", () => { .expect('Content-Type', /json/) .expect(200) - expect(res.body.message).toEqual(`Automation ${AUTOMATION_ID} updated successfully.`) + expect(res.body.message).toEqual(`Automation ${automation._id} updated successfully.`) expect(res.body.automation.name).toEqual("Updated Name") }) }) describe("fetch", () => { it("return all the automations for an instance", async () => { - await createAutomation() + await clearAllAutomations(config) + const autoConfig = basicAutomation() + automation = await config.createAutomation(autoConfig) const res = await request .get(`/api/automations`) .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) - expect(res.body[0]).toEqual(expect.objectContaining(TEST_AUTOMATION)) + expect(res.body[0]).toEqual(expect.objectContaining(autoConfig)) }) it("should apply authorization to endpoint", async () => { - await builderEndpointShouldBlockNormalUsers({ - request, + await checkBuilderEndpoint({ + config, method: "GET", url: `/api/automations`, - appId: appId, }) }) }) describe("destroy", () => { it("deletes a automation by its ID", async () => { - await createAutomation() + const automation = await config.createAutomation() const res = await request .delete(`/api/automations/${automation.id}/${automation.rev}`) .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) - expect(res.body.id).toEqual(TEST_AUTOMATION._id) + expect(res.body.id).toEqual(automation._id) }) it("should apply authorization to endpoint", async () => { - await createAutomation() - await builderEndpointShouldBlockNormalUsers({ - request, + const automation = await config.createAutomation() + await checkBuilderEndpoint({ + config, method: "DELETE", url: `/api/automations/${automation.id}/${automation._rev}`, - appId: appId, }) }) }) diff --git a/packages/server/src/api/routes/tests/utilities/TestConfiguration.js b/packages/server/src/api/routes/tests/utilities/TestConfiguration.js index be9192b7f2..50ce00b11f 100644 --- a/packages/server/src/api/routes/tests/utilities/TestConfiguration.js +++ b/packages/server/src/api/routes/tests/utilities/TestConfiguration.js @@ -1,6 +1,11 @@ const { BUILTIN_ROLE_IDS } = require("../../../../utilities/security/roles") const env = require("../../../../environment") -const { basicTable, basicRow, basicRole } = require("./structures") +const { + basicTable, + basicRow, + basicRole, + basicAutomation, +} = require("./structures") const tableController = require("../../../controllers/table") const rowController = require("../../../controllers/row") const roleController = require("../../../controllers/role") @@ -8,6 +13,7 @@ const permsController = require("../../../controllers/permission") const viewController = require("../../../controllers/view") const appController = require("../../../controllers/application") const userController = require("../../../controllers/user") +const autoController = require("../../../controllers/automation") const EMAIL = "babs@babs.com" const PASSWORD = "babs_password" @@ -19,6 +25,7 @@ class TestConfiguration { this.appId = null this.table = null this.linkedTable = null + this.automation = null } async _req(config, params, controlFunc) { @@ -119,6 +126,33 @@ class TestConfiguration { return this._req(view, null, viewController.save) } + async createAutomation(config) { + config = config || basicAutomation() + if (config._rev) { + delete config._rev + } + this.automation = ( + await this._req(config, null, autoController.create) + ).automation + return this.automation + } + + async getAllAutomations() { + return this._req(null, null, autoController.fetch) + } + + async deleteAutomation(automation) { + automation = automation || this.automation + if (!automation) { + return + } + return this._req( + null, + { id: automation._id, rev: automation._rev }, + autoController.destroy + ) + } + async createUser( email = EMAIL, password = PASSWORD, diff --git a/packages/server/src/api/routes/tests/utilities/TestFunctions.js b/packages/server/src/api/routes/tests/utilities/TestFunctions.js index dcb944b0a1..d7a32f4416 100644 --- a/packages/server/src/api/routes/tests/utilities/TestFunctions.js +++ b/packages/server/src/api/routes/tests/utilities/TestFunctions.js @@ -1,5 +1,6 @@ const rowController = require("../../../controllers/row") const appController = require("../../../controllers/application") +const autoController = require("../../../controllers/automation") const CouchDB = require("../../../../db") function Request(appId, params) { @@ -7,8 +8,8 @@ function Request(appId, params) { this.params = params } -exports.getAllTableRows = async (appId, tableId) => { - const req = new Request(appId, { tableId }) +exports.getAllTableRows = async config => { + const req = new Request(config.appId, { tableId: config.table._id }) await rowController.fetchTableRows(req) return req.body } @@ -26,6 +27,13 @@ exports.clearAllApps = async () => { } } +exports.clearAllAutomations = async config => { + const automations = await config.getAllAutomations() + for (let auto of automations) { + await config.deleteAutomation(auto) + } +} + exports.createRequest = (request, method, url, body) => { let req @@ -38,16 +46,10 @@ exports.createRequest = (request, method, url, body) => { return req } -exports.checkBuilderEndpoint = async ({ - config, - request, - method, - url, - body, -}) => { +exports.checkBuilderEndpoint = async ({ config, method, url, body }) => { const headers = await config.login() await exports - .createRequest(request, method, url, body) + .createRequest(config.request, method, url, body) .set(headers) .expect(403) } diff --git a/packages/server/src/api/routes/tests/utilities/structures.js b/packages/server/src/api/routes/tests/utilities/structures.js index cf98a2eb79..1aa81ea2a3 100644 --- a/packages/server/src/api/routes/tests/utilities/structures.js +++ b/packages/server/src/api/routes/tests/utilities/structures.js @@ -25,6 +25,22 @@ exports.basicTable = () => { } } +exports.basicAutomation = () => { + return { + name: "My Automation", + screenId: "kasdkfldsafkl", + live: true, + uiTree: {}, + definition: { + trigger: { + inputs: {}, + }, + steps: [], + }, + type: "automation", + } +} + exports.basicRow = tableId => { return { name: "Test Contact",