Skip to content

Commit

Permalink
Return a Placements::SupportUser for suppor users
Browse files Browse the repository at this point in the history
Make sure the `current_user` method returns an instance of
Placements::SupportUser when in the Placements service and the current
user has `support_user = true`.

While `Placements::User` and `Placements::SupportUser` are both
effectively the same right now, we envisage functional changes in the
near future. For example, `user.organisations` will differ to
`support_user.organisations`.
  • Loading branch information
ollietreend committed Dec 21, 2023
1 parent c0c24aa commit ed8b50e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
11 changes: 10 additions & 1 deletion app/models/dfe_sign_in_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ def self.load_from_session(session)

def user
# TODO: When dfe sign-in is fully implemented, we will be able to find the user by the id == id_token.
@user ||= user_klass.find_by(email:)
@user ||=
begin
user = user_klass.find_by(email:)

if service == :placements && user&.support_user?
user = user.becomes(Placements::SupportUser)
end

user
end
end

def self.end_session!(session)
Expand Down
24 changes: 22 additions & 2 deletions spec/models/dfe_sign_in_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

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

session = {
Expand All @@ -63,11 +63,12 @@
dfe_sign_in_user = DfESignInUser.load_from_session(session)

expect(dfe_sign_in_user.user).to eq claims_user
expect(dfe_sign_in_user.user).to be_a Claims::User
end
end

describe "placements service" do
it "returns the placements user" do
it "returns the current Placements::User" do
placements_user = create(:placements_user)

session = {
Expand All @@ -82,6 +83,25 @@
dfe_sign_in_user = DfESignInUser.load_from_session(session)

expect(dfe_sign_in_user.user).to eq placements_user
expect(dfe_sign_in_user.user).to be_a Placements::User
end

it "returns a Placements::SupportUser for support users" do
support_user = create(:placements_user, :support)

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

dfe_sign_in_user = DfESignInUser.load_from_session(session)

expect(dfe_sign_in_user.user.id).to eq support_user.id
expect(dfe_sign_in_user.user).to be_a Placements::SupportUser
end
end
end
Expand Down

0 comments on commit ed8b50e

Please sign in to comment.