Skip to content

Commit

Permalink
Sanitize email when name has special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Jaime committed May 30, 2020
1 parent f9e1161 commit 54543c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ def email(legacy_name = NOT_GIVEN, legacy_separators = NOT_GIVEN, name: nil, sep
end

if separators
[username(specifier: name, separators: separators), domain_name(domain: domain)].join('@')
sanitize_email(username(specifier: name, separators: separators), domain_name(domain: domain))
else
[username(specifier: name), domain_name(domain: domain)].join('@')
sanitize_email(username(specifier: name), domain_name(domain: domain))
end
end

Expand All @@ -21,15 +21,15 @@ def free_email(legacy_name = NOT_GIVEN, name: nil)
keywords << :name if legacy_name != NOT_GIVEN
end

[username(specifier: name), fetch('internet.free_email')].join('@')
sanitize_email(username(specifier: name), fetch('internet.free_email'))
end

def safe_email(legacy_name = NOT_GIVEN, name: nil)
warn_for_deprecated_arguments do |keywords|
keywords << :name if legacy_name != NOT_GIVEN
end

[username(specifier: name), 'example.' + sample(%w[org com net])].join('@')
sanitize_email(username(specifier: name), 'example.' + sample(%w[org com net]))
end

def username(legacy_specifier = NOT_GIVEN, legacy_separators = NOT_GIVEN, specifier: nil, separators: %w[. _])
Expand Down Expand Up @@ -325,6 +325,23 @@ def base64(length: 16, padding: false, urlsafe: true)
end

alias user_name username

private

def sanitize_email(local_part, domain_name)
char_range = [
Array('0'..'9'),
Array('A'..'Z'),
Array('a'..'z'),
"!#$%&'*+-/=?^_`{|}~.".split(//)
].flatten

sanitized_local_part = local_part.split(//).map do |char|
char_range.include?(char) ? char : '#'
end.join

[sanitized_local_part, domain_name].join('@')
end
end
end
end
12 changes: 12 additions & 0 deletions test/faker/default/test_faker_internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def test_email
assert @tester.email.match(/.+@.+\.\w+/)
end

def test_email_with_non_permitted_characters
assert @tester.email(name: 'martín').match(/mart#n@.+\.\w+/)
end

def test_email_with_separators
assert @tester.email(name: 'jane doe', separators: '+').match(/.+\+.+@.+\.\w+/)
end
Expand All @@ -27,10 +31,18 @@ def test_free_email
assert @tester.free_email.match(/.+@(gmail|hotmail|yahoo)\.com/)
end

def test_free_email_with_non_permitted_characters
assert @tester.free_email(name: 'martín').match(/mart#n@.+\.\w+/)
end

def test_safe_email
assert @tester.safe_email.match(/.+@example.(com|net|org)/)
end

def test_safe_email_with_non_permitted_characters
assert @tester.safe_email(name: 'martín').match(/mart#n@.+\.\w+/)
end

def test_username
assert @tester.username(specifier: 0..3).match(/[a-z]+((_|\.)[a-z]+)?/)
assert @tester.username.match(/[a-z]+((_|\.)[a-z]+)?/)
Expand Down

0 comments on commit 54543c5

Please sign in to comment.