Skip to content

Commit 8773eae

Browse files
author
davidpil
committed
Add Password Hardening CLI test
1 parent 03b3a57 commit 8773eae

File tree

3 files changed

+243
-0
lines changed

3 files changed

+243
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Module holding the correct values for show CLI command outputs for the passw_hardening_test.py
3+
"""
4+
5+
show_passw_hardening_policies_default="""\
6+
STATE EXPIRATION EXPIRATION WARNING HISTORY CNT LEN MIN REJECT USER PASSW MATCH LOWER CLASS UPPER CLASS DIGITS CLASS SPECIAL CLASS
7+
-------- ------------ -------------------- ------------- --------- ------------------------- ------------- ------------- -------------- ---------------
8+
disabled 180 15 10 8 true true true true true
9+
"""
10+
11+
show_passw_hardening_policies_classes_disabled="""\
12+
STATE EXPIRATION EXPIRATION WARNING HISTORY CNT LEN MIN REJECT USER PASSW MATCH LOWER CLASS UPPER CLASS DIGITS CLASS SPECIAL CLASS
13+
-------- ------------ -------------------- ------------- --------- ------------------------- ------------- ------------- -------------- ---------------
14+
disabled 180 15 10 8 false false false false false
15+
"""
16+
17+
show_passw_hardening_policies_enabled="""\
18+
STATE EXPIRATION EXPIRATION WARNING HISTORY CNT LEN MIN REJECT USER PASSW MATCH LOWER CLASS UPPER CLASS DIGITS CLASS SPECIAL CLASS
19+
------- ------------ -------------------- ------------- --------- ------------------------- ------------- ------------- -------------- ---------------
20+
enabled 180 15 10 8 true true true true true
21+
"""
22+
23+
24+
show_passw_hardening_policies_expiration="""\
25+
STATE EXPIRATION EXPIRATION WARNING HISTORY CNT LEN MIN REJECT USER PASSW MATCH LOWER CLASS UPPER CLASS DIGITS CLASS SPECIAL CLASS
26+
------- ------------ -------------------- ------------- --------- ------------------------- ------------- ------------- -------------- ---------------
27+
enabled 100 15 10 8 true true true true true
28+
"""
29+
30+
show_passw_hardening_policies_history_cnt="""\
31+
STATE EXPIRATION EXPIRATION WARNING HISTORY CNT LEN MIN REJECT USER PASSW MATCH LOWER CLASS UPPER CLASS DIGITS CLASS SPECIAL CLASS
32+
-------- ------------ -------------------- ------------- --------- ------------------------- ------------- ------------- -------------- ---------------
33+
disabled 180 15 40 8 true true true true true
34+
"""
35+
36+
show_passw_hardening_policies_len_min="""\
37+
STATE EXPIRATION EXPIRATION WARNING HISTORY CNT LEN MIN REJECT USER PASSW MATCH LOWER CLASS UPPER CLASS DIGITS CLASS SPECIAL CLASS
38+
-------- ------------ -------------------- ------------- --------- ------------------------- ------------- ------------- -------------- ---------------
39+
disabled 180 15 10 30 true true true true true
40+
"""
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"PASSW_HARDENING": {
3+
"POLICIES": {
4+
"state": "enabled",
5+
"expiration": "180",
6+
"expiration_warning": "15",
7+
"history_cnt": "10",
8+
"len_min": "8",
9+
"reject_user_passw_match": "true",
10+
"digits_class": "true",
11+
"lower_class": "true",
12+
"special_class": "true",
13+
"upper_class": "true"
14+
}
15+
}
16+
}

tests/passw_hardening_test.py

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import logging
5+
import show.main as show
6+
import config.main as config
7+
8+
from .passw_hardening_input import assert_show_output
9+
from utilities_common.db import Db
10+
from click.testing import CliRunner
11+
from .mock_tables import dbconnector
12+
13+
logger = logging.getLogger(__name__)
14+
test_path = os.path.dirname(os.path.abspath(__file__))
15+
mock_db_path = os.path.join(test_path, "passw_hardening_input")
16+
17+
SUCCESS = 0
18+
ERROR = 1
19+
INVALID_VALUE = 'INVALID'
20+
EXP_GOOD_FLOW = 1
21+
EXP_BAD_FLOW = 0
22+
23+
class TestPasswHardening:
24+
@classmethod
25+
def setup_class(cls):
26+
logger.info("SETUP")
27+
os.environ['UTILITIES_UNIT_TESTING'] = "2"
28+
29+
30+
@classmethod
31+
def teardown_class(cls):
32+
logger.info("TEARDOWN")
33+
os.environ['UTILITIES_UNIT_TESTING'] = "0"
34+
os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] = ""
35+
dbconnector.dedicated_dbs['CONFIG_DB'] = None
36+
37+
def verify_passw_policies_output(self, db, runner, output, expected=EXP_GOOD_FLOW):
38+
result = runner.invoke(show.cli.commands["passw-hardening"].commands["policies"], [], obj=db)
39+
logger.debug("\n" + result.output)
40+
logger.debug(result.exit_code)
41+
42+
if expected: # good flow expected (default)
43+
assert result.exit_code == SUCCESS
44+
assert result.output == output
45+
else: # bad flow expected
46+
assert result.exit_code == ERROR
47+
48+
def passw_hardening_set_policy(self, runner, db, attr, value, expected=EXP_GOOD_FLOW):
49+
result = runner.invoke(
50+
config.config.commands["passw-hardening"].commands["policies"].commands[attr],
51+
[value], obj=db
52+
)
53+
54+
if expected: # good flow expected (default)
55+
logger.debug("\n" + result.output)
56+
logger.debug(result.exit_code)
57+
assert result.exit_code == SUCCESS
58+
else: # bad flow expected
59+
assert result.exit_code == ERROR
60+
61+
def set_passw_default_values(self, runner, db):
62+
63+
passw_policies = {
64+
"state": "disabled",
65+
"expiration": "180",
66+
"expiration-warning": "15",
67+
"history-cnt": "10",
68+
"len-min": "8",
69+
"reject-user-passw-match": "true",
70+
"digits-class": "true",
71+
"lower-class": "true",
72+
"special-class": "true",
73+
"upper-class": "true"
74+
}
75+
76+
for k, v in passw_policies.items():
77+
self.passw_hardening_set_policy(runner, db, k, v)
78+
79+
######### PASSW-HARDENING #########
80+
81+
def test_passw_hardening_default(self, set_passw_hardening_default):
82+
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'default_config_db')
83+
db = Db()
84+
runner = CliRunner()
85+
86+
self.verify_passw_policies_output(db, runner, assert_show_output.show_passw_hardening_policies_default)
87+
88+
def test_passw_hardening_feature_enabled(self):
89+
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'default_config_db')
90+
db = Db()
91+
runner = CliRunner()
92+
93+
self.set_passw_default_values(runner, db)
94+
95+
self.passw_hardening_set_policy(runner, db, "state", "enabled")
96+
97+
self.verify_passw_policies_output(db, runner, assert_show_output.show_passw_hardening_policies_enabled)
98+
99+
def test_passw_hardening_policies_classes_disabled(self):
100+
"""Disable passw hardening classes & reject user passw match policies"""
101+
102+
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'default_config_db')
103+
db = Db()
104+
runner = CliRunner()
105+
106+
self.set_passw_default_values(runner, db)
107+
passw_classes = { "reject-user-passw-match": "false",
108+
"digits-class": "false",
109+
"lower-class": "false",
110+
"special-class": "false",
111+
"upper-class": "false"
112+
}
113+
114+
for k, v in passw_classes.items():
115+
self.passw_hardening_set_policy(runner, db, k, v)
116+
117+
self.verify_passw_policies_output(db, runner, assert_show_output.show_passw_hardening_policies_classes_disabled)
118+
119+
def test_passw_hardening_policies_exp_time(self):
120+
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'default_config_db')
121+
db = Db()
122+
runner = CliRunner()
123+
124+
self.set_passw_default_values(runner, db)
125+
126+
self.passw_hardening_set_policy(runner, db, "state", "enabled")
127+
self.passw_hardening_set_policy(runner, db, "expiration", "100")
128+
self.passw_hardening_set_policy(runner, db, "expiration-warning", "15")
129+
130+
self.verify_passw_policies_output(db, runner, assert_show_output.show_passw_hardening_policies_expiration)
131+
132+
def test_passw_hardening_policies_history(self):
133+
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'default_config_db')
134+
db = Db()
135+
runner = CliRunner()
136+
137+
self.set_passw_default_values(runner, db)
138+
self.passw_hardening_set_policy(runner, db, "history-cnt", "40")
139+
140+
self.verify_passw_policies_output(db, runner, assert_show_output.show_passw_hardening_policies_history_cnt)
141+
142+
def test_passw_hardening_policies_len_min(self):
143+
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'default_config_db')
144+
db = Db()
145+
runner = CliRunner()
146+
147+
self.set_passw_default_values(runner, db)
148+
self.passw_hardening_set_policy(runner, db, "len-min", "30")
149+
150+
self.verify_passw_policies_output(db, runner, assert_show_output.show_passw_hardening_policies_len_min)
151+
152+
def test_passw_hardening_policy_expiration_invalid(self):
153+
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'default_config_db')
154+
db = Db()
155+
runner = CliRunner()
156+
INVALID_EXP_TIME = "600"
157+
158+
self.set_passw_default_values(runner, db)
159+
self.passw_hardening_set_policy(runner, db, "expiration", INVALID_EXP_TIME, EXP_BAD_FLOW)
160+
161+
# expect default values, because invalid values should not succed to modify default configuration
162+
self.verify_passw_policies_output(db, runner, assert_show_output.show_passw_hardening_policies_default)
163+
164+
def test_passw_hardening_policy_len_min_invalid(self):
165+
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'default_config_db')
166+
db = Db()
167+
runner = CliRunner()
168+
INVALID_EXP_LEN = "500"
169+
170+
self.set_passw_default_values(runner, db)
171+
self.passw_hardening_set_policy(runner, db, "len-min", INVALID_EXP_LEN, EXP_BAD_FLOW)
172+
173+
# expect default values, because invalid values should not succed to modify default configuration
174+
self.verify_passw_policies_output(db, runner, assert_show_output.show_passw_hardening_policies_default)
175+
176+
def test_passw_hardening_policy_class_invalid(self):
177+
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'default_config_db')
178+
db = Db()
179+
runner = CliRunner()
180+
INVALID_VALUE = '?'
181+
182+
self.set_passw_default_values(runner, db)
183+
self.passw_hardening_set_policy(runner, db, "expiration", INVALID_VALUE, EXP_BAD_FLOW)
184+
185+
# expect default values, because invalid values should not succed to modify default configuration
186+
self.verify_passw_policies_output(db, runner, assert_show_output.show_passw_hardening_policies_default)
187+

0 commit comments

Comments
 (0)