From f10ac0a1781eae3ec32778177d674dac9a21732e Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 4 Mar 2021 10:05:50 +0000 Subject: [PATCH] Refactoring data source test cases. --- .../src/api/routes/tests/datasource.spec.js | 69 ++++++------------- .../tests/utilities/TestConfiguration.js | 50 ++++++++------ .../api/routes/tests/utilities/controllers.js | 12 ++++ .../api/routes/tests/utilities/structures.js | 20 ++++++ 4 files changed, 80 insertions(+), 71 deletions(-) create mode 100644 packages/server/src/api/routes/tests/utilities/controllers.js diff --git a/packages/server/src/api/routes/tests/datasource.spec.js b/packages/server/src/api/routes/tests/datasource.spec.js index 768d54fbcd..0bded03020 100644 --- a/packages/server/src/api/routes/tests/datasource.spec.js +++ b/packages/server/src/api/routes/tests/datasource.spec.js @@ -1,42 +1,22 @@ const { supertest, - createApplication, defaultHeaders, - builderEndpointShouldBlockNormalUsers, - getDocument, - insertDocument -} = require("./couchTestUtils") -let { generateDatasourceID, generateQueryID } = require("../../../db/utils") - -const DATASOURCE_ID = generateDatasourceID() -const TEST_DATASOURCE = { - _id: DATASOURCE_ID, - type: "datasource", - name: "Test", - source: "POSTGRES", - config: {}, - type: "datasource", -} - -const TEST_QUERY = { - _id: generateQueryID(DATASOURCE_ID), - datasourceId: DATASOURCE_ID, - name:"New Query", - parameters:[], - fields:{}, - schema:{}, - queryVerb:"read", -} +} = require("./utilities") +let TestConfig = require("./utilities/TestConfiguration") +let { basicDatasource } = require("./utilities/structures") +let { checkBuilderEndpoint } = require("./utilities/TestFunctions") describe("/datasources", () => { let request let server let app let appId + let config let datasource beforeAll(async () => { ({ request, server } = await supertest()) + config = new TestConfig(request) }); afterAll(() => { @@ -44,23 +24,15 @@ describe("/datasources", () => { }) beforeEach(async () => { - app = await createApplication(request) + app = await config.init() appId = app.instance._id }); - async function createDatasource() { - return await insertDocument(appId, TEST_DATASOURCE) - } - - async function createQuery() { - return await insertDocument(appId, TEST_QUERY) - } - describe("create", () => { it("should create a new datasource", async () => { const res = await request .post(`/api/datasources`) - .send(TEST_DATASOURCE) + .send(basicDatasource()) .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -74,7 +46,7 @@ describe("/datasources", () => { let datasource beforeEach(async () => { - datasource = await createDatasource() + datasource = await config.createDatasource() }); afterEach(() => { @@ -88,30 +60,30 @@ describe("/datasources", () => { .expect('Content-Type', /json/) .expect(200) - const datasources = res.body; + const datasources = res.body expect(datasources).toEqual([ { + "_id": datasources[0]._id, "_rev": datasources[0]._rev, - ...TEST_DATASOURCE + ...basicDatasource() } ]); }) it("should apply authorization to endpoint", async () => { - await builderEndpointShouldBlockNormalUsers({ - request, + await checkBuilderEndpoint({ + config, method: "GET", url: `/api/datasources`, - appId: appId, }) }) }); describe("destroy", () => { - let datasource; + let datasource beforeEach(async () => { - datasource = await createDatasource() + datasource = await config.createDatasource() }); afterEach(() => { @@ -119,10 +91,10 @@ describe("/datasources", () => { }); it("deletes queries for the datasource after deletion and returns a success message", async () => { - await createQuery(datasource.id) + await config.createQuery() await request - .delete(`/api/datasources/${datasource.id}/${datasource.rev}`) + .delete(`/api/datasources/${datasource._id}/${datasource._rev}`) .set(defaultHeaders(appId)) .expect(200) @@ -136,11 +108,10 @@ describe("/datasources", () => { }) it("should apply authorization to endpoint", async () => { - await builderEndpointShouldBlockNormalUsers({ - request, + await checkBuilderEndpoint({ + config, method: "DELETE", url: `/api/datasources/${datasource._id}/${datasource._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 50ce00b11f..544c5434e6 100644 --- a/packages/server/src/api/routes/tests/utilities/TestConfiguration.js +++ b/packages/server/src/api/routes/tests/utilities/TestConfiguration.js @@ -5,15 +5,10 @@ const { basicRow, basicRole, basicAutomation, + basicDatasource, + basicQuery, } = require("./structures") -const tableController = require("../../../controllers/table") -const rowController = require("../../../controllers/row") -const roleController = require("../../../controllers/role") -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 controllers = require("./controllers") const EMAIL = "babs@babs.com" const PASSWORD = "babs_password" @@ -23,9 +18,6 @@ class TestConfiguration { // we need the request for logging in, involves cookies, hard to fake this.request = request this.appId = null - this.table = null - this.linkedTable = null - this.automation = null } async _req(config, params, controlFunc) { @@ -50,14 +42,14 @@ class TestConfiguration { } async createApp(appName) { - this.app = await this._req({ name: appName }, null, appController.create) + this.app = await this._req({ name: appName }, null, controllers.app.create) this.appId = this.app._id return this.app } async updateTable(config = null) { config = config || basicTable() - this.table = await this._req(config, null, tableController.save) + this.table = await this._req(config, null, controllers.table.save) return this.table } @@ -95,12 +87,12 @@ class TestConfiguration { throw "Test requires table to be configured." } config = config || basicRow(this.table._id) - return this._req(config, { tableId: this.table._id }, rowController.save) + return this._req(config, { tableId: this.table._id }, controllers.row.save) } async createRole(config = null) { config = config || basicRole() - return this._req(config, null, roleController.save) + return this._req(config, null, controllers.role.save) } async addPermission(roleId, resourceId, level = "read") { @@ -111,7 +103,7 @@ class TestConfiguration { resourceId, level, }, - permsController.addPermission + controllers.perms.addPermission ) } @@ -123,7 +115,7 @@ class TestConfiguration { map: "function(doc) { emit(doc[doc.key], doc._id); } ", tableId: this.table._id, } - return this._req(view, null, viewController.save) + return this._req(view, null, controllers.view.save) } async createAutomation(config) { @@ -132,16 +124,16 @@ class TestConfiguration { delete config._rev } this.automation = ( - await this._req(config, null, autoController.create) + await this._req(config, null, controllers.automation.create) ).automation return this.automation } async getAllAutomations() { - return this._req(null, null, autoController.fetch) + return this._req(null, null, controllers.automation.fetch) } - async deleteAutomation(automation) { + async deleteAutomation(automation = null) { automation = automation || this.automation if (!automation) { return @@ -149,10 +141,24 @@ class TestConfiguration { return this._req( null, { id: automation._id, rev: automation._rev }, - autoController.destroy + controllers.automation.destroy ) } + async createDatasource(config = null) { + config = config || basicDatasource() + this.datasource = await this._req(config, null, controllers.datasource.save) + return this.datasource + } + + async createQuery(config = null) { + if (!this.datasource && !config) { + throw "No data source created for query." + } + config = config || basicQuery(this.datasource._id) + return this._req(config, null, controllers.query.save) + } + async createUser( email = EMAIL, password = PASSWORD, @@ -165,7 +171,7 @@ class TestConfiguration { roleId, }, null, - userController.create + controllers.user.create ) } diff --git a/packages/server/src/api/routes/tests/utilities/controllers.js b/packages/server/src/api/routes/tests/utilities/controllers.js new file mode 100644 index 0000000000..d1e23d4527 --- /dev/null +++ b/packages/server/src/api/routes/tests/utilities/controllers.js @@ -0,0 +1,12 @@ +module.exports = { + table: require("../../../controllers/table"), + row: require("../../../controllers/row"), + role: require("../../../controllers/role"), + perms: require("../../../controllers/permission"), + view: require("../../../controllers/view"), + app: require("../../../controllers/application"), + user: require("../../../controllers/user"), + automation: require("../../../controllers/automation"), + datasource: require("../../../controllers/datasource"), + query: require("../../../controllers/query") +} diff --git a/packages/server/src/api/routes/tests/utilities/structures.js b/packages/server/src/api/routes/tests/utilities/structures.js index 1aa81ea2a3..4a21602e40 100644 --- a/packages/server/src/api/routes/tests/utilities/structures.js +++ b/packages/server/src/api/routes/tests/utilities/structures.js @@ -57,3 +57,23 @@ exports.basicRole = () => { permissionId: BUILTIN_PERMISSION_IDS.READ_ONLY, } } + +exports.basicDatasource = () => { + return { + type: "datasource", + name: "Test", + source: "POSTGRES", + config: {}, + } +} + +exports.basicQuery = datasourceId => { + return { + datasourceId: datasourceId, + name: "New Query", + parameters: [], + fields: {}, + schema: {}, + queryVerb: "read", + } +}