From 41ad89d8acd483d63940db57cbd49cd025ccaff1 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 4 Apr 2019 12:03:03 +0000 Subject: [PATCH] commencer: fix redirection with invalid path --- app/controllers/users/commencer_controller.rb | 42 ++++++++++++------- config/locales/fr.yml | 1 + .../users/commencer_controller_spec.rb | 24 +++++++++++ 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/app/controllers/users/commencer_controller.rb b/app/controllers/users/commencer_controller.rb index a20a009fe..9a16ee2aa 100644 --- a/app/controllers/users/commencer_controller.rb +++ b/app/controllers/users/commencer_controller.rb @@ -4,38 +4,39 @@ module Users def commencer @procedure = Procedure.publiees.find_by(path: params[:path]) - - if @procedure.blank? - flash.alert = "La démarche est inconnue, ou la création de nouveaux dossiers pour cette démarche est terminée." - return redirect_to root_path - end + return procedure_not_found if @procedure.blank? render 'commencer/show' end def commencer_test @procedure = Procedure.brouillons.find_by(path: params[:path]) - - if @procedure.blank? - flash.alert = "La démarche est inconnue, ou cette démarche n’est maintenant plus en test." - return redirect_to root_path - end + return procedure_not_found if @procedure.blank? render 'commencer/show' end def sign_in - store_user_location! + @procedure = Procedure.find_by(path: params[:path]) + return procedure_not_found if @procedure.blank? + + store_user_location!(@procedure) redirect_to new_user_session_path end def sign_up - store_user_location! + @procedure = Procedure.find_by(path: params[:path]) + return procedure_not_found if @procedure.blank? + + store_user_location!(@procedure) redirect_to new_user_registration_path end def france_connect - store_user_location! + @procedure = Procedure.find_by(path: params[:path]) + return procedure_not_found if @procedure.blank? + + store_user_location!(@procedure) redirect_to france_connect_particulier_path end @@ -45,8 +46,21 @@ module Users private - def store_user_location! + def procedure_not_found procedure = Procedure.find_by(path: params[:path]) + + if procedure&.archivee? + flash.alert = t('errors.messages.procedure_archived') + elsif procedure&.publiee? + flash.alert = t('errors.messages.procedure_not_draft') + else + flash.alert = t('errors.messages.procedure_not_found') + end + + return redirect_to root_path + end + + def store_user_location!(procedure) store_location_for(:user, helpers.procedure_lien(procedure)) end end diff --git a/config/locales/fr.yml b/config/locales/fr.yml index f1a446173..c7aa81890 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -188,6 +188,7 @@ fr: connexion: "Erreur lors de la connexion à France Connect." extension_white_list_error: "Le format de fichier de la pièce jointe n'est pas valide." procedure_archived: "Cette démarche en ligne a été fermée, il n'est plus possible de déposer de dossier." + procedure_not_draft: "Cette démarche n’est maintenant plus en brouillon." cadastres_empty: one: "Aucune parcelle cadastrale sur la zone séléctionnée" other: "Aucune parcelle cadastrale sur les zones séléctionnées" diff --git a/spec/controllers/users/commencer_controller_spec.rb b/spec/controllers/users/commencer_controller_spec.rb index ae8f2d01a..539cfaa7c 100644 --- a/spec/controllers/users/commencer_controller_spec.rb +++ b/spec/controllers/users/commencer_controller_spec.rb @@ -87,6 +87,14 @@ describe Users::CommencerController, type: :controller do it { expect(subject).to redirect_to(new_user_session_path) } end + + context 'when the path doesn’t exist' do + subject { get :sign_in, params: { path: 'hello' } } + + it 'redirects with an error message' do + expect(subject).to redirect_to(root_path) + end + end end describe '#sign_up' do @@ -111,6 +119,14 @@ describe Users::CommencerController, type: :controller do it { expect(subject).to redirect_to(new_user_registration_path) } end + + context 'when the path doesn’t exist' do + subject { get :sign_up, params: { path: 'hello' } } + + it 'redirects with an error message' do + expect(subject).to redirect_to(root_path) + end + end end describe '#france_connect' do @@ -135,5 +151,13 @@ describe Users::CommencerController, type: :controller do it { expect(subject).to redirect_to(france_connect_particulier_path) } end + + context 'when the path doesn’t exist' do + subject { get :france_connect, params: { path: 'hello' } } + + it 'redirects with an error message' do + expect(subject).to redirect_to(root_path) + end + end end end