rougail-user-data-bitwarden/tests/test_load.py

372 lines
12 KiB
Python
Raw Normal View History

2025-02-05 11:30:51 +01:00
import os
from pathlib import Path
2025-02-12 15:36:48 +01:00
from rougail import Rougail, RougailConfig
from rougail.error import DictConsistencyError
2025-02-05 11:30:51 +01:00
#########################
from rougail.user_data_bitwarden import RougailUserDataBitwarden as RougailUserData
from json import load, dump
#########################
2025-02-12 15:36:48 +01:00
from pytest import raises
from unittest import mock
2025-02-05 11:30:51 +01:00
2025-02-12 15:36:48 +01:00
from rougail_tests.utils import config_to_dict
2025-02-05 11:30:51 +01:00
2025-02-12 15:36:48 +01:00
test_dir = Path(__file__).parent / 'structures'
2025-02-05 11:30:51 +01:00
def _test_dictionaries(test_dir, command, env=False):
2025-02-12 15:36:48 +01:00
rougailconfig = RougailConfig.copy()
rougailconfig['main_dictionaries'] = [str(test_dir)]
# rougailconfig['tiramisu_cache'] = "cache.py"
2025-02-05 11:30:51 +01:00
rougailconfig['step.user_data'] = ['bitwarden']
2025-03-01 18:04:38 +01:00
rougailconfig['bitwarden.command'] = command
2025-02-05 11:30:51 +01:00
rougail = Rougail(rougailconfig)
config = rougail.run()
# loads variables in the tiramisu config
errors = RougailUserData(config, rougailconfig=rougailconfig).run()
#expected output
2025-02-17 15:25:59 +01:00
config_dict = dict(config_to_dict(config.forcepermissive.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)
2025-03-01 18:04:38 +01:00
if not ok_file.is_file():
ok_file = Path('tests') / 'results' / test_dir.name / 'makedict' / base_filename
2025-02-05 11:30:51 +01:00
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)
2025-03-01 18:04:38 +01:00
if not errors_file.is_file():
errors_file = Path('tests') / 'results' / test_dir.name / 'errors' / base_filename
2025-02-05 11:30:51 +01:00
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)
# expected_errors = {
# 'errors': [],
# 'warnings': [],
# }
assert expected_errors == errors
#
assert expected == config_dict
2025-03-01 18:04:38 +01:00
def test_dictionaries_1_secret_rbw():
"tests the output"
_test_dictionaries(test_dir / '1_secret', 'rbw')
def test_dictionaries_1_secret_bw():
"tests the output"
_test_dictionaries(test_dir / '1_secret', 'bw')
def test_dictionaries_1_secret_unknown_rbw():
"tests the output"
_test_dictionaries(test_dir / '1_secret_unknown', 'rbw')
def test_dictionaries_1_secret_unknown_bw():
"tests the output"
_test_dictionaries(test_dir / '1_secret_unknown', 'bw')
def test_dictionaries_2_username_secret_rbw():
"tests the output"
_test_dictionaries(test_dir / '2_username_secret', 'rbw')
def test_dictionaries_2_username_secret_bw():
"tests the output"
_test_dictionaries(test_dir / '2_username_secret', 'bw')
def test_dictionaries_2_username_secret_upper_rbw():
"tests the output"
_test_dictionaries(test_dir / '2_username_secret_upper', 'rbw')
def test_dictionaries_2_username_secret_upper_bw():
"tests the output"
_test_dictionaries(test_dir / '2_username_secret_upper', 'bw')
def test_dictionaries_2_username_secret_invalid_rbw():
"tests the output"
_test_dictionaries(test_dir / '2_username_secret_invalid', 'rbw')
def test_dictionaries_2_username_secret_invalid_bw():
"tests the output"
_test_dictionaries(test_dir / '2_username_secret_invalid', 'bw')
def test_dictionaries_2_username_secret_hidden_rbw():
"tests the output"
_test_dictionaries(test_dir / '2_username_secret_hidden', 'rbw')
def test_dictionaries_2_username_secret_hidden_bw():
"tests the output"
_test_dictionaries(test_dir / '2_username_secret_hidden', 'bw')
def test_dictionaries_3_leadership_secret_rbw():
2025-02-12 15:36:48 +01:00
"tests the output"
2025-03-01 18:04:38 +01:00
_test_dictionaries(test_dir / '3_leadership_secret', 'rbw')
2025-02-12 15:36:48 +01:00
2025-03-01 18:04:38 +01:00
def test_dictionaries_3_leadership_secret_bw():
2025-02-12 15:36:48 +01:00
"tests the output"
2025-03-01 18:04:38 +01:00
_test_dictionaries(test_dir / '3_leadership_secret', 'bw')
2025-02-12 15:36:48 +01:00
2025-03-01 18:04:38 +01:00
def test_dictionaries_3_leadership_secret_several_rbw():
"tests the output"
2025-03-01 18:04:38 +01:00
_test_dictionaries(test_dir / '3_leadership_secret_several', 'rbw')
2025-03-01 18:04:38 +01:00
def test_dictionaries_3_leadership_secret_several_bw():
2025-02-17 15:25:59 +01:00
"tests the output"
2025-03-01 18:04:38 +01:00
_test_dictionaries(test_dir / '3_leadership_secret_several', 'bw')
2025-02-17 15:25:59 +01:00
2025-03-01 18:04:38 +01:00
def test_dictionaries_4_several_secrets_rbw():
2025-02-12 15:36:48 +01:00
"tests the output"
2025-03-01 18:04:38 +01:00
_test_dictionaries(test_dir / '4_several_secrets', 'rbw')
2025-02-12 15:36:48 +01:00
2025-03-01 18:04:38 +01:00
def test_dictionaries_4_several_secrets_bw():
2025-02-12 15:36:48 +01:00
"tests the output"
2025-03-01 18:04:38 +01:00
_test_dictionaries(test_dir / '4_several_secrets', 'bw')
2025-02-12 15:36:48 +01:00
2025-03-01 18:04:38 +01:00
def test_dictionaries_4_several_secrets_upper_rbw():
2025-02-12 15:36:48 +01:00
"tests the output"
2025-03-01 18:04:38 +01:00
_test_dictionaries(test_dir / '4_several_secrets_upper', 'rbw')
2025-02-12 15:36:48 +01:00
2025-03-01 18:04:38 +01:00
def test_dictionaries_4_several_secrets_upper_bw():
"tests the output"
_test_dictionaries(test_dir / '4_several_secrets_upper', 'bw')
2025-03-19 10:22:27 +01:00
def test_dictionaries_5_secret_calc_rbw():
2025-02-12 15:36:48 +01:00
"tests the output"
2025-03-19 10:22:27 +01:00
_test_dictionaries(test_dir / '5_secret_calc', 'rbw')
2025-03-01 18:04:38 +01:00
2025-03-19 10:22:27 +01:00
def test_dictionaries_5_secret_calc_bw():
2025-03-01 18:04:38 +01:00
"tests the output"
2025-03-19 10:22:27 +01:00
_test_dictionaries(test_dir / '5_secret_calc', 'bw')
2025-02-05 11:30:51 +01:00
2025-04-09 14:20:56 +02:00
def test_dictionaries_6_no_username_rbw():
"tests the output"
_test_dictionaries(test_dir / '6_no_username', 'rbw')
def test_dictionaries_6_no_username_bw():
"tests the output"
_test_dictionaries(test_dir / '6_no_username', 'bw')
2025-03-01 18:04:38 +01:00
def test_dictionaries_8_multi_variable_rbw():
"tests the output"
with raises(DictConsistencyError) as err:
_test_dictionaries(test_dir / '8_multi_variable', 'rbw')
2025-03-19 10:22:27 +01:00
assert err.value.errno == 57
2025-03-01 18:04:38 +01:00
def test_dictionaries_8_multi_variable_bw():
2025-02-05 11:30:51 +01:00
"tests the output"
2025-02-12 15:36:48 +01:00
with raises(DictConsistencyError) as err:
2025-03-01 18:04:38 +01:00
_test_dictionaries(test_dir / '8_multi_variable', 'bw')
2025-03-19 10:22:27 +01:00
assert err.value.errno == 57
2025-02-05 11:30:51 +01:00
2025-03-01 18:04:38 +01:00
def test_dictionaries_9_unknown_type_rbw():
"tests the output"
with raises(DictConsistencyError) as err:
_test_dictionaries(test_dir / '9_unknown_type', 'rbw')
2025-03-19 10:22:27 +01:00
assert err.value.errno == 56
2025-03-01 18:04:38 +01:00
def test_dictionaries_9_unknown_type_bw():
2025-02-05 11:30:51 +01:00
"tests the output"
2025-02-12 15:36:48 +01:00
with raises(DictConsistencyError) as err:
2025-03-01 18:04:38 +01:00
_test_dictionaries(test_dir / '9_unknown_type', 'bw')
2025-03-19 10:22:27 +01:00
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