Skip to content

Implements missing validations #93

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/optimizely.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ def get_variation(experiment_key, user_id, attributes = nil)
return nil
end

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

Expand Down Expand Up @@ -196,6 +196,11 @@ def track(event_key, user_id, attributes = nil, event_tags = nil)
return nil
end

if user_id.to_s.empty?
@logger.log(Logger::ERROR, 'User ID cannot be empty.')
return nil
end

if event_tags && event_tags.is_a?(Numeric)
event_tags = {
'revenue' => event_tags
Expand Down Expand Up @@ -428,7 +433,7 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type,
return nil
end

unless user_id
if user_id.to_s.empty?
@logger.log(Logger::ERROR, 'User ID cannot be empty.')
return nil
end
Expand Down
18 changes: 16 additions & 2 deletions spec/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ class InvalidErrorHandler; end
}
end

it 'should return nil when user_id is empty or nil' do
expect(project_instance.track('test_event', '', nil, 'revenue' => 42)).to eq(nil)
expect(project_instance.track('test_event', nil, nil, 'revenue' => 42)).to eq(nil)
expect(spy_logger).to have_received(:log).twice.with(Logger::ERROR, 'User ID cannot be empty.')
end

it 'should properly track an event by calling dispatch_event with right params' do
params = @expected_track_event_params

Expand Down Expand Up @@ -529,6 +535,12 @@ class InvalidErrorHandler; end
end

describe '#get_variation' do
it 'should return nil when user_id is empty or nil' do
expect(project_instance.get_variation('test_experiment_with_audience', '', nil)).to eq(nil)
expect(project_instance.get_variation('test_experiment_with_audience', nil, nil)).to eq(nil)
expect(spy_logger).to have_received(:log).twice.with(Logger::ERROR, 'User ID cannot be empty.')
end

it 'should have get_variation return expected variation when there are no audiences' do
expect(project_instance.get_variation('test_experiment', 'test_user'))
.to eq(config_body['experiments'][0]['variations'][0]['key'])
Expand Down Expand Up @@ -1012,10 +1024,12 @@ class InvalidErrorHandler; end
expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'Variable key cannot be empty.')
end

it 'should return nil if user_id is nil' do
it 'should return nil if user_id is empty or nil' do
expect(project_instance.get_feature_variable_integer('integer_single_variable_feature', 'integer_variable', nil, user_attributes))
.to eq(nil)
expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'User ID cannot be empty.')
expect(project_instance.get_feature_variable_integer('integer_single_variable_feature', 'integer_variable', '', user_attributes))
.to eq(nil)
expect(spy_logger).to have_received(:log).twice.with(Logger::ERROR, 'User ID cannot be empty.')
end
end

Expand Down