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

Conversation

mfahadahmed
Copy link
Contributor

No description provided.

@coveralls
Copy link

coveralls commented Sep 3, 2018

Coverage Status

Coverage increased (+0.02%) to 97.135% when pulling ad1a30b on fahad/unsupported-datafile into a72082b on master.

@mfahadahmed mfahadahmed changed the title Prevent SDK from initializing if the datafile version in invalid. fix(initialize): Prevent SDK from initializing if the datafile version in invalid. Sep 3, 2018
Copy link
Contributor

@oakbani oakbani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Just minor changes

@@ -146,6 +146,22 @@ describe('lib/core/project_config', function() {
assert.deepEqual(configObj.variationIdMap, expectedVariationIdMap);
});

it('should throw exception with invalid config version', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. throw an error with unsupported datafile version
Same below.

@@ -152,6 +152,27 @@ 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 valid', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. is not supported

/*
* Possible types of variables attached to features
*/
exports.CONFIG_VERSIONS = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. DATAFILE_VERSIONS

@@ -186,3 +187,12 @@ exports.FEATURE_VARIABLE_TYPES = {
INTEGER: 'integer',
STRING: 'string',
};

/*
* Possible types of variables attached to features
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. Remove comment

@@ -59,6 +59,7 @@ exports.ERROR_MESSAGES = {
VARIATION_ID_NOT_IN_DATAFILE: '%s: No variation ID %s defined in datafile for experiment %s.',
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_CONFIG_VERSION: '%s: This version of the Javascript SDK does not support the given datafile version: %s',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit UNSUPPORTED_DATAFILE_VERSION

Copy link
Contributor

@mikeproeng37 mikeproeng37 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have all the validation in the config_validator module. Please move this logic there are well: https://github.com/optimizely/javascript-sdk/blob/master/packages/optimizely-sdk/lib/utils/config_validator/index.js#L35

@mfahadahmed
Copy link
Contributor Author

Currently most of the datafile parsing and validations are performed inside Optimizely constructor. The config_validator module actually validates logger and error handler instances.
Should we move all those datafile validations from Optimizely constructor to config_validator?

@mikeproeng37
Copy link
Contributor

Yep, we should move the version validation out of the project_config into the config validator, as well as lines 63 through 77 from the Optimizely constructor into the config validator as well. Moving code out of that constructor makes it more maintainable and easier to follow

@msohailhussain
Copy link
Contributor

Please don't review this PR. Some of the changes needed to make in this PR, will let you know when ready.

@mfahadahmed
Copy link
Contributor Author

@mikeng13 : The approach we should take to isolate datafile validations from Optimizely constructor requires more rework. I have some queries related to that.

Right now, I moved datafile validations in a separate config_validator method and called it from Optimizely constructor.

To completely isolate validations from Optimizely constructor, we should perform it in createInstance method and move all the instance creation unit tests from optimizely.tests to index.node.tests.js and index.browser.tests.js files. Should I go with that or the current implementation is enough?

Copy link
Contributor

@mikeproeng37 mikeproeng37 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mfahadahmed no need to move the other validation code for now. This is enough of a refactor. I've also requested some changes in a few places.

* Validates the datafile
* @param {string} datafile
* @return {Boolean} True if the datafile is valid
* @throws If any of the datafile validations get failed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@throws If the datafile is not valid for any of the following reasons:
                - The datafile string is undefined
                - The datafile string cannot be parsed as a JSON object
                - The datafile version is not supported

}, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, 'CONFIG_VALIDATOR'));
});

it('should complain if datafile version is not supported', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some more test cases for things like boolean, number, array

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm this, it is not really needed.

@@ -59,6 +59,7 @@ exports.ERROR_MESSAGES = {
VARIATION_ID_NOT_IN_DATAFILE: '%s: No variation ID %s defined in datafile for experiment %s.',
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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaScript

createdLogger.log.restore();
});

it('should log an error when datafile is not provided', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are duplicate test cases from the config validator. We just need two test cases:

  • datafile is valid
  • datafile validation throws error (any) error

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we need to add only 1 test case here. That config validator does get called from createInstance and if it throws an error, assert that the isValidInstance is set to False.
The datafile validations are already taken care of in config validator tests.


it('should create optimizely instance with valid datafile', function() {
var optlyInstance = optimizelyFactory.createInstance({
datafile: JSON.stringify(testData.getUnsupportedVersionConfig()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case, you keep this test case, assert that validator doesn't thrown an error and isValidInstance is true. Instance will always be created like it does here with UnsupportedVersionConfig

@mfahadahmed
Copy link
Contributor Author

Oh I think none of the datafile validation tests are needed in index.node.tests.js as all of these test cases are already covered in optimizely.tests. Sorry, I forgot to remove these test cases from index.node.tests.js after changing validation logic.

@@ -17,6 +17,13 @@ var sprintf = require('sprintf-js').sprintf;

var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES;
var MODULE_NAME = 'CONFIG_VALIDATOR';
var DATAFILE_VERSIONS = require('../enums').DATAFILE_VERSIONS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mfahadahmed Please update header for this file and it's test file.

Copy link
Contributor

@mikeproeng37 mikeproeng37 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@mikeproeng37 mikeproeng37 merged commit 0d263fc into master Sep 13, 2018
@msohailhussain msohailhussain deleted the fahad/unsupported-datafile branch September 20, 2018 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants