Skip to content

fix(initialize): Prevent SDK from initializing if the datafile version in invalid. #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 13, 2018
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
109 changes: 49 additions & 60 deletions packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var projectConfigSchema = require('./project_config_schema');
var sprintf = require('sprintf-js').sprintf;
var userProfileServiceValidator = require('../utils/user_profile_service_validator');
var stringValidator = require('../utils/string_value_validator');
var configValidator = require('../utils/config_validator');

var ERROR_MESSAGES = enums.ERROR_MESSAGES;
var LOG_LEVEL = enums.LOG_LEVEL;
Expand All @@ -47,74 +48,62 @@ var FEATURE_VARIABLE_TYPES = enums.FEATURE_VARIABLE_TYPES;
* @param {Object} config.userProfileService
*/
function Optimizely(config) {
var clientEngine = config.clientEngine;
if (clientEngine !== enums.NODE_CLIENT_ENGINE && clientEngine !== enums.JAVASCRIPT_CLIENT_ENGINE) {
config.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.INVALID_CLIENT_ENGINE, MODULE_NAME, clientEngine));
clientEngine = enums.NODE_CLIENT_ENGINE;
}
var clientEngine = config.clientEngine;
if (clientEngine !== enums.NODE_CLIENT_ENGINE && clientEngine !== enums.JAVASCRIPT_CLIENT_ENGINE) {
config.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.INVALID_CLIENT_ENGINE, MODULE_NAME, clientEngine));
clientEngine = enums.NODE_CLIENT_ENGINE;
}

this.clientEngine = clientEngine;
this.clientVersion = config.clientVersion || enums.NODE_CLIENT_VERSION;
this.errorHandler = config.errorHandler;
this.eventDispatcher = config.eventDispatcher;
this.isValidInstance = config.isValidInstance;
this.logger = config.logger;

if (!config.datafile) {
this.logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, MODULE_NAME));
this.errorHandler.handleError(new Error(sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, MODULE_NAME)));
this.isValidInstance = false;
} else {
if (typeof config.datafile === 'string' || config.datafile instanceof String) {
// Attempt to parse the datafile string
try {
config.datafile = JSON.parse(config.datafile);
} catch (ex) {
this.isValidInstance = false;
this.logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, MODULE_NAME));
return;
}
}
this.clientEngine = clientEngine;
this.clientVersion = config.clientVersion || enums.NODE_CLIENT_VERSION;
this.errorHandler = config.errorHandler;
this.eventDispatcher = config.eventDispatcher;
this.isValidInstance = config.isValidInstance;
this.logger = config.logger;

if (config.skipJSONValidation === true) {
try {
configValidator.validateDatafile(config.datafile);
if (typeof config.datafile === 'string' || config.datafile instanceof String) {
config.datafile = JSON.parse(config.datafile);
}

if (config.skipJSONValidation === true) {
this.configObj = projectConfig.createProjectConfig(config.datafile);
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.SKIPPING_JSON_VALIDATION, MODULE_NAME));
} else {
if (config.jsonSchemaValidator.validate(projectConfigSchema, config.datafile)) {
this.configObj = projectConfig.createProjectConfig(config.datafile);
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.SKIPPING_JSON_VALIDATION, MODULE_NAME));
} else {
try {
if (config.jsonSchemaValidator.validate(projectConfigSchema, config.datafile)) {
this.configObj = projectConfig.createProjectConfig(config.datafile);
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VALID_DATAFILE, MODULE_NAME));
}
} catch (ex) {
this.isValidInstance = false;
this.logger.log(LOG_LEVEL.ERROR, ex.message);
this.errorHandler.handleError(ex);
}
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VALID_DATAFILE, MODULE_NAME));
}
}
} catch (ex) {
this.isValidInstance = false;
this.logger.log(LOG_LEVEL.ERROR, ex.message);
this.errorHandler.handleError(ex);
}

var userProfileService = null;
if (config.userProfileService) {
try {
if (userProfileServiceValidator.validate(config.userProfileService)) {
userProfileService = config.userProfileService;
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VALID_USER_PROFILE_SERVICE, MODULE_NAME));
}
} catch (ex) {
this.logger.log(LOG_LEVEL.WARNING, ex.message);
}
var userProfileService = null;
if (config.userProfileService) {
try {
if (userProfileServiceValidator.validate(config.userProfileService)) {
userProfileService = config.userProfileService;
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VALID_USER_PROFILE_SERVICE, MODULE_NAME));
}
} catch (ex) {
this.logger.log(LOG_LEVEL.WARNING, ex.message);
}
}

this.decisionService = decisionService.createDecisionService({
configObj: this.configObj,
userProfileService: userProfileService,
logger: this.logger,
});
this.decisionService = decisionService.createDecisionService({
configObj: this.configObj,
userProfileService: userProfileService,
logger: this.logger,
});

this.notificationCenter = notificationCenter.createNotificationCenter({
logger: this.logger,
errorHandler: this.errorHandler
});
}
this.notificationCenter = notificationCenter.createNotificationCenter({
logger: this.logger,
errorHandler: this.errorHandler
});
}

/**
Expand Down
24 changes: 21 additions & 3 deletions packages/optimizely-sdk/lib/optimizely/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ describe('lib/optimizely', function() {
});
sinon.assert.calledOnce(stubErrorHandler.handleError);
var errorMessage = stubErrorHandler.handleError.lastCall.args[0].message;
assert.strictEqual(errorMessage, sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, 'OPTIMIZELY'));
assert.strictEqual(errorMessage, sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, 'CONFIG_VALIDATOR'));

sinon.assert.calledOnce(createdLogger.log);
var logMessage = createdLogger.log.args[0][1];
assert.strictEqual(logMessage, sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, 'OPTIMIZELY'));
assert.strictEqual(logMessage, sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, 'CONFIG_VALIDATOR'));

assert.isFalse(optly.isValidInstance);
});
Expand All @@ -129,7 +129,7 @@ describe('lib/optimizely', function() {

sinon.assert.calledOnce(createdLogger.log);
var logMessage = createdLogger.log.args[0][1];
assert.strictEqual(logMessage, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, 'OPTIMIZELY'));
assert.strictEqual(logMessage, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, 'CONFIG_VALIDATOR'));
});

it('should throw an error if the datafile is not valid', function() {
Expand All @@ -152,6 +152,24 @@ describe('lib/optimizely', function() {
assert.strictEqual(logMessage, sprintf(ERROR_MESSAGES.INVALID_DATAFILE, 'JSON_SCHEMA_VALIDATOR', 'projectId', 'is missing and it is required'));
});

it('should log an error if the datafile version is not supported', function() {
new Optimizely({
clientEngine: 'node-sdk',
errorHandler: stubErrorHandler,
datafile: testData.getUnsupportedVersionConfig(),
jsonSchemaValidator: jsonSchemaValidator,
logger: createdLogger,
});

sinon.assert.calledOnce(stubErrorHandler.handleError);
var errorMessage = stubErrorHandler.handleError.lastCall.args[0].message;
assert.strictEqual(errorMessage, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, 'CONFIG_VALIDATOR', '5'));

sinon.assert.calledOnce(createdLogger.log);
var logMessage = createdLogger.log.args[0][1];
assert.strictEqual(logMessage, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, 'CONFIG_VALIDATOR', '5'));
});

describe('skipping JSON schema validation', function() {
beforeEach(function() {
sinon.spy(jsonSchemaValidator, 'validate');
Expand Down
Loading