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

103 lines
3.1 KiB
Python

import os
from pytest import fixture # , raises
from pathlib import Path
from rougail import Rougail
#########################
from dotenv import load_dotenv
from rougail.user_data_bitwarden import RougailUserDataBitwarden as RougailUserData
from json import load, dump
#########################
from rougail_tests.utils import get_structures_list, get_rougail_config, get_values_for_config, config_to_dict
EXT = "env"
#########################
#let's save the original environment
save = os.environ.copy()
#########################
excludes = []
test_ok = get_structures_list(excludes)
test_ok = [Path('../rougail-tests/structures/00_6secret')]
def idfn(fixture_value):
return fixture_value.name
@fixture(scope="module", params=test_ok, ids=idfn)
def test_dir(request):
return request.param
def _test_dictionaries(test_dir, namespace, ext):
rougailconfig = get_rougail_config(test_dir, namespace)
if not rougailconfig:
return
##################################
rougailconfig['step.user_data'] = ['bitwarden']
##################################
dir_name = 'test'
if namespace:
dir_name += '_namespace'
elif (test_dir / 'force_namespace').is_file():
return
rougail = Rougail(rougailconfig)
config = rougail.run()
##################################
if not has_secrets(config):
return
##################################
# loads variables in the tiramisu config
errors = RougailUserData(config, rougailconfig=rougailconfig).run()
#expected output
config_dict = dict(config_to_dict(config.value.get()))
ok_file = Path('tests') / 'results' / dir_name / test_dir.name / 'makedict' / 'bitwarden.json'
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' / dir_name / test_dir.name / 'errors' / 'bitwarden.json'
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
#
config.property.read_only()
assert expected == config_dict
######################################
#teardown: set the original environement again
os.environ = save.copy()
######################################
def has_secrets(optiondescription):
for option in optiondescription:
if option.isoptiondescription():
if has_secrets(option):
return True
elif option.information.get('type', None) == 'secret':
return True
return False
def test_dictionaries_all(test_dir):
"tests the output"
_test_dictionaries(test_dir, False, EXT)
def test_dictionaries_namespace_all(test_dir):
"tests the output"
_test_dictionaries(test_dir, True, EXT)