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,
}
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
ctx.status = 200
ctx.message = `User ${ctx.request.body.email} updated successfully.`
ctx.body = response
}
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)
}

View File

@ -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
)

View File

@ -5,11 +5,13 @@ 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`)
@ -51,7 +43,7 @@ describe("/datasources", () => {
"_rev": datasources[0]._rev,
...basicDatasource()
}
]);
])
})
it("should apply authorization to endpoint", async () => {
@ -61,19 +53,20 @@ describe("/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", () => {
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()
@ -99,5 +92,5 @@ describe("/datasources", () => {
})
})
});
});
})
})