budibase/packages/server/src/api/routes/tests/datasource.spec.js

181 lines
5.5 KiB
JavaScript
Raw Normal View History

jest.mock("pg")
let setup = require("./utilities")
let { basicDatasource } = setup.structures
let { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const pg = require("pg")
const { checkCacheForDynamicVariable } = require("../../../threads/utils")
const { events, testUtils } = require("@budibase/backend-core")
testUtils.mocks.date.mock()
2021-01-14 21:51:03 +01:00
describe("/datasources", () => {
let request = setup.getRequest()
let config = setup.getConfig()
let datasource
2021-01-14 21:51:03 +01:00
afterAll(setup.afterAll)
2021-01-14 21:51:03 +01:00
beforeEach(async () => {
await config.init()
datasource = await config.createDatasource()
jest.clearAllMocks()
})
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())
.set(config.defaultHeaders())
2021-01-14 21:51:03 +01:00
.expect('Content-Type', /json/)
.expect(200)
2021-10-27 14:10:46 +02:00
expect(res.body.datasource.name).toEqual("Test")
expect(res.body.errors).toBeUndefined()
expect(events.datasource.created.mock.calls.length).toBe(1)
})
})
2021-01-14 21:51:03 +01:00
describe("update", () => {
it("should update an existing datasource", async () => {
datasource.name = "Updated Test"
const res = await request
.put(`/api/datasources/${datasource._id}`)
.send(datasource)
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(res.body.datasource.name).toEqual("Updated Test")
expect(res.body.errors).toBeUndefined()
expect(events.datasource.updated.mock.calls.length).toBe(1)
})
describe("dynamic variables", () => {
async function preview(datasource, fields) {
return config.previewQuery(request, config, datasource, fields)
}
it("should invalidate changed or removed variables", async () => {
const { datasource, query } = await config.dynamicVariableDatasource()
// preview once to cache variables
await preview(datasource, { path: "www.test.com", queryString: "test={{ variable3 }}" })
// check variables in cache
let contents = await checkCacheForDynamicVariable(query._id, "variable3")
expect(contents.rows.length).toEqual(1)
// update the datasource to remove the variables
datasource.config.dynamicVariables = []
const res = await request
.put(`/api/datasources/${datasource._id}`)
.send(datasource)
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(res.body.errors).toBeUndefined()
// check variables no longer in cache
contents = await checkCacheForDynamicVariable(query._id, "variable3")
expect(contents).toBe(null)
})
})
})
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`)
.set(config.defaultHeaders())
2021-01-14 21:51:03 +01:00
.expect('Content-Type', /json/)
.expect(200)
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 () => {
await checkBuilderEndpoint({
config,
method: "GET",
url: `/api/datasources`,
2021-01-14 21:51:03 +01:00
})
})
})
2021-01-14 21:51:03 +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
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: {
2021-08-06 13:33:04 +02:00
fields: ["users.name", "users.age"],
},
filters: {
string: {
name: "John",
},
},
})
.set(config.defaultHeaders())
.expect(200)
// this is mock data, can't test it
expect(res.body).toBeDefined()
2021-09-23 18:43:06 +02:00
const expSql = `select "users"."name" as "users.name", "users"."age" as "users.age" from (select * from "users" where "users"."name" ilike $1 limit $2) as "users"`
expect(pg.queryMock).toHaveBeenCalledWith(expSql, ["John%", 5000])
})
})
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}`)
.set(config.defaultHeaders())
2021-01-14 21:51:03 +01:00
.expect(200)
const res = await request
.get(`/api/datasources`)
.set(config.defaultHeaders())
2021-01-14 21:51:03 +01:00
.expect('Content-Type', /json/)
.expect(200)
2021-06-17 17:35:58 +02:00
expect(res.body.length).toEqual(1)
expect(events.datasource.deleted.mock.calls.length).toBe(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}`,
})
})
})
})