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

View File

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

View File

@ -23,6 +23,7 @@ import {
TableSourceType,
AutomationIOType,
} from "@budibase/types"
import { Query } from "@budibase/types"
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 {
datasourceId: datasourceId,
name: "New Query",
@ -368,6 +369,8 @@ export function basicQuery(datasourceId: string) {
fields: {},
schema: {},
queryVerb: "read",
transformer: null,
readable: true,
}
}