Skip to content

Commit

Permalink
add tests for password and fix an edge case (#2741)
Browse files Browse the repository at this point in the history
* add test for password and fix an edge case

* fix invalid password case

* fix invalid case

* update error assertion and change error message

* fix min,max length error message

* fix rubocop

* fix rubocop

---------

Co-authored-by: DeepakRaj-Jaganathan <deepakraj.jaganathan@freshworks.com>
  • Loading branch information
DeepakRaj228 and DeepakRajJaganathan committed Apr 24, 2023
1 parent 6aae8e2 commit cfdcdc0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ def username(specifier: nil, separators: %w[. _])
#
# @faker.version 2.1.3
def password(min_length: 8, max_length: 16, mix_case: true, special_characters: false)
raise ArgumentError, 'max_length must be more than min_length' if max_length < min_length
raise ArgumentError, 'min_length and max_length must be greater than or equal to one' if min_length < 1 || max_length < 1
raise ArgumentError, 'min_length must be smaller than or equal to max_length' unless min_length <= max_length

character_types = []
required_min_length = 0
Expand All @@ -186,7 +187,7 @@ def password(min_length: 8, max_length: 16, mix_case: true, special_characters:
password << lower_chars[rand(lower_chars.count - 1)]
character_bag += lower_chars

digits = ('1'..'9').to_a
digits = ('0'..'9').to_a
password << digits[rand(digits.count - 1)]
character_bag += digits

Expand Down
40 changes: 37 additions & 3 deletions test/faker/default/test_faker_internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,16 @@ def test_username_with_range_and_separators
end
end

def test_password
def test_password_with_no_arguments
password = @tester.password
default_min_length = 8
default_max_length = 16

assert_match(/\w{3}/, password)
assert_match(/\d/, password)
assert_match(/[a-z]/, password)
assert_match(/[A-Z]/, password)
assert_match(/[0-9]/, password)
assert_includes((default_min_length..default_max_length), password.length, 'Generated password length is incorrect')
end

def test_password_with_integer_arg
Expand Down Expand Up @@ -233,8 +236,15 @@ def test_password_with_min_length_and_max_length
assert_includes (min_length..max_length), password.size, 'Password size is incorrect'
end

def test_password_with_same_min_max_length
password = @tester.password(min_length: 5, max_length: 5)

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

def test_password_with_max_length_less_than_min_length
assert_raise 'max_length must be more than min_length' do
assert_raise 'min_length must be smaller than or equal to max_length' do
@tester.password(min_length: 8, max_length: 4)
end
end
Expand Down Expand Up @@ -282,6 +292,30 @@ def test_password_with_invalid_min_length_for_mix_case_and_special_characters
end
end

def test_password_with_invalid_min_max_length
error = assert_raises(ArgumentError) do
@tester.password(min_length: 0, max_length: 0, mix_case: false, special_characters: false)
end

assert_equal 'min_length and max_length must be greater than or equal to one', error.message
end

def test_password_with_invalid_min_length_for_special_characters_only
error = assert_raises(ArgumentError) do
@tester.password(min_length: 0, mix_case: false, special_characters: true)
end

assert_equal 'min_length and max_length must be greater than or equal to one', error.message
end

def test_password_with_invalid_min_length_for_mix_case_only
error = assert_raises(ArgumentError) do
@tester.password(min_length: 1, mix_case: true)
end

assert_equal 'min_length should be at least 2 to enable mix_case configuration', error.message
end

def test_password_with_compatible_min_length_and_requirements
assert_nothing_raised do
[false, true].each do |value|
Expand Down

0 comments on commit cfdcdc0

Please sign in to comment.