Commit 05e4af5b4c7709ab08194d109ddec8e19f44758d
1 parent
dba98240
Exists in
spb-stable
and in
3 other branches
Better check on the validity of emails
At this moment it was possible to enter emails like: mailto:info@example.com. This was causing some issue in the frontend, since those links became html mailto: links. Fixes: #3516
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
@@ -103,7 +103,7 @@ class User < ActiveRecord::Base | @@ -103,7 +103,7 @@ class User < ActiveRecord::Base | ||
103 | # Validations | 103 | # Validations |
104 | # | 104 | # |
105 | validates :name, presence: true | 105 | validates :name, presence: true |
106 | - validates :email, presence: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/ }, uniqueness: true | 106 | + validates :email, presence: true, email: {strict_mode: true}, uniqueness: true |
107 | validates :bio, length: { maximum: 255 }, allow_blank: true | 107 | validates :bio, length: { maximum: 255 }, allow_blank: true |
108 | validates :extern_uid, allow_blank: true, uniqueness: {scope: :provider} | 108 | validates :extern_uid, allow_blank: true, uniqueness: {scope: :provider} |
109 | validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0} | 109 | validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0} |
spec/models/user_spec.rb
@@ -74,6 +74,27 @@ describe User do | @@ -74,6 +74,27 @@ describe User do | ||
74 | it { should_not allow_value(-1).for(:projects_limit) } | 74 | it { should_not allow_value(-1).for(:projects_limit) } |
75 | 75 | ||
76 | it { should ensure_length_of(:bio).is_within(0..255) } | 76 | it { should ensure_length_of(:bio).is_within(0..255) } |
77 | + | ||
78 | + describe 'email' do | ||
79 | + it 'accepts info@example.com' do | ||
80 | + user = build(:user, email: 'info@example.com') | ||
81 | + expect(user).to be_valid | ||
82 | + end | ||
83 | + it 'accepts info+test@example.com' do | ||
84 | + user = build(:user, email: 'info+test@example.com') | ||
85 | + expect(user).to be_valid | ||
86 | + end | ||
87 | + | ||
88 | + it 'rejects test@test@example.com' do | ||
89 | + user = build(:user, email: 'test@test@example.com') | ||
90 | + expect(user).to be_invalid | ||
91 | + end | ||
92 | + | ||
93 | + it 'rejects mailto:test@example.com' do | ||
94 | + user = build(:user, email: 'mailto:test@example.com') | ||
95 | + expect(user).to be_invalid | ||
96 | + end | ||
97 | + end | ||
77 | end | 98 | end |
78 | 99 | ||
79 | describe "Respond to" do | 100 | describe "Respond to" do |