Skip to content

Commit 5117816

Browse files
authored
Username splits names by space and doesn't regex match them (#2950)
* username generates apostrophe * Rubocop fixes * Stripped apostrophe from username * Removed apostrophe matching from username test * Updated regex
1 parent 7dc2f40 commit 5117816

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

lib/faker/default/internet.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ def email(name: nil, separators: nil, domain: nil)
6363
# Faker::Internet.username(specifier: 20, separators: ['_']) #=> "nikki_sawaynnikki_saway"
6464
def username(specifier: nil, separators: %w[. _])
6565
with_locale(:en) do
66-
return shuffle(specifier.scan(/[[:word:]]+/)).join(sample(separators)).downcase if specifier.respond_to?(:scan)
67-
6866
case specifier
67+
when ::String
68+
names = specifier&.gsub("'", '')&.split
69+
shuffled_names = shuffle(names)
70+
71+
return shuffled_names.join(sample(separators)).downcase
6972
when Integer
7073
# If specifier is Integer and has large value, Argument error exception is raised to overcome memory full error
7174
raise ArgumentError, 'Given argument is too large' if specifier > 10**6

test/faker/default/test_faker_internet.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ def test_email_name_with_non_permitted_characters
3636
end
3737
end
3838

39+
def test_email_with_apostrophes
40+
name = "Alexis O'Connell"
41+
42+
deterministically_verify -> { @tester.email(name: name) } do |result|
43+
assert_email_regex 'Alexis', 'OConnell', result
44+
end
45+
end
46+
3947
def test_email_with_separators
4048
deterministically_verify -> { @tester.email(name: 'jane doe', separators: '+') } do |result|
4149
name, domain = result.split('@')
@@ -83,6 +91,10 @@ def test_username
8391
assert_match(/[a-z]+((_|\.)[a-z]+)?/, @tester.username)
8492
end
8593

94+
def test_username_with_apostrophes
95+
assert_match(/\A[a-z]+([_\.][a-z]+)*\z/, @tester.username(specifier: "Alexis O'Connell"))
96+
end
97+
8698
def test_user_name_alias
8799
assert_equal @tester.method(:username), @tester.method(:user_name)
88100
end

test/faker/default/test_faker_omniauth.rb

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_omniauth_google
2020
assert_equal 'google_oauth2', provider
2121
assert_equal 9, auth[:uid].length
2222
assert_equal 2, word_count(info[:name])
23-
assert_match email_regex(info[:first_name], info[:last_name]), info[:email]
23+
assert_email_regex info[:first_name], info[:last_name], info[:email]
2424
assert_equal info[:name].split.first, info[:first_name]
2525
assert_equal info[:name].split.last, info[:last_name]
2626
assert_instance_of String, info[:image]
@@ -62,7 +62,7 @@ def test_omniauth_google_with_name
6262
assert_instance_of String, info[:name]
6363
assert_equal 2, word_count(info[:name])
6464
assert_equal custom_name, info[:name]
65-
assert_match email_regex(first_name, last_name), info[:email]
65+
assert_email_regex first_name, last_name, info[:email]
6666
assert_equal first_name, info[:first_name]
6767
assert_equal last_name, info[:last_name]
6868
assert_equal custom_name, extra_raw_info[:name]
@@ -108,7 +108,7 @@ def test_omniauth_facebook
108108

109109
assert_equal 'facebook', provider
110110
assert_equal 7, uid.length
111-
assert_match email_regex(info[:first_name], info[:last_name]), info[:email]
111+
assert_email_regex info[:first_name], info[:last_name], info[:email]
112112
assert_equal 2, word_count(info[:name])
113113
assert_instance_of String, info[:first_name]
114114
assert_instance_of String, info[:last_name]
@@ -154,7 +154,7 @@ def test_omniauth_facebook_with_name
154154
assert_equal last_name, info[:last_name]
155155
assert_equal last_name, extra_raw_info[:last_name]
156156

157-
assert_match email_regex(first_name, last_name), info[:email]
157+
assert_email_regex first_name, last_name, info[:email]
158158

159159
assert_equal url, extra_raw_info[:link]
160160
assert_equal username, extra_raw_info[:username]
@@ -308,7 +308,7 @@ def test_omniauth_linkedin
308308
assert_equal 'linkedin', auth[:provider]
309309
assert_equal 6, auth[:uid].length
310310
assert_equal 2, word_count(info[:name])
311-
# assert_match email_regex(first_name, last_name), info[:email]
311+
assert_email_regex first_name, last_name, info[:email]
312312
assert_equal info[:name], info[:nickname]
313313
assert_instance_of String, info[:first_name]
314314
assert_instance_of String, info[:last_name]
@@ -341,15 +341,15 @@ def test_omniauth_linkedin
341341
end
342342

343343
def test_omniauth_linkedin_with_name
344-
custom_name = 'Happy Gilmore'
344+
custom_name = "Alexis O'Connell"
345345
first_name, last_name = custom_name.split
346346
auth = @tester.linkedin(name: custom_name)
347347
info = auth[:info]
348348

349349
assert_equal 2, word_count(info[:name])
350350
assert_instance_of String, info[:name]
351351
assert_equal custom_name, info[:name]
352-
assert_match email_regex(first_name, last_name), info[:email]
352+
assert_email_regex first_name, last_name, info[:email]
353353
assert_equal custom_name, info[:nickname]
354354
assert_equal first_name, info[:first_name]
355355
assert_equal last_name, info[:last_name]
@@ -388,7 +388,7 @@ def test_omniauth_github
388388
assert_equal 'github', provider
389389
assert_equal 8, uid.length
390390
assert_equal uid, extra_raw_info[:id]
391-
assert_match email_regex(info[:first_name], info[:last_name]), info[:email]
391+
assert_email_regex info[:first_name], info[:last_name], info[:email]
392392
assert_equal info[:email], extra_raw_info[:email]
393393
assert_equal 2, word_count(name)
394394
assert_instance_of String, name
@@ -438,14 +438,13 @@ def test_omniauth_github_with_name
438438
auth = @tester.github(name: custom_name)
439439
info = auth[:info]
440440
extra_raw_info = auth[:extra][:raw_info]
441-
expected_email_regex = email_regex(info[:first_name], info[:last_name])
442441

443442
assert_equal custom_name, info[:name]
444443
assert_equal 2, word_count(info[:name])
445444
assert_instance_of String, info[:name]
446445
assert_equal custom_name, extra_raw_info[:name]
447-
assert_match expected_email_regex, info[:email]
448-
assert_match expected_email_regex, extra_raw_info[:email]
446+
assert_email_regex info[:first_name], info[:last_name], info[:email]
447+
assert_email_regex info[:first_name], info[:last_name], extra_raw_info[:email]
449448
assert_equal login, info[:nickname]
450449
end
451450

@@ -482,7 +481,7 @@ def test_omniauth_apple
482481
assert_equal 'apple', auth[:provider]
483482
assert_instance_of String, auth[:uid]
484483
assert_equal 44, auth[:uid].length
485-
assert_match email_regex(first_name, last_name), info[:email]
484+
assert_email_regex first_name, last_name, info[:email]
486485
assert_equal auth[:uid], info[:sub]
487486
assert_instance_of String, info[:first_name]
488487
assert_instance_of String, info[:last_name]
@@ -513,7 +512,7 @@ def test_omniauth_auth0
513512
assert_equal 'auth0', auth[:provider]
514513
assert_instance_of String, auth[:uid]
515514
assert_equal 30, auth[:uid].length
516-
assert_match email_regex(first_name, last_name), info[:email]
515+
assert_email_regex first_name, last_name, info[:email]
517516
assert_equal auth[:uid], info[:name]
518517
assert_instance_of String, info[:image]
519518
assert_instance_of String, info[:nickname]
@@ -545,8 +544,4 @@ def boolean?(test)
545544
def gender?(test)
546545
%w[female male].include?(test)
547546
end
548-
549-
def email_regex(first_name, last_name)
550-
/(#{first_name}(.|_)#{last_name}|#{last_name}(.|_)#{first_name})@(.*).(example|test)/i
551-
end
552547
end

test/support/assert_email_regex.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
def assert_email_regex(first_name, last_name, email)
4+
sanitized_first_name = first_name&.gsub("'", '')
5+
sanitized_last_name = last_name&.gsub("'", '')
6+
7+
regex = email_regex(sanitized_first_name, sanitized_last_name)
8+
9+
assert_match(regex, email)
10+
end
11+
12+
def email_regex(first_name, last_name)
13+
/(#{first_name}([_.])?#{last_name}|#{last_name}([_.])?#{first_name})@(.*)\.(example|test)/i
14+
end

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
end
1111

1212
require_relative 'support/assert_not_english'
13+
require_relative 'support/assert_email_regex'
1314
require 'minitest/autorun'
1415
require 'test/unit'
1516
require 'rubygems'

0 commit comments

Comments
 (0)