[Fix #1479] Validate Avis email format

This commit is contained in:
Mathieu Magnin 2018-02-27 16:58:22 +01:00
parent 247526578c
commit 38e1609d73
3 changed files with 28 additions and 2 deletions

View file

@ -3,6 +3,8 @@ class Avis < ApplicationRecord
belongs_to :gestionnaire
belongs_to :claimant, class_name: 'Gestionnaire'
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
before_save :clean_email
before_create :try_to_assign_gestionnaire
after_create :notify_gestionnaire

View file

@ -108,6 +108,16 @@ describe NewGestionnaire::AvisController, type: :controller do
post :create_avis, params: { id: previous_avis.id, avis: { email: email, introduction: intro, confidentiel: asked_confidentiel } }
end
context 'when an invalid email' do
let(:previous_avis_confidentiel) { false }
let(:asked_confidentiel) { false }
let(:email) { "toto.fr" }
it { expect(response).to render_template :instruction }
it { expect(flash.alert).to eq(["Email n'est pas valide"]) }
it { expect(Avis.last).to eq(previous_avis) }
end
context 'when the previous avis is public' do
let(:previous_avis_confidentiel) { false }

View file

@ -337,20 +337,34 @@ describe NewGestionnaire::DossiersController, type: :controller do
describe "#create_avis" do
let(:saved_avis) { dossier.avis.first }
before do
subject do
post :create_avis, params: {
procedure_id: procedure.id,
dossier_id: dossier.id,
avis: { email: 'email@a.com', introduction: 'intro', confidentiel: true }
avis: { email: email, introduction: 'intro', confidentiel: true }
}
end
before do
subject
end
let(:email) { 'email@a.com' }
it { expect(saved_avis.email).to eq('email@a.com') }
it { expect(saved_avis.introduction).to eq('intro') }
it { expect(saved_avis.confidentiel).to eq(true) }
it { expect(saved_avis.dossier).to eq(dossier) }
it { expect(saved_avis.claimant).to eq(gestionnaire) }
it { expect(response).to redirect_to(avis_gestionnaire_dossier_path(dossier.procedure, dossier)) }
context "with an invalid email" do
let(:email) { 'emaila.com' }
it { expect(response).to render_template :avis }
it { expect(flash.alert).to eq(["Email n'est pas valide"]) }
it { expect { subject }.not_to change(Avis, :count) }
end
end
describe "#update_annotations" do