From 1fdc542c7c83916451158eddbc67e5338a4c48a8 Mon Sep 17 00:00:00 2001 From: Christopher Kolstad Date: Fri, 29 Nov 2024 14:24:13 +0100 Subject: [PATCH] fix: added separate method for resolving isOss flag to be able to test behaviour when NODE_ENV !== test --- src/lib/create-config.test.ts | 21 ++++++++++++++++++++- src/lib/create-config.ts | 21 ++++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/lib/create-config.test.ts b/src/lib/create-config.test.ts index e69eb7b500df..06646635e2a1 100644 --- a/src/lib/create-config.test.ts +++ b/src/lib/create-config.test.ts @@ -1,4 +1,4 @@ -import { createConfig } from './create-config'; +import { createConfig, resolveIsOss } from './create-config'; import { ApiTokenType } from './types/models/api-token'; test('should create default config', async () => { @@ -498,3 +498,22 @@ test('Config with enterpriseVersion set and not pro environment should set isEnt }); expect(config.isEnterprise).toBe(true); }); + +describe('isOSS', () => { + test('Config with pro environment should set isOss to false regardless of pro casing', async () => { + const isOss = resolveIsOss(false, false, 'Pro'); + expect(isOss).toBe(false); + }); + test('Config with enterpriseVersion set should set isOss to false', async () => { + const isOss = resolveIsOss(true, false, 'Enterprise'); + expect(isOss).toBe(false); + }); + test('Config with no enterprise version and any other environment than pro should have isOss as true', async () => { + const isOss = resolveIsOss(false, false, 'my oss environment'); + expect(isOss).toBe(true); + }); + test('Config with enterprise false and isOss option set to false should return false in test mode', async () => { + const isOss = resolveIsOss(false, false, 'my environment', true); + expect(isOss).toBe(false); + }); +}); diff --git a/src/lib/create-config.ts b/src/lib/create-config.ts index 015e7a9973ac..25194c47217a 100644 --- a/src/lib/create-config.ts +++ b/src/lib/create-config.ts @@ -496,6 +496,17 @@ const parseFrontendApiOrigins = (options: IUnleashOptions): string[] => { return frontendApiOrigins; }; +export function resolveIsOss( + isEnterprise: boolean, + isOssOption?: boolean, + uiEnvironment?: string, + testEnvironmentActive: boolean = false, +): boolean { + return testEnvironmentActive + ? (isOssOption ?? false) + : !isEnterprise && uiEnvironment?.toLowerCase() !== 'pro'; +} + export function createConfig(options: IUnleashOptions): IUnleashConfig { let extraDbOptions = {}; @@ -621,9 +632,12 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig { ui.environment?.toLowerCase() !== 'pro'; const isTest = process.env.NODE_ENV === 'test'; - const isOss = isTest - ? options.isOss || false - : !isEnterprise && ui.environment !== 'pro'; + const isOss = resolveIsOss( + isEnterprise, + options.isOss, + ui.environment, + isTest, + ); const metricsRateLimiting = loadMetricsRateLimitingConfig(options); const rateLimiting = loadRateLimitingConfig(options); @@ -776,5 +790,6 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig { module.exports = { createConfig, + resolveIsOss, authTypeFromString, };