2021-06-25 14:46:02 +02:00
|
|
|
jest.mock("pg")
|
|
|
|
|
2021-03-05 13:11:44 +01:00
|
|
|
let setup = require("./utilities")
|
2021-03-11 19:29:48 +01:00
|
|
|
let { basicDatasource } = setup.structures
|
|
|
|
let { checkBuilderEndpoint } = require("./utilities/TestFunctions")
|
2021-06-04 16:16:15 +02:00
|
|
|
const pg = require("pg")
|
|
|
|
|
2021-01-14 21:51:03 +01:00
|
|
|
describe("/datasources", () => {
|
2021-03-05 13:11:44 +01:00
|
|
|
let request = setup.getRequest()
|
|
|
|
let config = setup.getConfig()
|
2021-03-10 12:56:52 +01:00
|
|
|
let datasource
|
2021-01-14 21:51:03 +01:00
|
|
|
|
2021-03-05 13:11:44 +01:00
|
|
|
afterAll(setup.afterAll)
|
2021-01-14 21:51:03 +01:00
|
|
|
|
|
|
|
beforeEach(async () => {
|
2021-03-04 11:40:27 +01:00
|
|
|
await config.init()
|
2021-03-10 12:56:52 +01:00
|
|
|
datasource = await config.createDatasource()
|
2021-03-05 13:11:44 +01:00
|
|
|
})
|
2021-01-14 21:51:03 +01:00
|
|
|
|
|
|
|
describe("create", () => {
|
|
|
|
it("should create a new datasource", async () => {
|
|
|
|
const res = await request
|
|
|
|
.post(`/api/datasources`)
|
2021-03-04 11:05:50 +01:00
|
|
|
.send(basicDatasource())
|
2021-03-04 11:40:27 +01:00
|
|
|
.set(config.defaultHeaders())
|
2021-01-14 21:51:03 +01:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
|
2021-03-10 12:56:52 +01:00
|
|
|
expect(res.res.statusMessage).toEqual("Datasource saved successfully.")
|
|
|
|
expect(res.body.name).toEqual("Test")
|
|
|
|
})
|
|
|
|
})
|
2021-01-14 21:51:03 +01:00
|
|
|
|
|
|
|
describe("fetch", () => {
|
|
|
|
it("returns all the datasources from the server", async () => {
|
|
|
|
const res = await request
|
|
|
|
.get(`/api/datasources`)
|
2021-03-04 11:40:27 +01:00
|
|
|
.set(config.defaultHeaders())
|
2021-01-14 21:51:03 +01:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
|
2021-03-10 12:56:52 +01:00
|
|
|
const datasources = res.body
|
2021-06-17 17:52:52 +02:00
|
|
|
|
|
|
|
// remove non-deterministic fields
|
|
|
|
for (let source of datasources) {
|
|
|
|
delete source._id
|
|
|
|
delete source._rev
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:35:58 +02:00
|
|
|
expect(datasources).toMatchSnapshot()
|
2021-01-14 21:51:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should apply authorization to endpoint", async () => {
|
2021-03-10 12:56:52 +01:00
|
|
|
await checkBuilderEndpoint({
|
|
|
|
config,
|
|
|
|
method: "GET",
|
|
|
|
url: `/api/datasources`,
|
2021-01-14 21:51:03 +01:00
|
|
|
})
|
2021-03-10 12:56:52 +01:00
|
|
|
})
|
|
|
|
})
|
2021-01-14 21:51:03 +01:00
|
|
|
|
2021-03-10 12:56:52 +01:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
2021-01-14 21:51:03 +01:00
|
|
|
|
2021-06-04 16:16:15 +02:00
|
|
|
describe("query", () => {
|
|
|
|
it("should be able to query a pg datasource", async () => {
|
|
|
|
const res = await request
|
|
|
|
.post(`/api/datasources/query`)
|
|
|
|
.send({
|
|
|
|
endpoint: {
|
|
|
|
datasourceId: datasource._id,
|
|
|
|
operation: "READ",
|
|
|
|
// table name below
|
|
|
|
entityId: "users",
|
|
|
|
},
|
|
|
|
resource: {
|
|
|
|
fields: ["name", "age"],
|
|
|
|
},
|
|
|
|
filters: {
|
|
|
|
string: {
|
|
|
|
name: "John",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.set(config.defaultHeaders())
|
|
|
|
.expect(200)
|
|
|
|
// this is mock data, can't test it
|
|
|
|
expect(res.body).toBeDefined()
|
2021-07-02 15:49:47 +02:00
|
|
|
expect(pg.queryMock).toHaveBeenCalledWith(`select "name", "age" from "users" where "users"."name" like $1 limit $2`, ["John%", 5000])
|
2021-06-04 16:16:15 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-03-10 12:56:52 +01:00
|
|
|
describe("destroy", () => {
|
2021-01-14 21:51:03 +01:00
|
|
|
it("deletes queries for the datasource after deletion and returns a success message", async () => {
|
2021-03-04 11:05:50 +01:00
|
|
|
await config.createQuery()
|
2021-01-14 21:51:03 +01:00
|
|
|
|
|
|
|
await request
|
2021-03-04 11:05:50 +01:00
|
|
|
.delete(`/api/datasources/${datasource._id}/${datasource._rev}`)
|
2021-03-04 11:40:27 +01:00
|
|
|
.set(config.defaultHeaders())
|
2021-01-14 21:51:03 +01:00
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
const res = await request
|
|
|
|
.get(`/api/datasources`)
|
2021-03-04 11:40:27 +01:00
|
|
|
.set(config.defaultHeaders())
|
2021-01-14 21:51:03 +01:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2021-03-10 12:56:52 +01:00
|
|
|
|
2021-06-17 17:35:58 +02:00
|
|
|
expect(res.body.length).toEqual(1)
|
2021-01-14 21:51:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should apply authorization to endpoint", async () => {
|
2021-03-04 11:05:50 +01:00
|
|
|
await checkBuilderEndpoint({
|
|
|
|
config,
|
2021-01-14 21:51:03 +01:00
|
|
|
method: "DELETE",
|
|
|
|
url: `/api/datasources/${datasource._id}/${datasource._rev}`,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-03-10 12:56:52 +01:00
|
|
|
})
|
|
|
|
})
|