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 31, 2020
1 parent f9e1161 commit 384e602
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
38 changes: 33 additions & 5 deletions lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,36 @@ def email(legacy_name = NOT_GIVEN, legacy_separators = NOT_GIVEN, name: nil, sep
keywords << :separators if legacy_separators != NOT_GIVEN
end

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

sanitized_local_part = sanitize_email_local_part(local_part)
construct_email(sanitized_local_part, domain_name(domain: domain))
end

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

[username(specifier: name), fetch('internet.free_email')].join('@')
construct_email(
sanitize_email_local_part(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('@')
construct_email(
sanitize_email_local_part(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 +334,25 @@ def base64(length: 16, padding: false, urlsafe: true)
end

alias user_name username

private

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

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

def construct_email(local_part, domain_name)
[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 384e602

Please sign in to comment.