[Fix #1479] Validate email format with Rails format validation

This commit is contained in:
Mathieu Magnin 2018-02-26 14:51:25 +01:00
parent b7de632d6c
commit e00e8ba01d
2 changed files with 32 additions and 1 deletions

View file

@ -5,5 +5,5 @@ class Invite < ApplicationRecord
validates :email, presence: true
validates :email, uniqueness: { scope: :dossier_id }
validates :email, email_format: true
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
end

View file

@ -24,5 +24,36 @@ describe Invite do
it { expect{ subject }.to raise_error ActiveRecord::RecordInvalid }
end
context "email validation" do
let(:invite) { build(:invite, email: email, dossier: dossier1) }
context 'when an email is invalid' do
let(:email) { 'toto.fr' }
it do
expect(invite.save).to be false
expect(invite.errors.full_messages).to eq(["Email n'est pas valide"])
end
context 'when an email is empty' do
let(:email) { nil }
it do
expect(invite.save).to be false
expect(invite.errors.full_messages).to eq(["Email est vide"])
end
end
end
context 'when an email is valid' do
let(:email) { 'toto@toto.fr' }
it do
expect(invite.save).to be true
expect(invite.errors.full_messages).to eq([])
end
end
end
end
end