Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Username splits names by space and doesn't regex match them #2950

Merged
merged 9 commits into from
May 28, 2024
7 changes: 5 additions & 2 deletions lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ def email(name: nil, separators: nil, domain: nil)
# Faker::Internet.username(specifier: 20, separators: ['_']) #=> "nikki_sawaynnikki_saway"
def username(specifier: nil, separators: %w[. _])
with_locale(:en) do
return shuffle(specifier.scan(/[[:word:]]+/)).join(sample(separators)).downcase if specifier.respond_to?(:scan)

case specifier
when ::String
names = specifier&.gsub("'", '')&.split
shuffled_names = shuffle(names)

return shuffled_names.join(sample(separators)).downcase
when Integer
# If specifier is Integer and has large value, Argument error exception is raised to overcome memory full error
raise ArgumentError, 'Given argument is too large' if specifier > 10**6
Expand Down
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 @@ -36,6 +36,14 @@ def test_email_name_with_non_permitted_characters
end
end

def test_email_with_apostrophes
name = "Alexis O'Connell"

deterministically_verify -> { @tester.email(name: name) } do |result|
assert_email_regex 'Alexis', 'OConnell', result
end
end

def test_email_with_separators
deterministically_verify -> { @tester.email(name: 'jane doe', separators: '+') } do |result|
name, domain = result.split('@')
Expand Down Expand Up @@ -83,6 +91,10 @@ def test_username
assert_match(/[a-z]+((_|\.)[a-z]+)?/, @tester.username)
end

def test_username_with_apostrophes
assert_match(/\A[a-z]+([_.][a-z]+)*\z/, @tester.username(specifier: "Alexis O'Connell"))
keshavbiswa marked this conversation as resolved.
Show resolved Hide resolved
end

def test_user_name_alias
assert_equal @tester.method(:username), @tester.method(:user_name)
end
Expand Down
29 changes: 12 additions & 17 deletions test/faker/default/test_faker_omniauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_omniauth_google
assert_equal 'google_oauth2', provider
assert_equal 9, auth[:uid].length
assert_equal 2, word_count(info[:name])
assert_match email_regex(info[:first_name], info[:last_name]), info[:email]
assert_email_regex info[:first_name], info[:last_name], info[:email]
assert_equal info[:name].split.first, info[:first_name]
assert_equal info[:name].split.last, info[:last_name]
assert_instance_of String, info[:image]
Expand Down Expand Up @@ -62,7 +62,7 @@ def test_omniauth_google_with_name
assert_instance_of String, info[:name]
assert_equal 2, word_count(info[:name])
assert_equal custom_name, info[:name]
assert_match email_regex(first_name, last_name), info[:email]
assert_email_regex first_name, last_name, info[:email]
assert_equal first_name, info[:first_name]
assert_equal last_name, info[:last_name]
assert_equal custom_name, extra_raw_info[:name]
Expand Down Expand Up @@ -108,7 +108,7 @@ def test_omniauth_facebook

assert_equal 'facebook', provider
assert_equal 7, uid.length
assert_match email_regex(info[:first_name], info[:last_name]), info[:email]
assert_email_regex info[:first_name], info[:last_name], info[:email]
assert_equal 2, word_count(info[:name])
assert_instance_of String, info[:first_name]
assert_instance_of String, info[:last_name]
Expand Down Expand Up @@ -154,7 +154,7 @@ def test_omniauth_facebook_with_name
assert_equal last_name, info[:last_name]
assert_equal last_name, extra_raw_info[:last_name]

assert_match email_regex(first_name, last_name), info[:email]
assert_email_regex first_name, last_name, info[:email]

assert_equal url, extra_raw_info[:link]
assert_equal username, extra_raw_info[:username]
Expand Down Expand Up @@ -308,7 +308,7 @@ def test_omniauth_linkedin
assert_equal 'linkedin', auth[:provider]
assert_equal 6, auth[:uid].length
assert_equal 2, word_count(info[:name])
# assert_match email_regex(first_name, last_name), info[:email]
assert_email_regex first_name, last_name, info[:email]
assert_equal info[:name], info[:nickname]
assert_instance_of String, info[:first_name]
assert_instance_of String, info[:last_name]
Expand Down Expand Up @@ -341,15 +341,15 @@ def test_omniauth_linkedin
end

def test_omniauth_linkedin_with_name
keshavbiswa marked this conversation as resolved.
Show resolved Hide resolved
custom_name = 'Happy Gilmore'
custom_name = "Alexis O'Connell"
first_name, last_name = custom_name.split
auth = @tester.linkedin(name: custom_name)
info = auth[:info]

assert_equal 2, word_count(info[:name])
assert_instance_of String, info[:name]
assert_equal custom_name, info[:name]
assert_match email_regex(first_name, last_name), info[:email]
assert_email_regex first_name, last_name, info[:email]
assert_equal custom_name, info[:nickname]
assert_equal first_name, info[:first_name]
assert_equal last_name, info[:last_name]
Expand Down Expand Up @@ -388,7 +388,7 @@ def test_omniauth_github
assert_equal 'github', provider
assert_equal 8, uid.length
assert_equal uid, extra_raw_info[:id]
assert_match email_regex(info[:first_name], info[:last_name]), info[:email]
assert_email_regex info[:first_name], info[:last_name], info[:email]
assert_equal info[:email], extra_raw_info[:email]
assert_equal 2, word_count(name)
assert_instance_of String, name
Expand Down Expand Up @@ -438,14 +438,13 @@ def test_omniauth_github_with_name
auth = @tester.github(name: custom_name)
info = auth[:info]
extra_raw_info = auth[:extra][:raw_info]
expected_email_regex = email_regex(info[:first_name], info[:last_name])

assert_equal custom_name, info[:name]
assert_equal 2, word_count(info[:name])
assert_instance_of String, info[:name]
assert_equal custom_name, extra_raw_info[:name]
assert_match expected_email_regex, info[:email]
assert_match expected_email_regex, extra_raw_info[:email]
assert_email_regex info[:first_name], info[:last_name], info[:email]
assert_email_regex info[:first_name], info[:last_name], extra_raw_info[:email]
assert_equal login, info[:nickname]
end

Expand Down Expand Up @@ -482,7 +481,7 @@ def test_omniauth_apple
assert_equal 'apple', auth[:provider]
assert_instance_of String, auth[:uid]
assert_equal 44, auth[:uid].length
assert_match email_regex(first_name, last_name), info[:email]
assert_email_regex first_name, last_name, info[:email]
assert_equal auth[:uid], info[:sub]
assert_instance_of String, info[:first_name]
assert_instance_of String, info[:last_name]
Expand Down Expand Up @@ -513,7 +512,7 @@ def test_omniauth_auth0
assert_equal 'auth0', auth[:provider]
assert_instance_of String, auth[:uid]
assert_equal 30, auth[:uid].length
assert_match email_regex(first_name, last_name), info[:email]
assert_email_regex first_name, last_name, info[:email]
assert_equal auth[:uid], info[:name]
assert_instance_of String, info[:image]
assert_instance_of String, info[:nickname]
Expand Down Expand Up @@ -545,8 +544,4 @@ def boolean?(test)
def gender?(test)
%w[female male].include?(test)
end

def email_regex(first_name, last_name)
/(#{first_name}(.|_)#{last_name}|#{last_name}(.|_)#{first_name})@(.*).(example|test)/i
end
end
14 changes: 14 additions & 0 deletions test/support/assert_email_regex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

def assert_email_regex(first_name, last_name, email)
sanitized_first_name = first_name&.gsub("'", '')
sanitized_last_name = last_name&.gsub("'", '')

regex = email_regex(sanitized_first_name, sanitized_last_name)

assert_match(regex, email)
end

def email_regex(first_name, last_name)
/(#{first_name}(.|_)#{last_name}|#{last_name}(.|_)#{first_name})@(.*).(example|test)/i
keshavbiswa marked this conversation as resolved.
Show resolved Hide resolved
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
end

require_relative 'support/assert_not_english'
require_relative 'support/assert_email_regex'
require 'minitest/autorun'
require 'test/unit'
require 'rubygems'
Expand Down
Loading