Commit 3628eb84c4e50ffb73776683aabd19b37c4cd299
Exists in
spb-stable
and in
3 other branches
Merge pull request #6072 from jvanbaarsen/fix-3516
Better check on the validity of emails
Showing
4 changed files
with
28 additions
and
1 deletions
Show diff stats
Gemfile
| @@ -52,6 +52,9 @@ gem "grape", "~> 0.6.1" | @@ -52,6 +52,9 @@ gem "grape", "~> 0.6.1" | ||
| 52 | gem "grape-entity", "~> 0.3.0" | 52 | gem "grape-entity", "~> 0.3.0" |
| 53 | gem 'rack-cors', require: 'rack/cors' | 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 | # Format dates and times | 58 | # Format dates and times |
| 56 | # based on human-friendly examples | 59 | # based on human-friendly examples |
| 57 | gem "stamp" | 60 | gem "stamp" |
Gemfile.lock
| @@ -114,6 +114,8 @@ GEM | @@ -114,6 +114,8 @@ GEM | ||
| 114 | email_spec (1.5.0) | 114 | email_spec (1.5.0) |
| 115 | launchy (~> 2.1) | 115 | launchy (~> 2.1) |
| 116 | mail (~> 2.2) | 116 | mail (~> 2.2) |
| 117 | + email_validator (1.4.0) | ||
| 118 | + activemodel | ||
| 117 | enumerize (0.7.0) | 119 | enumerize (0.7.0) |
| 118 | activesupport (>= 3.2) | 120 | activesupport (>= 3.2) |
| 119 | equalizer (0.0.8) | 121 | equalizer (0.0.8) |
| @@ -567,6 +569,7 @@ DEPENDENCIES | @@ -567,6 +569,7 @@ DEPENDENCIES | ||
| 567 | devise (= 3.0.4) | 569 | devise (= 3.0.4) |
| 568 | devise-async (= 0.8.0) | 570 | devise-async (= 0.8.0) |
| 569 | email_spec | 571 | email_spec |
| 572 | + email_validator (~> 1.4.0) | ||
| 570 | enumerize | 573 | enumerize |
| 571 | factory_girl_rails | 574 | factory_girl_rails |
| 572 | ffaker | 575 | ffaker |
app/models/user.rb
| @@ -104,7 +104,7 @@ class User < ActiveRecord::Base | @@ -104,7 +104,7 @@ class User < ActiveRecord::Base | ||
| 104 | # Validations | 104 | # Validations |
| 105 | # | 105 | # |
| 106 | validates :name, presence: true | 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 | validates :bio, length: { maximum: 255 }, allow_blank: true | 108 | validates :bio, length: { maximum: 255 }, allow_blank: true |
| 109 | validates :extern_uid, allow_blank: true, uniqueness: {scope: :provider} | 109 | validates :extern_uid, allow_blank: true, uniqueness: {scope: :provider} |
| 110 | validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0} | 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,6 +76,27 @@ describe User do | ||
| 76 | it { should_not allow_value(-1).for(:projects_limit) } | 76 | it { should_not allow_value(-1).for(:projects_limit) } |
| 77 | 77 | ||
| 78 | it { should ensure_length_of(:bio).is_within(0..255) } | 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 | end | 100 | end |
| 80 | 101 | ||
| 81 | describe "Respond to" do | 102 | describe "Respond to" do |