Fixing issue with datasource find endpoint, removing un-used code and updating data source test.

This commit is contained in:
mike12345567 2021-03-10 11:56:52 +00:00
parent dd16c84ecd
commit 28329d7f6b
3 changed files with 43 additions and 74 deletions

View File

@ -26,35 +26,12 @@ exports.save = async function(ctx) {
...ctx.request.body, ...ctx.request.body,
} }
try { const response = await db.post(datasource)
const response = await db.post(datasource) datasource._rev = response.rev
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
ctx.status = 200 ctx.status = 200
ctx.message = `User ${ctx.request.body.email} updated successfully.` ctx.message = "Datasource saved successfully."
ctx.body = response ctx.body = datasource
} }
exports.destroy = async function(ctx) { exports.destroy = async function(ctx) {
@ -73,6 +50,5 @@ exports.destroy = async function(ctx) {
exports.find = async function(ctx) { exports.find = async function(ctx) {
const database = new CouchDB(ctx.user.appId) const database = new CouchDB(ctx.user.appId)
const datasource = await database.get(ctx.params.datasourceId) ctx.body = await database.get(ctx.params.datasourceId)
ctx.body = datasource
} }

View File

@ -12,7 +12,7 @@ const router = Router()
router router
.get("/api/datasources", authorized(BUILDER), datasourceController.fetch) .get("/api/datasources", authorized(BUILDER), datasourceController.fetch)
.get( .get(
"/api/datasources/:id", "/api/datasources/:datasourceId",
authorized(PermissionTypes.TABLE, PermissionLevels.READ), authorized(PermissionTypes.TABLE, PermissionLevels.READ),
datasourceController.find datasourceController.find
) )

View File

@ -1,15 +1,17 @@
let { basicDatasource } = require("./utilities/structures") let {basicDatasource} = require("./utilities/structures")
let { checkBuilderEndpoint } = require("./utilities/TestFunctions") let {checkBuilderEndpoint} = require("./utilities/TestFunctions")
let setup = require("./utilities") let setup = require("./utilities")
describe("/datasources", () => { describe("/datasources", () => {
let request = setup.getRequest() let request = setup.getRequest()
let config = setup.getConfig() let config = setup.getConfig()
let datasource
afterAll(setup.afterAll) afterAll(setup.afterAll)
beforeEach(async () => { beforeEach(async () => {
await config.init() await config.init()
datasource = await config.createDatasource()
}) })
describe("create", () => { describe("create", () => {
@ -21,22 +23,12 @@ describe("/datasources", () => {
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
expect(res.res.statusMessage).toEqual("Datasource saved successfully."); expect(res.res.statusMessage).toEqual("Datasource saved successfully.")
expect(res.body.name).toEqual("Test"); expect(res.body.name).toEqual("Test")
}) })
}); })
describe("fetch", () => { describe("fetch", () => {
let datasource
beforeEach(async () => {
datasource = await config.createDatasource()
});
afterEach(() => {
delete datasource._rev
});
it("returns all the datasources from the server", async () => { it("returns all the datasources from the server", async () => {
const res = await request const res = await request
.get(`/api/datasources`) .get(`/api/datasources`)
@ -44,36 +36,37 @@ describe("/datasources", () => {
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
const datasources = res.body const datasources = res.body
expect(datasources).toEqual([ expect(datasources).toEqual([
{ {
"_id": datasources[0]._id, "_id": datasources[0]._id,
"_rev": datasources[0]._rev, "_rev": datasources[0]._rev,
...basicDatasource() ...basicDatasource()
} }
]); ])
}) })
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
await checkBuilderEndpoint({ await checkBuilderEndpoint({
config, config,
method: "GET", method: "GET",
url: `/api/datasources`, 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", () => { 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 () => { it("deletes queries for the datasource after deletion and returns a success message", async () => {
await config.createQuery() await config.createQuery()
@ -88,7 +81,7 @@ describe("/datasources", () => {
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
expect(res.body).toEqual([]) expect(res.body).toEqual([])
}) })
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
@ -99,5 +92,5 @@ describe("/datasources", () => {
}) })
}) })
}); })
}); })