Skip to content

Commit 122c69d

Browse files
rashidspthomaszurkan-optimizely
authored andcommitted
Implements missing validations (#93)
1 parent 2d0cc98 commit 122c69d

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib/optimizely.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ def get_variation(experiment_key, user_id, attributes = nil)
135135
return nil
136136
end
137137

138-
unless user_id.is_a? String
139-
@logger.log(Logger::ERROR, "User id: #{user_id} is not a string")
138+
if user_id.to_s.empty?
139+
@logger.log(Logger::ERROR, 'User ID cannot be empty.')
140140
return nil
141141
end
142142

@@ -196,6 +196,11 @@ def track(event_key, user_id, attributes = nil, event_tags = nil)
196196
return nil
197197
end
198198

199+
if user_id.to_s.empty?
200+
@logger.log(Logger::ERROR, 'User ID cannot be empty.')
201+
return nil
202+
end
203+
199204
if event_tags && event_tags.is_a?(Numeric)
200205
event_tags = {
201206
'revenue' => event_tags
@@ -428,7 +433,7 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type,
428433
return nil
429434
end
430435

431-
unless user_id
436+
if user_id.to_s.empty?
432437
@logger.log(Logger::ERROR, 'User ID cannot be empty.')
433438
return nil
434439
end

spec/project_spec.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,12 @@ class InvalidErrorHandler; end
361361
}
362362
end
363363

364+
it 'should return nil when user_id is empty or nil' do
365+
expect(project_instance.track('test_event', '', nil, 'revenue' => 42)).to eq(nil)
366+
expect(project_instance.track('test_event', nil, nil, 'revenue' => 42)).to eq(nil)
367+
expect(spy_logger).to have_received(:log).twice.with(Logger::ERROR, 'User ID cannot be empty.')
368+
end
369+
364370
it 'should properly track an event by calling dispatch_event with right params' do
365371
params = @expected_track_event_params
366372

@@ -529,6 +535,12 @@ class InvalidErrorHandler; end
529535
end
530536

531537
describe '#get_variation' do
538+
it 'should return nil when user_id is empty or nil' do
539+
expect(project_instance.get_variation('test_experiment_with_audience', '', nil)).to eq(nil)
540+
expect(project_instance.get_variation('test_experiment_with_audience', nil, nil)).to eq(nil)
541+
expect(spy_logger).to have_received(:log).twice.with(Logger::ERROR, 'User ID cannot be empty.')
542+
end
543+
532544
it 'should have get_variation return expected variation when there are no audiences' do
533545
expect(project_instance.get_variation('test_experiment', 'test_user'))
534546
.to eq(config_body['experiments'][0]['variations'][0]['key'])
@@ -1012,10 +1024,12 @@ class InvalidErrorHandler; end
10121024
expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'Variable key cannot be empty.')
10131025
end
10141026

1015-
it 'should return nil if user_id is nil' do
1027+
it 'should return nil if user_id is empty or nil' do
10161028
expect(project_instance.get_feature_variable_integer('integer_single_variable_feature', 'integer_variable', nil, user_attributes))
10171029
.to eq(nil)
1018-
expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'User ID cannot be empty.')
1030+
expect(project_instance.get_feature_variable_integer('integer_single_variable_feature', 'integer_variable', '', user_attributes))
1031+
.to eq(nil)
1032+
expect(spy_logger).to have_received(:log).twice.with(Logger::ERROR, 'User ID cannot be empty.')
10191033
end
10201034
end
10211035

0 commit comments

Comments
 (0)