Commit 3628eb84c4e50ffb73776683aabd19b37c4cd299

Authored by Dmitriy Zaporozhets
2 parents 25da1415 05e4af5b

Merge pull request #6072 from jvanbaarsen/fix-3516

Better check on the validity of emails
Gemfile
... ... @@ -52,6 +52,9 @@ gem "grape", "~> 0.6.1"
52 52 gem "grape-entity", "~> 0.3.0"
53 53 gem 'rack-cors', require: 'rack/cors'
54 54  
  55 +# Email validation
  56 +gem "email_validator", "~> 1.4.0", :require => 'email_validator/strict'
  57 +
55 58 # Format dates and times
56 59 # based on human-friendly examples
57 60 gem "stamp"
... ...
Gemfile.lock
... ... @@ -114,6 +114,8 @@ GEM
114 114 email_spec (1.5.0)
115 115 launchy (~> 2.1)
116 116 mail (~> 2.2)
  117 + email_validator (1.4.0)
  118 + activemodel
117 119 enumerize (0.7.0)
118 120 activesupport (>= 3.2)
119 121 equalizer (0.0.8)
... ... @@ -567,6 +569,7 @@ DEPENDENCIES
567 569 devise (= 3.0.4)
568 570 devise-async (= 0.8.0)
569 571 email_spec
  572 + email_validator (~> 1.4.0)
570 573 enumerize
571 574 factory_girl_rails
572 575 ffaker
... ...
app/models/user.rb
... ... @@ -104,7 +104,7 @@ class User < ActiveRecord::Base
104 104 # Validations
105 105 #
106 106 validates :name, presence: true
107   - validates :email, presence: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/ }, uniqueness: true
  107 + validates :email, presence: true, email: {strict_mode: true}, uniqueness: true
108 108 validates :bio, length: { maximum: 255 }, allow_blank: true
109 109 validates :extern_uid, allow_blank: true, uniqueness: {scope: :provider}
110 110 validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0}
... ...
spec/models/user_spec.rb
... ... @@ -76,6 +76,27 @@ describe User do
76 76 it { should_not allow_value(-1).for(:projects_limit) }
77 77  
78 78 it { should ensure_length_of(:bio).is_within(0..255) }
  79 +
  80 + describe 'email' do
  81 + it 'accepts info@example.com' do
  82 + user = build(:user, email: 'info@example.com')
  83 + expect(user).to be_valid
  84 + end
  85 + it 'accepts info+test@example.com' do
  86 + user = build(:user, email: 'info+test@example.com')
  87 + expect(user).to be_valid
  88 + end
  89 +
  90 + it 'rejects test@test@example.com' do
  91 + user = build(:user, email: 'test@test@example.com')
  92 + expect(user).to be_invalid
  93 + end
  94 +
  95 + it 'rejects mailto:test@example.com' do
  96 + user = build(:user, email: 'mailto:test@example.com')
  97 + expect(user).to be_invalid
  98 + end
  99 + end
79 100 end
80 101  
81 102 describe "Respond to" do
... ...