From d80f7d995c2d7468334f0bd06adcc0dcd34e7fd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Hanser?= Date: Wed, 3 Mar 2021 13:15:43 +0100 Subject: [PATCH] =?UTF-8?q?Permet=20=C3=A0=20un=20conseiller=20de=20cr?= =?UTF-8?q?=C3=A9er=20un=20compte=20pour=20un=20de=20ses=20coll=C3=A8gues?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marine Dominé --- app/admin/comptes.rb | 8 ++++++-- app/models/ability.rb | 1 + spec/features/admin/compte_spec.rb | 33 +++++++++++++++++++++++------- spec/integrations/ability_spec.rb | 1 + 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app/admin/comptes.rb b/app/admin/comptes.rb index b2c8f0444..05645a562 100644 --- a/app/admin/comptes.rb +++ b/app/admin/comptes.rb @@ -23,8 +23,12 @@ form do |f| f.inputs do f.input :email - f.input :role, as: :select, collection: %w[administrateur organisation] - f.input :structure + if can? :manage, Compte + f.input :role, as: :select, collection: %w[administrateur organisation] + f.input :structure + else + f.input :structure_id, as: :hidden, input_html: { value: current_compte.structure_id } + end f.input :password f.input :password_confirmation end diff --git a/app/models/ability.rb b/app/models/ability.rb index eed41728a..985853d3d 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -81,6 +81,7 @@ def droit_question end def droit_compte(compte) + can :create, Compte can :read, Compte, structure_id: compte.structure_id cannot :destroy, Compte do |q| Campagne.where(compte: q).present? diff --git a/spec/features/admin/compte_spec.rb b/spec/features/admin/compte_spec.rb index b709011bc..f7f4f1439 100644 --- a/spec/features/admin/compte_spec.rb +++ b/spec/features/admin/compte_spec.rb @@ -36,20 +36,21 @@ context 'en conseiller' do let(:ma_structure) { create :structure } - let(:autre_structure) { create :structure } let(:conseiller_connecte) do create :compte_organisation, structure: ma_structure, email: 'moi@structure' end - let!(:collegue) do - create :compte_organisation, structure: ma_structure, email: 'collegue@structure' - end - let!(:inconnu) do - create :compte_organisation, structure: autre_structure, email: 'inconnu@structure' - end before(:each) { connecte conseiller_connecte } describe 'je vois mes collègues' do + let(:autre_structure) { create :structure } + let!(:inconnu) do + create :compte_organisation, structure: autre_structure, email: 'inconnu@structure' + end + let!(:collegue) do + create :compte_organisation, structure: ma_structure, email: 'collegue@structure' + end + before { visit admin_comptes_path } it do @@ -58,5 +59,23 @@ expect(page).to_not have_content 'inconnu@structure' end end + + describe 'ajouter un collegue' do + before { visit new_admin_compte_path } + + it do + fill_in :compte_email, with: 'collegue@exemple.fr' + fill_in :compte_password, with: 'billyjoel' + fill_in :compte_password_confirmation, with: 'billyjoel' + + expect do + click_on 'Créer un compte' + end.to change(Compte, :count) + + compte_cree = Compte.last + expect(compte_cree.structure).to eq ma_structure + expect(compte_cree.role).to eq 'organisation' + end + end end end diff --git a/spec/integrations/ability_spec.rb b/spec/integrations/ability_spec.rb index 2b4378247..c04afffdd 100644 --- a/spec/integrations/ability_spec.rb +++ b/spec/integrations/ability_spec.rb @@ -158,6 +158,7 @@ it { is_expected.to_not be_able_to(:create, Campagne.new) } it { is_expected.to_not be_able_to(:read, SourceAide.new) } it { is_expected.to_not be_able_to(:read, Aide::QuestionFrequente.new) } + it { is_expected.to be_able_to(:create, Compte.new) } it { is_expected.to be_able_to(:read, Question.new) } it { is_expected.to be_able_to(%i[read destroy], evaluation_organisation) } it { is_expected.to be_able_to(:read, evenement_organisation) }