Convert query.sec.spec.ts to ts

This commit is contained in:
Adria Navarro 2024-01-29 22:57:20 +01:00
parent 782ad7fe8c
commit ff75fbf99e
3 changed files with 49 additions and 28 deletions

View File

@ -1,4 +1,4 @@
const tk = require("timekeeper") import tk from "timekeeper"
tk.freeze(Date.now()) tk.freeze(Date.now())
// Mock out postgres for this // Mock out postgres for this
@ -17,16 +17,21 @@ jest.mock("@budibase/backend-core", () => {
}, },
} }
}) })
const setup = require("./utilities") import * as setup from "./utilities"
const { checkBuilderEndpoint } = require("./utilities/TestFunctions") import { checkBuilderEndpoint } from "./utilities/TestFunctions"
const { checkCacheForDynamicVariable } = require("../../../threads/utils") import { checkCacheForDynamicVariable } from "../../../threads/utils"
const { basicQuery, basicDatasource } = setup.structures const { basicQuery, basicDatasource } = setup.structures
const { events, db: dbCore } = require("@budibase/backend-core") import { events, db as dbCore } from "@budibase/backend-core"
import { Datasource, Query, SourceName } from "@budibase/types"
const mockIsProdAppID = dbCore.isProdAppID as jest.MockedFunction<
typeof dbCore.isProdAppID
>
describe("/queries", () => { describe("/queries", () => {
let request = setup.getRequest() let request = setup.getRequest()
let config = setup.getConfig() let config = setup.getConfig()
let datasource, query let datasource: Datasource & Required<Pick<Datasource, "_id">>, query: Query
afterAll(setup.afterAll) afterAll(setup.afterAll)
@ -44,14 +49,14 @@ describe("/queries", () => {
const datasource = await config.createDatasource({ const datasource = await config.createDatasource({
datasource: { datasource: {
...basicDatasource().datasource, ...basicDatasource().datasource,
source: "INVALID_INTEGRATION", source: "INVALID_INTEGRATION" as SourceName,
}, },
}) })
const query = await config.createQuery() const query = await config.createQuery()
return { datasource, query } return { datasource, query }
} }
const createQuery = async query => { const createQuery = async (query: Query) => {
return request return request
.post(`/api/queries`) .post(`/api/queries`)
.send(query) .send(query)
@ -67,7 +72,7 @@ describe("/queries", () => {
jest.clearAllMocks() jest.clearAllMocks()
const res = await createQuery(query) const res = await createQuery(query)
expect(res.res.statusMessage).toEqual( expect((res as any).res.statusMessage).toEqual(
`Query ${query.name} saved successfully.` `Query ${query.name} saved successfully.`
) )
expect(res.body).toEqual({ expect(res.body).toEqual({
@ -92,7 +97,7 @@ describe("/queries", () => {
query._rev = res.body._rev query._rev = res.body._rev
await createQuery(query) await createQuery(query)
expect(res.res.statusMessage).toEqual( expect((res as any).res.statusMessage).toEqual(
`Query ${query.name} saved successfully.` `Query ${query.name} saved successfully.`
) )
expect(res.body).toEqual({ expect(res.body).toEqual({
@ -168,8 +173,8 @@ describe("/queries", () => {
it("should remove sensitive info for prod apps", async () => { it("should remove sensitive info for prod apps", async () => {
// Mock isProdAppID to pretend we are using a prod app // Mock isProdAppID to pretend we are using a prod app
dbCore.isProdAppID.mockClear() mockIsProdAppID.mockClear()
dbCore.isProdAppID.mockImplementation(() => true) mockIsProdAppID.mockImplementation(() => true)
const query = await config.createQuery() const query = await config.createQuery()
const res = await request const res = await request
@ -184,7 +189,7 @@ describe("/queries", () => {
// Reset isProdAppID mock // Reset isProdAppID mock
expect(dbCore.isProdAppID).toHaveBeenCalledTimes(1) expect(dbCore.isProdAppID).toHaveBeenCalledTimes(1)
dbCore.isProdAppID.mockImplementation(() => false) mockIsProdAppID.mockImplementation(() => false)
}) })
}) })
@ -211,10 +216,11 @@ describe("/queries", () => {
}) })
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
const query = await config.createQuery()
await checkBuilderEndpoint({ await checkBuilderEndpoint({
config, config,
method: "DELETE", method: "DELETE",
url: `/api/queries/${config._id}/${config._rev}`, url: `/api/queries/${query._id}/${query._rev}`,
}) })
}) })
}) })
@ -284,8 +290,8 @@ describe("/queries", () => {
}) })
describe("variables", () => { describe("variables", () => {
async function preview(datasource, fields) { async function preview(datasource: Datasource, fields: any) {
return config.previewQuery(request, config, datasource, fields) return config.previewQuery(request, config, datasource, fields, undefined)
} }
it("should work with static variables", async () => { it("should work with static variables", async () => {
@ -370,11 +376,19 @@ describe("/queries", () => {
}) })
describe("Current User Request Mapping", () => { describe("Current User Request Mapping", () => {
async function previewGet(datasource, fields, params) { async function previewGet(
datasource: Datasource,
fields: any,
params: any
) {
return config.previewQuery(request, config, datasource, fields, params) return config.previewQuery(request, config, datasource, fields, params)
} }
async function previewPost(datasource, fields, params) { async function previewPost(
datasource: Datasource,
fields: any,
params: any
) {
return config.previewQuery( return config.previewQuery(
request, request,
config, config,
@ -394,14 +408,18 @@ describe("/queries", () => {
emailHdr: "{{[user].[email]}}", emailHdr: "{{[user].[email]}}",
}, },
}) })
const res = await previewGet(datasource, { const res = await previewGet(
datasource,
{
path: "www.google.com", path: "www.google.com",
queryString: "email={{[user].[email]}}", queryString: "email={{[user].[email]}}",
headers: { headers: {
queryHdr: "{{[user].[firstName]}}", queryHdr: "{{[user].[firstName]}}",
secondHdr: "1234", secondHdr: "1234",
}, },
}) },
undefined
)
const parsedRequest = JSON.parse(res.body.extra.raw) const parsedRequest = JSON.parse(res.body.extra.raw)
expect(parsedRequest.opts.headers).toEqual({ expect(parsedRequest.opts.headers).toEqual({

View File

@ -794,11 +794,11 @@ class TestConfiguration {
async createDatasource(config?: { async createDatasource(config?: {
datasource: Datasource datasource: Datasource
}): Promise<Datasource> { }): Promise<Datasource & Required<Pick<Datasource, "_id">>> {
config = config || basicDatasource() config = config || basicDatasource()
const response = await this.api.datasource.create(config.datasource) const response = await this.api.datasource.create(config.datasource)
this.datasource = response this.datasource = response
return this.datasource! return { ...this.datasource, _id: this.datasource!._id! }
} }
async updateDatasource(datasource: Datasource): Promise<Datasource> { async updateDatasource(datasource: Datasource): Promise<Datasource> {

View File

@ -23,6 +23,7 @@ import {
TableSourceType, TableSourceType,
AutomationIOType, AutomationIOType,
} from "@budibase/types" } from "@budibase/types"
import { Query } from "@budibase/types"
const { BUILTIN_ROLE_IDS } = roles const { BUILTIN_ROLE_IDS } = roles
@ -360,7 +361,7 @@ export function basicDatasource(): { datasource: Datasource } {
} }
} }
export function basicQuery(datasourceId: string) { export function basicQuery(datasourceId: string): Query {
return { return {
datasourceId: datasourceId, datasourceId: datasourceId,
name: "New Query", name: "New Query",
@ -368,6 +369,8 @@ export function basicQuery(datasourceId: string) {
fields: {}, fields: {},
schema: {}, schema: {},
queryVerb: "read", queryVerb: "read",
transformer: null,
readable: true,
} }
} }