From 67f47df7188b8b608ecaa72bc1f88782250e17dd Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 5 Mar 2021 12:11:44 +0000 Subject: [PATCH] Reducing the boilerplate needed around setting up a test case. --- .../server/src/api/controllers/datasource.js | 3 +- .../src/api/routes/tests/application.spec.js | 15 +++------ .../src/api/routes/tests/automation.spec.js | 20 ++++------- .../src/api/routes/tests/datasource.spec.js | 17 +++------- .../src/api/routes/tests/permissions.spec.js | 15 +++------ .../server/src/api/routes/tests/query.spec.js | 15 +++------ .../server/src/api/routes/tests/role.spec.js | 15 +++------ .../server/src/api/routes/tests/row.spec.js | 26 +++++---------- .../server/src/api/routes/tests/table.spec.js | 15 +++------ .../server/src/api/routes/tests/user.spec.js | 27 ++++----------- .../tests/utilities/TestConfiguration.js | 8 +++++ .../src/api/routes/tests/utilities/index.js | 33 +++++++++++++++++-- .../server/src/api/routes/tests/view.spec.js | 15 +++------ 13 files changed, 89 insertions(+), 135 deletions(-) diff --git a/packages/server/src/api/controllers/datasource.js b/packages/server/src/api/controllers/datasource.js index 8fc3efc8a9..15cea72da7 100644 --- a/packages/server/src/api/controllers/datasource.js +++ b/packages/server/src/api/controllers/datasource.js @@ -8,14 +8,13 @@ const { exports.fetch = async function(ctx) { const database = new CouchDB(ctx.user.appId) - const datasources = ( + ctx.body = ( await database.allDocs( getDatasourceParams(null, { include_docs: true, }) ) ).rows.map(row => row.doc) - ctx.body = datasources } exports.save = async function(ctx) { diff --git a/packages/server/src/api/routes/tests/application.spec.js b/packages/server/src/api/routes/tests/application.spec.js index a453e60c0a..c146cc63fb 100644 --- a/packages/server/src/api/routes/tests/application.spec.js +++ b/packages/server/src/api/routes/tests/application.spec.js @@ -1,24 +1,17 @@ -const TestConfig = require("./utilities/TestConfiguration") const { clearAllApps, checkBuilderEndpoint } = require("./utilities/TestFunctions") +const setup = require("./utilities") describe("/applications", () => { - let request - let config + let request = setup.getRequest() + let config = setup.getConfig() - beforeAll(async () => { - config = new TestConfig() - request = config.request - }) + afterAll(setup.afterAll) beforeEach(async () => { await clearAllApps() await config.init() }) - afterAll(() => { - config.end() - }) - describe("create", () => { it("returns a success message when the application is successfully created", async () => { const res = await request diff --git a/packages/server/src/api/routes/tests/automation.spec.js b/packages/server/src/api/routes/tests/automation.spec.js index c737f1da6b..0648bfefa5 100644 --- a/packages/server/src/api/routes/tests/automation.spec.js +++ b/packages/server/src/api/routes/tests/automation.spec.js @@ -1,11 +1,10 @@ -const TestConfig = require("./utilities/TestConfiguration") const { checkBuilderEndpoint, getAllTableRows, clearAllAutomations, } = require("./utilities/TestFunctions") const { basicAutomation } = require("./utilities/structures") -const { delay } = require("./utilities") +const setup = require("./utilities") const MAX_RETRIES = 4 @@ -14,23 +13,16 @@ let TRIGGER_DEFINITIONS = {} let LOGIC_DEFINITIONS = {} describe("/automations", () => { - let request - let config + let request = setup.getRequest() + let config = setup.getConfig() let automation - beforeAll(async () => { - config = new TestConfig() - request = config.request - }) + afterAll(setup.afterAll) beforeEach(async () => { await config.init() }) - afterAll(() => { - config.end() - }) - const triggerWorkflow = async automation => { return await request .post(`/api/automations/${automation._id}/trigger`) @@ -133,7 +125,7 @@ describe("/automations", () => { automation.definition.trigger.inputs.tableId = table._id automation.definition.steps[0].inputs.row.tableId = table._id automation = await config.createAutomation(automation) - await delay(500) + await setup.delay(500) 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 @@ -142,7 +134,7 @@ describe("/automations", () => { 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(automation.name) - await delay(500) + await setup.delay(500) let elements = await getAllTableRows(config) // don't test it unless there are values to test if (elements.length > 1) { diff --git a/packages/server/src/api/routes/tests/datasource.spec.js b/packages/server/src/api/routes/tests/datasource.spec.js index 280c54f94b..7602b027e1 100644 --- a/packages/server/src/api/routes/tests/datasource.spec.js +++ b/packages/server/src/api/routes/tests/datasource.spec.js @@ -1,23 +1,16 @@ -let TestConfig = require("./utilities/TestConfiguration") let { basicDatasource } = require("./utilities/structures") let { checkBuilderEndpoint } = require("./utilities/TestFunctions") +let setup = require("./utilities") describe("/datasources", () => { - let request - let config + let request = setup.getRequest() + let config = setup.getConfig() - beforeAll(async () => { - config = new TestConfig() - request = config.request - }) - - afterAll(() => { - config.end() - }) + afterAll(setup.afterAll) beforeEach(async () => { await config.init() - }); + }) describe("create", () => { it("should create a new datasource", async () => { diff --git a/packages/server/src/api/routes/tests/permissions.spec.js b/packages/server/src/api/routes/tests/permissions.spec.js index 13b07eeb2a..93e6e29131 100644 --- a/packages/server/src/api/routes/tests/permissions.spec.js +++ b/packages/server/src/api/routes/tests/permissions.spec.js @@ -1,25 +1,18 @@ const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles") -const TestConfig = require("./utilities/TestConfiguration") +const setup = require("./utilities") const { basicRow } = require("./utilities/structures") const HIGHER_ROLE_ID = BUILTIN_ROLE_IDS.BASIC const STD_ROLE_ID = BUILTIN_ROLE_IDS.PUBLIC describe("/permission", () => { - let request + let request = setup.getRequest() + let config = setup.getConfig() let table let perms let row - let config - beforeAll(async () => { - config = new TestConfig() - request = config.request - }) - - afterAll(() => { - config.end() - }) + afterAll(setup.afterAll) beforeEach(async () => { await config.init() diff --git a/packages/server/src/api/routes/tests/query.spec.js b/packages/server/src/api/routes/tests/query.spec.js index 98ef8f78cb..765baa4426 100644 --- a/packages/server/src/api/routes/tests/query.spec.js +++ b/packages/server/src/api/routes/tests/query.spec.js @@ -1,19 +1,12 @@ -const TestConfig = require("./utilities/TestConfiguration") const { checkBuilderEndpoint } = require("./utilities/TestFunctions") const { basicQuery } = require("./utilities/structures") +const setup = require("./utilities") describe("/queries", () => { - let request - let config + let request = setup.getRequest() + let config = setup.getConfig() - beforeAll(async () => { - config = new TestConfig() - request = config.request - }) - - afterAll(() => { - config.end() - }) + afterAll(setup.afterAll) beforeEach(async () => { await config.init() diff --git a/packages/server/src/api/routes/tests/role.spec.js b/packages/server/src/api/routes/tests/role.spec.js index 122afdb07e..9c08bf289a 100644 --- a/packages/server/src/api/routes/tests/role.spec.js +++ b/packages/server/src/api/routes/tests/role.spec.js @@ -3,20 +3,13 @@ const { BUILTIN_PERMISSION_IDS, } = require("../../../utilities/security/permissions") const { basicRole } = require("./utilities/structures") -const TestConfig = require("./utilities/TestConfiguration") +const setup = require("./utilities") describe("/roles", () => { - let request - let config + let request = setup.getRequest() + let config = setup.getConfig() - beforeAll(async () => { - config = new TestConfig() - request = config.request - }) - - afterAll(() => { - config.close() - }) + afterAll(setup.afterAll) beforeEach(async () => { await config.init() diff --git a/packages/server/src/api/routes/tests/row.spec.js b/packages/server/src/api/routes/tests/row.spec.js index ebb2e009e8..cc50520b77 100644 --- a/packages/server/src/api/routes/tests/row.spec.js +++ b/packages/server/src/api/routes/tests/row.spec.js @@ -1,28 +1,18 @@ const { outputProcessing } = require("../../../utilities/rowProcessor") const env = require("../../../environment") -const TestConfig = require("./utilities/TestConfiguration") const { basicRow } = require("./utilities/structures") +const setup = require("./utilities") describe("/rows", () => { - let request - let appId + let request = setup.getRequest() + let config = setup.getConfig() let table let row - let app - let config - beforeAll(async () => { - config = new TestConfig() - request = config.request - }) - - afterAll(() => { - config.end() - }) + afterAll(setup.afterAll) beforeEach(async () => { - app = await config.init() - appId = app.instance._id + await config.init() table = await config.createTable() row = basicRow(table._id) }) @@ -272,7 +262,7 @@ describe("/rows", () => { link: [{_id: firstRow._id}], tableId: table._id, }) - const enriched = await outputProcessing(appId, table, [secondRow]) + const enriched = await outputProcessing(config.getAppId(), table, [secondRow]) expect(enriched[0].link.length).toBe(1) expect(enriched[0].link[0]._id).toBe(firstRow._id) expect(enriched[0].link[0].primaryDisplay).toBe("Test Contact") @@ -292,8 +282,8 @@ describe("/rows", () => { // the environment needs configured for this env.CLOUD = 1 env.SELF_HOSTED = 1 - const enriched = await outputProcessing(appId, table, [row]) - expect(enriched[0].attachment[0].url).toBe(`/app-assets/assets/${appId}/test/thing`) + const enriched = await outputProcessing(config.getAppId(), table, [row]) + expect(enriched[0].attachment[0].url).toBe(`/app-assets/assets/${config.getAppId()}/test/thing`) // remove env config env.CLOUD = undefined env.SELF_HOSTED = undefined diff --git a/packages/server/src/api/routes/tests/table.spec.js b/packages/server/src/api/routes/tests/table.spec.js index 976db6ee56..1a2df624f1 100644 --- a/packages/server/src/api/routes/tests/table.spec.js +++ b/packages/server/src/api/routes/tests/table.spec.js @@ -1,18 +1,11 @@ -const TestConfig = require("./utilities/TestConfiguration") const { checkBuilderEndpoint } = require("./utilities/TestFunctions") +const setup = require("./utilities") describe("/tables", () => { - let request - let config + let request = setup.getRequest() + let config = setup.getConfig() - beforeAll(async () => { - config = new TestConfig() - request = config.request - }) - - afterAll(() => { - config.end() - }) + afterAll(setup.afterAll) beforeEach(async () => { await config.init() diff --git a/packages/server/src/api/routes/tests/user.spec.js b/packages/server/src/api/routes/tests/user.spec.js index 3405f0716d..6ec607a093 100644 --- a/packages/server/src/api/routes/tests/user.spec.js +++ b/packages/server/src/api/routes/tests/user.spec.js @@ -1,31 +1,18 @@ const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles") -const TestConfig = require("./utilities/TestConfiguration") const { checkPermissionsEndpoint } = require("./utilities/TestFunctions") -const { cloneDeep } = require("lodash/fp") - -const baseBody = { - email: "bill@bill.com", - password: "yeeooo", - roleId: BUILTIN_ROLE_IDS.POWER, -} +const { basicUser } = require("./utilities/structures") +const setup = require("./utilities") describe("/users", () => { - let request - let config + let request = setup.getRequest() + let config = setup.getConfig() - beforeAll(async () => { - config = new TestConfig() - request = config.request - }) + afterAll(setup.afterAll) beforeEach(async () => { await config.init() }) - afterAll(() => { - config.end() - }) - describe("fetch", () => { it("returns a list of users from an instance db", async () => { await config.createUser("brenda@brenda.com", "brendas_password") @@ -56,7 +43,7 @@ describe("/users", () => { describe("create", () => { it("returns a success message when a user is successfully created", async () => { - const body = cloneDeep(baseBody) + const body = basicUser(BUILTIN_ROLE_IDS.POWER) body.email = "bill@budibase.com" const res = await request .post(`/api/users`) @@ -70,7 +57,7 @@ describe("/users", () => { }) it("should apply authorization to endpoint", async () => { - const body = cloneDeep(baseBody) + const body = basicUser(BUILTIN_ROLE_IDS.POWER) body.email = "brandNewUser@user.com" await checkPermissionsEndpoint({ config, diff --git a/packages/server/src/api/routes/tests/utilities/TestConfiguration.js b/packages/server/src/api/routes/tests/utilities/TestConfiguration.js index 8482d27118..5f6b1cc267 100644 --- a/packages/server/src/api/routes/tests/utilities/TestConfiguration.js +++ b/packages/server/src/api/routes/tests/utilities/TestConfiguration.js @@ -24,6 +24,14 @@ class TestConfiguration { this.appId = null } + getRequest() { + return this.request + } + + getAppId() { + return this.appId + } + async _req(config, params, controlFunc) { const request = {} // fake cookies, we don't need them diff --git a/packages/server/src/api/routes/tests/utilities/index.js b/packages/server/src/api/routes/tests/utilities/index.js index 7786dc2bcc..7e9260ce18 100644 --- a/packages/server/src/api/routes/tests/utilities/index.js +++ b/packages/server/src/api/routes/tests/utilities/index.js @@ -1,5 +1,32 @@ -const TEST_CLIENT_ID = "test-client-id" - -exports.TEST_CLIENT_ID = TEST_CLIENT_ID +const TestConfig = require("./TestConfiguration") exports.delay = ms => new Promise(resolve => setTimeout(resolve, ms)) + +let request, config + +exports.beforeAll = () => { + config = new TestConfig() + request = config.getRequest() +} + +exports.afterAll = () => { + if (config) { + config.end() + } + request = null + config = null +} + +exports.getRequest = () => { + if (!request) { + exports.beforeAll() + } + return request +} + +exports.getConfig = () => { + if (!config) { + exports.beforeAll() + } + return config +} diff --git a/packages/server/src/api/routes/tests/view.spec.js b/packages/server/src/api/routes/tests/view.spec.js index 39549daa98..a80b09d3a0 100644 --- a/packages/server/src/api/routes/tests/view.spec.js +++ b/packages/server/src/api/routes/tests/view.spec.js @@ -1,23 +1,16 @@ -const TestConfig = require("./utilities/TestConfiguration") +const setup = require("./utilities") describe("/views", () => { - let request - let config + let request = setup.getRequest() + let config = setup.getConfig() let table - beforeAll(async () => { - config = new TestConfig() - request = config.request - }) + afterAll(setup.afterAll) beforeEach(async () => { await config.init() }) - afterAll(() => { - config.end() - }) - describe("create", () => { beforeEach(async () => { table = await config.createTable()