Skip to content

Commit

Permalink
Create a DsiUserSession record when creating or updating the user
Browse files Browse the repository at this point in the history
We only do this if supplied an optional role. Bypass mechanisisms will still function without a role.
  • Loading branch information
steventux committed Sep 13, 2023
1 parent be17658 commit fb9f34f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
12 changes: 11 additions & 1 deletion app/models/dsi_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ class DsiUser < ApplicationRecord
encrypts :first_name, :last_name

has_many :search_logs
has_many :dsi_user_sessions, dependent: :destroy

def self.create_or_update_from_dsi(dsi_payload)
def self.create_or_update_from_dsi(dsi_payload, role = nil)
dsi_user = find_or_initialize_by(email: dsi_payload.info.fetch(:email))

dsi_user.update!(
Expand All @@ -13,6 +14,15 @@ def self.create_or_update_from_dsi(dsi_payload)
uid: dsi_payload.uid
)

if role.present?
dsi_user.dsi_user_sessions.create!(
role_id: role["id"],
role_code: role["code"],
organisation_id: dsi_payload.extra.raw_info.organisation.id,
organisation_name: dsi_payload.extra.raw_info.organisation.name,
)
end

dsi_user
end
end
3 changes: 3 additions & 0 deletions app/models/dsi_user_session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class DsiUserSession < ApplicationRecord
belongs_to :dsi_user
end
27 changes: 26 additions & 1 deletion spec/models/dsi_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@
RSpec.describe DsiUser, type: :model do
describe ".create_or_update_from_dsi" do
let(:dsi_payload) do
OmniAuth::AuthHash.new(uid: "123456", info: { email:, first_name: "John", last_name: "Doe" })
OmniAuth::AuthHash.new(
uid: "123456",
info: { email:, first_name: "John", last_name: "Doe" },
extra: {
raw_info: {
organisation: {
id: "09876",
name: "Test Org",
}
}
}
)
end
let(:email) { "[email protected]" }

Expand Down Expand Up @@ -32,5 +43,19 @@
expect(dsi_user.last_name).to eq dsi_payload.info.last_name
end
end

context "when the user has a role" do
it "creates a session record" do
role = { "id" => "123", "code" => "TestRole_code" }
described_class.create_or_update_from_dsi(dsi_payload, role)

dsi_user_session = DsiUser.first.dsi_user_sessions.first
expect(dsi_user_session).to be_present
expect(dsi_user_session.role_id).to eq "123"
expect(dsi_user_session.role_code).to eq "TestRole_code"
expect(dsi_user_session.organisation_id).to eq "09876"
expect(dsi_user_session.organisation_name).to eq "Test Org"
end
end
end
end

0 comments on commit fb9f34f

Please sign in to comment.