2019-06-27 16:26:07 +02:00
|
|
|
|
describe ChampPolicy do
|
2020-04-01 15:55:40 +02:00
|
|
|
|
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) }
|
|
|
|
|
let(:dossier) { create(:dossier, procedure: procedure, user: dossier_owner) }
|
2019-10-08 14:28:26 +02:00
|
|
|
|
let(:dossier_owner) { create(:user) }
|
2019-06-27 16:26:07 +02:00
|
|
|
|
|
2019-10-08 14:28:26 +02:00
|
|
|
|
let(:signed_in_user) { create(:user) }
|
|
|
|
|
let(:account) { { user: signed_in_user } }
|
2019-08-01 15:48:27 +02:00
|
|
|
|
|
|
|
|
|
subject { Pundit.policy_scope(account, Champ) }
|
2019-06-27 16:26:07 +02:00
|
|
|
|
|
2022-11-10 22:21:14 +01:00
|
|
|
|
let(:champ) { dossier.champs_public.first }
|
2020-04-01 15:55:40 +02:00
|
|
|
|
let(:champ_private) { dossier.champs_private.first }
|
|
|
|
|
|
2019-10-08 14:28:26 +02:00
|
|
|
|
shared_examples_for 'they can access a public champ' do
|
|
|
|
|
it { expect(subject.find_by(id: champ.id)).to eq(champ) }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
shared_examples_for 'they can’t access a public champ' do
|
|
|
|
|
it { expect(subject.find_by(id: champ.id)).to eq(nil) }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
shared_examples_for 'they can access a private champ' do
|
2020-04-01 15:55:40 +02:00
|
|
|
|
it { expect(subject.find_by(id: champ_private.id)).to eq(champ_private) }
|
2019-10-08 14:28:26 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
shared_examples_for 'they can’t access a private champ' do
|
2020-04-01 15:55:40 +02:00
|
|
|
|
it { expect(subject.find_by(id: champ_private.id)).to eq(nil) }
|
2019-10-08 14:28:26 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when an user only has user rights' do
|
|
|
|
|
context 'as the dossier owner' do
|
|
|
|
|
let(:signed_in_user) { dossier_owner }
|
|
|
|
|
|
|
|
|
|
it_behaves_like 'they can access a public champ'
|
|
|
|
|
it_behaves_like 'they can’t access a private champ'
|
|
|
|
|
end
|
2019-06-27 16:26:07 +02:00
|
|
|
|
|
2020-04-01 16:10:45 +02:00
|
|
|
|
context 'as a person invited on the dossier' do
|
|
|
|
|
let(:invite) { create(:invite, :with_user, dossier: dossier) }
|
|
|
|
|
let(:signed_in_user) { invite.user }
|
|
|
|
|
|
|
|
|
|
it_behaves_like 'they can access a public champ'
|
|
|
|
|
it_behaves_like 'they can’t access a private champ'
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-08 14:28:26 +02:00
|
|
|
|
context 'as another user' do
|
|
|
|
|
let(:signed_in_user) { create(:user) }
|
|
|
|
|
|
|
|
|
|
it_behaves_like 'they can’t access a public champ'
|
|
|
|
|
it_behaves_like 'they can’t access a private champ'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the user also has instruction rights' do
|
2021-10-25 15:50:48 +02:00
|
|
|
|
let(:instructeur) { create(:instructeur, user: signed_in_user) }
|
2019-10-08 14:28:26 +02:00
|
|
|
|
let(:account) { { user: signed_in_user, instructeur: instructeur } }
|
|
|
|
|
|
|
|
|
|
context 'as the dossier instructeur and owner' do
|
|
|
|
|
let(:signed_in_user) { dossier_owner }
|
|
|
|
|
before { instructeur.assign_to_procedure(dossier.procedure) }
|
|
|
|
|
|
|
|
|
|
it_behaves_like 'they can access a public champ'
|
|
|
|
|
it_behaves_like 'they can access a private champ'
|
2019-06-27 16:26:07 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-10-08 14:28:26 +02:00
|
|
|
|
context 'as the dossier instructeur (but not owner)' do
|
|
|
|
|
let(:signed_in_user) { create(:user) }
|
|
|
|
|
before { instructeur.assign_to_procedure(dossier.procedure) }
|
|
|
|
|
|
|
|
|
|
it_behaves_like 'they can’t access a public champ'
|
|
|
|
|
it_behaves_like 'they can access a private champ'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'as an instructeur not assigned to the procedure' do
|
|
|
|
|
let(:signed_in_user) { create(:user) }
|
|
|
|
|
|
|
|
|
|
it_behaves_like 'they can’t access a public champ'
|
|
|
|
|
it_behaves_like 'they can’t access a private champ'
|
2019-06-27 16:26:07 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2023-05-24 16:53:51 +02:00
|
|
|
|
|
|
|
|
|
context 'when the champ is on a forked dossier' do
|
|
|
|
|
let(:signed_in_user) { dossier_owner }
|
|
|
|
|
let(:origin) { create(:dossier, procedure: procedure, user: dossier_owner) }
|
|
|
|
|
let(:dossier) { origin.find_or_create_editing_fork(dossier_owner) }
|
|
|
|
|
|
|
|
|
|
it_behaves_like 'they can access a public champ'
|
|
|
|
|
it_behaves_like 'they can’t access a private champ'
|
|
|
|
|
|
|
|
|
|
context 'when the user is invited on the origin dossier' do
|
|
|
|
|
let(:invite) { create(:invite, :with_user, dossier: origin) }
|
|
|
|
|
let(:signed_in_user) { invite.user }
|
|
|
|
|
|
|
|
|
|
it_behaves_like 'they can access a public champ'
|
|
|
|
|
it_behaves_like 'they can’t access a private champ'
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-06-27 16:26:07 +02:00
|
|
|
|
end
|