Skip to content

Commit

Permalink
[SDKS-7653] Improvements & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Zamora committed Oct 23, 2023
1 parent 2bbd1d0 commit bc3f755
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 31 deletions.
8 changes: 7 additions & 1 deletion .jest/setEnvVars.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Environments for testing
process.env.SPLIT_EVALUATOR_ENVIRONMENTS='[{"API_KEY":"localhost","AUTH_TOKEN":"test"},{"API_KEY":"apikey1","AUTH_TOKEN":"key_blue"},{"API_KEY":"apikey2","AUTH_TOKEN":"key_red"}]'
process.env.SPLIT_EVALUATOR_ENVIRONMENTS = `[
{"API_KEY":"localhost","AUTH_TOKEN":"test"},
{"API_KEY":"apikey1","AUTH_TOKEN":"key_blue"},
{"API_KEY":"apikey2","AUTH_TOKEN":"key_red"},
{"API_KEY":"apikey3","AUTH_TOKEN":"key_green","FLAG_SET_FILTER":"set_green"},
{"API_KEY":"apikey4","AUTH_TOKEN":"key_purple","FLAG_SET_FILTER":"set_purple"}
]`;

// Before all tests, sdk module is mocked to create a wrapper where a different yaml file is assigned to each environment
// sdk factory mock to set a different yaml for each apikey and localhost mode
Expand Down
40 changes: 40 additions & 0 deletions environmentManager/__tests__/globalConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,46 @@ describe('environmentManager', () => {
// impressionsMode should be NONE as configured in global config
expect(factorySettings.sync.impressionsMode).toBe('NONE');
});
await environmentManagerFactory.destroy();
});
});

describe('flag sets', () => {
test('Environment manager should initialize for legacy configuration without filters', async () => {
delete process.env.SPLIT_EVALUATOR_ENVIRONMENTS;
process.env.SPLIT_EVALUATOR_AUTH_TOKEN = 'test';
process.env.SPLIT_EVALUATOR_API_KEY = 'test';
const environmentManagerFactory = require('../');
expect(() => environmentManagerFactory.getInstance()).not.toThrow();
expect(environmentManagerFactory.hasInstance()).toBe(true);
await environmentManagerFactory.destroy();
});

test('Environment manager should throw an error if is initialized with environments and filters on global config', async () => {
process.env.SPLIT_EVALUATOR_GLOBAL_CONFIG = JSON.stringify({
urls: urls,
sync: {
splitFilters: [{type: 'bySet', values: ['set_a', 'set_b']}],
},
});
process.env.SPLIT_EVALUATOR_ENVIRONMENTS = JSON.stringify(environmentsConfig);
expect(() => require('../')).toThrow();
});

test('Environment manager should initialize for legacy configuration with filters', async () => {
delete process.env.SPLIT_EVALUATOR_ENVIRONMENTS;
process.env.SPLIT_EVALUATOR_GLOBAL_CONFIG = JSON.stringify({
urls: urls,
sync: {
splitFilters: [{type: 'bySet', values: ['set_a', 'set_b']}],
},
});

const environmentManagerFactory = require('../');
const environmentManager = environmentManagerFactory.getInstance();
const factorySettings = environmentManager.getFactory(process.env.SPLIT_EVALUATOR_AUTH_TOKEN).settings;
expect(factorySettings.sync.splitFilters).toEqual([{type: 'bySet', values: ['set_a', 'set_b']}]);
await environmentManagerFactory.destroy();
});
});
});
55 changes: 52 additions & 3 deletions environmentManager/__tests__/validation.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable no-useless-escape */
const environmentManagerFactory = require('../index.js');

const validEnvironment = '[{"API_KEY":"localhost","AUTH_TOKEN":"test"},{"API_KEY":"apikey1","AUTH_TOKEN":"key_blue"},{"API_KEY":"apikey2","AUTH_TOKEN":"key_red"}]';
const environmentNotString = [{'API_KEY':'localhost','AUTH_TOKEN':'test'},{'API_KEY':'apikey1','AUTH_TOKEN':'key_blue'},{'API_KEY':'apikey2','AUTH_TOKEN':'key_red'}];
Expand All @@ -14,10 +13,17 @@ const environmentWithAuthTokenNotString1 = '[{"API_KEY":"localhost","AUTH_TOKEN"
const environmentWithAuthTokenNotString2 = '[{"API_KEY":"localhost","AUTH_TOKEN":"key_red"},{"API_KEY":"apikey1","AUTH_TOKEN":{"key":"value"}}]';
const environmentWithAuthTokenNotString3 = '[{"API_KEY":"localhost","AUTH_TOKEN":"key_red"},{"API_KEY":"apikey1","AUTH_TOKEN":true}]';
const environmentWithAuthTokenEmpty = '[{"API_KEY":"localhost","AUTH_TOKEN":""},{"API_KEY":"apikey1","AUTH_TOKEN":"key_red"}]';
const environmentWithValidFlagSets = '[{"API_KEY":"key_green","AUTH_TOKEN":"key_green","FLAG_SET_FILTER":"set_a,set_b"},{"API_KEY":"key_red","AUTH_TOKEN":"key_red"}]';
const environmentsWithValidFlagSets1 = '[{"API_KEY":"key_green","AUTH_TOKEN":"key_green","FLAG_SET_FILTER":"set_a"},{"API_KEY":"key_red","AUTH_TOKEN":"key_red","FLAG_SET_FILTER":"set_x"}]';
const environmentsWithValidFlagSets2 = '[{"API_KEY":"key_green","AUTH_TOKEN":"key_green","FLAG_SET_FILTER":"set_1"},{"API_KEY":"key_red","AUTH_TOKEN":"key_red","FLAG_SET_FILTER":"set_c"}]';
const environmentWithInvalidFlagSets1 = '[{"API_KEY":"key_green","AUTH_TOKEN":"key_green","FLAG_SET_FILTER":"Set_3,_set_4"},{"API_KEY":"key_red","AUTH_TOKEN":"key_red"}]';
const environmentWithInvalidFlagSets2 = '[{"API_KEY":"key_green","AUTH_TOKEN":"key_green","FLAG_SET_FILTER":["set_y","set_z"]},{"API_KEY":"key_red","AUTH_TOKEN":"key_red"}]';
const environmentWithInvalidFlagSets3 = '[{"API_KEY":"key_green","AUTH_TOKEN":"key_green","FLAG_SET_FILTER":set_t},{"API_KEY":"key_red","AUTH_TOKEN":"key_red"}]';

// Multiple environment - client endpoints
describe('environmentManager - input validations', () => {
test('SPLIT_EVALUATOR_ENVIRONMENTS ', () => {
describe('environmentManager - input validations', () => {
test('SPLIT_EVALUATOR_ENVIRONMENTS ',async () => {
const environmentManagerFactory = require('../');

// Testing environment not string
process.env.SPLIT_EVALUATOR_ENVIRONMENTS=environmentNotString;
Expand Down Expand Up @@ -70,6 +76,49 @@ describe('environmentManager - input validations', () => {
// Testing environment With AuthToken Empty
process.env.SPLIT_EVALUATOR_ENVIRONMENTS= validEnvironment;
expect(() => environmentManagerFactory.getInstance()).not.toThrow();
await environmentManagerFactory.destroy();

});

test('Flag sets validation', async () => {

const evaluateFlagSets = async (greenQuery, redQuery) => {
const environmentManagerFactory = require('../');
expect(environmentManagerFactory.hasInstance()).toBe(false);
let environmentManager = environmentManagerFactory.getInstance();

let environmentSettings = environmentManager.getFactory('key_green').settings;
let queryString = environmentSettings.sync.__splitFiltersValidation.queryString;
expect(queryString).toStrictEqual(greenQuery);

environmentSettings = environmentManager.getFactory('key_red').settings;
queryString = environmentSettings.sync.__splitFiltersValidation.queryString;
expect(queryString).toStrictEqual(redQuery);
await environmentManager.destroy();
await environmentManagerFactory.destroy();
};

process.env.SPLIT_EVALUATOR_ENVIRONMENTS = environmentWithValidFlagSets;
await evaluateFlagSets('&sets=set_a,set_b',null);

process.env.SPLIT_EVALUATOR_ENVIRONMENTS = environmentsWithValidFlagSets1;
await evaluateFlagSets('&sets=set_a','&sets=set_x');

process.env.SPLIT_EVALUATOR_ENVIRONMENTS = environmentsWithValidFlagSets2;
await evaluateFlagSets('&sets=set_1','&sets=set_c');

process.env.SPLIT_EVALUATOR_ENVIRONMENTS = environmentWithInvalidFlagSets1;
await evaluateFlagSets('&sets=set_3',null);

const environmentManagerFactory = require('../');

process.env.SPLIT_EVALUATOR_ENVIRONMENTS = environmentWithInvalidFlagSets2;
expect(() => environmentManagerFactory.getInstance()).toThrow();
expect(environmentManagerFactory.hasInstance()).toBe(false);

process.env.SPLIT_EVALUATOR_ENVIRONMENTS = environmentWithInvalidFlagSets3;
expect(() => environmentManagerFactory.getInstance()).toThrow();
expect(environmentManagerFactory.hasInstance()).toBe(false);

});
});
9 changes: 1 addition & 8 deletions environmentManager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,11 @@ const EnvironmentManagerFactory = (function(){
}

const environmentConfigs = validEnvironmentConfig(SPLIT_EVALUATOR_ENVIRONMENTS);

if (!defaultEnvironment && settings.sync && settings.sync.splitFilters) {
throwError('Flag sets must be defined in SPLIT_EVALUATOR_ENVIRONMENTS, initialization aborted');
process.exit(0);
}

environmentConfigs.forEach(environment => {

validEnvironment(environment);
const authToken = environment['AUTH_TOKEN'];
const apiKey = environment['API_KEY'];
const maybeFlagSets = environment['FLAG_SET_FILTER'];
settings.core.authorizationKey = apiKey;

if(!isString(authToken)) {
Expand All @@ -71,7 +64,7 @@ const EnvironmentManagerFactory = (function(){
}

if (!defaultEnvironment) {
const flagSets = validFlagSets(maybeFlagSets);
const flagSets = validFlagSets(environment['FLAG_SET_FILTER']);
settings.sync = {
...settings.sync,
splitFilters: flagSets,
Expand Down
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"test": "NODE_ENV=test jest"
},
"dependencies": {
"@splitsoftware/splitio": "10.23.2-rc.0",
"@splitsoftware/splitio": "10.23.2-rc.1",
"config": "^3.3.9",
"express": "^4.17.1",
"morgan": "^1.9.1",
Expand Down
4 changes: 2 additions & 2 deletions utils/inputValidation/flagSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const TRIMMABLE_SPACES_REGEX = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/;

const validateFlagSet = (maybeFlagSet) => {
// eslint-disable-next-line
if (maybeFlagSet == undefined) return okWrapper(null);
if (maybeFlagSet == undefined) return errorWrapper('you passed a null or undefined set-names, set-names must be a non-empty array.');

if (TRIMMABLE_SPACES_REGEX.test(maybeFlagSet)) {
console.log(`set-names "${maybeFlagSet}" has extra whitespace, trimming.`);
maybeFlagSet = maybeFlagSet.trim();
}
if (maybeFlagSet.length === 0) return errorWrapper('you passed an empty set-names, set-names must be a non-empty string.');
if (maybeFlagSet.length === 0) return errorWrapper('you passed an empty set-names, set-names must be a non-empty array.');

return okWrapper(maybeFlagSet.split(','));
};
Expand Down
24 changes: 24 additions & 0 deletions utils/mocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ const apiKeyMocksMap = {
timeUntilReady: 860,
httpErrors: {},
},
'apikey3': {
splitUrl: '/split3.yml',
splitNames: ['testing_split_green','testing_split_color','testing_split_green_with_config'],
segments: [],
lastSynchronization: {
sp: 1674857489885,
to: 1674857489898,
te: 1674857488696,
},
timeUntilReady: 360,
httpErrors: {},
},
'apikey4': {
splitUrl: '/split4.yml',
splitNames: ['testing_split_purple','testing_split_color','testing_split_purple_with_config'],
segments: [],
lastSynchronization: {
sp: 1674857489895,
to: 1674857489908,
te: 1674857488706,
},
timeUntilReady: 900,
httpErrors: {},
},
};

const core = {
Expand Down
8 changes: 7 additions & 1 deletion utils/parserConfigs/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require('path');
const { parseNumber, validUrl, validLogLevel, validGlobalConfig } = require('./validators');
const { parseNumber, validUrl, validLogLevel, validGlobalConfig, throwError } = require('./validators');

const getConfigs = () => {
let configs = {
Expand All @@ -18,6 +18,12 @@ const getConfigs = () => {
console.info('Setting global config');
const globalConfig = validGlobalConfig('SPLIT_EVALUATOR_GLOBAL_CONFIG');

if (process.env.SPLIT_EVALUATOR_ENVIRONMENTS){
if (globalConfig.sync && globalConfig.sync.splitFilters) {
throwError('Flag sets must be defined in SPLIT_EVALUATOR_ENVIRONMENTS, initialization aborted');
}
}

configs = Object.assign(globalConfig, nulleableConfigs, configs);
if (configs.sync) Object.assign(configs.sync, {enabled: undefined} );

Expand Down
9 changes: 9 additions & 0 deletions utils/split3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Always green
- testing_split_green:
treatment: "green"
- testing_split_color:
treatment: "green"
# All keys with config
- testing_split_with_config:
treatment: "green"
config: "{\"color\": \"green\"}"
9 changes: 9 additions & 0 deletions utils/split4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Always purple
- testing_split_purple:
treatment: "purple"
- testing_split_color:
treatment: "purple"
# All keys with config
- testing_split_with_config:
treatment: "purple"
config: "{\"color\": \"purple\"}"

0 comments on commit bc3f755

Please sign in to comment.