diff --git a/packages/worker/src/api/controllers/global/tests/configs.spec.ts b/packages/worker/src/api/controllers/global/tests/configs.spec.ts index 419c12ccb0..1a6e6b7075 100644 --- a/packages/worker/src/api/controllers/global/tests/configs.spec.ts +++ b/packages/worker/src/api/controllers/global/tests/configs.spec.ts @@ -1,46 +1,36 @@ -import { configs } from "@budibase/backend-core" -import { UserCtx } from "@budibase/types" import * as pro from "@budibase/pro" -import { find, verifyAIConfig } from "../configs" - -jest.mock("@budibase/backend-core", () => ({ - ...jest.requireActual("@budibase/backend-core"), - configs: { - getConfig: jest.fn(), - save: jest.fn(), - }, -})) +import { verifyAIConfig } from "../configs" +import { TestConfiguration, structures } from "../../../../tests" describe("Global configs controller", () => { + const config = new TestConfiguration() + + beforeAll(async () => { + await config.beforeAll() + }) + + afterAll(async () => { + await config.afterAll() + }) + afterEach(() => { jest.resetAllMocks() }) it("Should strip secrets when pulling AI config", async () => { - configs.getConfig.mockResolvedValue({ - config: { - ai: { - apiKey: "abc123APIKey", - baseUrl: "https://api.example.com", - }, - }, - }) - const ctx = { - params: { - type: "ai", - }, - throw: jest.fn(), - } as UserCtx - - await find(ctx) - - expect(ctx.body).toEqual({ - config: { - ai: { - apiKey: "--secret-value--", - baseUrl: "https://api.example.com", - }, - }, + const data = structures.configs.ai() + await config.api.configs.saveConfig(data) + const response = await config.api.configs.getAIConfig() + expect(response.body.config).toEqual({ + ai: { + active: true, + apiKey: "--secret-value--", + baseUrl: "https://api.example.com", + defaultModel: "gpt4", + isDefault: false, + name: "Test", + provider: "OpenAI" + } }) }) @@ -48,36 +38,25 @@ describe("Global configs controller", () => { jest .spyOn(pro.features, "isBudibaseAIEnabled") .mockImplementation(() => true) - configs.getConfig.mockResolvedValue({ - config: { - ai: { - apiKey: "abc123APIKey", - baseUrl: "https://api.example.com", - }, - }, - }) - const ctx = { - params: { - type: "ai", - }, - throw: jest.fn(), - } as UserCtx + const data = structures.configs.ai() + await config.api.configs.saveConfig(data) + const response = await config.api.configs.getAIConfig() - await find(ctx) - - expect(ctx.body).toEqual({ - config: { - budibase_ai: { - provider: "OpenAI", - active: true, - isDefault: true, - defaultModel: undefined, - name: "Budibase AI", - }, - ai: { - apiKey: "--secret-value--", - baseUrl: "https://api.example.com", - }, + expect(response.body.config).toEqual({ + budibase_ai: { + provider: "OpenAI", + active: true, + isDefault: true, + name: "Budibase AI", + }, + ai: { + active: true, + apiKey: "--secret-value--", + baseUrl: "https://api.example.com", + defaultModel: "gpt4", + isDefault: false, + name: "Test", + provider: "OpenAI" }, }) }) @@ -86,49 +65,29 @@ describe("Global configs controller", () => { jest .spyOn(pro.features, "isBudibaseAIEnabled") .mockImplementation(() => false) - configs.getConfig.mockResolvedValue({ - config: { - ai: { - apiKey: "abc123APIKey", - baseUrl: "https://api.example.com", - }, - }, - }) - const ctx = { - params: { - type: "ai", - }, - throw: jest.fn(), - } as UserCtx + const data = structures.configs.ai() + await config.api.configs.saveConfig(data) + const response = await config.api.configs.getAIConfig() - await find(ctx) - - expect(ctx.body).toEqual({ - config: { - ai: { - apiKey: "--secret-value--", - baseUrl: "https://api.example.com", - }, + expect(response.body.config).toEqual({ + ai: { + active: true, + apiKey: "--secret-value--", + baseUrl: "https://api.example.com", + defaultModel: "gpt4", + isDefault: false, + name: "Test", + provider: "OpenAI" }, }) }) it("Should not update existing secrets when updating an existing AI Config", async () => { - const newConfig = { - type: "ai", - config: { - aiconfig: { - provider: "OpenAI", - isDefault: true, - name: "MyConfig", - active: true, - defaultModel: "gpt4", - apiKey: "--secret-value--", - }, - }, - } + const data = structures.configs.ai() + await config.api.configs.saveConfig(data) + const existingConfig = await config.api.configs.getAIConfig() - const existingConfig = { + const newConfig = { type: "ai", config: { aiconfig: { diff --git a/packages/worker/src/tests/api/configs.ts b/packages/worker/src/tests/api/configs.ts index 86fe0830ca..c64419b490 100644 --- a/packages/worker/src/tests/api/configs.ts +++ b/packages/worker/src/tests/api/configs.ts @@ -22,6 +22,15 @@ export class ConfigAPI extends TestAPI { .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) => { return this.request .post(`/api/global/configs`) diff --git a/packages/worker/src/tests/structures/configs.ts b/packages/worker/src/tests/structures/configs.ts index bac94a4154..e1fd835d41 100644 --- a/packages/worker/src/tests/structures/configs.ts +++ b/packages/worker/src/tests/structures/configs.ts @@ -4,7 +4,7 @@ import { ConfigType, SMTPConfig, GoogleConfig, - OIDCConfig, + OIDCConfig, AIConfig, } from "@budibase/types" 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", + }, + } + } +}