Skip to content

Commit

Permalink
Method for getting support user from dfe user
Browse files Browse the repository at this point in the history
  • Loading branch information
elceebee committed Dec 19, 2023
1 parent 1583675 commit 387afe7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ def sign_in_user
def current_user
@current_user ||= sign_in_user&.user
end

def support_user
@support_user ||= sign_in_user&.support_user
end
end
16 changes: 16 additions & 0 deletions app/models/dfe_sign_in_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def user
@user ||= user_klass.find_by(email:)
end

def support_user
@support_user ||= support_user_klass.find(user.id)
rescue ActiveRecord::RecordNotFound
nil
end

def self.end_session!(session)
session.clear
end
Expand All @@ -64,4 +70,14 @@ def user_klass
Placements::User
end
end

def support_user_klass
case service
# when :claims
# Claims::SupportUser
when :placements
Placements::SupportUser
end

end
end
4 changes: 4 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@
factory :placements_user, class: "Placements::User", parent: :user do
service { "placements" }
end

factory :placements_support_user, class: "Placements::SupportUser", parent: :user do
service { "placements" }
end
end
57 changes: 57 additions & 0 deletions spec/models/dfe_sign_in_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,63 @@
end
end

describe "#support_user" do
describe "claims service" do
it "returns the claims user" do
claims_user = create(:claims_user)

session = {
"dfe_sign_in_user" => {
"first_name" => claims_user.first_name,
"last_name" => claims_user.last_name,
"email" => claims_user.email
},
"service" => :claims
}

dfe_sign_in_user = DfESignInUser.load_from_session(session)

expect(dfe_sign_in_user.user).to eq claims_user
end
end

describe "placements service" do
it "returns nil if user is not support" do
placements_user = create(:placements_user)

session = {
"dfe_sign_in_user" => {
"first_name" => placements_user.first_name,
"last_name" => placements_user.last_name,
"email" => placements_user.email
},
"service" => :placements
}

dfe_sign_in_user = DfESignInUser.load_from_session(session)

expect(dfe_sign_in_user.support_user).to eq nil
end

it "returns the placements support user" do
support_placements_user = create(:placements_support_user)

session = {
"dfe_sign_in_user" => {
"first_name" => support_placements_user.first_name,
"last_name" => support_placements_user.last_name,
"email" => support_placements_user.email
},
"service" => :placements
}

dfe_sign_in_user = DfESignInUser.load_from_session(session)

expect(dfe_sign_in_user.support_user).to eq support_placements_user
end
end
end

describe "#user" do
describe "claims service" do
it "returns the claims user" do
Expand Down

0 comments on commit 387afe7

Please sign in to comment.