From 2c3b3d03e1734730d52bea73845c0f19521107cf Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 11 Dec 2023 16:49:44 +0000 Subject: [PATCH] Respond to PR comments. --- .../src/api/controllers/system/environment.ts | 7 +++- .../routes/system/tests/environment.spec.ts | 17 +++++++++ .../worker/src/tests/TestConfiguration.ts | 35 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/packages/worker/src/api/controllers/system/environment.ts b/packages/worker/src/api/controllers/system/environment.ts index c619d484f7..b62da949ad 100644 --- a/packages/worker/src/api/controllers/system/environment.ts +++ b/packages/worker/src/api/controllers/system/environment.ts @@ -30,6 +30,11 @@ export const fetch = async (ctx: Ctx) => { disableAccountPortal: env.DISABLE_ACCOUNT_PORTAL, baseUrl: env.PLATFORM_URL, isDev: env.isDev() && !env.isTest(), - isSqsAvailable: await isSqsAvailable(), + } + + if (env.SELF_HOSTED) { + ctx.body.infrastructure = { + sqs: await isSqsAvailable(), + } } } diff --git a/packages/worker/src/api/routes/system/tests/environment.spec.ts b/packages/worker/src/api/routes/system/tests/environment.spec.ts index 897cc970cc..5dc2e552e1 100644 --- a/packages/worker/src/api/routes/system/tests/environment.spec.ts +++ b/packages/worker/src/api/routes/system/tests/environment.spec.ts @@ -27,5 +27,22 @@ describe("/api/system/environment", () => { offlineMode: false, }) }) + + it("returns the expected environment for self hosters", async () => { + config.withEnv({ SELF_HOSTED: true }, async () => { + const env = await config.api.environment.getEnvironment() + expect(env.body).toEqual({ + cloud: true, + disableAccountPortal: 0, + isDev: false, + multiTenancy: true, + baseUrl: "http://localhost:10000", + offlineMode: false, + infrastructure: { + sqs: false, + }, + }) + }) + }) }) }) diff --git a/packages/worker/src/tests/TestConfiguration.ts b/packages/worker/src/tests/TestConfiguration.ts index c43d1b9d13..41a074ee89 100644 --- a/packages/worker/src/tests/TestConfiguration.ts +++ b/packages/worker/src/tests/TestConfiguration.ts @@ -36,6 +36,7 @@ import { } from "@budibase/types" import API from "./api" import jwt, { Secret } from "jsonwebtoken" +import { cloneDeep } from "lodash" class TestConfiguration { server: any @@ -240,6 +241,40 @@ class TestConfiguration { return { message: "Admin user only endpoint.", status: 403 } } + async withEnv(newEnvVars: Partial, f: () => Promise) { + let cleanup = this.setEnv(newEnvVars) + try { + await f() + } finally { + cleanup() + } + } + + /* + * Sets the environment variables to the given values and returns a function + * that can be called to reset the environment variables to their original values. + */ + setEnv(newEnvVars: Partial): () => void { + const oldEnv = cloneDeep(env) + const oldCoreEnv = cloneDeep(coreEnv) + + let key: keyof typeof newEnvVars + for (key in newEnvVars) { + env._set(key, newEnvVars[key]) + coreEnv._set(key, newEnvVars[key]) + } + + return () => { + for (const [key, value] of Object.entries(oldEnv)) { + env._set(key, value) + } + + for (const [key, value] of Object.entries(oldCoreEnv)) { + coreEnv._set(key, value) + } + } + } + // USERS async createDefaultUser() {