Skip to content

Commit 129ef49

Browse files
authored
Merge pull request demarche-numerique#12988 from demarche-numerique/fix/idor-contact-informations-cross-procedure
Tech: cantonner le groupe instructeur à sa procédure sur les infos de contact
2 parents 5427d65 + 2f037c9 commit 129ef49

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

app/controllers/instructeurs/contact_informations_controller.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ def destroy
4343
private
4444

4545
def assign_procedure_and_groupe_instructeur
46-
@procedure = current_instructeur.procedures.find params[:procedure_id]
47-
@groupe_instructeur = current_instructeur.groupe_instructeurs.find params[:groupe_id]
46+
@procedure = current_instructeur.procedures.find(params[:procedure_id])
47+
@groupe_instructeur = @procedure.groupe_instructeurs
48+
.merge(current_instructeur.groupe_instructeurs)
49+
.find(params[:groupe_id])
4850
end
4951

5052
def contact_information_params

spec/controllers/instructeurs/contact_informations_controller_spec.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
let(:gi) { assign_to.groupe_instructeur }
88
let(:from_admin) { nil }
99

10+
let(:other_procedure) { create(:procedure) }
11+
let(:other_assign_to) { create(:assign_to, instructeur: instructeur, groupe_instructeur: build(:groupe_instructeur, procedure: other_procedure)) }
12+
let(:other_gi) { other_assign_to.groupe_instructeur }
13+
1014
before do
1115
sign_in(instructeur.user)
1216
end
@@ -116,4 +120,69 @@
116120
expect(response).to redirect_to(instructeur_groupe_path(gi, procedure_id: procedure.id))
117121
end
118122
end
123+
124+
context 'when procedure_id and groupe_id belong to different procedures' do
125+
before do
126+
gi # instructeur has legitimate access to `procedure` via gi
127+
other_gi # instructeur is also a member of other_gi on other_procedure
128+
end
129+
130+
let(:valid_contact_information_params) do
131+
{
132+
nom: 'cross service',
133+
email: 'email@toto.com',
134+
telephone: '1234',
135+
horaires: 'horaires',
136+
adresse: 'adresse',
137+
}
138+
end
139+
140+
describe '#create' do
141+
subject(:mismatched_create) do
142+
post :create, params: {
143+
contact_information: valid_contact_information_params,
144+
procedure_id: procedure.id,
145+
groupe_id: other_gi.id,
146+
}
147+
end
148+
149+
it 'does not create a contact_information on the other procedure groupe' do
150+
expect { mismatched_create rescue nil }.not_to change { ContactInformation.count }
151+
expect(other_gi.reload.contact_information).to be_nil
152+
end
153+
end
154+
155+
describe '#update' do
156+
let!(:other_contact_information) { create(:contact_information, groupe_instructeur: other_gi, nom: 'original') }
157+
158+
subject(:mismatched_update) do
159+
patch :update, params: {
160+
id: other_contact_information.id,
161+
contact_information: { nom: 'tampered' },
162+
procedure_id: procedure.id,
163+
groupe_id: other_gi.id,
164+
}
165+
end
166+
167+
it 'does not modify the other procedure groupe contact_information' do
168+
expect { mismatched_update rescue nil }.not_to change { other_contact_information.reload.nom }
169+
end
170+
end
171+
172+
describe '#destroy' do
173+
let!(:other_contact_information) { create(:contact_information, groupe_instructeur: other_gi) }
174+
175+
subject(:mismatched_destroy) do
176+
delete :destroy, params: {
177+
id: other_contact_information.id,
178+
procedure_id: procedure.id,
179+
groupe_id: other_gi.id,
180+
}
181+
end
182+
183+
it 'does not destroy the other procedure groupe contact_information' do
184+
expect { mismatched_destroy rescue nil }.not_to change { ContactInformation.exists?(other_contact_information.id) }
185+
end
186+
end
187+
end
119188
end

0 commit comments

Comments
 (0)