Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/optimizely-sdk/lib/core/project_config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
var fns = require('../../utils/fns');
var enums = require('../../utils/enums');
var sprintf = require('sprintf-js').sprintf;
var stringValidator = require('../../utils/string_value_validator');

var EXPERIMENT_LAUNCHED_STATUS = 'Launched';
var EXPERIMENT_RUNNING_STATUS = 'Running';
Expand Down Expand Up @@ -404,6 +405,11 @@ module.exports = {
* @return {boolean} A boolean value that indicates if the set completed successfully.
*/
setForcedVariation: function(projectConfig, experimentKey, userId, variationKey, logger) {
if (variationKey != null && !stringValidator.validate(variationKey)) {
logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.INVALID_VARIATION_KEY, MODULE_NAME));
return false;
}

var experimentId;
try {
var experiment = this.getExperimentFromKey(projectConfig, experimentKey);
Expand All @@ -420,7 +426,7 @@ module.exports = {
return false;
}

if (!variationKey) {
if (variationKey == null) {
try {
this.removeForcedVariation(projectConfig, userId, experimentId, experimentKey, logger);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,5 +741,13 @@ describe('lib/core/project_config', function() {
var didSetVariation = projectConfig.setForcedVariation(configObj, 'testExperiment', undefined, 'control', createdLogger);
assert.strictEqual(didSetVariation, false);
});

it('should return false for an empty variation key', function() {
var testData = testDatafile.getTestProjectConfig();
var configObj = projectConfig.createProjectConfig(testData);

var didSetVariation = projectConfig.setForcedVariation(configObj, 'testExperiment', 'user1', '', createdLogger);
assert.strictEqual(didSetVariation, false);
});
});
});
1 change: 1 addition & 0 deletions packages/optimizely-sdk/lib/utils/enums/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ exports.ERROR_MESSAGES = {
VARIATION_ID_NOT_IN_DATAFILE_NO_EXPERIMENT: '%s: Variation ID %s is not in the datafile.',
INVALID_INPUT_FORMAT: '%s: Provided %s is in an invalid format.',
INVALID_DATAFILE_VERSION: '%s: This version of the JavaScript SDK does not support the given datafile version: %s',
INVALID_VARIATION_KEY: '%s: Provided variation key is in an invalid format.',
};

exports.LOG_MESSAGES = {
Expand Down