use real data instead of mocks

This commit is contained in:
Martin McKeaveney 2024-09-10 16:01:00 +01:00
parent 2eb53c018a
commit 1ee8a12e29
3 changed files with 86 additions and 101 deletions

View File

@ -1,46 +1,36 @@
import { configs } from "@budibase/backend-core"
import { UserCtx } from "@budibase/types"
import * as pro from "@budibase/pro" import * as pro from "@budibase/pro"
import { find, verifyAIConfig } from "../configs" import { verifyAIConfig } from "../configs"
import { TestConfiguration, structures } from "../../../../tests"
jest.mock("@budibase/backend-core", () => ({
...jest.requireActual("@budibase/backend-core"),
configs: {
getConfig: jest.fn(),
save: jest.fn(),
},
}))
describe("Global configs controller", () => { describe("Global configs controller", () => {
const config = new TestConfiguration()
beforeAll(async () => {
await config.beforeAll()
})
afterAll(async () => {
await config.afterAll()
})
afterEach(() => { afterEach(() => {
jest.resetAllMocks() jest.resetAllMocks()
}) })
it("Should strip secrets when pulling AI config", async () => { it("Should strip secrets when pulling AI config", async () => {
configs.getConfig.mockResolvedValue({ const data = structures.configs.ai()
config: { await config.api.configs.saveConfig(data)
ai: { const response = await config.api.configs.getAIConfig()
apiKey: "abc123APIKey", expect(response.body.config).toEqual({
baseUrl: "https://api.example.com", ai: {
}, active: true,
}, apiKey: "--secret-value--",
}) baseUrl: "https://api.example.com",
const ctx = { defaultModel: "gpt4",
params: { isDefault: false,
type: "ai", name: "Test",
}, provider: "OpenAI"
throw: jest.fn(), }
} as UserCtx
await find(ctx)
expect(ctx.body).toEqual({
config: {
ai: {
apiKey: "--secret-value--",
baseUrl: "https://api.example.com",
},
},
}) })
}) })
@ -48,36 +38,25 @@ describe("Global configs controller", () => {
jest jest
.spyOn(pro.features, "isBudibaseAIEnabled") .spyOn(pro.features, "isBudibaseAIEnabled")
.mockImplementation(() => true) .mockImplementation(() => true)
configs.getConfig.mockResolvedValue({ const data = structures.configs.ai()
config: { await config.api.configs.saveConfig(data)
ai: { const response = await config.api.configs.getAIConfig()
apiKey: "abc123APIKey",
baseUrl: "https://api.example.com",
},
},
})
const ctx = {
params: {
type: "ai",
},
throw: jest.fn(),
} as UserCtx
await find(ctx) expect(response.body.config).toEqual({
budibase_ai: {
expect(ctx.body).toEqual({ provider: "OpenAI",
config: { active: true,
budibase_ai: { isDefault: true,
provider: "OpenAI", name: "Budibase AI",
active: true, },
isDefault: true, ai: {
defaultModel: undefined, active: true,
name: "Budibase AI", apiKey: "--secret-value--",
}, baseUrl: "https://api.example.com",
ai: { defaultModel: "gpt4",
apiKey: "--secret-value--", isDefault: false,
baseUrl: "https://api.example.com", name: "Test",
}, provider: "OpenAI"
}, },
}) })
}) })
@ -86,49 +65,29 @@ describe("Global configs controller", () => {
jest jest
.spyOn(pro.features, "isBudibaseAIEnabled") .spyOn(pro.features, "isBudibaseAIEnabled")
.mockImplementation(() => false) .mockImplementation(() => false)
configs.getConfig.mockResolvedValue({ const data = structures.configs.ai()
config: { await config.api.configs.saveConfig(data)
ai: { const response = await config.api.configs.getAIConfig()
apiKey: "abc123APIKey",
baseUrl: "https://api.example.com",
},
},
})
const ctx = {
params: {
type: "ai",
},
throw: jest.fn(),
} as UserCtx
await find(ctx) expect(response.body.config).toEqual({
ai: {
expect(ctx.body).toEqual({ active: true,
config: { apiKey: "--secret-value--",
ai: { baseUrl: "https://api.example.com",
apiKey: "--secret-value--", defaultModel: "gpt4",
baseUrl: "https://api.example.com", isDefault: false,
}, name: "Test",
provider: "OpenAI"
}, },
}) })
}) })
it("Should not update existing secrets when updating an existing AI Config", async () => { it("Should not update existing secrets when updating an existing AI Config", async () => {
const newConfig = { const data = structures.configs.ai()
type: "ai", await config.api.configs.saveConfig(data)
config: { const existingConfig = await config.api.configs.getAIConfig()
aiconfig: {
provider: "OpenAI",
isDefault: true,
name: "MyConfig",
active: true,
defaultModel: "gpt4",
apiKey: "--secret-value--",
},
},
}
const existingConfig = { const newConfig = {
type: "ai", type: "ai",
config: { config: {
aiconfig: { aiconfig: {

View File

@ -22,6 +22,15 @@ export class ConfigAPI extends TestAPI {
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
} }
getAIConfig = () => {
return this.request
.get(`/api/global/configs/ai`)
.set(this.config.defaultHeaders())
.expect(200)
.expect("Content-Type", /json/)
}
saveConfig = (data: any) => { saveConfig = (data: any) => {
return this.request return this.request
.post(`/api/global/configs`) .post(`/api/global/configs`)

View File

@ -4,7 +4,7 @@ import {
ConfigType, ConfigType,
SMTPConfig, SMTPConfig,
GoogleConfig, GoogleConfig,
OIDCConfig, OIDCConfig, AIConfig,
} from "@budibase/types" } from "@budibase/types"
export function oidc(conf?: any): OIDCConfig { export function oidc(conf?: any): OIDCConfig {
@ -81,3 +81,20 @@ export function settings(conf?: any): SettingsConfig {
}, },
} }
} }
export function ai(): AIConfig {
return {
type: ConfigType.AI,
config: {
ai: {
provider: "OpenAI",
isDefault: false,
name: "Test",
active: true,
defaultModel: "gpt4",
apiKey: "abc123APIKey",
baseUrl: "https://api.example.com",
},
}
}
}