From f0d16686d262a45eed16861bd9399ecdcb19c875 Mon Sep 17 00:00:00 2001 From: Trey Evans Date: Mon, 29 Sep 2025 13:23:56 -0400 Subject: [PATCH] Fix routing match checks. Subscriber routing specs now check the routed action more carefully. --- lib/event_source/protocols/amqp_protocol.rb | 4 ++++ lib/event_source/protocols/arn_protocol.rb | 4 ++++ lib/event_source/protocols/http_protocol.rb | 4 ++++ lib/event_source/protocols/registry.rb | 11 +++++++++++ lib/event_source/rspec/event_routing_matchers.rb | 13 +++++++++++-- .../rails_app/spec/determination_subscriber_spec.rb | 7 +++++++ 6 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/event_source/protocols/amqp_protocol.rb b/lib/event_source/protocols/amqp_protocol.rb index df3ba607..1eeb658c 100644 --- a/lib/event_source/protocols/amqp_protocol.rb +++ b/lib/event_source/protocols/amqp_protocol.rb @@ -36,6 +36,10 @@ def self.proxy_klass def self.subscribe_operation_name_for(app_name, subscriber_key) "on_#{app_name}.#{subscriber_key}" end + + def self.subscriber_action_name_for(_app_name, action_name) + "on_#{action_name}" + end end end end diff --git a/lib/event_source/protocols/arn_protocol.rb b/lib/event_source/protocols/arn_protocol.rb index e86dc7b3..d8b3ee58 100644 --- a/lib/event_source/protocols/arn_protocol.rb +++ b/lib/event_source/protocols/arn_protocol.rb @@ -29,6 +29,10 @@ def self.proxy_klass def self.subscribe_operation_name_for(app_name, subscriber_key) "on_#{app_name}.#{subscriber_key}" end + + def self.subscriber_action_name_for(_app_name, action_name) + "on_#{action_name}" + end end end end diff --git a/lib/event_source/protocols/http_protocol.rb b/lib/event_source/protocols/http_protocol.rb index 5611c0b4..0c69b04b 100644 --- a/lib/event_source/protocols/http_protocol.rb +++ b/lib/event_source/protocols/http_protocol.rb @@ -35,6 +35,10 @@ def self.proxy_klass def self.subscribe_operation_name_for(_app_name, subscriber_key) "/on#{subscriber_key}" end + + def self.subscriber_action_name_for(_app_name, action_name) + action_name.gsub("/", "_").delete_prefix("_") + end end end end diff --git a/lib/event_source/protocols/registry.rb b/lib/event_source/protocols/registry.rb index 2b2508fb..6541d21e 100644 --- a/lib/event_source/protocols/registry.rb +++ b/lib/event_source/protocols/registry.rb @@ -34,6 +34,13 @@ def protocol_klass_for(protocol) match ? match[1] : nil end + def subscriber_action_name_for(protocol, app_name, action_name) + match = @connection_modules.detect do |r_item| + r_item[0].any? { |ri| ri == protocol} + end + match ? match[1].subscriber_action_name_for(app_name, action_name) : nil + end + def subscribe_operation_name_for(protocol, app_name, subscriber_key) match = @connection_modules.detect do |r_item| r_item[0].any? { |ri| ri == protocol} @@ -52,6 +59,10 @@ def self.protocol_klass_for(protocol) def self.subscribe_operation_name_for(protocol, app_name, subscriber_key) instance.subscribe_operation_name_for(protocol.to_sym, app_name, subscriber_key) end + + def self.subscriber_action_name_for(protocol, app_name, action_name) + instance.subscriber_action_name_for(protocol.to_sym, app_name, action_name) + end end end end diff --git a/lib/event_source/rspec/event_routing_matchers.rb b/lib/event_source/rspec/event_routing_matchers.rb index ec420144..6e84c63b 100644 --- a/lib/event_source/rspec/event_routing_matchers.rb +++ b/lib/event_source/rspec/event_routing_matchers.rb @@ -53,7 +53,12 @@ module EventRoutingMatchers ) return false unless conn s_key = s_class.subscriber_executable_key_for(s_action) - !EventSource::Subscriber.executable_container[s_key].blank? + action_key = EventSource::Protocols::Registry.subscriber_action_name_for( + message_spec[0], + EventSource.app_name, + message_spec[2] + ) + !EventSource::Subscriber.executable_container[s_key].blank? && s_action.to_s == action_key.to_s end failure_message do |message_spec| @@ -75,11 +80,15 @@ module EventRoutingMatchers } ) return "#{base_message}, but could not resolve a connection" unless conn + s_key = s_class.subscriber_executable_key_for(s_action) + s_exe = EventSource::Subscriber.executable_container[s_key] + return "#{base_message}, but could not resolve an executable" unless s_exe + return "#{base_message}, but routed instead to #{s_action}" unless s_action.to_s == message_spec[2].to_s base_message end failure_message_when_negated do |message_spec| - "expected #{message_spec} not to route to the :#{s_action} action of the #{s_class} subscriber" + "expected #{message_spec} not to route to the :#{s_action} action of the #{s_class} subscriber, but it did" end description do diff --git a/spec/rails_app/spec/determination_subscriber_spec.rb b/spec/rails_app/spec/determination_subscriber_spec.rb index 0984ef10..203d48d7 100644 --- a/spec/rails_app/spec/determination_subscriber_spec.rb +++ b/spec/rails_app/spec/determination_subscriber_spec.rb @@ -10,4 +10,11 @@ :on_determined_aqhp_eligible ) end + + it "does not route a message to the wrong handler for 'magi_medicaid.mitc.eligibilities', :on_totally_made_up_event" do + expect([:amqp, 'magi_medicaid.mitc.eligibilities', 'totally_made_up_event']).not_to route_to_subscription( + Subscribers::DeterminationSubscriber, + :on_determined_aqhp_eligible + ) + end end