2021-11-26 02:00:43 +13:00
|
|
|
// Mock out postgres for this
|
2021-03-10 00:56:32 +13:00
|
|
|
jest.mock("pg")
|
2022-01-05 07:23:45 +13:00
|
|
|
jest.mock("node-fetch")
|
2021-03-10 00:56:32 +13:00
|
|
|
|
2021-11-26 02:00:43 +13:00
|
|
|
// Mock isProdAppID to we can later mock the implementation and pretend we are
|
|
|
|
// using prod app IDs
|
|
|
|
const authDb = require("@budibase/auth/db")
|
|
|
|
const { isProdAppID } = authDb
|
|
|
|
const mockIsProdAppID = jest.fn(isProdAppID)
|
|
|
|
authDb.isProdAppID = mockIsProdAppID
|
|
|
|
|
2021-03-06 01:11:44 +13:00
|
|
|
const setup = require("./utilities")
|
2021-03-12 07:29:48 +13:00
|
|
|
const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
|
2022-01-06 04:01:28 +13:00
|
|
|
const { checkCacheForDynamicVariable } = require("../../../threads/utils")
|
2021-03-12 07:29:48 +13:00
|
|
|
const { basicQuery, basicDatasource } = setup.structures
|
2021-01-15 09:51:03 +13:00
|
|
|
|
|
|
|
describe("/queries", () => {
|
2021-03-06 01:11:44 +13:00
|
|
|
let request = setup.getRequest()
|
|
|
|
let config = setup.getConfig()
|
2021-03-10 00:56:32 +13:00
|
|
|
let datasource, query
|
2021-01-15 09:51:03 +13:00
|
|
|
|
2021-03-06 01:11:44 +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()
|
2021-03-09 07:03:26 +13:00
|
|
|
datasource = await config.createDatasource()
|
2021-03-10 00:56:32 +13:00
|
|
|
query = await config.createQuery()
|
2021-02-10 04:24:56 +13:00
|
|
|
})
|
2021-01-15 09:51:03 +13:00
|
|
|
|
2021-03-10 00:56:32 +13:00
|
|
|
async function createInvalidIntegration() {
|
2021-11-26 00:52:02 +13:00
|
|
|
const datasource = await config.createDatasource({
|
|
|
|
datasource: {
|
|
|
|
...basicDatasource().datasource,
|
|
|
|
source: "INVALID_INTEGRATION",
|
|
|
|
},
|
|
|
|
})
|
2021-03-10 00:56:32 +13:00
|
|
|
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
|
|
|
|
2021-03-09 07:03:26 +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
|
|
|
|
2021-03-09 07:03:26 +13:00
|
|
|
it("should find a query in cloud", async () => {
|
2021-03-25 07:21:23 +13:00
|
|
|
await setup.switchToSelfHosted(async () => {
|
2021-03-09 07:03:26 +13:00
|
|
|
const query = await config.createQuery()
|
|
|
|
const res = await request
|
|
|
|
.get(`/api/queries/${query._id}`)
|
2021-10-26 04:59:09 +13:00
|
|
|
.set(await config.defaultHeaders())
|
2021-03-09 07:03:26 +13:00
|
|
|
.expect(200)
|
2021-10-26 04:59:09 +13:00
|
|
|
.expect("Content-Type", /json/)
|
2021-11-26 02:00:43 +13:00
|
|
|
expect(res.body.fields).toBeDefined()
|
|
|
|
expect(res.body.parameters).toBeDefined()
|
|
|
|
expect(res.body.schema).toBeDefined()
|
2021-03-09 07:03:26 +13:00
|
|
|
})
|
2021-02-10 04:24:56 +13:00
|
|
|
})
|
2021-11-26 02:00:43 +13:00
|
|
|
|
|
|
|
it("should remove sensitive info for prod apps", async () => {
|
|
|
|
// Mock isProdAppID to pretend we are using a prod app
|
|
|
|
mockIsProdAppID.mockClear()
|
|
|
|
mockIsProdAppID.mockImplementation(() => true)
|
|
|
|
|
|
|
|
const query = await config.createQuery()
|
|
|
|
const res = await request
|
|
|
|
.get(`/api/queries/${query._id}`)
|
|
|
|
.set(await config.defaultHeaders())
|
|
|
|
.expect("Content-Type", /json/)
|
|
|
|
.expect(200)
|
|
|
|
expect(res.body._id).toEqual(query._id)
|
|
|
|
expect(res.body.fields).toBeUndefined()
|
|
|
|
expect(res.body.parameters).toBeUndefined()
|
|
|
|
expect(res.body.schema).toBeDefined()
|
|
|
|
|
|
|
|
// Reset isProdAppID mock
|
|
|
|
expect(mockIsProdAppID).toHaveBeenCalledTimes(1)
|
|
|
|
mockIsProdAppID.mockImplementation(isProdAppID)
|
|
|
|
})
|
2021-03-09 07:03:26 +13:00
|
|
|
})
|
2021-01-15 09:51:03 +13:00
|
|
|
|
2021-03-09 07:03:26 +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",
|
2021-03-10 00:56:32 +13:00
|
|
|
url: `/api/queries/${config._id}/${config._rev}`,
|
2021-01-15 09:51:03 +13:00
|
|
|
})
|
|
|
|
})
|
2021-02-10 04:24:56 +13:00
|
|
|
})
|
2021-03-09 07:03:26 +13:00
|
|
|
|
|
|
|
describe("preview", () => {
|
2021-03-10 00:56:32 +13:00
|
|
|
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)
|
|
|
|
})
|
2021-03-09 07:03:26 +13:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("execute", () => {
|
2021-03-10 00:56:32 +13:00
|
|
|
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 () => {
|
2021-09-29 03:29:42 +13:00
|
|
|
const { query, datasource } = await createInvalidIntegration()
|
2021-03-10 00:56:32 +13:00
|
|
|
await request
|
|
|
|
.post(`/api/queries/${query._id}`)
|
|
|
|
.send({
|
2021-09-29 03:29:42 +13:00
|
|
|
datasourceId: datasource._id,
|
2021-03-10 00:56:32 +13:00
|
|
|
parameters: {},
|
2021-09-29 03:29:42 +13:00
|
|
|
fields: {},
|
|
|
|
queryVerb: "read",
|
2021-03-10 00:56:32 +13:00
|
|
|
})
|
|
|
|
.set(config.defaultHeaders())
|
|
|
|
.expect(400)
|
|
|
|
})
|
2021-03-09 07:03:26 +13:00
|
|
|
})
|
2022-01-05 07:23:45 +13:00
|
|
|
|
|
|
|
describe("test variables", () => {
|
|
|
|
async function restDatasource(cfg) {
|
|
|
|
return await config.createDatasource({
|
|
|
|
datasource: {
|
|
|
|
...basicDatasource().datasource,
|
|
|
|
source: "REST",
|
|
|
|
config: cfg || {},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-06 04:01:28 +13:00
|
|
|
async function dynamicVariableDatasource() {
|
2022-01-05 07:23:45 +13:00
|
|
|
const datasource = await restDatasource()
|
|
|
|
const basedOnQuery = await config.createQuery({
|
|
|
|
...basicQuery(datasource._id),
|
|
|
|
fields: {
|
|
|
|
path: "www.google.com",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await config.updateDatasource({
|
|
|
|
...datasource,
|
|
|
|
config: {
|
|
|
|
dynamicVariables: [
|
|
|
|
{ queryId: basedOnQuery._id, name: "variable3", value: "{{ data.0.[value] }}" }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
2022-01-06 04:01:28 +13:00
|
|
|
return { datasource, query: basedOnQuery }
|
|
|
|
}
|
|
|
|
|
|
|
|
async function preview(datasource, fields) {
|
|
|
|
return await request
|
2022-01-05 07:23:45 +13:00
|
|
|
.post(`/api/queries/preview`)
|
|
|
|
.send({
|
|
|
|
datasourceId: datasource._id,
|
|
|
|
parameters: {},
|
2022-01-06 04:01:28 +13:00
|
|
|
fields,
|
2022-01-05 07:23:45 +13:00
|
|
|
queryVerb: "read",
|
|
|
|
})
|
|
|
|
.set(config.defaultHeaders())
|
|
|
|
.expect("Content-Type", /json/)
|
|
|
|
.expect(200)
|
2022-01-06 04:01:28 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
it("should work with static variables", async () => {
|
|
|
|
const datasource = await restDatasource({
|
|
|
|
staticVariables: {
|
|
|
|
variable: "google",
|
|
|
|
variable2: "1",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const res = await preview(datasource, {
|
|
|
|
path: "www.{{ variable }}.com",
|
|
|
|
queryString: "test={{ variable2 }}",
|
|
|
|
})
|
|
|
|
// these responses come from the mock
|
|
|
|
expect(res.body.schemaFields).toEqual(["url", "opts", "value"])
|
|
|
|
expect(res.body.rows[0].url).toEqual("http://www.google.com?test=1")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should work with dynamic variables", async () => {
|
|
|
|
const { datasource } = await dynamicVariableDatasource()
|
|
|
|
const res = await preview(datasource, {
|
|
|
|
path: "www.google.com",
|
|
|
|
queryString: "test={{ variable3 }}",
|
|
|
|
})
|
2022-01-05 07:23:45 +13:00
|
|
|
expect(res.body.schemaFields).toEqual(["url", "opts", "value"])
|
|
|
|
expect(res.body.rows[0].url).toContain("doctype html")
|
|
|
|
})
|
2022-01-06 04:01:28 +13:00
|
|
|
|
|
|
|
it("check that it automatically retries on fail with cached dynamics", async () => {
|
|
|
|
const { datasource, query: base } = await dynamicVariableDatasource()
|
|
|
|
// preview once to cache
|
|
|
|
await preview(datasource, { path: "www.google.com", queryString: "test={{ variable3 }}" })
|
|
|
|
// check its in cache
|
|
|
|
const contents = await checkCacheForDynamicVariable(base._id, "variable3")
|
|
|
|
expect(contents.rows.length).toEqual(1)
|
|
|
|
const res = await preview(datasource, {
|
|
|
|
path: "www.failonce.com",
|
|
|
|
queryString: "test={{ variable3 }}",
|
|
|
|
})
|
|
|
|
expect(res.body.schemaFields).toEqual(["fails", "url", "opts"])
|
|
|
|
expect(res.body.rows[0].fails).toEqual(1)
|
|
|
|
})
|
2022-01-05 07:23:45 +13:00
|
|
|
})
|
2021-02-10 04:24:56 +13:00
|
|
|
})
|