Skip to content

Commit 0d263fc

Browse files
mfahadahmedmikeproeng37
authored andcommitted
fix(initialize): Prevent SDK from initializing if the datafile version in invalid. (#161)
1 parent a72082b commit 0d263fc

File tree

6 files changed

+384
-65
lines changed

6 files changed

+384
-65
lines changed

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

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var projectConfigSchema = require('./project_config_schema');
2626
var sprintf = require('sprintf-js').sprintf;
2727
var userProfileServiceValidator = require('../utils/user_profile_service_validator');
2828
var stringValidator = require('../utils/string_value_validator');
29+
var configValidator = require('../utils/config_validator');
2930

3031
var ERROR_MESSAGES = enums.ERROR_MESSAGES;
3132
var LOG_LEVEL = enums.LOG_LEVEL;
@@ -47,74 +48,62 @@ var FEATURE_VARIABLE_TYPES = enums.FEATURE_VARIABLE_TYPES;
4748
* @param {Object} config.userProfileService
4849
*/
4950
function Optimizely(config) {
50-
var clientEngine = config.clientEngine;
51-
if (clientEngine !== enums.NODE_CLIENT_ENGINE && clientEngine !== enums.JAVASCRIPT_CLIENT_ENGINE) {
52-
config.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.INVALID_CLIENT_ENGINE, MODULE_NAME, clientEngine));
53-
clientEngine = enums.NODE_CLIENT_ENGINE;
54-
}
51+
var clientEngine = config.clientEngine;
52+
if (clientEngine !== enums.NODE_CLIENT_ENGINE && clientEngine !== enums.JAVASCRIPT_CLIENT_ENGINE) {
53+
config.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.INVALID_CLIENT_ENGINE, MODULE_NAME, clientEngine));
54+
clientEngine = enums.NODE_CLIENT_ENGINE;
55+
}
5556

56-
this.clientEngine = clientEngine;
57-
this.clientVersion = config.clientVersion || enums.NODE_CLIENT_VERSION;
58-
this.errorHandler = config.errorHandler;
59-
this.eventDispatcher = config.eventDispatcher;
60-
this.isValidInstance = config.isValidInstance;
61-
this.logger = config.logger;
62-
63-
if (!config.datafile) {
64-
this.logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, MODULE_NAME));
65-
this.errorHandler.handleError(new Error(sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, MODULE_NAME)));
66-
this.isValidInstance = false;
67-
} else {
68-
if (typeof config.datafile === 'string' || config.datafile instanceof String) {
69-
// Attempt to parse the datafile string
70-
try {
71-
config.datafile = JSON.parse(config.datafile);
72-
} catch (ex) {
73-
this.isValidInstance = false;
74-
this.logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, MODULE_NAME));
75-
return;
76-
}
77-
}
57+
this.clientEngine = clientEngine;
58+
this.clientVersion = config.clientVersion || enums.NODE_CLIENT_VERSION;
59+
this.errorHandler = config.errorHandler;
60+
this.eventDispatcher = config.eventDispatcher;
61+
this.isValidInstance = config.isValidInstance;
62+
this.logger = config.logger;
7863

79-
if (config.skipJSONValidation === true) {
64+
try {
65+
configValidator.validateDatafile(config.datafile);
66+
if (typeof config.datafile === 'string' || config.datafile instanceof String) {
67+
config.datafile = JSON.parse(config.datafile);
68+
}
69+
70+
if (config.skipJSONValidation === true) {
71+
this.configObj = projectConfig.createProjectConfig(config.datafile);
72+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.SKIPPING_JSON_VALIDATION, MODULE_NAME));
73+
} else {
74+
if (config.jsonSchemaValidator.validate(projectConfigSchema, config.datafile)) {
8075
this.configObj = projectConfig.createProjectConfig(config.datafile);
81-
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.SKIPPING_JSON_VALIDATION, MODULE_NAME));
82-
} else {
83-
try {
84-
if (config.jsonSchemaValidator.validate(projectConfigSchema, config.datafile)) {
85-
this.configObj = projectConfig.createProjectConfig(config.datafile);
86-
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VALID_DATAFILE, MODULE_NAME));
87-
}
88-
} catch (ex) {
89-
this.isValidInstance = false;
90-
this.logger.log(LOG_LEVEL.ERROR, ex.message);
91-
this.errorHandler.handleError(ex);
92-
}
76+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VALID_DATAFILE, MODULE_NAME));
9377
}
78+
}
79+
} catch (ex) {
80+
this.isValidInstance = false;
81+
this.logger.log(LOG_LEVEL.ERROR, ex.message);
82+
this.errorHandler.handleError(ex);
83+
}
9484

95-
var userProfileService = null;
96-
if (config.userProfileService) {
97-
try {
98-
if (userProfileServiceValidator.validate(config.userProfileService)) {
99-
userProfileService = config.userProfileService;
100-
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VALID_USER_PROFILE_SERVICE, MODULE_NAME));
101-
}
102-
} catch (ex) {
103-
this.logger.log(LOG_LEVEL.WARNING, ex.message);
104-
}
85+
var userProfileService = null;
86+
if (config.userProfileService) {
87+
try {
88+
if (userProfileServiceValidator.validate(config.userProfileService)) {
89+
userProfileService = config.userProfileService;
90+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VALID_USER_PROFILE_SERVICE, MODULE_NAME));
10591
}
92+
} catch (ex) {
93+
this.logger.log(LOG_LEVEL.WARNING, ex.message);
94+
}
95+
}
10696

107-
this.decisionService = decisionService.createDecisionService({
108-
configObj: this.configObj,
109-
userProfileService: userProfileService,
110-
logger: this.logger,
111-
});
97+
this.decisionService = decisionService.createDecisionService({
98+
configObj: this.configObj,
99+
userProfileService: userProfileService,
100+
logger: this.logger,
101+
});
112102

113-
this.notificationCenter = notificationCenter.createNotificationCenter({
114-
logger: this.logger,
115-
errorHandler: this.errorHandler
116-
});
117-
}
103+
this.notificationCenter = notificationCenter.createNotificationCenter({
104+
logger: this.logger,
105+
errorHandler: this.errorHandler
106+
});
118107
}
119108

120109
/**

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ describe('lib/optimizely', function() {
107107
});
108108
sinon.assert.calledOnce(stubErrorHandler.handleError);
109109
var errorMessage = stubErrorHandler.handleError.lastCall.args[0].message;
110-
assert.strictEqual(errorMessage, sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, 'OPTIMIZELY'));
110+
assert.strictEqual(errorMessage, sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, 'CONFIG_VALIDATOR'));
111111

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

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

130130
sinon.assert.calledOnce(createdLogger.log);
131131
var logMessage = createdLogger.log.args[0][1];
132-
assert.strictEqual(logMessage, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, 'OPTIMIZELY'));
132+
assert.strictEqual(logMessage, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, 'CONFIG_VALIDATOR'));
133133
});
134134

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

155+
it('should log an error if the datafile version is not supported', function() {
156+
new Optimizely({
157+
clientEngine: 'node-sdk',
158+
errorHandler: stubErrorHandler,
159+
datafile: testData.getUnsupportedVersionConfig(),
160+
jsonSchemaValidator: jsonSchemaValidator,
161+
logger: createdLogger,
162+
});
163+
164+
sinon.assert.calledOnce(stubErrorHandler.handleError);
165+
var errorMessage = stubErrorHandler.handleError.lastCall.args[0].message;
166+
assert.strictEqual(errorMessage, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, 'CONFIG_VALIDATOR', '5'));
167+
168+
sinon.assert.calledOnce(createdLogger.log);
169+
var logMessage = createdLogger.log.args[0][1];
170+
assert.strictEqual(logMessage, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, 'CONFIG_VALIDATOR', '5'));
171+
});
172+
155173
describe('skipping JSON schema validation', function() {
156174
beforeEach(function() {
157175
sinon.spy(jsonSchemaValidator, 'validate');

0 commit comments

Comments
 (0)