From 6231c25ed556d1b529bb4036a1a03d77adb00609 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 9 Mar 2021 11:56:32 +0000 Subject: [PATCH] Updating query test to include mocked preview/execute and adding layout tests. --- packages/server/__mocks__/pg.js | 21 +++++ packages/server/src/api/controllers/layout.js | 2 +- packages/server/src/api/controllers/query.js | 2 - .../src/api/routes/tests/layout.spec.js | 55 ++++++++++++ .../server/src/api/routes/tests/query.spec.js | 87 ++++++++++++++++--- .../tests/utilities/TestConfiguration.js | 6 ++ .../api/routes/tests/utilities/controllers.js | 1 + .../api/routes/tests/utilities/structures.js | 6 ++ 8 files changed, 167 insertions(+), 13 deletions(-) create mode 100644 packages/server/__mocks__/pg.js create mode 100644 packages/server/src/api/routes/tests/layout.spec.js diff --git a/packages/server/__mocks__/pg.js b/packages/server/__mocks__/pg.js new file mode 100644 index 0000000000..2bda8afad0 --- /dev/null +++ b/packages/server/__mocks__/pg.js @@ -0,0 +1,21 @@ +const pg = {} + +// constructor +function Client() {} + +Client.prototype.query = async function() { + return { + rows: [ + { + a: "string", + b: 1, + }, + ], + } +} + +Client.prototype.connect = async function() {} + +pg.Client = Client + +module.exports = pg diff --git a/packages/server/src/api/controllers/layout.js b/packages/server/src/api/controllers/layout.js index de4c647c9f..f270e95bec 100644 --- a/packages/server/src/api/controllers/layout.js +++ b/packages/server/src/api/controllers/layout.js @@ -38,6 +38,6 @@ exports.destroy = async function(ctx) { } await db.remove(layoutId, layoutRev) - ctx.message = "Layout deleted successfully" + ctx.body = { message: "Layout deleted successfully" } ctx.status = 200 } diff --git a/packages/server/src/api/controllers/query.js b/packages/server/src/api/controllers/query.js index 55c2ad14b0..7012219c39 100644 --- a/packages/server/src/api/controllers/query.js +++ b/packages/server/src/api/controllers/query.js @@ -108,7 +108,6 @@ exports.preview = async function(ctx) { if (!Integration) { ctx.throw(400, "Integration type does not exist.") - return } const { fields, parameters, queryVerb } = ctx.request.body @@ -138,7 +137,6 @@ exports.execute = async function(ctx) { if (!Integration) { ctx.throw(400, "Integration type does not exist.") - return } const enrichedQuery = await enrichQueryFields( diff --git a/packages/server/src/api/routes/tests/layout.spec.js b/packages/server/src/api/routes/tests/layout.spec.js new file mode 100644 index 0000000000..4be1c9e18e --- /dev/null +++ b/packages/server/src/api/routes/tests/layout.spec.js @@ -0,0 +1,55 @@ +const { checkBuilderEndpoint } = require("./utilities/TestFunctions") +const setup = require("./utilities") +const { basicLayout } = require("./utilities/structures") + +describe("/queries", () => { + let request = setup.getRequest() + let config = setup.getConfig() + let layout + + afterAll(setup.afterAll) + + beforeEach(async () => { + await config.init() + layout = await config.createLayout() + }) + + describe("save", () => { + it("should be able to create a layout", async () => { + const res = await request + .post(`/api/layouts`) + .send(basicLayout()) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + expect(res.body._rev).toBeDefined() + }) + + it("should apply authorization to endpoint", async () => { + await checkBuilderEndpoint({ + config, + method: "POST", + url: `/api/layouts`, + }) + }) + }) + + describe("destroy", () => { + it("should be able to delete the layout", async () => { + const res = await request + .delete(`/api/layouts/${layout._id}/${layout._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/layouts/${layout._id}/${layout._rev}`, + }) + }) + }) +}) \ No newline at end of file diff --git a/packages/server/src/api/routes/tests/query.spec.js b/packages/server/src/api/routes/tests/query.spec.js index 5867c863dc..aa0e5428c5 100644 --- a/packages/server/src/api/routes/tests/query.spec.js +++ b/packages/server/src/api/routes/tests/query.spec.js @@ -1,20 +1,32 @@ -const { checkBuilderEndpoint } = require("./utilities/TestFunctions") -const { basicQuery } = require("./utilities/structures") -const setup = require("./utilities") +// mock out postgres for this +jest.mock("pg") +const { checkBuilderEndpoint } = require("./utilities/TestFunctions") +const { basicQuery, basicDatasource } = require("./utilities/structures") +const setup = require("./utilities") describe("/queries", () => { let request = setup.getRequest() let config = setup.getConfig() - let datasource + let datasource, query afterAll(setup.afterAll) beforeEach(async () => { await config.init() datasource = await config.createDatasource() + query = await config.createQuery() }) + async function createInvalidIntegration() { + const datasource = await config.createDatasource({ + ...basicDatasource(), + source: "INVALID_INTEGRATION", + }) + const query = await config.createQuery() + return { datasource, query } + } + describe("create", () => { it("should create a new query", async () => { const { _id } = await config.createDatasource() @@ -39,7 +51,6 @@ describe("/queries", () => { describe("fetch", () => { it("returns all the queries from the server", async () => { - const query = await config.createQuery() const res = await request .get(`/api/queries`) .set(config.defaultHeaders()) @@ -94,8 +105,6 @@ describe("/queries", () => { describe("destroy", () => { it("deletes a query and returns a success message", async () => { - const query = await config.createQuery() - await request .delete(`/api/queries/${query._id}/${query._rev}`) .set(config.defaultHeaders()) @@ -114,16 +123,74 @@ describe("/queries", () => { await checkBuilderEndpoint({ config, method: "DELETE", - url: `/api/datasources/${datasource._id}/${datasource._rev}`, + url: `/api/queries/${config._id}/${config._rev}`, }) }) }) describe("preview", () => { - // TODO: need to mock out an integration with a test one and try this + it("should be able to preview the query", async () => { + const res = await request + .post(`/api/queries/preview`) + .send({ + datasourceId: datasource._id, + parameters: {}, + fields: {}, + queryVerb: "read", + }) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + // these responses come from the mock + expect(res.body.schemaFields).toEqual(["a", "b"]) + expect(res.body.rows.length).toEqual(1) + }) + + it("should apply authorization to endpoint", async () => { + await checkBuilderEndpoint({ + config, + method: "POST", + url: `/api/queries/preview`, + }) + }) + + it("should fail with invalid integration type", async () => { + const { datasource } = await createInvalidIntegration() + await request + .post(`/api/queries/preview`) + .send({ + datasourceId: datasource._id, + parameters: {}, + fields: {}, + queryVerb: "read", + }) + .set(config.defaultHeaders()) + .expect(400) + }) }) describe("execute", () => { - // TODO: need to mock out an integration with a test one and try this + it("should be able to execute the query", async () => { + const res = await request + .post(`/api/queries/${query._id}`) + .send({ + parameters: {}, + }) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + expect(res.body.length).toEqual(1) + }) + + it("should fail with invalid integration type", async () => { + const { query } = await createInvalidIntegration() + await request + .post(`/api/queries/${query._id}`) + .send({ + parameters: {}, + }) + .set(config.defaultHeaders()) + .expect(400) + }) }) }) diff --git a/packages/server/src/api/routes/tests/utilities/TestConfiguration.js b/packages/server/src/api/routes/tests/utilities/TestConfiguration.js index f92321ddfb..b72f4f4e5f 100644 --- a/packages/server/src/api/routes/tests/utilities/TestConfiguration.js +++ b/packages/server/src/api/routes/tests/utilities/TestConfiguration.js @@ -9,6 +9,7 @@ const { basicDatasource, basicQuery, basicScreen, + basicLayout, basicWebhook, } = require("./structures") const controllers = require("./controllers") @@ -232,6 +233,11 @@ class TestConfiguration { return (await this._req(config, null, controllers.webhook.save)).webhook } + async createLayout(config = null) { + config = config || basicLayout() + return await this._req(config, null, controllers.layout.save) + } + async createUser( email = EMAIL, password = PASSWORD, diff --git a/packages/server/src/api/routes/tests/utilities/controllers.js b/packages/server/src/api/routes/tests/utilities/controllers.js index d6524bb7f0..a4eb9ac9de 100644 --- a/packages/server/src/api/routes/tests/utilities/controllers.js +++ b/packages/server/src/api/routes/tests/utilities/controllers.js @@ -11,4 +11,5 @@ module.exports = { query: require("../../../controllers/query"), screen: require("../../../controllers/screen"), webhook: require("../../../controllers/webhook"), + layout: require("../../../controllers/layout"), } diff --git a/packages/server/src/api/routes/tests/utilities/structures.js b/packages/server/src/api/routes/tests/utilities/structures.js index 500ff72044..ff3a239211 100644 --- a/packages/server/src/api/routes/tests/utilities/structures.js +++ b/packages/server/src/api/routes/tests/utilities/structures.js @@ -3,6 +3,8 @@ const { BUILTIN_PERMISSION_IDS, } = require("../../../../utilities/security/permissions") const { createHomeScreen } = require("../../../../constants/screens") +const { EMPTY_LAYOUT } = require("../../../../constants/layouts") +const { cloneDeep } = require("lodash/fp") exports.basicTable = () => { return { @@ -91,6 +93,10 @@ exports.basicScreen = () => { return createHomeScreen() } +exports.basicLayout = () => { + return cloneDeep(EMPTY_LAYOUT) +} + exports.basicWebhook = automationId => { return { live: true,