Skip to content

Commit

Permalink
updated email_regexp and added test cases (#4001)
Browse files Browse the repository at this point in the history
Add a more permissive default e-mail regex.
  • Loading branch information
kimgb authored and ulissesalmeida committed Apr 26, 2016
1 parent 209b97d commit 830d3e8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 24 deletions.
22 changes: 5 additions & 17 deletions lib/devise.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,11 @@ def strip_whitespace_keys=(strip_whitespace_keys)
mattr_accessor :http_authentication_realm
@@http_authentication_realm = "Application"

# Email regex used to validate email formats. It simply asserts that
# an one (and only one) @ exists in the given string. This is mainly
# to give user feedback and not to assert the e-mail validity.
# TODO: 4.1 Do: @@email_regexp = [/\A[^@\s]+@[^@\s]+\z/]
mattr_reader :email_regexp
@@email_regexp = /\A[^@\s]+@([^@\s]+\.)+[^@\W]+\z/

def self.email_regexp=(email_regexp)
app_set_configs << :email_regexp
@@email_regexp = email_regexp
end

def email_regexp=(email_regexp)
app_set_configs << :email_regexp
@@email_regexp = email_regexp
end
# Email regex used to validate email formats. It asserts that there are no
# @ symbols or whitespaces in either the localpart or the domain, and that
# there is a single @ symbol separating the localpart and the domain.
mattr_accessor :email_regexp
@@email_regexp = /\A[^@\s]+@[^@\s]+\z/

# Range validation for password length
mattr_accessor :password_length
Expand Down Expand Up @@ -341,7 +330,6 @@ def sign_out_via=(sign_out_via)
def self.setup
yield self

warn_default_config_changed(:email_regexp, '/\A[^@\s]+@([^@\s]+\.)+[^@\W]+\z/', '/\A[^@\s]+@[^@\s]+\z/')
warn_default_config_changed(:reconfirmable, 'false', 'true')
warn_default_config_changed(:sign_out_via, ':get', ':delete')
warn_default_config_changed(:skip_session_storage, '[]', '[:http_auth]')
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/templates/devise.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
# Email regex used to validate email formats. It simply asserts that
# one (and only one) @ exists in the given string. This is mainly
# to give user feedback and not to assert the e-mail validity.
config.email_regexp = /\A[^@]+@[^@]+\z/
config.email_regexp = /\A[^@\s]+@[^@\s]+\z/

# ==> Configuration for :timeoutable
# The time you want to timeout the user session without activity. After this
Expand Down
8 changes: 3 additions & 5 deletions test/devise_test.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class DeviseTest < ActiveSupport::TestCase

test 'setup block warns about defaults changing' do
Devise.app_set_configs = Set.new

ActiveSupport::Deprecation.expects(:warn).with() { |value| value =~ /email_regexp/ }

ActiveSupport::Deprecation.expects(:warn).with() { |value| value =~ /reconfirmable/ }
ActiveSupport::Deprecation.expects(:warn).with() { |value| value =~ /sign_out_via/ }
ActiveSupport::Deprecation.expects(:warn).with() { |value| value =~ /skip_session_storage/ }
Expand All @@ -52,7 +51,6 @@ class DeviseTest < ActiveSupport::TestCase
ActiveSupport::Deprecation.expects(:warn).never

swap Devise,
email_regexp: /@/,
reconfirmable: false,
sign_out_via: :get,
skip_session_storage: [],
Expand Down Expand Up @@ -121,8 +119,8 @@ class DeviseTest < ActiveSupport::TestCase
end

test 'Devise.email_regexp should match valid email addresses' do
valid_emails = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
non_valid_emails = ["rex", "test@go,com", "test [email protected]", "test_user@example server.com", "[email protected]."]
valid_emails = ["[email protected]", "[email protected]", "[email protected]", "[email protected]", "test@tt", "[email protected]"]
non_valid_emails = ["rex", "test [email protected]", "test_user@example server.com"]

valid_emails.each do |email|
assert_match Devise.email_regexp, email
Expand Down
2 changes: 1 addition & 1 deletion test/models/validatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ValidatableTest < ActiveSupport::TestCase
assert user.invalid?
assert_not_equal 'is invalid', user.errors[:email].join

%w{invalid_email_format 123 $$$ () bla@bla.}.each do |email|
%w{invalid_email_format 123 $$$ () }.each do |email|
user.email = email
assert user.invalid?, 'should be invalid with email ' << email
assert_equal 'is invalid', user.errors[:email].join
Expand Down

0 comments on commit 830d3e8

Please sign in to comment.