From 317d6633a28f0c8b17594475b2160f10ff593359 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 4 Mar 2021 12:32:31 +0000 Subject: [PATCH] Refactoring to remove the use of the supertest, making this part of the test config. --- .../src/api/routes/tests/application.spec.js | 12 ++---- .../src/api/routes/tests/automation.spec.js | 13 ++---- .../src/api/routes/tests/datasource.spec.js | 12 ++---- .../src/api/routes/tests/permissions.spec.js | 10 ++--- .../server/src/api/routes/tests/query.spec.js | 10 ++--- .../server/src/api/routes/tests/role.spec.js | 40 +++++++------------ .../tests/utilities/TestConfiguration.js | 11 ++++- .../src/api/routes/tests/utilities/index.js | 13 ------ 8 files changed, 42 insertions(+), 79 deletions(-) diff --git a/packages/server/src/api/routes/tests/application.spec.js b/packages/server/src/api/routes/tests/application.spec.js index 8aa3d0288a..a453e60c0a 100644 --- a/packages/server/src/api/routes/tests/application.spec.js +++ b/packages/server/src/api/routes/tests/application.spec.js @@ -1,26 +1,22 @@ -const { - supertest, -} = require("./utilities") const TestConfig = require("./utilities/TestConfiguration") const { clearAllApps, checkBuilderEndpoint } = require("./utilities/TestFunctions") describe("/applications", () => { let request - let server let config beforeAll(async () => { - ({ request, server } = await supertest()) - }); + config = new TestConfig() + request = config.request + }) beforeEach(async () => { await clearAllApps() - config = new TestConfig(request) await config.init() }) afterAll(() => { - server.close() + config.end() }) describe("create", () => { diff --git a/packages/server/src/api/routes/tests/automation.spec.js b/packages/server/src/api/routes/tests/automation.spec.js index 3118f36903..8038ea8481 100644 --- a/packages/server/src/api/routes/tests/automation.spec.js +++ b/packages/server/src/api/routes/tests/automation.spec.js @@ -1,6 +1,3 @@ -const { - supertest, -} = require("./utilities") const TestConfig = require("./utilities/TestConfiguration") const { checkBuilderEndpoint, @@ -19,22 +16,20 @@ let LOGIC_DEFINITIONS = {} describe("/automations", () => { let request - let server - let app let config let automation beforeAll(async () => { - ({ request, server } = await supertest()) + config = new TestConfig() + request = config.request }) beforeEach(async () => { - config = new TestConfig(request) - app = await config.init() + await config.init() }) afterAll(() => { - server.close() + config.end() }) const triggerWorkflow = async automation => { diff --git a/packages/server/src/api/routes/tests/datasource.spec.js b/packages/server/src/api/routes/tests/datasource.spec.js index 1f78c65f07..280c54f94b 100644 --- a/packages/server/src/api/routes/tests/datasource.spec.js +++ b/packages/server/src/api/routes/tests/datasource.spec.js @@ -1,22 +1,18 @@ -const { - supertest, -} = require("./utilities") let TestConfig = require("./utilities/TestConfiguration") let { basicDatasource } = require("./utilities/structures") let { checkBuilderEndpoint } = require("./utilities/TestFunctions") describe("/datasources", () => { let request - let server let config beforeAll(async () => { - ({ request, server } = await supertest()) - config = new TestConfig(request) - }); + config = new TestConfig() + request = config.request + }) afterAll(() => { - server.close() + config.end() }) beforeEach(async () => { diff --git a/packages/server/src/api/routes/tests/permissions.spec.js b/packages/server/src/api/routes/tests/permissions.spec.js index 046a0dc00b..13b07eeb2a 100644 --- a/packages/server/src/api/routes/tests/permissions.spec.js +++ b/packages/server/src/api/routes/tests/permissions.spec.js @@ -1,6 +1,3 @@ -const { - supertest, -} = require("./utilities") const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles") const TestConfig = require("./utilities/TestConfiguration") const { basicRow } = require("./utilities/structures") @@ -9,7 +6,6 @@ const HIGHER_ROLE_ID = BUILTIN_ROLE_IDS.BASIC const STD_ROLE_ID = BUILTIN_ROLE_IDS.PUBLIC describe("/permission", () => { - let server let request let table let perms @@ -17,12 +13,12 @@ describe("/permission", () => { let config beforeAll(async () => { - ;({ request, server } = await supertest()) - config = new TestConfig(request) + config = new TestConfig() + request = config.request }) afterAll(() => { - server.close() + config.end() }) beforeEach(async () => { diff --git a/packages/server/src/api/routes/tests/query.spec.js b/packages/server/src/api/routes/tests/query.spec.js index 1151d7e622..98ef8f78cb 100644 --- a/packages/server/src/api/routes/tests/query.spec.js +++ b/packages/server/src/api/routes/tests/query.spec.js @@ -1,22 +1,18 @@ -const { - supertest, -} = require("./utilities") const TestConfig = require("./utilities/TestConfiguration") const { checkBuilderEndpoint } = require("./utilities/TestFunctions") const { basicQuery } = require("./utilities/structures") describe("/queries", () => { let request - let server let config beforeAll(async () => { - ;({ request, server } = await supertest()) - config = new TestConfig(request) + config = new TestConfig() + request = config.request }) afterAll(() => { - server.close() + config.end() }) beforeEach(async () => { diff --git a/packages/server/src/api/routes/tests/role.spec.js b/packages/server/src/api/routes/tests/role.spec.js index b86b285dbc..122afdb07e 100644 --- a/packages/server/src/api/routes/tests/role.spec.js +++ b/packages/server/src/api/routes/tests/role.spec.js @@ -1,43 +1,33 @@ -const { - createApplication, - supertest, - defaultHeaders, -} = require("./couchTestUtils") const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles") const { BUILTIN_PERMISSION_IDS, } = require("../../../utilities/security/permissions") - -const roleBody = { - name: "NewRole", - inherits: BUILTIN_ROLE_IDS.BASIC, - permissionId: BUILTIN_PERMISSION_IDS.READ_ONLY, -} +const { basicRole } = require("./utilities/structures") +const TestConfig = require("./utilities/TestConfiguration") describe("/roles", () => { - let server let request - let appId + let config beforeAll(async () => { - ;({ request, server } = await supertest()) + config = new TestConfig() + request = config.request }) afterAll(() => { - server.close() + config.close() }) beforeEach(async () => { - let app = await createApplication(request) - appId = app.instance._id + await config.init() }) describe("create", () => { it("returns a success message when role is successfully created", async () => { const res = await request .post(`/api/roles`) - .send(roleBody) - .set(defaultHeaders(appId)) + .send(basicRole()) + .set(config.defaultHeaders()) .expect("Content-Type", /json/) .expect(200) @@ -53,8 +43,8 @@ describe("/roles", () => { it("should list custom roles, plus 2 default roles", async () => { const createRes = await request .post(`/api/roles`) - .send(roleBody) - .set(defaultHeaders(appId)) + .send(basicRole()) + .set(config.defaultHeaders()) .expect("Content-Type", /json/) .expect(200) @@ -62,7 +52,7 @@ describe("/roles", () => { const res = await request .get(`/api/roles`) - .set(defaultHeaders(appId)) + .set(config.defaultHeaders()) .expect("Content-Type", /json/) .expect(200) @@ -92,7 +82,7 @@ describe("/roles", () => { const createRes = await request .post(`/api/roles`) .send({ name: "user", permissionId: BUILTIN_PERMISSION_IDS.READ_ONLY }) - .set(defaultHeaders(appId)) + .set(config.defaultHeaders()) .expect("Content-Type", /json/) .expect(200) @@ -100,12 +90,12 @@ describe("/roles", () => { await request .delete(`/api/roles/${customRole._id}/${customRole._rev}`) - .set(defaultHeaders(appId)) + .set(config.defaultHeaders()) .expect(200) await request .get(`/api/roles/${customRole._id}`) - .set(defaultHeaders(appId)) + .set(config.defaultHeaders()) .expect(404) }) }) diff --git a/packages/server/src/api/routes/tests/utilities/TestConfiguration.js b/packages/server/src/api/routes/tests/utilities/TestConfiguration.js index 1c07a8f7ff..4e411b04bf 100644 --- a/packages/server/src/api/routes/tests/utilities/TestConfiguration.js +++ b/packages/server/src/api/routes/tests/utilities/TestConfiguration.js @@ -10,14 +10,17 @@ const { basicQuery, } = require("./structures") const controllers = require("./controllers") +const supertest = require("supertest") const EMAIL = "babs@babs.com" const PASSWORD = "babs_password" class TestConfiguration { - constructor(request) { + constructor() { + env.PORT = 4002 + this.server = require("../../../../app") + this.request = supertest(this.server) // we need the request for logging in, involves cookies, hard to fake - this.request = request this.appId = null } @@ -42,6 +45,10 @@ class TestConfiguration { return this.createApp(appName) } + end() { + this.server.close() + } + defaultHeaders() { const builderUser = { userId: "BUILDER", diff --git a/packages/server/src/api/routes/tests/utilities/index.js b/packages/server/src/api/routes/tests/utilities/index.js index 1421521a26..6e7825b599 100644 --- a/packages/server/src/api/routes/tests/utilities/index.js +++ b/packages/server/src/api/routes/tests/utilities/index.js @@ -1,16 +1,3 @@ -const supertest = require("supertest") -const env = require("../../../../environment") - const TEST_CLIENT_ID = "test-client-id" exports.TEST_CLIENT_ID = TEST_CLIENT_ID - -exports.supertest = async () => { - let request - let server - env.PORT = 4002 - server = require("../../../../app") - - request = supertest(server) - return { request, server } -}