Skip to content

Commit

Permalink
fixes for system account (#107)
Browse files Browse the repository at this point in the history
* fixes for system account

* updated fetch_session spec
  • Loading branch information
raghuramg authored Jan 30, 2024
1 parent e368d61 commit f4e83cb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
12 changes: 6 additions & 6 deletions lib/event_source/operations/build_message_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]> 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)
Expand Down
11 changes: 10 additions & 1 deletion lib/event_source/operations/fetch_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
44 changes: 22 additions & 22 deletions spec/event_source/operations/fetch_session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f4e83cb

Please sign in to comment.