Fixing issue with datasource find endpoint, removing un-used code and updating data source test.
This commit is contained in:
parent
dd16c84ecd
commit
28329d7f6b
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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", () => {
|
|||
})
|
||||
})
|
||||
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue