From 28329d7f6b1f7b532665685af78e51827ac80c7e Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 10 Mar 2021 11:56:52 +0000 Subject: [PATCH] Fixing issue with datasource find endpoint, removing un-used code and updating data source test. --- .../server/src/api/controllers/datasource.js | 34 ++------ packages/server/src/api/routes/datasource.js | 2 +- .../src/api/routes/tests/datasource.spec.js | 81 +++++++++---------- 3 files changed, 43 insertions(+), 74 deletions(-) diff --git a/packages/server/src/api/controllers/datasource.js b/packages/server/src/api/controllers/datasource.js index 15cea72da7..d73a316adb 100644 --- a/packages/server/src/api/controllers/datasource.js +++ b/packages/server/src/api/controllers/datasource.js @@ -26,35 +26,12 @@ exports.save = async function(ctx) { ...ctx.request.body, } - try { - const response = await db.post(datasource) - datasource._rev = response.rev - - ctx.status = 200 - ctx.message = "Datasource saved successfully." - ctx.body = datasource - } catch (err) { - ctx.throw(err.status, err) - } -} - -exports.update = async function(ctx) { - const db = new CouchDB(ctx.user.appId) - const user = ctx.request.body - const dbUser = await db.get(ctx.request.body._id) - if (user.password) { - user.password = await bcrypt.hash(user.password) - } else { - delete user.password - } - const newData = { ...dbUser, ...user } - - const response = await db.put(newData) - user._rev = response.rev + const response = await db.post(datasource) + datasource._rev = response.rev ctx.status = 200 - ctx.message = `User ${ctx.request.body.email} updated successfully.` - ctx.body = response + ctx.message = "Datasource saved successfully." + ctx.body = datasource } exports.destroy = async function(ctx) { @@ -73,6 +50,5 @@ exports.destroy = async function(ctx) { exports.find = async function(ctx) { const database = new CouchDB(ctx.user.appId) - const datasource = await database.get(ctx.params.datasourceId) - ctx.body = datasource + ctx.body = await database.get(ctx.params.datasourceId) } diff --git a/packages/server/src/api/routes/datasource.js b/packages/server/src/api/routes/datasource.js index 5746ce1839..ee2210704f 100644 --- a/packages/server/src/api/routes/datasource.js +++ b/packages/server/src/api/routes/datasource.js @@ -12,7 +12,7 @@ const router = Router() router .get("/api/datasources", authorized(BUILDER), datasourceController.fetch) .get( - "/api/datasources/:id", + "/api/datasources/:datasourceId", authorized(PermissionTypes.TABLE, PermissionLevels.READ), datasourceController.find ) diff --git a/packages/server/src/api/routes/tests/datasource.spec.js b/packages/server/src/api/routes/tests/datasource.spec.js index 7602b027e1..ee1a1c47f5 100644 --- a/packages/server/src/api/routes/tests/datasource.spec.js +++ b/packages/server/src/api/routes/tests/datasource.spec.js @@ -1,15 +1,17 @@ -let { basicDatasource } = require("./utilities/structures") -let { checkBuilderEndpoint } = require("./utilities/TestFunctions") +let {basicDatasource} = require("./utilities/structures") +let {checkBuilderEndpoint} = require("./utilities/TestFunctions") let setup = require("./utilities") describe("/datasources", () => { let request = setup.getRequest() let config = setup.getConfig() + let datasource afterAll(setup.afterAll) beforeEach(async () => { await config.init() + datasource = await config.createDatasource() }) describe("create", () => { @@ -21,22 +23,12 @@ describe("/datasources", () => { .expect('Content-Type', /json/) .expect(200) - expect(res.res.statusMessage).toEqual("Datasource saved successfully."); - expect(res.body.name).toEqual("Test"); - }) - }); + expect(res.res.statusMessage).toEqual("Datasource saved successfully.") + expect(res.body.name).toEqual("Test") + }) + }) describe("fetch", () => { - let datasource - - beforeEach(async () => { - datasource = await config.createDatasource() - }); - - afterEach(() => { - delete datasource._rev - }); - it("returns all the datasources from the server", async () => { const res = await request .get(`/api/datasources`) @@ -44,36 +36,37 @@ describe("/datasources", () => { .expect('Content-Type', /json/) .expect(200) - const datasources = res.body - expect(datasources).toEqual([ - { - "_id": datasources[0]._id, - "_rev": datasources[0]._rev, - ...basicDatasource() - } - ]); + const datasources = res.body + expect(datasources).toEqual([ + { + "_id": datasources[0]._id, + "_rev": datasources[0]._rev, + ...basicDatasource() + } + ]) }) it("should apply authorization to endpoint", async () => { - await checkBuilderEndpoint({ - config, - method: "GET", - url: `/api/datasources`, - }) + await checkBuilderEndpoint({ + config, + method: "GET", + url: `/api/datasources`, }) - }); + }) + }) + + describe("find", () => { + it("should be able to find a datasource", async () => { + const res = await request + .get(`/api/datasources/${datasource._id}`) + .set(config.defaultHeaders()) + .expect(200) + expect(res.body._rev).toBeDefined() + expect(res.body._id).toEqual(datasource._id) + }) + }) describe("destroy", () => { - let datasource - - beforeEach(async () => { - datasource = await config.createDatasource() - }); - - afterEach(() => { - delete datasource._rev - }); - it("deletes queries for the datasource after deletion and returns a success message", async () => { await config.createQuery() @@ -87,8 +80,8 @@ describe("/datasources", () => { .set(config.defaultHeaders()) .expect('Content-Type', /json/) .expect(200) - - expect(res.body).toEqual([]) + + expect(res.body).toEqual([]) }) it("should apply authorization to endpoint", async () => { @@ -99,5 +92,5 @@ describe("/datasources", () => { }) }) - }); -}); + }) +})