Skip to content

Ensure Faker::Internet.password method behavior is consistent with length parameters #3034

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 5 additions & 10 deletions lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def username(specifier: nil, separators: %w[. _])
#
# @param min_length [Integer] The minimum length of the password
# @param max_length [Integer] The maximum length of the password
# @param mix_case [Boolean] Toggles if uppercased letters are allowed. If true, at least one will be added.
# @param mix_case [Boolean] Toggles if uppercased and lowercased letters are allowed. If true, at least one of each will be added. Otherwise, only lowercased letters will be used.
# @param special_characters [Boolean] Toggles if special characters are allowed. If true, at least one will be added.
#
# @return [String]
Expand Down Expand Up @@ -144,21 +144,16 @@ def password(min_length: 8, max_length: 16, mix_case: true, special_characters:

target_length = rand(min_length..max_length)

password = []
character_bag = []

# use lower_chars by default and add upper_chars if mix_case
lower_chars = self::LLetters
password << sample(lower_chars)
character_bag += lower_chars

digits = ('0'..'9').to_a
password << sample(digits)
character_bag += digits

password = []
character_bag = lower_chars + digits

if mix_case
upper_chars = self::ULetters
password << sample(upper_chars)
password << sample(lower_chars)
character_bag += upper_chars
end

Expand Down
14 changes: 11 additions & 3 deletions test/faker/default/test_faker_internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ def test_password_with_integer_arg
end
end

def test_password_with_specific_integer_arg
(1..32).each do |length|
assert_equal length, @tester.password(min_length: length, max_length: length, mix_case: false).length
end
end

def test_password_max_with_integer_arg
(1..32).each do |min_length|
max_length = min_length + 4
Expand Down Expand Up @@ -241,10 +247,12 @@ def test_password_with_min_length_and_max_length
end

def test_password_with_same_min_max_length
password = @tester.password(min_length: 5, max_length: 5)
(2..32).each do |length|
password = @tester.password(min_length: length, max_length: length)

assert_match(/\w+/, password)
assert_equal(5, password.length)
assert_match(/\w+/, password)
assert_equal(length, password.length)
end
end

def test_password_with_max_length_less_than_min_length
Expand Down