From 01f9981f110589333c3de198b203fb7df2b270bf Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 4 Mar 2021 14:36:59 +0000 Subject: [PATCH] Refactoring table testcases. --- .../server/src/api/routes/tests/table.spec.js | 125 ++++++++---------- .../tests/utilities/TestConfiguration.js | 5 + 2 files changed, 62 insertions(+), 68 deletions(-) diff --git a/packages/server/src/api/routes/tests/table.spec.js b/packages/server/src/api/routes/tests/table.spec.js index 67d53a489b..976db6ee56 100644 --- a/packages/server/src/api/routes/tests/table.spec.js +++ b/packages/server/src/api/routes/tests/table.spec.js @@ -1,30 +1,22 @@ -const { - createTable, - supertest, - createApplication, - defaultHeaders, - builderEndpointShouldBlockNormalUsers, - getDocument -} = require("./couchTestUtils") +const TestConfig = require("./utilities/TestConfiguration") +const { checkBuilderEndpoint } = require("./utilities/TestFunctions") describe("/tables", () => { let request - let server - let app - let appId + let config beforeAll(async () => { - ({ request, server } = await supertest()) - }); + config = new TestConfig() + request = config.request + }) afterAll(() => { - server.close() + config.end() }) beforeEach(async () => { - app = await createApplication(request) - appId = app.instance._id - }); + await config.init() + }) describe("create", () => { it("returns a success message when the table is successfully created", done => { @@ -37,25 +29,25 @@ describe("/tables", () => { name: { type: "string" } } }) - .set(defaultHeaders(appId)) + .set(config.defaultHeaders()) .expect('Content-Type', /json/) .expect(200) .end(async (err, res) => { - expect(res.res.statusMessage).toEqual("Table TestTable saved successfully."); - expect(res.body.name).toEqual("TestTable"); - done(); - }); + expect(res.res.statusMessage).toEqual("Table TestTable saved successfully.") + expect(res.body.name).toEqual("TestTable") + done() + }) }) it("renames all the row fields for a table when a schema key is renamed", async () => { - const testTable = await createTable(request, appId); + const testTable = await config.createTable() const testRow = await request .post(`/api/${testTable._id}/rows`) .send({ name: "test" }) - .set(defaultHeaders(appId)) + .set(config.defaultHeaders()) .expect('Content-Type', /json/) .expect(200) @@ -74,29 +66,28 @@ describe("/tables", () => { updatedName: { type: "string" } } }) - .set(defaultHeaders(appId)) + .set(config.defaultHeaders()) .expect('Content-Type', /json/) .expect(200) - expect(updatedTable.res.statusMessage).toEqual("Table TestTable saved successfully."); - expect(updatedTable.body.name).toEqual("TestTable"); + expect(updatedTable.res.statusMessage).toEqual("Table TestTable saved successfully.") + expect(updatedTable.body.name).toEqual("TestTable") const res = await request .get(`/api/${testTable._id}/rows/${testRow.body._id}`) - .set(defaultHeaders(appId)) + .set(config.defaultHeaders()) .expect('Content-Type', /json/) .expect(200) - expect(res.body.updatedName).toEqual("test"); - expect(res.body.name).toBeUndefined(); - }); + expect(res.body.updatedName).toEqual("test") + expect(res.body.name).toBeUndefined() + }) it("should apply authorization to endpoint", async () => { - await builderEndpointShouldBlockNormalUsers({ - request, + await checkBuilderEndpoint({ + config, method: "POST", url: `/api/tables`, - appId: appId, body: { name: "TestTable", key: "name", @@ -106,68 +97,67 @@ describe("/tables", () => { } }) }) - }); + }) describe("fetch", () => { let testTable beforeEach(async () => { - testTable = await createTable(request, appId, testTable) - }); + testTable = await config.createTable(testTable) + }) afterEach(() => { delete testTable._rev - }); + }) it("returns all the tables for that instance in the response body", done => { request .get(`/api/tables`) - .set(defaultHeaders(appId)) + .set(config.defaultHeaders()) .expect('Content-Type', /json/) .expect(200) .end(async (_, res) => { - const fetchedTable = res.body[0]; - expect(fetchedTable.name).toEqual(testTable.name); - expect(fetchedTable.type).toEqual("table"); - done(); - }); + const fetchedTable = res.body[0] + expect(fetchedTable.name).toEqual(testTable.name) + expect(fetchedTable.type).toEqual("table") + done() + }) }) it("should apply authorization to endpoint", async () => { - await builderEndpointShouldBlockNormalUsers({ - request, + await checkBuilderEndpoint({ + config, method: "GET", url: `/api/tables`, - appId: appId, }) }) - }); + }) describe("destroy", () => { - let testTable; + let testTable beforeEach(async () => { - testTable = await createTable(request, appId, testTable) - }); + testTable = await config.createTable(testTable) + }) afterEach(() => { delete testTable._rev - }); + }) it("returns a success response when a table is deleted.", async done => { request .delete(`/api/tables/${testTable._id}/${testTable._rev}`) - .set(defaultHeaders(appId)) + .set(config.defaultHeaders()) .expect('Content-Type', /json/) .expect(200) .end(async (_, res) => { - expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`); - done(); - }); + expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`) + done() + }) }) it("deletes linked references to the table after deletion", async done => { - const linkedTable = await createTable(request, appId, { + const linkedTable = await config.createTable({ name: "LinkedTable", type: "table", key: "name", @@ -190,25 +180,24 @@ describe("/tables", () => { request .delete(`/api/tables/${testTable._id}/${testTable._rev}`) - .set(defaultHeaders(appId)) + .set(config.defaultHeaders()) .expect('Content-Type', /json/) .expect(200) .end(async (_, res) => { - expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`); - const dependentTable = await getDocument(appId, linkedTable._id) - expect(dependentTable.schema.TestTable).not.toBeDefined(); - done(); - }); + expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`) + const dependentTable = await config.getTable(linkedTable._id) + expect(dependentTable.schema.TestTable).not.toBeDefined() + done() + }) }) it("should apply authorization to endpoint", async () => { - await builderEndpointShouldBlockNormalUsers({ - request, + await checkBuilderEndpoint({ + config, method: "DELETE", url: `/api/tables/${testTable._id}/${testTable._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 093a19c9d1..6ecf9fc1c2 100644 --- a/packages/server/src/api/routes/tests/utilities/TestConfiguration.js +++ b/packages/server/src/api/routes/tests/utilities/TestConfiguration.js @@ -94,6 +94,11 @@ class TestConfiguration { return this.updateTable(config) } + async getTable(tableId = null) { + tableId = tableId || this.table._id + return this._req(null, { id: tableId }, controllers.table.find) + } + async createLinkedTable() { if (!this.table) { throw "Must have created a table first."