diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index 005498b53..7ae605da6 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -51,6 +51,17 @@ class Admin::ProceduresController < AdminController redirect_to admin_procedures_draft_path end + def destroy + procedure = current_administrateur.procedures.find(params[:id]) + + return render json: {}, status: 401 if procedure.publiee_ou_archivee? + + procedure.destroy + + flash.notice = 'Procédure supprimée' + redirect_to admin_procedures_draft_path + end + def new @procedure ||= Procedure.new @procedure.module_api_carto ||= ModuleAPICarto.new diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index 8942bfd11..941929b80 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -54,6 +54,54 @@ describe Admin::ProceduresController, type: :controller do it { expect(response.status).to eq(200) } end + describe 'DELETE #destroy' do + let(:procedure_draft) { create :procedure, administrateur: admin, published_at: nil, archived_at: nil } + let(:procedure_published) { create :procedure, administrateur: admin, published_at: Time.now, archived_at: nil } + let(:procedure_archived) { create :procedure, administrateur: admin, published_at: nil, archived_at: Time.now } + + subject { delete :destroy, params: {id: procedure.id} } + + context 'when procedure is draft' do + let!(:procedure) { procedure_draft } + + describe 'tech params' do + before do + subject + end + + it { expect(subject.status).to eq 302 } + it { expect(flash[:notice]).to be_present } + end + + it 'destroy procedure is call' do + expect_any_instance_of(Procedure).to receive(:destroy) + subject + end + + it { expect { subject }.to change { Procedure.count }.by(-1) } + end + + context 'when procedure is published' do + let(:procedure) { procedure_published } + + it { expect(subject.status).to eq 401 } + end + + context 'when procedure is archived' do + let(:procedure) { procedure_published } + + it { expect(subject.status).to eq 401 } + end + + context "when administrateur does not own the procedure" do + let(:procedure_not_owned) { create :procedure, administrateur: create(:administrateur), published_at: nil, archived_at: nil } + + subject { delete :destroy, params: {id: procedure_not_owned.id} } + + it { expect{ subject }.to raise_error(ActiveRecord::RecordNotFound) } + end + end + describe 'GET #edit' do let(:published_at) { nil } let(:procedure) { create(:procedure, administrateur: admin, published_at: published_at) }