diff --git a/packages/backend-core/tests/utilities/mocks/licenses.ts b/packages/backend-core/tests/utilities/mocks/licenses.ts index 1fbda5655e..e374612f5f 100644 --- a/packages/backend-core/tests/utilities/mocks/licenses.ts +++ b/packages/backend-core/tests/utilities/mocks/licenses.ts @@ -74,6 +74,10 @@ export const useGroups = () => { return useFeature(Feature.USER_GROUPS) } +export const useEnvironmentVariables = () => { + return useFeature(Feature.ENVIRONMENT_VARIABLES) +} + // QUOTAS export const setAutomationLogsQuota = (value: number) => { diff --git a/packages/server/src/api/routes/tests/environmentVariables.spec.ts b/packages/server/src/api/routes/tests/environmentVariables.spec.ts new file mode 100644 index 0000000000..86ff8367da --- /dev/null +++ b/packages/server/src/api/routes/tests/environmentVariables.spec.ts @@ -0,0 +1,79 @@ +jest.mock("../../../integrations/postgres") +import * as setup from "./utilities" +import postgres from "../../../integrations/postgres" +import { mocks } from "@budibase/backend-core/tests" +import { env } from "@budibase/backend-core" +const structures = setup.structures + +env._set("ENCRYPTION_KEY", "budibase") +mocks.licenses.useEnvironmentVariables() + +describe("/api/env/variables", () => { + let request = setup.getRequest() + let config = setup.getConfig() + + afterAll(setup.afterAll) + + beforeEach(async () => { + await config.init() + }) + + it("should be able check the status of env var API", async () => { + const res = await request + .get(`/api/env/variables/status`) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + + expect(res.body.encryptionKeyAvailable).toEqual(true) + }) + + it("should be able to create an environment variable", async () => { + await request + .post(`/api/env/variables`) + .send(structures.basicEnvironmentVariable("test", "test")) + .set(config.defaultHeaders()) + .expect(200) + }) + + it("should be able to fetch the 'test' variable name", async () => { + const res = await request + .get(`/api/env/variables`) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + expect(res.body.variables.length).toEqual(1) + expect(res.body.variables[0]).toEqual("test") + }) + + it("should be able to update the environment variable 'test'", async () => {}) + + it("should be able to delete the environment variable 'test'", async () => {}) + + it("should be able to create an environment variable", async () => {}) + + it("should create a datasource (using the environment variable) and query", async () => { + const datasourceBase = structures.basicDatasource() + // TODO: we need to use an environment variable in the datasource configuration + const datasource = await request + .post(`/api/datasources`) + .send(datasourceBase) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + expect(datasource._id).toBeDefined() + + const query = await request + .post(`/api/queries`) + .send(structures.basicQuery(datasource._id)) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + expect(query._id).toBeDefined() + }) + + it("should run a query preview and check the mocked results", async () => { + // TODO: use the preview API + expect(postgres.integration).toHaveBeenCalledWith() + }) +}) diff --git a/packages/server/src/tests/utilities/structures.ts b/packages/server/src/tests/utilities/structures.ts index 9d66fecc5e..4a5cd2ed20 100644 --- a/packages/server/src/tests/utilities/structures.ts +++ b/packages/server/src/tests/utilities/structures.ts @@ -255,3 +255,15 @@ export function basicWebhook(automationId: string) { }, } } + +export function basicEnvironmentVariable( + name: string, + prod: string, + dev?: string +) { + return { + name, + production: prod, + development: dev || prod, + } +}