From 889940af2a6cccf98976cb8c3746b743273c5719 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Tue, 13 Jun 2023 19:02:18 +0200 Subject: [PATCH] fix(administrateur): renders errors when publication fails --- .../administrateurs/procedures_controller.rb | 7 ++--- .../procedures_controller_spec.rb | 29 +++++++++++++++---- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index af76e6257..860f6e485 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -282,8 +282,6 @@ module Administrateurs if @procedure.publish_or_reopen!(current_administrateur) @procedure.publish_revision! flash.notice = "Démarche publiée" - else - flash.alert = @procedure.errors.full_messages end else @procedure.publish_revision! @@ -291,8 +289,6 @@ module Administrateurs end elsif @procedure.publish_or_reopen!(current_administrateur) flash.notice = "Démarche publiée" - else - flash.alert = @procedure.errors.full_messages end if params[:old_procedure].present? && @procedure.errors.empty? @@ -305,6 +301,9 @@ module Administrateurs end redirect_to admin_procedure_path(@procedure) + rescue ActiveRecord::RecordInvalid + flash.alert = @procedure.errors.full_messages + redirect_to admin_procedure_publication_path(@procedure) end def reset_draft diff --git a/spec/controllers/administrateurs/procedures_controller_spec.rb b/spec/controllers/administrateurs/procedures_controller_spec.rb index 24882f6bc..5002ff100 100644 --- a/spec/controllers/administrateurs/procedures_controller_spec.rb +++ b/spec/controllers/administrateurs/procedures_controller_spec.rb @@ -870,13 +870,15 @@ describe Administrateurs::ProceduresController, type: :controller do let(:procedure3) { create(:procedure, :published, lien_site_web: lien_site_web) } let(:lien_site_web) { 'http://some.administration/' } + subject(:perform_request) { put :publish, params: { procedure_id: procedure.id, path: path, lien_site_web: lien_site_web } } + context 'when admin is the owner of the procedure' do context 'procedure path does not exist' do let(:path) { 'new_path' } let(:lien_site_web) { 'http://mon-site.gouv.fr' } before do - put :publish, params: { procedure_id: procedure.id, path: path, lien_site_web: lien_site_web } + perform_request procedure.reload end @@ -895,7 +897,7 @@ describe Administrateurs::ProceduresController, type: :controller do context 'procedure path exists and is owned by current administrator' do before do - put :publish, params: { procedure_id: procedure.id, path: path, lien_site_web: lien_site_web } + perform_request procedure.reload procedure2.reload end @@ -923,13 +925,22 @@ describe Administrateurs::ProceduresController, type: :controller do context 'procedure path exists and is not owned by current administrator' do let(:path) { procedure3.path } let(:lien_site_web) { 'http://mon-site.gouv.fr' } - it { expect { put :publish, params: { procedure_id: procedure.id, path: path, lien_site_web: lien_site_web } }.to raise_error(ActiveRecord::RecordInvalid) } + + it { + expect { perform_request }.not_to change { procedure.reload.updated_at } + expect(response).to redirect_to(admin_procedure_publication_path(procedure.id)) + expect(flash[:alert]).to have_content "« Lien public » est déjà utilisé" + } end context 'procedure path is invalid' do let(:lien_site_web) { 'http://mon-site.gouv.fr' } let(:path) { 'Invalid Procedure Path' } - it { expect { put :publish, params: { procedure_id: procedure.id, path: path, lien_site_web: lien_site_web } }.to raise_error(ActiveRecord::RecordInvalid) } + + it { + expect { perform_request }.not_to change { procedure.reload.updated_at } + expect(flash[:alert]).to have_content "« Lien public » n’est pas valide" + } end context 'procedure revision is invalid' do @@ -941,7 +952,10 @@ describe Administrateurs::ProceduresController, type: :controller do types_de_champ_public: [{ type: :repetition, children: [] }]) end - it { expect { put :publish, params: { procedure_id: procedure.id, path: path, lien_site_web: lien_site_web } }.to raise_error(ActiveRecord::RecordInvalid) } + it { + expect { perform_request }.not_to change { procedure.reload.updated_at } + expect(flash[:alert]).to have_content "doit comporter au moins un champ" + } end end @@ -964,7 +978,10 @@ describe Administrateurs::ProceduresController, type: :controller do context 'procedure path is valid but lien_site_web is missing' do let(:path) { 'new_path2' } let(:lien_site_web) { nil } - it { expect { put :publish, params: { procedure_id: procedure.id, path: path, lien_site_web: lien_site_web } }.to raise_error(ActiveRecord::RecordInvalid) } + it { + expect { perform_request }.not_to change { procedure.reload.updated_at } + expect(flash[:alert]).to have_content "doit être rempli" + } end end end