From 999169a558512e268a19882979e40da3db42d64a Mon Sep 17 00:00:00 2001 From: rashidsp Date: Tue, 26 Mar 2019 20:42:45 +0500 Subject: [PATCH] feat(api): Feature variable APIs return default variable value when featureEnabled property is false. --- lib/optimizely.rb | 2 +- spec/project_spec.rb | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/optimizely.rb b/lib/optimizely.rb index 204625f7..20f7d7d5 100644 --- a/lib/optimizely.rb +++ b/lib/optimizely.rb @@ -473,7 +473,7 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type, variation = decision['variation'] variation_variable_usages = @config.variation_id_to_variable_usage_map[variation['id']] variable_id = variable['id'] - if variation_variable_usages&.key?(variable_id) + if variation['featureEnabled'] && variation_variable_usages&.key?(variable_id) variable_value = variation_variable_usages[variable_id]['value'] @logger.log(Logger::INFO, "Got variable value '#{variable_value}' for variable '#{variable_key}' of feature flag '#{feature_flag_key}'.") diff --git a/spec/project_spec.rb b/spec/project_spec.rb index ae27ef5b..672e2ad6 100644 --- a/spec/project_spec.rb +++ b/spec/project_spec.rb @@ -1732,6 +1732,53 @@ class InvalidErrorHandler; end end end + describe '#get_feature_variable_for_type with default variables' do + it 'should return default variable type and value, when user in experiment and feature is not enabled' do + integer_feature = project_instance.config.feature_flag_key_map['integer_single_variable_feature'] + experiment_to_return = project_instance.config.experiment_id_map[integer_feature['experimentIds'][0]] + variation_to_return = experiment_to_return['variations'][0] + variation_to_return['featureEnabled'] = false + decision_to_return = Optimizely::DecisionService::Decision.new( + experiment_to_return, + variation_to_return, + Optimizely::DecisionService::DECISION_SOURCE_EXPERIMENT + ) + + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + + expect(project_instance.send( + :get_feature_variable_for_type, + 'integer_single_variable_feature', + 'integer_variable', + 'integer', + 'test_user', + 'browser_type' => 'firefox' + )).to eq(7) + end + + it 'should return default variable type and value, when user in rollout and feature is not enabled' do + experiment_to_return = config_body['rollouts'][0]['experiments'][1] + variation_to_return = experiment_to_return['variations'][0] + decision_to_return = Optimizely::DecisionService::Decision.new( + experiment_to_return, + variation_to_return, + Optimizely::DecisionService::DECISION_SOURCE_ROLLOUT + ) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + + expect(variation_to_return['featureEnabled']).to be false + + expect(project_instance.send( + :get_feature_variable_for_type, + 'boolean_single_variable_feature', + 'boolean_variable', + 'boolean', + 'test_user', + {} + )).to eq(true) + end + end + describe 'when forced variation is used' do # setForcedVariation on a paused experiment and then call getVariation. it 'should return null when getVariation is called on a paused experiment after setForcedVariation' do