fix(invitation): don't fail when dossier is hidden & user signed on another account

Suite de 45994ff567
Cf https://sentry.io/organizations/demarches-simplifiees/issues/3422144920/events/e1c83ea74dbc410ea22aa17ef29fb329/?project=1429550
This commit is contained in:
Colin Darie 2022-10-24 14:31:34 +02:00 committed by LeSim
parent 9ac8c85b28
commit 9f2fa5ee1c
2 changed files with 18 additions and 6 deletions

View file

@ -25,7 +25,8 @@ class TargetedUserLink < ApplicationRecord
when 'avis'
user.email
when 'invite'
target_model.user&.email || target_model.email
invite = find_invite!
invite.user&.email || invite.email
else
raise 'invalid target_context'
end
@ -34,9 +35,7 @@ class TargetedUserLink < ApplicationRecord
def redirect_url(url_helper)
case target_context
when "invite"
invite = target_model
fail ActiveRecord::RecordNotFound if invite.nil?
invite = find_invite!
user = User.find_by(email: target_email)
user&.active? ?
@ -49,4 +48,10 @@ class TargetedUserLink < ApplicationRecord
url_helper.sign_up_expert_avis_path(avis.procedure, avis, email: avis.expert.email)
end
end
private
def find_invite!
target_model || (fail ActiveRecord::RecordNotFound.new("Could not find Invite with id `#{target_model_id}`"))
end
end

View file

@ -102,6 +102,13 @@ describe TargetedUserLinksController, type: :controller do
expect(response).to redirect_to(root_path)
expect(flash[:error]).to match(/dossier n'est plus accessible/)
end
it 'redirect nicely also when user is signed on another account' do
sign_in(create(:expert).user)
get :show, params: { id: targeted_user_link.id }
expect(response).to redirect_to(root_path)
expect(flash[:error]).to match(/dossier n'est plus accessible/)
end
end
end