diff --git a/src/rougail/user_data_bitwarden/data.py b/src/rougail/user_data_bitwarden/data.py index 415f437..12384fe 100644 --- a/src/rougail/user_data_bitwarden/data.py +++ b/src/rougail/user_data_bitwarden/data.py @@ -185,9 +185,13 @@ class RougailUserDataBitwarden: self.errors.append(_('the default value for "{0}" must be the Bitwarden item name').format(path)) return None, None if 'ROUGAIL_BITWARDEN_MOCK_ENABLE' in environ: + if type_ == 'secret': + value = 'Ex4mpL3_P4ssw0rD' + else: + value = 'example_login' if allow_multiple: - return ['example_login'], ['Ex4mpL3_P4ssw0rD'] - return 'example_login', 'Ex4mpL3_P4ssw0rD' + return [key_bitwarden], [value] + return key_bitwarden, value try: data = self.get_key_from_commandline(key_bitwarden, allow_multiple) except Exception as exc: diff --git a/tests/results/1_secret/errors/bitwarden_env.json b/tests/results/1_secret/errors/bitwarden_env.json new file mode 100644 index 0000000..217db0a --- /dev/null +++ b/tests/results/1_secret/errors/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "errors": [], + "warnings": [] +} \ No newline at end of file diff --git a/tests/results/1_secret/makedict/bitwarden_env.json b/tests/results/1_secret/makedict/bitwarden_env.json new file mode 100644 index 0000000..3cbabff --- /dev/null +++ b/tests/results/1_secret/makedict/bitwarden_env.json @@ -0,0 +1,3 @@ +{ + "rougail.secret": "Ex4mpL3_P4ssw0rD" +} \ No newline at end of file diff --git a/tests/results/1_secret_unknown/errors/bitwarden_env.json b/tests/results/1_secret_unknown/errors/bitwarden_env.json new file mode 100644 index 0000000..217db0a --- /dev/null +++ b/tests/results/1_secret_unknown/errors/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "errors": [], + "warnings": [] +} \ No newline at end of file diff --git a/tests/results/1_secret_unknown/makedict/bitwarden_env.json b/tests/results/1_secret_unknown/makedict/bitwarden_env.json new file mode 100644 index 0000000..3cbabff --- /dev/null +++ b/tests/results/1_secret_unknown/makedict/bitwarden_env.json @@ -0,0 +1,3 @@ +{ + "rougail.secret": "Ex4mpL3_P4ssw0rD" +} \ No newline at end of file diff --git a/tests/results/2_username_secret/errors/bitwarden_env.json b/tests/results/2_username_secret/errors/bitwarden_env.json new file mode 100644 index 0000000..217db0a --- /dev/null +++ b/tests/results/2_username_secret/errors/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "errors": [], + "warnings": [] +} \ No newline at end of file diff --git a/tests/results/2_username_secret/makedict/bitwarden_env.json b/tests/results/2_username_secret/makedict/bitwarden_env.json new file mode 100644 index 0000000..44799da --- /dev/null +++ b/tests/results/2_username_secret/makedict/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "rougail.username": "example_login", + "rougail.secret": "Ex4mpL3_P4ssw0rD" +} \ No newline at end of file diff --git a/tests/results/2_username_secret_hidden/errors/bitwarden_env.json b/tests/results/2_username_secret_hidden/errors/bitwarden_env.json new file mode 100644 index 0000000..217db0a --- /dev/null +++ b/tests/results/2_username_secret_hidden/errors/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "errors": [], + "warnings": [] +} \ No newline at end of file diff --git a/tests/results/2_username_secret_hidden/makedict/bitwarden_env.json b/tests/results/2_username_secret_hidden/makedict/bitwarden_env.json new file mode 100644 index 0000000..44799da --- /dev/null +++ b/tests/results/2_username_secret_hidden/makedict/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "rougail.username": "example_login", + "rougail.secret": "Ex4mpL3_P4ssw0rD" +} \ No newline at end of file diff --git a/tests/results/2_username_secret_invalid/errors/bitwarden_env.json b/tests/results/2_username_secret_invalid/errors/bitwarden_env.json new file mode 100644 index 0000000..217db0a --- /dev/null +++ b/tests/results/2_username_secret_invalid/errors/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "errors": [], + "warnings": [] +} \ No newline at end of file diff --git a/tests/results/2_username_secret_invalid/makedict/bitwarden_env.json b/tests/results/2_username_secret_invalid/makedict/bitwarden_env.json new file mode 100644 index 0000000..44799da --- /dev/null +++ b/tests/results/2_username_secret_invalid/makedict/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "rougail.username": "example_login", + "rougail.secret": "Ex4mpL3_P4ssw0rD" +} \ No newline at end of file diff --git a/tests/results/2_username_secret_upper/errors/bitwarden_env.json b/tests/results/2_username_secret_upper/errors/bitwarden_env.json new file mode 100644 index 0000000..217db0a --- /dev/null +++ b/tests/results/2_username_secret_upper/errors/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "errors": [], + "warnings": [] +} \ No newline at end of file diff --git a/tests/results/2_username_secret_upper/makedict/bitwarden_env.json b/tests/results/2_username_secret_upper/makedict/bitwarden_env.json new file mode 100644 index 0000000..44799da --- /dev/null +++ b/tests/results/2_username_secret_upper/makedict/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "rougail.username": "example_login", + "rougail.secret": "Ex4mpL3_P4ssw0rD" +} \ No newline at end of file diff --git a/tests/results/3_leadership_secret/errors/bitwarden_env.json b/tests/results/3_leadership_secret/errors/bitwarden_env.json new file mode 100644 index 0000000..217db0a --- /dev/null +++ b/tests/results/3_leadership_secret/errors/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "errors": [], + "warnings": [] +} \ No newline at end of file diff --git a/tests/results/3_leadership_secret/makedict/bitwarden_env.json b/tests/results/3_leadership_secret/makedict/bitwarden_env.json new file mode 100644 index 0000000..5f5bc6c --- /dev/null +++ b/tests/results/3_leadership_secret/makedict/bitwarden_env.json @@ -0,0 +1,8 @@ +{ + "rougail.leader.username": [ + { + "rougail.leader.username": "example_login", + "rougail.leader.secret": "Ex4mpL3_P4ssw0rD" + } + ] +} \ No newline at end of file diff --git a/tests/results/3_leadership_secret_several/errors/bitwarden_env.json b/tests/results/3_leadership_secret_several/errors/bitwarden_env.json new file mode 100644 index 0000000..217db0a --- /dev/null +++ b/tests/results/3_leadership_secret_several/errors/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "errors": [], + "warnings": [] +} \ No newline at end of file diff --git a/tests/results/3_leadership_secret_several/makedict/bitwarden_env.json b/tests/results/3_leadership_secret_several/makedict/bitwarden_env.json new file mode 100644 index 0000000..5f5bc6c --- /dev/null +++ b/tests/results/3_leadership_secret_several/makedict/bitwarden_env.json @@ -0,0 +1,8 @@ +{ + "rougail.leader.username": [ + { + "rougail.leader.username": "example_login", + "rougail.leader.secret": "Ex4mpL3_P4ssw0rD" + } + ] +} \ No newline at end of file diff --git a/tests/results/4_several_secrets/errors/bitwarden_env.json b/tests/results/4_several_secrets/errors/bitwarden_env.json new file mode 100644 index 0000000..217db0a --- /dev/null +++ b/tests/results/4_several_secrets/errors/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "errors": [], + "warnings": [] +} \ No newline at end of file diff --git a/tests/results/4_several_secrets/makedict/bitwarden_env.json b/tests/results/4_several_secrets/makedict/bitwarden_env.json new file mode 100644 index 0000000..3cbabff --- /dev/null +++ b/tests/results/4_several_secrets/makedict/bitwarden_env.json @@ -0,0 +1,3 @@ +{ + "rougail.secret": "Ex4mpL3_P4ssw0rD" +} \ No newline at end of file diff --git a/tests/results/4_several_secrets_upper/errors/bitwarden_env.json b/tests/results/4_several_secrets_upper/errors/bitwarden_env.json new file mode 100644 index 0000000..217db0a --- /dev/null +++ b/tests/results/4_several_secrets_upper/errors/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "errors": [], + "warnings": [] +} \ No newline at end of file diff --git a/tests/results/4_several_secrets_upper/makedict/bitwarden_env.json b/tests/results/4_several_secrets_upper/makedict/bitwarden_env.json new file mode 100644 index 0000000..3cbabff --- /dev/null +++ b/tests/results/4_several_secrets_upper/makedict/bitwarden_env.json @@ -0,0 +1,3 @@ +{ + "rougail.secret": "Ex4mpL3_P4ssw0rD" +} \ No newline at end of file diff --git a/tests/results/5_secret_calc/errors/bitwarden_env.json b/tests/results/5_secret_calc/errors/bitwarden_env.json new file mode 100644 index 0000000..217db0a --- /dev/null +++ b/tests/results/5_secret_calc/errors/bitwarden_env.json @@ -0,0 +1,4 @@ +{ + "errors": [], + "warnings": [] +} \ No newline at end of file diff --git a/tests/results/5_secret_calc/makedict/bitwarden_env.json b/tests/results/5_secret_calc/makedict/bitwarden_env.json new file mode 100644 index 0000000..acb7387 --- /dev/null +++ b/tests/results/5_secret_calc/makedict/bitwarden_env.json @@ -0,0 +1,10 @@ +{ + "rougail.host": "test", + "rougail.project": "5_secret_calc", + "rougail.environment": "environment", + "rougail.service": "service", + "rougail.user": "user_1", + "rougail.secret": "Ex4mpL3_P4ssw0rD", + "rougail.dyn_user_1.secret": "Ex4mpL3_P4ssw0rD", + "rougail.dyn_user_2.secret": "Ex4mpL3_P4ssw0rD" +} \ No newline at end of file diff --git a/tests/test_load.py b/tests/test_load.py index 823ab86..aca668a 100644 --- a/tests/test_load.py +++ b/tests/test_load.py @@ -7,6 +7,7 @@ from rougail.user_data_bitwarden import RougailUserDataBitwarden as RougailUserD from json import load, dump ######################### from pytest import raises +from unittest import mock from rougail_tests.utils import config_to_dict @@ -15,7 +16,7 @@ from rougail_tests.utils import config_to_dict test_dir = Path(__file__).parent / 'structures' -def _test_dictionaries(test_dir, command): +def _test_dictionaries(test_dir, command, env=False): rougailconfig = RougailConfig.copy() rougailconfig['main_dictionaries'] = [str(test_dir)] # rougailconfig['tiramisu_cache'] = "cache.py" @@ -27,18 +28,22 @@ def _test_dictionaries(test_dir, command): errors = RougailUserData(config, rougailconfig=rougailconfig).run() #expected output config_dict = dict(config_to_dict(config.forcepermissive.value.get())) - ok_file = Path('tests') / 'results' / test_dir.name / 'makedict' / ('bitwarden.json.' + command) + if not env: + base_filename = 'bitwarden.json' + else: + base_filename = 'bitwarden_env.json' + ok_file = Path('tests') / 'results' / test_dir.name / 'makedict' / (base_filename + '.' + command) if not ok_file.is_file(): - ok_file = Path('tests') / 'results' / test_dir.name / 'makedict' / 'bitwarden.json' + ok_file = Path('tests') / 'results' / test_dir.name / 'makedict' / base_filename if not ok_file.is_file(): ok_file.parent.mkdir(parents=True, exist_ok=True) with open(ok_file, 'a') as json_file: dump(config_dict, json_file, indent=4) with open(ok_file) as json_file: expected = load(json_file) - errors_file = Path('tests') / 'results' / test_dir.name / 'errors' / ('bitwarden.json.' + command) + errors_file = Path('tests') / 'results' / test_dir.name / 'errors' / (base_filename + '.' + command) if not errors_file.is_file(): - errors_file = Path('tests') / 'results' / test_dir.name / 'errors' / 'bitwarden.json' + errors_file = Path('tests') / 'results' / test_dir.name / 'errors' / base_filename if not errors_file.is_file(): errors_file.parent.mkdir(parents=True, exist_ok=True) with open(errors_file, 'a') as json_file: @@ -190,3 +195,167 @@ def test_dictionaries_9_unknown_type_bw(): with raises(DictConsistencyError) as err: _test_dictionaries(test_dir / '9_unknown_type', 'bw') assert err.value.errno == 56 + + +def test_dictionaries_env_1_secret_rbw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '1_secret', 'rbw', True) + + +def test_dictionaries_env_1_secret_bw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '1_secret', 'bw', True) + + +def test_dictionaries_env_1_secret_unknown_rbw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '1_secret_unknown', 'rbw', True) + + +def test_dictionaries_env_1_secret_unknown_bw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '1_secret_unknown', 'bw', True) + + +def test_dictionaries_env_2_username_secret_rbw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '2_username_secret', 'rbw', True) + + +def test_dictionaries_env_2_username_secret_bw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '2_username_secret', 'bw', True) + + +def test_dictionaries_env_2_username_secret_upper_rbw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '2_username_secret_upper', 'rbw', True) + + +def test_dictionaries_env_2_username_secret_upper_bw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '2_username_secret_upper', 'bw', True) + + +def test_dictionaries_env_2_username_secret_invalid_rbw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '2_username_secret_invalid', 'rbw', True) + + +def test_dictionaries_env_2_username_secret_invalid_bw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '2_username_secret_invalid', 'bw', True) + + +def test_dictionaries_env_2_username_secret_hidden_rbw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '2_username_secret_hidden', 'rbw', True) + + +def test_dictionaries_env_2_username_secret_hidden_bw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '2_username_secret_hidden', 'bw', True) + + +def test_dictionaries_env_3_leadership_secret_rbw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '3_leadership_secret', 'rbw', True) + + +def test_dictionaries_env_3_leadership_secret_bw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '3_leadership_secret', 'bw', True) + + +def test_dictionaries_env_3_leadership_secret_several_rbw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '3_leadership_secret_several', 'rbw', True) + + +def test_dictionaries_env_3_leadership_secret_several_bw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '3_leadership_secret_several', 'bw', True) + + +def test_dictionaries_env_4_several_secrets_rbw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '4_several_secrets', 'rbw', True) + + +def test_dictionaries_env_4_several_secrets_bw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '4_several_secrets', 'bw', True) + + +def test_dictionaries_env_4_several_secrets_upper_rbw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '4_several_secrets_upper', 'rbw', True) + + +def test_dictionaries_env_4_several_secrets_upper_bw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '4_several_secrets_upper', 'bw', True) + + +def test_dictionaries_env_5_secret_calc_rbw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '5_secret_calc', 'rbw', True) + + +def test_dictionaries_env_5_secret_calc_bw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + _test_dictionaries(test_dir / '5_secret_calc', 'bw', True) + + +def test_dictionaries_env_8_multi_variable_rbw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + with raises(DictConsistencyError) as err: + _test_dictionaries(test_dir / '8_multi_variable', 'rbw', True) + assert err.value.errno == 57 + + +def test_dictionaries_env_8_multi_variable_bw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + with raises(DictConsistencyError) as err: + _test_dictionaries(test_dir / '8_multi_variable', 'bw', True) + assert err.value.errno == 57 + + +def test_dictionaries_env_9_unknown_type_rbw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + with raises(DictConsistencyError) as err: + _test_dictionaries(test_dir / '9_unknown_type', 'rbw', True) + assert err.value.errno == 56 + + +def test_dictionaries_env_9_unknown_type_bw(): + "tests the output" + with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): + with raises(DictConsistencyError) as err: + _test_dictionaries(test_dir / '9_unknown_type', 'bw', True) + assert err.value.errno == 56