Commit f43e41973f912588d7af23a5575ede94a88d3a5d
1 parent
e57fdc11
Exists in
spb-stable
and in
2 other branches
Test for the apostrophe in the email
Showing
3 changed files
with
32 additions
and
21 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,21 @@ |
| 1 | +# Based on https://github.com/balexand/email_validator | |
| 2 | +# | |
| 3 | +# Extended to use only strict mode with following allowed characters: | |
| 4 | +# ' - apostrophe | |
| 5 | +# | |
| 6 | +# See http://www.remote.org/jochen/mail/info/chars.html | |
| 7 | +# | |
| 8 | +class EmailValidator < ActiveModel::EachValidator | |
| 9 | + @@default_options = {} | |
| 10 | + | |
| 11 | + def self.default_options | |
| 12 | + @@default_options | |
| 13 | + end | |
| 14 | + | |
| 15 | + def validate_each(record, attribute, value) | |
| 16 | + options = @@default_options.merge(self.options) | |
| 17 | + unless value =~ /\A\s*([-a-z0-9+._']{1,64})@((?:[-a-z0-9]+\.)+[a-z]{2,})\s*\z/i | |
| 18 | + record.errors.add(attribute, options[:message] || :invalid) | |
| 19 | + end | |
| 20 | + end | |
| 21 | +end | ... | ... |
lib/gitlab/email_validator.rb
| ... | ... | @@ -1,21 +0,0 @@ |
| 1 | -# Based on https://github.com/balexand/email_validator | |
| 2 | -# | |
| 3 | -# Extended to use only strict mode with following allowed characters: | |
| 4 | -# ' - apostrophe | |
| 5 | -# | |
| 6 | -# See http://www.remote.org/jochen/mail/info/chars.html | |
| 7 | -# | |
| 8 | -class EmailValidator < ActiveModel::EachValidator | |
| 9 | - @@default_options = {} | |
| 10 | - | |
| 11 | - def self.default_options | |
| 12 | - @@default_options | |
| 13 | - end | |
| 14 | - | |
| 15 | - def validate_each(record, attribute, value) | |
| 16 | - options = @@default_options.merge(self.options) | |
| 17 | - unless value =~ /\A\s*([-a-z0-9+._']{1,64})@((?:[-a-z0-9]+\.)+[a-z]{2,})\s*\z/i | |
| 18 | - record.errors.add(attribute, options[:message] || :invalid) | |
| 19 | - end | |
| 20 | - end | |
| 21 | -end |
spec/models/user_spec.rb
| ... | ... | @@ -83,11 +83,17 @@ describe User do |
| 83 | 83 | user = build(:user, email: 'info@example.com') |
| 84 | 84 | expect(user).to be_valid |
| 85 | 85 | end |
| 86 | + | |
| 86 | 87 | it 'accepts info+test@example.com' do |
| 87 | 88 | user = build(:user, email: 'info+test@example.com') |
| 88 | 89 | expect(user).to be_valid |
| 89 | 90 | end |
| 90 | 91 | |
| 92 | + it "accepts o'reilly@example.com" do | |
| 93 | + user = build(:user, email: "o'reilly@example.com") | |
| 94 | + expect(user).to be_valid | |
| 95 | + end | |
| 96 | + | |
| 91 | 97 | it 'rejects test@test@example.com' do |
| 92 | 98 | user = build(:user, email: 'test@test@example.com') |
| 93 | 99 | expect(user).to be_invalid |
| ... | ... | @@ -97,6 +103,11 @@ describe User do |
| 97 | 103 | user = build(:user, email: 'mailto:test@example.com') |
| 98 | 104 | expect(user).to be_invalid |
| 99 | 105 | end |
| 106 | + | |
| 107 | + it "rejects lol!'+=?><#$%^&*()@gmail.com" do | |
| 108 | + user = build(:user, email: "lol!'+=?><#$%^&*()@gmail.com") | |
| 109 | + expect(user).to be_invalid | |
| 110 | + end | |
| 100 | 111 | end |
| 101 | 112 | end |
| 102 | 113 | ... | ... |