-
Notifications
You must be signed in to change notification settings - Fork 28
refact(API): Adds missing input validations in all API methods and validates empty user Id. #127
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
Changes from 2 commits
e925856
2080c3a
14dcb12
1bd4112
b7d9c58
9a053be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,11 +96,15 @@ def activate(experiment_key, user_id, attributes = nil) | |
|
||
return nil unless Optimizely::Helpers::Validator.inputs_valid?( | ||
{ | ||
experiment_key: experiment_key, | ||
user_id: user_id | ||
experiment_key: experiment_key | ||
}, @logger, Logger::ERROR | ||
) | ||
|
||
unless user_id.is_a?(String) | ||
@logger.log(Logger::ERROR, "#{Optimizely::Helpers::Constants::INPUT_VARIABLES['USER_ID']} is invalid") | ||
return nil | ||
end | ||
|
||
variation_key = get_variation(experiment_key, user_id, attributes) | ||
|
||
if variation_key.nil? | ||
|
@@ -132,11 +136,15 @@ def get_variation(experiment_key, user_id, attributes = nil) | |
|
||
return nil unless Optimizely::Helpers::Validator.inputs_valid?( | ||
{ | ||
experiment_key: experiment_key, | ||
user_id: user_id | ||
experiment_key: experiment_key | ||
}, @logger, Logger::ERROR | ||
) | ||
|
||
unless user_id.is_a?(String) | ||
@logger.log(Logger::ERROR, "#{Optimizely::Helpers::Constants::INPUT_VARIABLES['USER_ID']} is invalid") | ||
return nil | ||
end | ||
|
||
unless user_inputs_valid?(attributes) | ||
@logger.log(Logger::INFO, "Not activating user '#{user_id}.") | ||
return nil | ||
|
@@ -194,11 +202,15 @@ def track(event_key, user_id, attributes = nil, event_tags = nil) | |
|
||
return nil unless Optimizely::Helpers::Validator.inputs_valid?( | ||
{ | ||
event_key: event_key, | ||
user_id: user_id | ||
event_key: event_key | ||
}, @logger, Logger::ERROR | ||
) | ||
|
||
unless user_id.is_a?(String) | ||
@logger.log(Logger::ERROR, "#{Optimizely::Helpers::Constants::INPUT_VARIABLES['USER_ID']} is invalid") | ||
return nil | ||
end | ||
|
||
return nil unless user_inputs_valid?(attributes, event_tags) | ||
|
||
experiment_ids = @config.get_experiment_ids_for_event(event_key) | ||
|
@@ -253,11 +265,17 @@ def is_feature_enabled(feature_flag_key, user_id, attributes = nil) | |
|
||
return false unless Optimizely::Helpers::Validator.inputs_valid?( | ||
{ | ||
feature_flag_key: feature_flag_key, | ||
user_id: user_id | ||
feature_flag_key: feature_flag_key | ||
}, @logger, Logger::ERROR | ||
) | ||
|
||
unless user_id.is_a?(String) | ||
@logger.log(Logger::ERROR, "#{Optimizely::Helpers::Constants::INPUT_VARIABLES['USER_ID']} is invalid") | ||
return false | ||
end | ||
|
||
return false unless user_inputs_valid?(attributes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we combine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Combined validations: rashid/input-validations...rashid/input-validations-2 |
||
|
||
feature_flag = @config.get_feature_flag_from_key(feature_flag_key) | ||
unless feature_flag | ||
@logger.log(Logger::ERROR, "No feature flag was found for key '#{feature_flag_key}'.") | ||
|
@@ -305,7 +323,12 @@ def get_enabled_features(user_id, attributes = nil) | |
return enabled_features | ||
end | ||
|
||
return enabled_features unless Optimizely::Helpers::Validator.inputs_valid?({user_id: user_id}, @logger, Logger::ERROR) | ||
unless user_id.is_a?(String) | ||
@logger.log(Logger::ERROR, "#{Optimizely::Helpers::Constants::INPUT_VARIABLES['USER_ID']} is invalid") | ||
return enabled_features | ||
end | ||
|
||
return enabled_features unless user_inputs_valid?(attributes) | ||
|
||
@config.feature_flags.each do |feature| | ||
enabled_features.push(feature['key']) if is_feature_enabled( | ||
|
@@ -444,12 +467,18 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type, | |
{ | ||
feature_flag_key: feature_flag_key, | ||
variable_key: variable_key, | ||
variable_type: variable_type, | ||
user_id: user_id | ||
variable_type: variable_type | ||
}, | ||
@logger, Logger::ERROR | ||
) | ||
|
||
unless user_id.is_a?(String) | ||
@logger.log(Logger::ERROR, "#{Optimizely::Helpers::Constants::INPUT_VARIABLES['USER_ID']} is invalid") | ||
return nil | ||
end | ||
|
||
return nil unless user_inputs_valid?(attributes) | ||
|
||
feature_flag = @config.get_feature_flag_from_key(feature_flag_key) | ||
unless feature_flag | ||
@logger.log(Logger::INFO, "No feature flag was found for key '#{feature_flag_key}'.") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather we refactor a bit and avoid using this same logic everywhere in the code. Let's mimic what the C# SDK does here: https://github.com/optimizely/csharp-sdk/blob/master/OptimizelySDK/Optimizely.cs#L642
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.