1
0
Fork 0
mirror of synced 2024-06-29 11:31:06 +12:00
budibase/packages/server/src/api/routes/tests/query.spec.js

197 lines
5.3 KiB
JavaScript
Raw Normal View History

// mock out postgres for this
jest.mock("pg")
const setup = require("./utilities")
const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const { basicQuery, basicDatasource } = setup.structures
2021-01-15 09:51:03 +13:00
describe("/queries", () => {
let request = setup.getRequest()
let config = setup.getConfig()
let datasource, query
2021-01-15 09:51:03 +13:00
afterAll(setup.afterAll)
2021-01-15 09:51:03 +13:00
beforeEach(async () => {
2021-03-04 23:57:41 +13:00
await config.init()
datasource = await config.createDatasource()
query = await config.createQuery()
2021-02-10 04:24:56 +13:00
})
2021-01-15 09:51:03 +13:00
async function createInvalidIntegration() {
const datasource = await config.createDatasource({
...basicDatasource(),
source: "INVALID_INTEGRATION",
})
const query = await config.createQuery()
return { datasource, query }
}
2021-01-15 09:51:03 +13:00
describe("create", () => {
it("should create a new query", async () => {
2021-03-04 23:57:41 +13:00
const { _id } = await config.createDatasource()
const query = basicQuery(_id)
2021-01-15 09:51:03 +13:00
const res = await request
.post(`/api/queries`)
2021-03-04 23:57:41 +13:00
.send(query)
.set(config.defaultHeaders())
2021-02-10 04:24:56 +13:00
.expect("Content-Type", /json/)
2021-01-15 09:51:03 +13:00
.expect(200)
2021-02-10 04:24:56 +13:00
expect(res.res.statusMessage).toEqual(
2021-03-04 23:57:41 +13:00
`Query ${query.name} saved successfully.`
2021-02-10 04:24:56 +13:00
)
expect(res.body).toEqual({
_rev: res.body._rev,
2021-03-04 23:57:41 +13:00
_id: res.body._id,
...query,
2021-01-15 09:51:03 +13:00
})
2021-02-10 04:24:56 +13:00
})
})
2021-01-15 09:51:03 +13:00
describe("fetch", () => {
it("returns all the queries from the server", async () => {
const res = await request
.get(`/api/queries`)
2021-03-04 23:57:41 +13:00
.set(config.defaultHeaders())
2021-02-10 04:24:56 +13:00
.expect("Content-Type", /json/)
2021-01-15 09:51:03 +13:00
.expect(200)
2021-02-10 04:24:56 +13:00
const queries = res.body
expect(queries).toEqual([
{
2021-03-04 23:57:41 +13:00
_rev: query._rev,
_id: query._id,
...basicQuery(datasource._id),
2021-02-10 04:24:56 +13:00
readable: true,
},
])
2021-01-15 09:51:03 +13:00
})
it("should apply authorization to endpoint", async () => {
2021-03-04 23:57:41 +13:00
await checkBuilderEndpoint({
config,
2021-02-10 04:24:56 +13:00
method: "GET",
url: `/api/datasources`,
2021-01-15 09:51:03 +13:00
})
2021-02-10 04:24:56 +13:00
})
})
2021-01-15 09:51:03 +13:00
describe("find", () => {
it("should find a query in builder", async () => {
const query = await config.createQuery()
const res = await request
.get(`/api/queries/${query._id}`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body._id).toEqual(query._id)
2021-02-10 04:24:56 +13:00
})
2021-01-15 09:51:03 +13:00
it("should find a query in cloud", async () => {
await setup.switchToCloudForFunction(async () => {
const query = await config.createQuery()
const res = await request
.get(`/api/queries/${query._id}`)
.set(await config.roleHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.fields).toBeUndefined()
expect(res.body.parameters).toBeUndefined()
expect(res.body.schema).toBeUndefined()
})
2021-02-10 04:24:56 +13:00
})
})
2021-01-15 09:51:03 +13:00
describe("destroy", () => {
2021-01-15 09:51:03 +13:00
it("deletes a query and returns a success message", async () => {
await request
2021-03-04 23:57:41 +13:00
.delete(`/api/queries/${query._id}/${query._rev}`)
.set(config.defaultHeaders())
2021-01-15 09:51:03 +13:00
.expect(200)
const res = await request
.get(`/api/queries`)
2021-03-04 23:57:41 +13:00
.set(config.defaultHeaders())
2021-02-10 04:24:56 +13:00
.expect("Content-Type", /json/)
2021-01-15 09:51:03 +13:00
.expect(200)
2021-02-10 04:24:56 +13:00
expect(res.body).toEqual([])
2021-01-15 09:51:03 +13:00
})
it("should apply authorization to endpoint", async () => {
2021-03-04 23:57:41 +13:00
await checkBuilderEndpoint({
config,
2021-01-15 09:51:03 +13:00
method: "DELETE",
url: `/api/queries/${config._id}/${config._rev}`,
2021-01-15 09:51:03 +13:00
})
})
2021-02-10 04:24:56 +13:00
})
describe("preview", () => {
it("should be able to preview the query", async () => {
const res = await request
.post(`/api/queries/preview`)
.send({
datasourceId: datasource._id,
parameters: {},
fields: {},
queryVerb: "read",
})
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
// these responses come from the mock
expect(res.body.schemaFields).toEqual(["a", "b"])
expect(res.body.rows.length).toEqual(1)
})
it("should apply authorization to endpoint", async () => {
await checkBuilderEndpoint({
config,
method: "POST",
url: `/api/queries/preview`,
})
})
it("should fail with invalid integration type", async () => {
const { datasource } = await createInvalidIntegration()
await request
.post(`/api/queries/preview`)
.send({
datasourceId: datasource._id,
parameters: {},
fields: {},
queryVerb: "read",
})
.set(config.defaultHeaders())
.expect(400)
})
})
describe("execute", () => {
it("should be able to execute the query", async () => {
const res = await request
.post(`/api/queries/${query._id}`)
.send({
parameters: {},
})
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.length).toEqual(1)
})
it("should fail with invalid integration type", async () => {
const { query } = await createInvalidIntegration()
await request
.post(`/api/queries/${query._id}`)
.send({
parameters: {},
})
.set(config.defaultHeaders())
.expect(400)
})
})
2021-02-10 04:24:56 +13:00
})