diff --git a/lib/event_source/operations/build_message_options.rb b/lib/event_source/operations/build_message_options.rb index 331371be..4456ae9c 100644 --- a/lib/event_source/operations/build_message_options.rb +++ b/lib/event_source/operations/build_message_options.rb @@ -38,17 +38,17 @@ def build_payload(params) def append_account_details(headers) output = FetchSession.new.call + return output unless output.success? + + session, current_user, system_account = output.value! account = {} - if output.success? - session, current_user = output.value! + if session.present? && current_user.present? account[:session] = session&.symbolize_keys + account[:id] = current_user&.id&.to_s else - # Create system account user when session is not available - current_user = system_account if defined?(system_account) + account[:id] = system_account&.id&.to_s end - - account[:id] = current_user&.id&.to_s headers[:account] = account Success(headers) diff --git a/lib/event_source/operations/fetch_session.rb b/lib/event_source/operations/fetch_session.rb index 9e72d280..ce2ce83e 100644 --- a/lib/event_source/operations/fetch_session.rb +++ b/lib/event_source/operations/fetch_session.rb @@ -13,8 +13,9 @@ def call helper = yield include_session_helper session = yield fetch_session current_user = yield fetch_current_user + system_account = yield fetch_system_account - Success([session, current_user]) + Success([session, current_user, system_account]) end private @@ -42,6 +43,14 @@ def fetch_current_user Failure("current_user is not defined") end end + + def fetch_system_account + if respond_to?(:system_account) + Success(system_account) + else + Failure("system_account is not defined") + end + end end end end diff --git a/spec/event_source/operations/fetch_session_spec.rb b/spec/event_source/operations/fetch_session_spec.rb index 568df596..29ce65a1 100644 --- a/spec/event_source/operations/fetch_session_spec.rb +++ b/spec/event_source/operations/fetch_session_spec.rb @@ -6,16 +6,20 @@ let(:fetch_session) { described_class.new } describe "when session helper not defined" do + before do + allow(fetch_session).to receive(:respond_to?).with(:session).and_return( + respond_to_session + ) + allow(fetch_session).to receive(:respond_to?).with( + :current_user + ).and_return(respond_to_current_user) + end + + let(:respond_to_session) { true } + let(:respond_to_current_user) { true } context "when current user not defined" do - before do - allow(fetch_session).to receive(:respond_to?).with(:session).and_return( - true - ) - allow(fetch_session).to receive(:respond_to?).with( - :current_user - ).and_return(false) - end + let(:respond_to_current_user) { false } it "should fail" do result = fetch_session.call @@ -26,14 +30,7 @@ end context "when session not defined" do - before do - allow(fetch_session).to receive(:respond_to?).with(:session).and_return( - false - ) - allow(fetch_session).to receive(:respond_to?).with( - :current_user - ).and_return(true) - end + let(:respond_to_session) { false } it "should fail" do result = fetch_session.call @@ -58,20 +55,23 @@ def session "login_session_id" => "ad465b7f-1d9e-44b1-ba72-b97e166f3acb" } end + + def system_account + OpenStruct.new(id: 2) + end end + let(:session_concern) { Class.new.extend(SessionConcern) } + it "should return session and current user" do result = fetch_session.call expect(result.success?).to be_truthy expect(result.value!).to eq( [ - { - "session_id" => "ad465b7f-1d9e-44b1-ba72-b97e166f3acb", - "portal" => "enroll/families/home", - "login_session_id" => "ad465b7f-1d9e-44b1-ba72-b97e166f3acb" - }, - OpenStruct.new(id: 1) + session_concern.session, + session_concern.current_user, + session_concern.system_account ] ) end