Skip to content

Commit 67a3bab

Browse files
[FSSDK-8949] fix: make odp event identifiers required (#326)
* make odp event identifiers required
1 parent 48607ea commit 67a3bab

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

lib/optimizely.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,15 @@ def get_optimizely_config
896896
# Send an event to the ODP server.
897897
#
898898
# @param action - the event action name.
899+
# @param identifiers - a hash for identifiers. The caller must provide at least one key-value pair.
899900
# @param type - the event type (default = "fullstack").
900-
# @param identifiers - a hash for identifiers.
901901
# @param data - a hash for associated data. The default event data will be added to this data before sending to the ODP server.
902902

903-
def send_odp_event(action:, type: Helpers::Constants::ODP_MANAGER_CONFIG[:EVENT_TYPE], identifiers: {}, data: {})
903+
def send_odp_event(action:, identifiers:, type: Helpers::Constants::ODP_MANAGER_CONFIG[:EVENT_TYPE], data: {})
904+
unless identifiers.is_a?(Hash) && !identifiers.empty?
905+
@logger.log(Logger::ERROR, 'ODP events must have at least one key-value pair in identifiers.')
906+
return
907+
end
904908
@odp_manager.send_event(type: type, action: action, identifiers: identifiers, data: data)
905909
end
906910

spec/project_spec.rb

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4802,7 +4802,7 @@ def running?; end
48024802
project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, 'sdk-key', nil, nil, nil, [], {}, sdk_settings)
48034803
event_manager = project.odp_manager.instance_variable_get('@event_manager')
48044804
expect(event_manager).to be_a CustomEventManager
4805-
project.send_odp_event(action: 'test')
4805+
project.send_odp_event(action: 'test', identifiers: {wow: 'great'})
48064806
project.close
48074807

48084808
expect(spy_logger).not_to have_received(:log).with(Logger::ERROR, anything)
@@ -4830,7 +4830,7 @@ class InvalidEventManager; end # rubocop:disable Lint/ConstantDefinitionInBlock
48304830
expect(spy_logger).to receive(:log).once.with(Logger::DEBUG, 'ODP event queue: flushing batch size 1.')
48314831
expect(spy_logger).not_to receive(:log).with(Logger::ERROR, anything)
48324832
project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger)
4833-
project.send_odp_event(type: 'wow', action: 'great', identifiers: {}, data: {})
4833+
project.send_odp_event(type: 'wow', action: 'great', identifiers: {amazing: 'fantastic'}, data: {})
48344834
project.close
48354835
end
48364836

@@ -4847,22 +4847,22 @@ class InvalidEventManager; end # rubocop:disable Lint/ConstantDefinitionInBlock
48474847
project.send(:project_config)
48484848
sleep 0.1 until project.odp_manager.instance_variable_get('@event_manager').instance_variable_get('@event_queue').empty?
48494849

4850-
project.send_odp_event(type: 'wow', action: 'great', identifiers: {}, data: {})
4850+
project.send_odp_event(type: 'wow', action: 'great', identifiers: {amazing: 'fantastic'}, data: {})
48514851
project.close
48524852
end
48534853

48544854
it 'should log error when odp disabled' do
48554855
expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'ODP is not enabled.')
48564856
sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(disable_odp: true)
48574857
custom_project_instance = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger, error_handler, false, nil, nil, nil, nil, nil, [], {}, sdk_settings)
4858-
custom_project_instance.send_odp_event(type: 'wow', action: 'great', identifiers: {}, data: {})
4858+
custom_project_instance.send_odp_event(type: 'wow', action: 'great', identifiers: {amazing: 'fantastic'}, data: {})
48594859
custom_project_instance.close
48604860
end
48614861

48624862
it 'should log debug if datafile not ready' do
48634863
expect(spy_logger).to receive(:log).once.with(Logger::DEBUG, 'ODP event queue: cannot send before config has been set.')
48644864
project = Optimizely::Project.new(nil, nil, spy_logger, nil, false, nil, sdk_key)
4865-
project.send_odp_event(type: 'wow', action: 'great', identifiers: {}, data: {})
4865+
project.send_odp_event(type: 'wow', action: 'great', identifiers: {amazing: 'fantastic'}, data: {})
48664866
project.close
48674867
end
48684868

@@ -4873,17 +4873,31 @@ class InvalidEventManager; end # rubocop:disable Lint/ConstantDefinitionInBlock
48734873
sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(disable_odp: true)
48744874
project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings)
48754875
sleep 0.1 until project.config_manager.ready?
4876-
project.send_odp_event(type: 'wow', action: 'great', identifiers: {}, data: {})
4876+
project.send_odp_event(type: 'wow', action: 'great', identifiers: {amazing: 'fantastic'}, data: {})
48774877
project.close
48784878
end
48794879

48804880
it 'should log error with invalid data' do
48814881
expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'ODP data is not valid.')
48824882
project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger)
4883+
project.send_odp_event(type: 'wow', action: 'great', identifiers: {amazing: 'fantastic'}, data: {'wow': {}})
4884+
project.close
4885+
end
4886+
4887+
it 'should log error with empty identifiers' do
4888+
expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'ODP events must have at least one key-value pair in identifiers.')
4889+
project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger)
48834890
project.send_odp_event(type: 'wow', action: 'great', identifiers: {}, data: {'wow': {}})
48844891
project.close
48854892
end
48864893

4894+
it 'should log error with nil identifiers' do
4895+
expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'ODP events must have at least one key-value pair in identifiers.')
4896+
project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger)
4897+
project.send_odp_event(type: 'wow', action: 'great', identifiers: nil, data: {'wow': {}})
4898+
project.close
4899+
end
4900+
48874901
it 'should not send odp events with legacy apis' do
48884902
experiment_key = 'experiment-segment'
48894903
feature_key = 'flag-segment'

0 commit comments

Comments
 (0)