test: cleanup

This commit is contained in:
Piotr Dec 2025-11-25 01:22:08 +01:00
parent 082ab463c1
commit 227d8107aa
Signed by: stawros
GPG key ID: 74B18A3F0F1E99C0
4 changed files with 48 additions and 31 deletions

View file

@ -7,13 +7,9 @@ from injectable import injectable, autowired, Autowired
from app.services import Passwords from app.services import Passwords
class ComplexValueTemplate(Template): class ValueTemplate(Template):
# Pozwala na kropki w nazwach placeholderów, np. ${user.name.first} # Pozwala na kropki i ukośniki w nazwach placeholderów, np. ${user.name/first}
idpattern = r'[_a-zA-Z][_a-zA-Z0-9.]*' idpattern = r'[_a-zA-Z][_a-zA-Z0-9.\/]*'
class SimpleValueTemplate(ComplexValueTemplate):
delimiter = '%'
@injectable @injectable
@ -26,12 +22,9 @@ class Mo:
raw = '' raw = ''
with open(mo_file, "r") as mo: with open(mo_file, "r") as mo:
raw = mo.read() raw = mo.read()
cmp = ComplexValueTemplate(raw) parsed = ValueTemplate(raw)
rendered = cmp.substitute(self._passwords.get_values(cmp.get_identifiers())) mappings = self._passwords.get_values(parsed.get_identifiers())
smp = SimpleValueTemplate(rendered) rendered = parsed.safe_substitute(mappings)
ids = [_id + '.password' for _id in smp.get_identifiers()]
mappings = {k.replace('.password', ''): v for k, v in self._passwords.get_values(ids).items()}
rendered = smp.substitute(mappings)
de_mo_ified = str(mo_file).replace(".mo", "") de_mo_ified = str(mo_file).replace(".mo", "")
with open(de_mo_ified, "w") as mo: with open(de_mo_ified, "w") as mo:
mo.write(rendered) mo.write(rendered)

View file

@ -6,6 +6,30 @@ import keyring
from injectable import injectable from injectable import injectable
from pykeepass import PyKeePass, create_database from pykeepass import PyKeePass, create_database
class KeyRequest:
def __init__(self, prompt: str):
self.field_name = None
self.entry_name = None
self.path = None
self._parse_prompt(prompt)
def _parse_prompt(self, prompt: str):
pass
# k_parts = k.split("/")
# field_name = None
# match len(k_parts):
# case 1:
# field_name = 'password'
# case 2:
# field_name = k_parts[1]
# k = k_parts[0]
# case _:
# output[k] = None
# continue
# key_parts = k.split(".")
# path = key_parts[:-1] if len(key_parts) > 2 else None
# entry_name = key_parts[-1]
@injectable(singleton=True) @injectable(singleton=True)
class Passwords: class Passwords:
@ -28,13 +52,10 @@ class Passwords:
def get_values(self, keys: list[str]) -> dict[str, str]: def get_values(self, keys: list[str]) -> dict[str, str]:
output = {} output = {}
for k in keys: for k in keys:
key_parts = k.split(".") request = KeyRequest(k)
path = key_parts[:-1] if len(key_parts) > 2 else None
entry_name = key_parts[-2]
field_name = key_parts[-1]
with self.open() as kp: with self.open() as kp:
kp_entry = kp.find_entries(path=path, first=True, title=entry_name) kp_entry = kp.find_entries(path=request.path, first=True, title=request.entry_name)
output[k] = self._get_field_value(kp_entry, field_name) output[k] = self._get_field_value(kp_entry, request.field_name)
return output return output
@staticmethod @staticmethod

View file

@ -2,3 +2,4 @@ value: ${sample}
nested: ${some.nested.value} nested: ${some.nested.value}
custom: ${custom/field} custom: ${custom/field}
uname: ${sample/username} uname: ${sample/username}
invalid: ${double/slash/example}

View file

@ -7,7 +7,7 @@ from app.services import Passwords
from app.services.mo import Mo from app.services.mo import Mo
@pytest.fixture @pytest.fixture(scope='class')
def target_path(): def target_path():
p = Path('tests/files/test1/test.yaml') p = Path('tests/files/test1/test.yaml')
# posprzątaj przed testem, gdyby plik istniał z poprzednich uruchomień # posprzątaj przed testem, gdyby plik istniał z poprzednich uruchomień
@ -19,7 +19,7 @@ def target_path():
p.unlink() p.unlink()
@pytest.fixture @pytest.fixture(scope='class')
def test1_content(target_path: Path): def test1_content(target_path: Path):
mo = Mo(Passwords()) mo = Mo(Passwords())
mo.process(Path('tests/files/test1/test.mo.yaml').absolute()) mo.process(Path('tests/files/test1/test.mo.yaml').absolute())
@ -29,20 +29,22 @@ def test1_content(target_path: Path):
content = target_path.read_text() content = target_path.read_text()
assert '${' not in content assert '${' not in content
return yaml.load(content, Loader=yaml.FullLoader) yield yaml.load(content, Loader=yaml.FullLoader)
def test_simple(test1_content: dict): class TestParsing:
assert test1_content['value'] == 'some_pass'
def test_simple(self, test1_content: dict):
assert test1_content['value'] == 'some_pass'
def test_nested(test1_content: dict): def test_nested(self, test1_content: dict):
assert test1_content['nested'] == 'nested_pass' assert test1_content['nested'] == 'nested_pass'
def test_custom_field(self, test1_content: dict):
assert test1_content['custom'] == 'custom_content'
def test_custom_field(test1_content: dict): def test_username_field(self, test1_content: dict):
assert test1_content['custom'] == 'custom_content' assert test1_content['uname'] == 'sample_username'
def test_invalid_key(self, test1_content: dict):
def test_username_field(test1_content: dict): assert test1_content.get('invalid') == 'None'
assert test1_content['uname'] == 'sample_username'