From 6d0ef12468c76a9158dd5c6e8b955c86ed0ea622 Mon Sep 17 00:00:00 2001 From: Keshav Biswa Date: Tue, 7 May 2024 22:35:00 +0530 Subject: [PATCH 1/5] username generates apostrophe --- lib/faker/default/internet.rb | 7 ++++++- test/faker/default/test_faker_internet.rb | 4 ++++ test/faker/default/test_faker_omniauth.rb | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/faker/default/internet.rb b/lib/faker/default/internet.rb index 3ecdcd9838..b3577ee604 100644 --- a/lib/faker/default/internet.rb +++ b/lib/faker/default/internet.rb @@ -63,7 +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) + if specifier.respond_to?(:scan) + names = specifier&.split + shuffled_names = shuffle(names) + + return shuffled_names.join(sample(separators)).downcase + end case specifier when Integer diff --git a/test/faker/default/test_faker_internet.rb b/test/faker/default/test_faker_internet.rb index f8a2a6574c..d5b3af192e 100644 --- a/test/faker/default/test_faker_internet.rb +++ b/test/faker/default/test_faker_internet.rb @@ -83,6 +83,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")) + end + def test_user_name_alias assert_equal @tester.method(:username), @tester.method(:user_name) end diff --git a/test/faker/default/test_faker_omniauth.rb b/test/faker/default/test_faker_omniauth.rb index f0c5400c4e..b9f4f6ea9c 100644 --- a/test/faker/default/test_faker_omniauth.rb +++ b/test/faker/default/test_faker_omniauth.rb @@ -341,7 +341,7 @@ def test_omniauth_linkedin end def test_omniauth_linkedin_with_name - 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] From bc49f6777f519d1c6a69c909685f238012fd48ed Mon Sep 17 00:00:00 2001 From: Keshav Biswa Date: Tue, 7 May 2024 22:40:00 +0530 Subject: [PATCH 2/5] Rubocop fixes --- lib/faker/default/internet.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/faker/default/internet.rb b/lib/faker/default/internet.rb index b3577ee604..61b1bc1340 100644 --- a/lib/faker/default/internet.rb +++ b/lib/faker/default/internet.rb @@ -67,7 +67,7 @@ def username(specifier: nil, separators: %w[. _]) names = specifier&.split shuffled_names = shuffle(names) - return shuffled_names.join(sample(separators)).downcase + return shuffled_names.join(sample(separators)).downcase end case specifier From dcedacb338ed782946686fcacc38c67a0d0e1d93 Mon Sep 17 00:00:00 2001 From: Keshav Biswa Date: Thu, 9 May 2024 13:14:52 +0530 Subject: [PATCH 3/5] Stripped apostrophe from username --- lib/faker/default/internet.rb | 8 +++---- test/faker/default/test_faker_internet.rb | 8 +++++++ test/faker/default/test_faker_omniauth.rb | 27 +++++++++-------------- test/support/assert_email_regex.rb | 14 ++++++++++++ test/test_helper.rb | 1 + 5 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 test/support/assert_email_regex.rb diff --git a/lib/faker/default/internet.rb b/lib/faker/default/internet.rb index 61b1bc1340..688b9f8976 100644 --- a/lib/faker/default/internet.rb +++ b/lib/faker/default/internet.rb @@ -63,14 +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 - if specifier.respond_to?(:scan) - names = specifier&.split + case specifier + when ::String + names = specifier&.gsub("'", '')&.split shuffled_names = shuffle(names) return shuffled_names.join(sample(separators)).downcase - end - - case specifier 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 diff --git a/test/faker/default/test_faker_internet.rb b/test/faker/default/test_faker_internet.rb index d5b3af192e..a3a475c160 100644 --- a/test/faker/default/test_faker_internet.rb +++ b/test/faker/default/test_faker_internet.rb @@ -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('@') diff --git a/test/faker/default/test_faker_omniauth.rb b/test/faker/default/test_faker_omniauth.rb index 252078fd5a..1cfba9126d 100644 --- a/test/faker/default/test_faker_omniauth.rb +++ b/test/faker/default/test_faker_omniauth.rb @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] @@ -349,7 +349,7 @@ def test_omniauth_linkedin_with_name 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] @@ -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 @@ -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 @@ -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] @@ -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] @@ -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 diff --git a/test/support/assert_email_regex.rb b/test/support/assert_email_regex.rb new file mode 100644 index 0000000000..10616805b8 --- /dev/null +++ b/test/support/assert_email_regex.rb @@ -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 +end diff --git a/test/test_helper.rb b/test/test_helper.rb index fab1465b52..5d615e92e6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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' From 05c0feff5216edfc69366228b526bdd65f6b9d5e Mon Sep 17 00:00:00 2001 From: Keshav Biswa Date: Thu, 9 May 2024 13:20:59 +0530 Subject: [PATCH 4/5] Removed apostrophe matching from username test --- test/faker/default/test_faker_internet.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/faker/default/test_faker_internet.rb b/test/faker/default/test_faker_internet.rb index a3a475c160..b3b58f55b3 100644 --- a/test/faker/default/test_faker_internet.rb +++ b/test/faker/default/test_faker_internet.rb @@ -92,7 +92,7 @@ def test_username end def test_username_with_apostrophes - assert_match(/\A[a-z']+([_.][a-z']+)*\z/, @tester.username(specifier: "Alexis O'Connell")) + assert_match(/\A[a-z]+([_.][a-z]+)*\z/, @tester.username(specifier: "Alexis O'Connell")) end def test_user_name_alias From ffd200b7c51e09b9bffc5722231aa3a5671802f2 Mon Sep 17 00:00:00 2001 From: Keshav Biswa Date: Wed, 15 May 2024 22:02:00 +0530 Subject: [PATCH 5/5] Updated regex --- test/faker/default/test_faker_internet.rb | 2 +- test/support/assert_email_regex.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/faker/default/test_faker_internet.rb b/test/faker/default/test_faker_internet.rb index b3b58f55b3..129640a71a 100644 --- a/test/faker/default/test_faker_internet.rb +++ b/test/faker/default/test_faker_internet.rb @@ -92,7 +92,7 @@ def test_username end def test_username_with_apostrophes - assert_match(/\A[a-z]+([_.][a-z]+)*\z/, @tester.username(specifier: "Alexis O'Connell")) + assert_match(/\A[a-z]+([_\.][a-z]+)*\z/, @tester.username(specifier: "Alexis O'Connell")) end def test_user_name_alias diff --git a/test/support/assert_email_regex.rb b/test/support/assert_email_regex.rb index 10616805b8..054906df37 100644 --- a/test/support/assert_email_regex.rb +++ b/test/support/assert_email_regex.rb @@ -10,5 +10,5 @@ def assert_email_regex(first_name, last_name, email) end def email_regex(first_name, last_name) - /(#{first_name}(.|_)#{last_name}|#{last_name}(.|_)#{first_name})@(.*).(example|test)/i + /(#{first_name}([_.])?#{last_name}|#{last_name}([_.])?#{first_name})@(.*)\.(example|test)/i end