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

Internet personal account #1744

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/default/internet.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,18 @@ Faker::Internet.user_agent #=> "Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7; AOLB
Faker::Internet.user_agent(vendor: :firefox) #=> "Mozilla/5.0 (Windows NT x.y; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0"

Faker::Internet.uuid #=> "929ef6ef-b11f-38c9-111b-accd67a258b2"

Faker::Internet.personal_account #=> {:username=>"sabina", :password=>"Yh3Oz73Na4LaFx", :free_email=>"[email protected]", :safe_email=>"[email protected]", :email=>"[email protected]"}

# Keyword arguments: options
Faker::Internet.personal_account({username: {specifier: 'Nancy'}}) #=> {:username=>"nancy", :password=>"UaGbCf5OdLxV", :free_email=>"[email protected]", :safe_email=>"[email protected]", :email=>"[email protected]"}

# Keyword arguments: attributes
Faker::Internet.personal_account(:username, 'password') #=> {:username=>"brant", :password=>"Hr85Wf4L1uQpHl"}

# Keyword arguments: attributes, options
Faker::Internet.personal_account(:username, :safe_email, :free_email, :email, :password, {username: {specifier: 8}, safe_email: {name: 'Nancy'}, free_email: {name: 'Nancy'}, email: {name: 'Janelle Santiago', separators: '+'}, password: {min_length: 10, max_length: 20, mix_case: true, special_characters: true}}) #=> {:username=>"willettewillette", :safe_email=>"[email protected]", :free_email=>"[email protected]", :email=>"[email protected]", :password=>"!!&*$%Rt74261I41z"}



```
16 changes: 16 additions & 0 deletions lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,22 @@ def uuid
'%08x-%04x-%04x-%04x-%04x%08x' % ary # rubocop:disable Style/FormatString
end

# email => name, separators
# free_email => name
# safe_email => name
# username => specifier, separators, min_length, max_length
# password => min_length, max_length, mix_case, special_characters
AmrAdelKhalil marked this conversation as resolved.
Show resolved Hide resolved
def personal_account(*attributes, **options)
attributes = %i[username password free_email safe_email email] if attributes.length.zero?
attributes = attributes.map &:to_sym
options = options.inject({}) do |symbolized_options,(attr, attr_options)|
attr_options = attr_options.inject({}) {|new_options, (key, value)| new_options[key.to_sym] = value; new_options}
symbolized_options[attr.to_sym] = attr_options
symbolized_options
end
Hash[attributes.map{ |attribute| [attribute, send(attribute.to_sym, **(options[attribute.to_sym] || {}))] }]
end

alias user_name username
end
end
Expand Down
20 changes: 20 additions & 0 deletions test/faker/default/test_faker_internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,24 @@ def test_uuid
assert_equal(36, uuid.size)
assert_match(/\A\h{8}-\h{4}-4\h{3}-\h{4}-\h{12}\z/, uuid)
end

def test_user_with_args
user = @tester.personal_account(:username, 'email', 'password', :free_email, :safe_email, {password: {min_length: 3, max_length: 5}})
assert user[:email].match(/.+@.+\.\w+/)
assert (3..5).include?(user[:password].length)
assert user[:username]
assert user[:free_email]
assert user[:safe_email]
end

def test_user_without_args
user = @tester.personal_account
assert user[:email].match(/.+@.+\.\w+/)
end

def test_user_with_invalid_args
assert_raises NoMethodError do
@tester.personal_account('xyz')
end
end
end