/
GeneratePassword_test.py
106 lines (99 loc) · 3.1 KB
/
GeneratePassword_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import pytest
from GeneratePassword import generate_password, SYMBOLS
from CommonServerPython import DemistoException
def does_password_meet_requirement(
password: str,
min_lowercase: int,
max_lowercase: int,
min_uppercase: int,
max_uppercase: int,
min_digits: int,
max_digits: int,
min_symbols: int,
max_symbols: int,
) -> bool:
lowercase_count = sum(1 for char in password if char.islower())
uppercase_count = sum(1 for char in password if char.isupper())
digit_count = sum(1 for char in password if char.isdigit())
symbol_count = sum(1 for char in password if char in SYMBOLS)
return all([
min_lowercase <= lowercase_count <= max_lowercase,
min_uppercase <= uppercase_count <= max_uppercase,
min_digits <= digit_count <= max_digits,
min_symbols <= symbol_count <= max_symbols
])
@pytest.mark.parametrize(
'min_lowercase, max_lowercase, min_uppercase, max_uppercase, min_digits, max_digits, min_symbols, max_symbols',
[
(1, 2, 1, 2, 1, 2, 1, 2), # Test case with all ranges set to 1-2
(2, 5, 3, 5, 4, 6, 1, 3), # Test case with various ranges
(2, 5, 3, 5, 4, 10, 0, 0), # Test case with no symbols
]
)
def test_generate_password(
min_lowercase: int,
max_lowercase: int,
min_uppercase: int,
max_uppercase: int,
min_digits: int,
max_digits: int,
min_symbols: int,
max_symbols: int,
):
args = {
'debug': 'true',
'min_lcase': min_lowercase,
'max_lcase': max_lowercase,
'min_ucase': min_uppercase,
'max_ucase': max_uppercase,
'min_digits': min_digits,
'max_digits': max_digits,
'min_symbols': min_symbols,
'max_symbols': max_symbols,
}
result = generate_password(args)
pwd = result.outputs
assert isinstance(pwd, str)
assert does_password_meet_requirement(
pwd,
min_lowercase,
max_lowercase,
min_uppercase,
max_uppercase,
min_digits,
max_digits,
min_symbols,
max_symbols,
)
@pytest.mark.parametrize(
'min_lowercase, max_lowercase, min_uppercase, max_uppercase, min_digits, max_digits, min_symbols, max_symbols, exception',
[
(0, 5, 0, 5, 0, 5, 0, 5,
"error: At least one of the following arguments should be above 0"), # Test case with all ranges set to 0-5
(-3, 5, 0, 5, 0, 5, 0, 5, "All numeral arguments must be positive.")
]
)
def test_generate_password_zero_inputs(
min_lowercase: int,
max_lowercase: int,
min_uppercase: int,
max_uppercase: int,
min_digits: int,
max_digits: int,
min_symbols: int,
max_symbols: int,
exception: str,
):
args = {
'debug': 'true',
'min_lcase': min_lowercase,
'max_lcase': max_lowercase,
'min_ucase': min_uppercase,
'max_ucase': max_uppercase,
'min_digits': min_digits,
'max_digits': max_digits,
'min_symbols': min_symbols,
'max_symbols': max_symbols,
}
with pytest.raises(DemistoException, match=exception):
generate_password(args)