Skip to content

Commit

Permalink
fix: added separate method for resolving isOss flag to be able to tes…
Browse files Browse the repository at this point in the history
…t behaviour when NODE_ENV !== test
  • Loading branch information
chriswk committed Nov 29, 2024
1 parent 93c6be8 commit 69bbcb2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
21 changes: 20 additions & 1 deletion src/lib/create-config.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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);
});
});
21 changes: 18 additions & 3 deletions src/lib/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -776,5 +790,6 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig {

module.exports = {
createConfig,
resolveIsOss,
authTypeFromString,
};

0 comments on commit 69bbcb2

Please sign in to comment.