import os from pathlib import Path from rougail import Rougail, RougailConfig from rougail.error import DictConsistencyError ######################### from rougail.user_data_bitwarden import RougailUserDataBitwarden as RougailUserData from json import load, dump ######################### from pytest import raises from unittest import mock from rougail_tests.utils import config_to_dict test_dir = Path(__file__).parent / 'structures' def _test_structural_files(test_dir, command, *, env=False, modified=False): rougailconfig = RougailConfig.copy() rougailconfig['main_structural_directories'] = [str(test_dir)] # rougailconfig['tiramisu_cache'] = "cache.py" rougailconfig['step.user_data'] = ['bitwarden'] rougailconfig['bitwarden.command'] = command rougail = Rougail(rougailconfig) config = rougail.run() config.property.read_write() # loads variables in the tiramisu config generated_user_data = RougailUserData(config, rougailconfig=rougailconfig).run() if modified: generated_user_data.insert(0, {'source': 'By Hand', 'errors': [], 'warnings': [], 'values': {'rougail.modified_variable': ['user_1', 'user_2'], 'rougail.modified_variable_single': 'user_2'}}) errors = rougail.user_datas(generated_user_data) #expected output config_dict = dict(config_to_dict(config.value.get())) 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' / 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' / (base_filename + '.' + command) if not errors_file.is_file(): 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: dump(errors, json_file, indent=4) with open(errors_file) as json_file: expected_errors = load(json_file) assert expected_errors == errors, errors_file # assert expected == config_dict, ok_file def test_structural_files_1_secret_rbw(): "tests the output" _test_structural_files(test_dir / '1_secret', 'rbw') def test_structural_files_1_secret_bw(): "tests the output" _test_structural_files(test_dir / '1_secret', 'bw') def test_structural_files_1_secret_unknown_rbw(): "tests the output" _test_structural_files(test_dir / '1_secret_unknown', 'rbw') def test_structural_files_1_secret_unknown_bw(): "tests the output" _test_structural_files(test_dir / '1_secret_unknown', 'bw') def test_structural_files_2_username_secret_rbw(): "tests the output" _test_structural_files(test_dir / '2_username_secret', 'rbw') def test_structural_files_2_username_secret_bw(): "tests the output" _test_structural_files(test_dir / '2_username_secret', 'bw') def test_structural_files_2_username_secret_upper_rbw(): "tests the output" _test_structural_files(test_dir / '2_username_secret_upper', 'rbw') def test_structural_files_2_username_secret_upper_bw(): "tests the output" _test_structural_files(test_dir / '2_username_secret_upper', 'bw') def test_structural_files_2_username_secret_invalid_rbw(): "tests the output" _test_structural_files(test_dir / '2_username_secret_invalid', 'rbw') def test_structural_files_2_username_secret_invalid_bw(): "tests the output" _test_structural_files(test_dir / '2_username_secret_invalid', 'bw') def test_structural_files_3_leadership_secret_rbw(): "tests the output" _test_structural_files(test_dir / '3_leadership_secret', 'rbw') def test_structural_files_3_leadership_secret_bw(): "tests the output" _test_structural_files(test_dir / '3_leadership_secret', 'bw') def test_structural_files_3_leadership_secret_several_rbw(): "tests the output" _test_structural_files(test_dir / '3_leadership_secret_several', 'rbw') def test_structural_files_3_leadership_secret_several_bw(): "tests the output" _test_structural_files(test_dir / '3_leadership_secret_several', 'bw') def test_structural_files_4_several_secrets_rbw(): "tests the output" _test_structural_files(test_dir / '4_several_secrets', 'rbw') def test_structural_files_4_several_secrets_bw(): "tests the output" _test_structural_files(test_dir / '4_several_secrets', 'bw') def test_structural_files_4_several_secrets_upper_rbw(): "tests the output" _test_structural_files(test_dir / '4_several_secrets_upper', 'rbw') def test_structural_files_4_several_secrets_upper_bw(): "tests the output" _test_structural_files(test_dir / '4_several_secrets_upper', 'bw') def test_structural_files_5_secret_calc_rbw(): "tests the output" _test_structural_files(test_dir / '5_secret_calc', 'rbw') def test_structural_files_5_secret_calc_bw(): "tests the output" _test_structural_files(test_dir / '5_secret_calc', 'bw') def test_structural_files_5_secret_calc_other_user_data_rbw(): "tests the output" _test_structural_files(test_dir / '5_secret_calc_other_user_data', 'rbw', modified=True) def test_structural_files_5_secret_calc_other_user_data2_rbw(): "tests the output" _test_structural_files(test_dir / '5_secret_calc_other_user_data2', 'rbw', modified=True) def test_structural_files_5_secret_calc_other_user_data3_rbw(): "tests the output" _test_structural_files(test_dir / '5_secret_calc_other_user_data3', 'rbw', modified=True) def test_structural_files_6_no_username_rbw(): "tests the output" _test_structural_files(test_dir / '6_no_username', 'rbw') def test_structural_files_6_no_username_bw(): "tests the output" _test_structural_files(test_dir / '6_no_username', 'bw') def test_structural_files_8_multi_variable_rbw(): "tests the output" with raises(DictConsistencyError) as err: _test_structural_files(test_dir / '8_multi_variable', 'rbw') assert err.value.errno == 57 def test_structural_files_8_multi_variable_bw(): "tests the output" with raises(DictConsistencyError) as err: _test_structural_files(test_dir / '8_multi_variable', 'bw') assert err.value.errno == 57 def test_structural_files_9_unknown_type_rbw(): "tests the output" with raises(DictConsistencyError) as err: _test_structural_files(test_dir / '9_unknown_type', 'rbw') assert err.value.errno == 56 def test_structural_files_9_unknown_type_bw(): "tests the output" with raises(DictConsistencyError) as err: _test_structural_files(test_dir / '9_unknown_type', 'bw') assert err.value.errno == 56 def test_structural_files_env_1_secret_rbw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '1_secret', 'rbw', env=True) def test_structural_files_env_1_secret_bw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '1_secret', 'bw', env=True) def test_structural_files_env_1_secret_unknown_rbw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '1_secret_unknown', 'rbw', env=True) def test_structural_files_env_1_secret_unknown_bw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '1_secret_unknown', 'bw', env=True) def test_structural_files_env_2_username_secret_rbw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '2_username_secret', 'rbw', env=True) def test_structural_files_env_2_username_secret_bw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '2_username_secret', 'bw', env=True) def test_structural_files_env_2_username_secret_upper_rbw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '2_username_secret_upper', 'rbw', env=True) def test_structural_files_env_2_username_secret_upper_bw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '2_username_secret_upper', 'bw', env=True) def test_structural_files_env_2_username_secret_invalid_rbw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '2_username_secret_invalid', 'rbw', env=True) def test_structural_files_env_2_username_secret_invalid_bw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '2_username_secret_invalid', 'bw', env=True) def test_structural_files_env_3_leadership_secret_rbw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '3_leadership_secret', 'rbw', env=True) def test_structural_files_env_3_leadership_secret_bw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '3_leadership_secret', 'bw', env=True) def test_structural_files_env_3_leadership_secret_several_rbw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '3_leadership_secret_several', 'rbw', env=True) def test_structural_files_env_3_leadership_secret_several_bw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '3_leadership_secret_several', 'bw', env=True) def test_structural_files_env_4_several_secrets_rbw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '4_several_secrets', 'rbw', env=True) def test_structural_files_env_4_several_secrets_bw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '4_several_secrets', 'bw', env=True) def test_structural_files_env_4_several_secrets_upper_rbw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '4_several_secrets_upper', 'rbw', env=True) def test_structural_files_env_4_several_secrets_upper_bw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '4_several_secrets_upper', 'bw', env=True) def test_structural_files_env_5_secret_calc_rbw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '5_secret_calc', 'rbw', env=True) def test_structural_files_env_5_secret_calc_bw(): "tests the output" with mock.patch.dict(os.environ, {'ROUGAIL_BITWARDEN_MOCK_ENABLE': '1'}): _test_structural_files(test_dir / '5_secret_calc', 'bw', env=True) def test_structural_files_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_structural_files(test_dir / '8_multi_variable', 'rbw', env=True) assert err.value.errno == 57 def test_structural_files_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_structural_files(test_dir / '8_multi_variable', 'bw', env=True) assert err.value.errno == 57 def test_structural_files_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_structural_files(test_dir / '9_unknown_type', 'rbw', env=True) assert err.value.errno == 56 def test_structural_files_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_structural_files(test_dir / '9_unknown_type', 'bw', env=True) assert err.value.errno == 56