diff --git a/README.md b/README.md index 9157788..d4d302c 100644 --- a/README.md +++ b/README.md @@ -122,10 +122,16 @@ Faker::Internet.user_name('Nancy') #=> "nancy" Faker::Internet.user_name('Nancy Johnson', %w(. _ -)) #=> "johnson-nancy" -# Optional argument min_length=0 -Faker::Internet.password #=> "doloremquealiquidrerum" +# Optional arguments: min_length=8, max_length=16 +Faker::Internet.password #=> "vg5msvy1uerg7" -Faker::Internet.password(8) #=> "consecteturadasperiores" +Faker::Internet.password(8) #=> "yfgjik0hgzdqs0" + +Faker::Internet.password(10, 20) #=> "eoc9shwd1hwq4vbgfw" + +Faker::Internet.password(10, 20, true) #=> "3k5qS15aNmG" + +Faker::Internet.password(10, 20, true, true) #=> "*%NkOnJsH4" Faker::Internet.domain_name #=> "effertz.info" diff --git a/spec/internet_spec.cr b/spec/internet_spec.cr index 4fc94dc..bc11ac8 100644 --- a/spec/internet_spec.cr +++ b/spec/internet_spec.cr @@ -62,32 +62,32 @@ describe Faker::Internet do end it "password_with_integer_arg" do - # (1..32).to_a.each do |min_length| - # assert { Faker::Internet.password(min_length).size >= min_length } - # end + (1..32).to_a.each do |min_length| + assert { (Faker::Internet.password(min_length).size >= min_length).should be_true } + end end it "password_max_with_integer_arg" do - # (1..32).to_a.each do |min_length| - # max_length = min_length + 4 - # assert { Faker::Internet.password(min_length, max_length).size <= max_length } - # end + (1..32).to_a.each do |min_length| + max_length = min_length + 4 + assert { (Faker::Internet.password(min_length, max_length).size <= max_length).should be_true } + end end it "password_with_mixed_case" do - # Faker::Internet.password.match(/[A-Z]+/).should_not eq nil + Faker::Internet.password.match(/[A-Z]+/).should_not eq nil end it "password_without_mixed_case" do - # Faker::Internet.password(8, 12, false).match(/[^A-Z]+/).should_not eq nil + Faker::Internet.password(8, 12, false).match(/[^A-Z]+/).should_not eq nil end it "password_with_special_chars" do - # Faker::Internet.password(8, 12, true, true).match(/[!@#\$%\^&\*]+/).should_not eq nil + Faker::Internet.password(8, 12, true, true).match(/[!@#\$%\^&\*]+/).should_not eq nil end it "password_without_special_chars" do - # Faker::Internet.password(8, 12, true).match(/[^!@#\$%\^&\*]+/).should_not eq nil + Faker::Internet.password(8, 12, true).match(/[^!@#\$%\^&\*]+/).should_not eq nil end it "domain_name" do diff --git a/src/faker/internet.cr b/src/faker/internet.cr index 8637cd4..4f9d27d 100644 --- a/src/faker/internet.cr +++ b/src/faker/internet.cr @@ -93,11 +93,26 @@ module Faker (words || Lorem.words(2).join(' ')).gsub(' ', glue).downcase end - def self.password(min_length = 0) - temp = Lorem.words.join - while temp.size < min_length - temp += Lorem.word + def self.password(min_length = 8, max_length = 16, mix_case = true, special_chars = false) + temp = Lorem.characters(min_length) + diff_length = max_length - min_length + if diff_length > 0 + diff_rand = rand(diff_length + 1) + temp += Lorem.characters(diff_rand) end + temp = temp[0..min_length] if min_length > 0 + + if mix_case + temp = temp.gsub(/.{1,2}/) { |s| "#{s[0]}#{s[1].upcase if s[1]?}" } + end + + if special_chars + chars = %w(! @ # $ % ^ & *) + Random.rand(min_length).times do |i| + temp = temp.sub({ temp[i] => chars[Random.rand(chars.size)] }) + end + end + return temp end end