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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/default/internet.md
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=>"viola.block@hotmail.com", :safe_email=>"shanti@example.org", :email=>"nella@schaefer.co"}

# Keyword arguments: options
Faker::Internet.personal_account({username: {specifier: 'Nancy'}}) #=> {:username=>"nancy", :password=>"UaGbCf5OdLxV", :free_email=>"antonia@yahoo.com", :safe_email=>"lavern_crooks@example.net", :email=>"lila_gottlieb@haley.biz"}

# 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=>"nancy@example.net", :free_email=>"nancy@hotmail.com", :email=>"santiago+janelle@thiel.com", :password=>"!!&*$%Rt74261I41z"}



```
40 changes: 40 additions & 0 deletions lib/faker/default/internet.rb
Expand Up @@ -282,6 +282,46 @@ def uuid
'%08x-%04x-%04x-%04x-%04x%08x' % ary # rubocop:disable Style/FormatString
end

##
# Produces a hash of personal account attributes
#
# @param attributes [Array] array of required fields
#
# @param options [Hash] apply specific constraints on the data returned
#
#
# @return [Hash]
#
# @example
# Faker::Internet.personal_account #=> {:username=>"sabina", :password=>"Yh3Oz73Na4LaFx", :free_email=>"viola.block@hotmail.com", :safe_email=>"shanti@example.org", :email=>"nella@schaefer.co"}
# @example
# Faker::Internet.personal_account(:username) #=> {:username=>"wilber"}
#
# @example
# Faker::Internet.personal_account({username: {specifier: 'Nancy'}}) #=> {:username=>"nancy", :password=>"UaGbCf5OdLxV", :free_email=>"antonia@yahoo.com", :safe_email=>"lavern_crooks@example.net", :email=>"lila_gottlieb@haley.biz"}
# @example
# Faker::Internet.personal_account(:username, 'password') #=> {:username=>"brant", :password=>"Hr85Wf4L1uQpHl"}
# @example
# 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=>"nancy@example.net", :free_email=>"nancy@hotmail.com", :email=>"santiago+janelle@thiel.com", :password=>"!!&*$%Rt74261I41z"}
#
#
# @note This method will return all the options available for *attributes parameter as a default.
#
# @faker.version 2.3.0
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.each_with_object({}) do |(attr, attr_options), symbolized_options|
attr_options = attr_options.each_with_object({}) do |(key, value), new_options|
new_options[key.to_sym] = value
new_options
end
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
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 user[:password].length.between?(3, 5)
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