Skip to content

Commit 2a47e5e

Browse files
mfahadahmedmikeproeng37
authored andcommitted
fix(set-forced-variation): Treats empty variation key as invalid and does not reset variation. (#185)
1 parent 42eb08e commit 2a47e5e

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

packages/optimizely-sdk/lib/core/project_config/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
var fns = require('../../utils/fns');
1717
var enums = require('../../utils/enums');
1818
var sprintf = require('sprintf-js').sprintf;
19+
var stringValidator = require('../../utils/string_value_validator');
1920

2021
var EXPERIMENT_LAUNCHED_STATUS = 'Launched';
2122
var EXPERIMENT_RUNNING_STATUS = 'Running';
@@ -404,6 +405,11 @@ module.exports = {
404405
* @return {boolean} A boolean value that indicates if the set completed successfully.
405406
*/
406407
setForcedVariation: function(projectConfig, experimentKey, userId, variationKey, logger) {
408+
if (variationKey != null && !stringValidator.validate(variationKey)) {
409+
logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.INVALID_VARIATION_KEY, MODULE_NAME));
410+
return false;
411+
}
412+
407413
var experimentId;
408414
try {
409415
var experiment = this.getExperimentFromKey(projectConfig, experimentKey);
@@ -420,7 +426,7 @@ module.exports = {
420426
return false;
421427
}
422428

423-
if (!variationKey) {
429+
if (variationKey == null) {
424430
try {
425431
this.removeForcedVariation(projectConfig, userId, experimentId, experimentKey, logger);
426432
return true;

packages/optimizely-sdk/lib/core/project_config/index.tests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,5 +741,13 @@ describe('lib/core/project_config', function() {
741741
var didSetVariation = projectConfig.setForcedVariation(configObj, 'testExperiment', undefined, 'control', createdLogger);
742742
assert.strictEqual(didSetVariation, false);
743743
});
744+
745+
it('should return false for an empty variation key', function() {
746+
var testData = testDatafile.getTestProjectConfig();
747+
var configObj = projectConfig.createProjectConfig(testData);
748+
749+
var didSetVariation = projectConfig.setForcedVariation(configObj, 'testExperiment', 'user1', '', createdLogger);
750+
assert.strictEqual(didSetVariation, false);
751+
});
744752
});
745753
});

packages/optimizely-sdk/lib/utils/enums/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ exports.ERROR_MESSAGES = {
6060
VARIATION_ID_NOT_IN_DATAFILE_NO_EXPERIMENT: '%s: Variation ID %s is not in the datafile.',
6161
INVALID_INPUT_FORMAT: '%s: Provided %s is in an invalid format.',
6262
INVALID_DATAFILE_VERSION: '%s: This version of the JavaScript SDK does not support the given datafile version: %s',
63+
INVALID_VARIATION_KEY: '%s: Provided variation key is in an invalid format.',
6364
};
6465

6566
exports.LOG_MESSAGES = {

0 commit comments

Comments
 (0)