From d53aba4d248e73b3f293fbb1ad33cddcfc5c7b1c Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Tue, 2 Aug 2022 15:20:33 +0200 Subject: [PATCH] fix(dossier_transfer): require a valid email Cf #7621 --- app/controllers/users/transfers_controller.rb | 22 ++++++++++---- app/views/users/dossiers/transferer.html.haml | 2 +- .../users/transfers_controller_spec.rb | 29 +++++++++++++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/app/controllers/users/transfers_controller.rb b/app/controllers/users/transfers_controller.rb index 285e0f7a3..8a82a90b8 100644 --- a/app/controllers/users/transfers_controller.rb +++ b/app/controllers/users/transfers_controller.rb @@ -2,8 +2,14 @@ module Users class TransfersController < UserController def create transfer = DossierTransfer.new(transfer_params) - transfer.save! - redirect_to dossiers_path + + if transfer.valid? + transfer.save! + redirect_to dossiers_path + else + flash.alert = transfer.errors.full_messages + redirect_to transferer_dossier_path(transfer_params[:dossiers].first) + end end def update @@ -23,12 +29,16 @@ module Users private def transfer_params - transfer_params = params.require(:dossier_transfer).permit(:email, :dossiers) - if transfer_params[:dossiers].present? - transfer_params.merge(dossiers: [current_user.dossiers.find(transfer_params[:dossiers])]) + transfer_params = params.require(:dossier_transfer).permit(:email, :dossier) + + dossier_id = transfer_params.delete(:dossier) + dossiers = if dossier_id.present? + [current_user.dossiers.find(dossier_id)] else - transfer_params.merge(dossiers: current_user.dossiers) + current_user.dossiers end + + transfer_params.merge(dossiers: dossiers) end end end diff --git a/app/views/users/dossiers/transferer.html.haml b/app/views/users/dossiers/transferer.html.haml index 6966f85c2..8b2e955cf 100644 --- a/app/views/users/dossiers/transferer.html.haml +++ b/app/views/users/dossiers/transferer.html.haml @@ -5,5 +5,5 @@ = form_for @transfer, url: transfers_path, html: { class: 'form mt-2' } do |f| = f.label :email, 'Email du compte destinataire' = f.email_field :email - = f.hidden_field :dossiers, value: dossier.id + = f.hidden_field :dossier, value: dossier.id = f.submit "Envoyer la demande de transfert", class: 'button primary' diff --git a/spec/controllers/users/transfers_controller_spec.rb b/spec/controllers/users/transfers_controller_spec.rb index dd13c32b6..9c8809063 100644 --- a/spec/controllers/users/transfers_controller_spec.rb +++ b/spec/controllers/users/transfers_controller_spec.rb @@ -13,4 +13,33 @@ describe Users::TransfersController, type: :controller do it { expect { dossier_transfert.reload }.to raise_error(ActiveRecord::RecordNotFound) } end + + describe "POST create" do + subject { post :create, params: { dossier_transfer: { email: email, dossier: dossier.id } } } + + before { subject } + + context "with valid email" do + let(:email) { "test@rspec.net" } + + it { expect(DossierTransfer.last.email).to eq(email) } + it { expect(DossierTransfer.last.dossiers).to eq([dossier]) } + end + + shared_examples 'email error' do + it { expect { subject }.not_to change { DossierTransfer.count } } + it { expect(flash.alert).to match([/invalide/]) } + it { is_expected.to redirect_to transferer_dossier_path(dossier.id) } + end + + context "when email is empty" do + let(:email) { "" } + it_behaves_like 'email error' + end + + context "when email is invalid" do + let(:email) { "not-an-email" } + it_behaves_like 'email error' + end + end end