Skip to content

Commit

Permalink
Stripped apostrophe from username
Browse files Browse the repository at this point in the history
  • Loading branch information
keshavbiswa committed May 9, 2024
1 parent a989db1 commit dcedacb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
8 changes: 3 additions & 5 deletions lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 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
27 changes: 11 additions & 16 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 @@ -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]
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
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

0 comments on commit dcedacb

Please sign in to comment.