Skip to content

Commit

Permalink
Rely on withEnv and setEnv functions in respective environment.ts files.
Browse files Browse the repository at this point in the history
  • Loading branch information
samwho committed Aug 9, 2024
1 parent 858bb77 commit 3d590e8
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 109 deletions.
2 changes: 1 addition & 1 deletion packages/backend-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export * as locks from "./redis/redlockImpl"
export * as utils from "./utils"
export * as errors from "./errors"
export * as timers from "./timers"
export { default as env } from "./environment"
export { default as env, withEnv, setEnv } from "./environment"
export * as blacklist from "./blacklist"
export * as docUpdates from "./docUpdates"
export * from "./utils/Duration"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const setup = require("./utilities")
const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
import { withEnv } from "../../../environment"
import { getRequest, getConfig, afterAll as _afterAll } from "./utilities"
import { checkBuilderEndpoint } from "./utilities/TestFunctions"

describe("/api/keys", () => {
let request = setup.getRequest()
let config = setup.getConfig()
let request = getRequest()
let config = getConfig()

afterAll(setup.afterAll)
afterAll(_afterAll)

beforeAll(async () => {
await config.init()
})

describe("fetch", () => {
it("should allow fetching", async () => {
await config.withEnv({ SELF_HOSTED: "true" }, async () => {
await withEnv({ SELF_HOSTED: "true" }, async () => {
const res = await request
.get(`/api/keys`)
.set(config.defaultHeaders())
Expand All @@ -34,7 +35,7 @@ describe("/api/keys", () => {

describe("update", () => {
it("should allow updating a value", async () => {
await config.withEnv({ SELF_HOSTED: "true" }, async () => {
await withEnv({ SELF_HOSTED: "true" }, async () => {
const res = await request
.put(`/api/keys/TEST`)
.send({
Expand Down
9 changes: 7 additions & 2 deletions packages/server/src/api/routes/tests/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ jest.mock("../../../utilities/redis", () => ({
import { checkBuilderEndpoint } from "./utilities/TestFunctions"
import * as setup from "./utilities"
import { AppStatus } from "../../../db/utils"
import { events, utils, context } from "@budibase/backend-core"
import {
events,
utils,
context,
withEnv as withCoreEnv,
} from "@budibase/backend-core"
import env from "../../../environment"
import { type App } from "@budibase/types"
import tk from "timekeeper"
Expand Down Expand Up @@ -353,7 +358,7 @@ describe("/applications", () => {
.delete(`/api/global/roles/${prodAppId}`)
.reply(200, {})

await config.withCoreEnv({ SQS_SEARCH_ENABLE: "true" }, async () => {
await withCoreEnv({ SQS_SEARCH_ENABLE: "true" }, async () => {
await config.api.application.delete(app.appId)
})
})
Expand Down
5 changes: 3 additions & 2 deletions packages/server/src/api/routes/tests/attachment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withEnv } from "../../../environment"
import * as setup from "./utilities"
import { APIError } from "@budibase/types"

Expand Down Expand Up @@ -28,7 +29,7 @@ describe("/api/applications/:appId/sync", () => {
})

it("should reject an upload with a malicious file extension", async () => {
await config.withEnv({ SELF_HOSTED: undefined }, async () => {
await withEnv({ SELF_HOSTED: undefined }, async () => {
let resp = (await config.api.attachment.process(
"ohno.exe",
Buffer.from([0]),
Expand All @@ -39,7 +40,7 @@ describe("/api/applications/:appId/sync", () => {
})

it("should reject an upload with a malicious uppercase file extension", async () => {
await config.withEnv({ SELF_HOSTED: undefined }, async () => {
await withEnv({ SELF_HOSTED: undefined }, async () => {
let resp = (await config.api.attachment.process(
"OHNO.EXE",
Buffer.from([0]),
Expand Down
7 changes: 4 additions & 3 deletions packages/server/src/api/routes/tests/row.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import _, { merge } from "lodash"
import * as uuid from "uuid"
import { Knex } from "knex"
import { InternalTables } from "../../../db/utils"
import { withEnv } from "../../../environment"

const timestamp = new Date("2023-01-26T11:48:57.597Z").toISOString()
tk.freeze(timestamp)
Expand Down Expand Up @@ -1688,7 +1689,7 @@ describe.each([
}
const row = await config.api.row.save(testTable._id!, draftRow)

await config.withEnv({ SELF_HOSTED: "true" }, async () => {
await withEnv({ SELF_HOSTED: "true" }, async () => {
return context.doInAppContext(config.getAppId(), async () => {
const enriched: Row[] = await outputProcessing(table, [row])
const [targetRow] = enriched
Expand Down Expand Up @@ -2456,7 +2457,7 @@ describe.each([

describe("Formula JS protection", () => {
it("should time out JS execution if a single cell takes too long", async () => {
await config.withEnv({ JS_PER_INVOCATION_TIMEOUT_MS: 40 }, async () => {
await withEnv({ JS_PER_INVOCATION_TIMEOUT_MS: 40 }, async () => {
const js = Buffer.from(
`
let i = 0;
Expand Down Expand Up @@ -2494,7 +2495,7 @@ describe.each([
})

it("should time out JS execution if a multiple cells take too long", async () => {
await config.withEnv(
await withEnv(
{
JS_PER_INVOCATION_TIMEOUT_MS: 40,
JS_PER_REQUEST_TIMEOUT_MS: 80,
Expand Down
8 changes: 5 additions & 3 deletions packages/server/src/api/routes/tests/search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
MIN_VALID_DATE,
SQLITE_DESIGN_DOC_ID,
utils,
withEnv as withCoreEnv,
setEnv as setCoreEnv,
} from "@budibase/backend-core"

import * as setup from "./utilities"
Expand Down Expand Up @@ -64,9 +66,9 @@ describe.each([
let rows: Row[]

beforeAll(async () => {
await config.withCoreEnv({ SQS_SEARCH_ENABLE: "true" }, () => config.init())
await withCoreEnv({ SQS_SEARCH_ENABLE: "true" }, () => config.init())
if (isSqs) {
envCleanup = config.setCoreEnv({
envCleanup = setCoreEnv({
SQS_SEARCH_ENABLE: "true",
SQS_SEARCH_ENABLE_TENANTS: [config.getTenantId()],
})
Expand Down Expand Up @@ -2668,7 +2670,7 @@ describe.each([
})

it("can still page when the hard limit is hit", async () => {
await config.withCoreEnv(
await withCoreEnv(
{
SQL_MAX_ROWS: "6",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@ jest.mock("aws-sdk", () => ({
})),
}))

const setup = require("./utilities")
const { constants } = require("@budibase/backend-core")
import { Datasource, SourceName } from "@budibase/types"
import { setEnv } from "../../../environment"
import { getRequest, getConfig, afterAll as _afterAll } from "./utilities"
import { constants } from "@budibase/backend-core"

describe("/static", () => {
let request = setup.getRequest()
let config = setup.getConfig()
let app
let cleanupEnv
let request = getRequest()
let config = getConfig()
let cleanupEnv: () => void

afterAll(() => {
setup.afterAll()
_afterAll()
cleanupEnv()
})

beforeAll(async () => {
cleanupEnv = config.setEnv({ SELF_HOSTED: "true" })
app = await config.init()
cleanupEnv = setEnv({ SELF_HOSTED: "true" })
await config.init()
})

describe("/app", () => {
Expand All @@ -49,7 +50,7 @@ describe("/static", () => {
delete headers[constants.Header.APP_ID]

const res = await request
.get(`/app${config.prodApp.url}`)
.get(`/app${config.getProdApp().url}`)
.set(headers)
.expect(200)

Expand All @@ -68,14 +69,14 @@ describe("/static", () => {

describe("/attachments", () => {
describe("generateSignedUrls", () => {
let datasource
let datasource: Datasource

beforeEach(async () => {
datasource = await config.createDatasource({
datasource: {
type: "datasource",
name: "Test",
source: "S3",
source: SourceName.S3,
config: {},
},
})
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/api/routes/tests/templates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as setup from "./utilities"
import path from "path"
import nock from "nock"
import { generator } from "@budibase/backend-core/tests"
import { withEnv as withCoreEnv } from "@budibase/backend-core"

interface App {
background: string
Expand Down Expand Up @@ -89,7 +90,7 @@ describe("/templates", () => {
SQS_SEARCH_ENABLE_TENANTS: [config.getTenantId()],
}

await config.withCoreEnv(env, async () => {
await withCoreEnv(env, async () => {
const name = generator.guid().replaceAll("-", "")
const url = `/${name}`

Expand Down
14 changes: 9 additions & 5 deletions packages/server/src/api/routes/tests/viewV2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ import { generator, mocks } from "@budibase/backend-core/tests"
import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
import merge from "lodash/merge"
import { quotas } from "@budibase/pro"
import { db, roles } from "@budibase/backend-core"
import {
db,
roles,
withEnv as withCoreEnv,
setEnv as setCoreEnv,
} from "@budibase/backend-core"

describe.each([
["lucene", undefined],
Expand Down Expand Up @@ -89,12 +94,11 @@ describe.each([
}

beforeAll(async () => {
await config.withCoreEnv(
{ SQS_SEARCH_ENABLE: isSqs ? "true" : "false" },
() => config.init()
await withCoreEnv({ SQS_SEARCH_ENABLE: isSqs ? "true" : "false" }, () =>
config.init()
)
if (isSqs) {
envCleanup = config.setCoreEnv({
envCleanup = setCoreEnv({
SQS_SEARCH_ENABLE: "true",
SQS_SEARCH_ENABLE_TENANTS: [config.getTenantId()],
})
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/api/routes/tests/webhook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Webhook } from "@budibase/types"
import * as setup from "./utilities"
import { checkBuilderEndpoint } from "./utilities/TestFunctions"
import { mocks } from "@budibase/backend-core/tests"
import { setEnv } from "../../../environment"

const { basicWebhook, basicAutomation, collectAutomation } = setup.structures

Expand All @@ -17,7 +18,7 @@ describe("/webhooks", () => {
})

const setupTest = async () => {
cleanupEnv = config.setEnv({ SELF_HOSTED: "true" })
cleanupEnv = setEnv({ SELF_HOSTED: "true" })
await config.init()
const autoConfig = basicAutomation()
autoConfig.definition.trigger.schema = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as setup from "../../../api/routes/tests/utilities"
import { basicTable } from "../../../tests/utilities/structures"
import { db as dbCore, SQLITE_DESIGN_DOC_ID } from "@budibase/backend-core"
import {
db as dbCore,
SQLITE_DESIGN_DOC_ID,
withEnv as withCoreEnv,
} from "@budibase/backend-core"
import {
LinkDocument,
DocumentType,
Expand Down Expand Up @@ -69,11 +73,11 @@ function oldLinkDocument(): Omit<LinkDocument, "tableId"> {
type SQSEnvVar = "SQS_MIGRATION_ENABLE" | "SQS_SEARCH_ENABLE"

async function sqsDisabled(envVar: SQSEnvVar, cb: () => Promise<void>) {
await config.withCoreEnv({ [envVar]: "", SQS_SEARCH_ENABLE_TENANTS: [] }, cb)
await withCoreEnv({ [envVar]: "", SQS_SEARCH_ENABLE_TENANTS: [] }, cb)
}

async function sqsEnabled(envVar: SQSEnvVar, cb: () => Promise<void>) {
await config.withCoreEnv(
await withCoreEnv(
{ [envVar]: "1", SQS_SEARCH_ENABLE_TENANTS: [config.getTenantId()] },
cb
)
Expand Down
5 changes: 3 additions & 2 deletions packages/server/src/automations/tests/openai.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getConfig, runStep, afterAll as _afterAll } from "./utilities"
import { OpenAI } from "openai"
import { withEnv as withCoreEnv, setEnv as setCoreEnv } from "@budibase/backend-core"

jest.mock("openai", () => ({
OpenAI: jest.fn().mockImplementation(() => ({
Expand Down Expand Up @@ -32,7 +33,7 @@ describe("test the openai action", () => {
})

beforeEach(() => {
resetEnv = config.setCoreEnv({ OPENAI_API_KEY: "abc123" })
resetEnv = setCoreEnv({ OPENAI_API_KEY: "abc123" })
})

afterEach(() => {
Expand All @@ -42,7 +43,7 @@ describe("test the openai action", () => {
afterAll(_afterAll)

it("should present the correct error message when the OPENAI_API_KEY variable isn't set", async () => {
await config.withCoreEnv({ OPENAI_API_KEY: "" }, async () => {
await withCoreEnv({ OPENAI_API_KEY: "" }, async () => {
let res = await runStep("OPENAI", { prompt: OPENAI_PROMPT })
expect(res.response).toEqual(
"OpenAI API Key not configured - please add the OPENAI_API_KEY environment variable."
Expand Down
27 changes: 27 additions & 0 deletions packages/server/src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { env as coreEnv } from "@budibase/backend-core"
import { ServiceType } from "@budibase/types"
import cloneDeep from "lodash/cloneDeep"

coreEnv._set("SERVICE_TYPE", ServiceType.APPS)
import { join } from "path"
Expand Down Expand Up @@ -133,6 +134,32 @@ const environment = {
},
}

export function setEnv(newEnvVars: Partial<typeof environment>): () => void {
const oldEnv = cloneDeep(environment)

let key: keyof typeof newEnvVars
for (key in newEnvVars) {
environment._set(key, newEnvVars[key])
}

return () => {
for (const [key, value] of Object.entries(oldEnv)) {
environment._set(key, value)
}
}
}

export function withEnv<T>(envVars: Partial<typeof environment>, f: () => T) {
const cleanup = setEnv(envVars)
const result = f()
if (result instanceof Promise) {
return result.finally(cleanup)
} else {
cleanup()
return result
}
}

function cleanVariables() {
// clean up any environment variable edge cases
for (let [key, value] of Object.entries(environment)) {
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/integrations/tests/googlesheets.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setEnv as setCoreEnv } from "@budibase/backend-core"
import type { GoogleSpreadsheetWorksheet } from "google-spreadsheet"
import nock from "nock"

Expand Down Expand Up @@ -40,7 +41,7 @@ describe("Google Sheets Integration", () => {
let cleanupEnv: () => void

beforeAll(() => {
cleanupEnv = config.setCoreEnv({
cleanupEnv = setCoreEnv({
GOOGLE_CLIENT_ID: "test",
GOOGLE_CLIENT_SECRET: "test",
})
Expand Down
Loading

0 comments on commit 3d590e8

Please sign in to comment.