Skip to content

Commit a760fdb

Browse files
rashidspmikeproeng37
authored andcommitted
fix(set_forced_variation): Treats empty variation key as invalid and does not reset forced variation. (#137)
1 parent 265d344 commit a760fdb

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

lib/optimizely/project_config.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,20 +329,17 @@ def set_forced_variation(experiment_key, user_id, variation_key)
329329
#
330330
# Returns a boolean value that indicates if the set completed successfully.
331331

332-
return false unless Optimizely::Helpers::Validator.inputs_valid?(
333-
{
334-
user_id: user_id,
335-
experiment_key: experiment_key
336-
}, @logger, Logger::DEBUG
337-
)
332+
input_values = {user_id: user_id, experiment_key: experiment_key}
333+
input_values[:variation_key] = variation_key unless variation_key.nil?
334+
return false unless Optimizely::Helpers::Validator.inputs_valid?(input_values, @logger, Logger::DEBUG)
338335

339336
experiment = get_experiment_from_key(experiment_key)
340337
experiment_id = experiment['id'] if experiment
341338
# check if the experiment exists in the datafile
342339
return false if experiment_id.nil? || experiment_id.empty?
343340

344341
# clear the forced variation if the variation key is null
345-
if variation_key.nil? || variation_key.empty?
342+
if variation_key.nil?
346343
@forced_variation_map[user_id].delete(experiment_id) if @forced_variation_map.key? user_id
347344
@logger.log(Logger::DEBUG, "Variation mapped to experiment '#{experiment_key}' has been removed for user "\
348345
"'#{user_id}'.")

spec/project_config_spec.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -927,10 +927,11 @@
927927
"Variation mapped to experiment '#{@valid_experiment[:key]}' has been removed for user '#{@user_id}'.")
928928
end
929929
# Variation key is an empty string
930-
it 'should delete forced varaition maping, log a message and return true when variation_key is passed as empty string' do
931-
expect(config.set_forced_variation(@valid_experiment[:key], @user_id, '')).to eq(true)
930+
it 'should persist forced variation mapping, log a message and return false when variation_key is passed as empty string' do
931+
expect(config.set_forced_variation(@valid_experiment[:key], @user_id, '')).to eq(false)
932932
expect(spy_logger).to have_received(:log).with(Logger::DEBUG,
933-
"Variation mapped to experiment '#{@valid_experiment[:key]}' has been removed for user '#{@user_id}'.")
933+
'Variation key is invalid')
934+
expect(config.get_forced_variation(@valid_experiment[:key], @user_id)).to eq(nil)
934935
end
935936
# Variation key does not exist in the datafile
936937
it 'return false when variation_key is not in datafile' do
@@ -958,7 +959,8 @@
958959
expect(Optimizely::Helpers::Validator).to receive(:inputs_valid?).with(
959960
{
960961
user_id: @user_id,
961-
experiment_key: @valid_experiment[:key]
962+
experiment_key: @valid_experiment[:key],
963+
variation_key: @valid_variation[:key]
962964
}, spy_logger, Logger::DEBUG
963965
)
964966
config.set_forced_variation(@valid_experiment[:key], @user_id, @valid_variation[:key])
@@ -985,9 +987,6 @@
985987
variation = config.get_forced_variation(@valid_experiment[:key], @user_id)
986988
expect(variation['id']).to eq(@valid_variation_2[:id])
987989
expect(variation['key']).to eq(@valid_variation_2[:key])
988-
989-
expect(config.set_forced_variation(@valid_experiment[:key], @user_id, '')).to eq(true)
990-
expect(config.get_forced_variation(@valid_experiment[:key], @user_id)).to eq(nil)
991990
end
992991

993992
# Set variation on multiple experiments for one user.

0 commit comments

Comments
 (0)