|
| 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