fix(dossier_transfer): require a valid email

Cf #7621
This commit is contained in:
Colin Darie 2022-08-02 15:20:33 +02:00
parent a5d5f4307f
commit d53aba4d24
3 changed files with 46 additions and 7 deletions

View file

@ -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

View file

@ -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'

View file

@ -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