Skip to content

Commit

Permalink
[1673] Ensure mix_case returns at least one lower and one upper case …
Browse files Browse the repository at this point in the history
…letter
  • Loading branch information
brad authored and bpleslie committed Aug 19, 2019
1 parent 6d9ef88 commit 6a329d6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
20 changes: 17 additions & 3 deletions lib/faker/default/alphanumeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Faker
class Alphanumeric < Base
class << self
ALPHABET = ('a'..'z').to_a
ALPHANUMS = ALPHABET + (0..9).to_a
NUMBERS = (0..9).to_a
ALPHANUMS = ALPHABET + NUMBERS

def alpha(number: 32)
char_count = resolve(number)
Expand All @@ -13,11 +14,24 @@ def alpha(number: 32)
Array.new(char_count) { sample(ALPHABET) }.join
end

def alphanumeric(number: 32)
def alphanumeric(number: 32, min_alpha: nil, min_numeric: nil)
char_count = resolve(number)
return '' if char_count.to_i < 1

Array.new(char_count) { sample(ALPHANUMS) }.join
alpha_count = min_alpha.to_i
numeric_count = min_numeric.to_i

return Array.new(char_count) { sample(ALPHANUMS) }.join if alpha_count.zero? && numeric_count.zero?
return '' if alpha_count + numeric_count > char_count

random_count = char_count - alpha_count - numeric_count

alphas = alpha_count.positive? ? Array.new(alpha_count) { sample(ALPHABET) } : []
numbers = numeric_count.positive? ? Array.new(numeric_count) { sample(NUMBERS) } : []
randoms = random_count.positive? ? Array.new(random_count) { sample(ALPHANUMS) } : []

combined = alphas + numbers + randoms
combined.shuffle.join
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def username(specifier: nil, separators: %w[. _])
end

def password(min_length: 8, max_length: 16, mix_case: true, special_characters: false)
temp = Lorem.characters(number: min_length)
min_alpha = mix_case ? 2 : nil
temp = Lorem.characters(number: min_length, min_alpha: min_alpha)
diff_length = max_length - min_length

if diff_length.positive?
Expand All @@ -65,8 +66,12 @@ def password(min_length: 8, max_length: 16, mix_case: true, special_characters:
end

if mix_case
upcased_count = 0
temp.chars.each_with_index do |char, index|
temp[index] = char.upcase if index.even?
if char =~ /[[:alpha:]]/ && upcased_count.even?
temp[index] = char.upcase
upcased_count += 1
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/faker/default/lorem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def character
sample(Types::CHARACTERS)
end

def characters(number: 255)
Alphanumeric.alphanumeric(number: number)
def characters(number: 255, min_alpha: nil, min_numeric: nil)
Alphanumeric.alphanumeric(number: number, min_alpha: min_alpha, min_numeric: min_numeric)
end

def multibyte
Expand Down
18 changes: 16 additions & 2 deletions test/faker/default/test_alphanum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,24 @@ def setup
end

def alpha
assert @tester.alpha(characters: 5).match(/[a-z]{5}/)
assert @tester.alpha(number: 5).match(/[a-z]{5}/)
end

def alphanum
assert @tester.alphanumeric(characters: 5).match(/[a-z0-9]{5}/)
assert @tester.alphanumeric(number: 5).match(/[a-z0-9]{5}/)
end

def test_alphanumeric_with_min_alpha
letters = @tester.alphanumeric(number: 5, min_alpha: 2).split('').map do |char|
char =~ /[[:alpha:]]/
end
assert letters.compact.size >= 2
end

def test_alphanumeric_with_min_numeric
numbers = @tester.alphanumeric(number: 5, min_numeric: 4).split('').map do |char|
char =~ /[[:digit:]]/
end
assert numbers.compact.size >= 4
end
end
11 changes: 10 additions & 1 deletion test/faker/default/test_faker_internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,16 @@ def test_password_could_achieve_max_length
end

def test_password_with_mixed_case
assert @tester.password.match(/[A-Z]+/)
password = @tester.password
upcase_count = 0
downcase_count = 0
password.chars.each do |char|
if char =~ /[[:alpha:]]/
char.capitalize == char ? upcase_count += 1 : downcase_count += 1
end
end
assert upcase_count >= 1
assert downcase_count >= 1
end

def test_password_without_mixed_case
Expand Down

0 comments on commit 6a329d6

Please sign in to comment.