halig/tests/test_config.py
cătălin 7be918c231
All checks were successful
continuous-integration/drone/push Build is passing
tests: add config tests
2022-08-27 16:14:08 +02:00

72 lines
2.2 KiB
Python

import shutil
import tempfile
from pathlib import Path
from tempfile import NamedTemporaryFile
import pytest
import yaml
from halig.config import get_config, Config, EncryptionKeysConfig
from halig.exceptions import ConfigFileDoesNotExist, ConfigFileIsInvalid
@pytest.fixture()
def tmpfile():
_tmpfile = NamedTemporaryFile(delete=False)
with open(_tmpfile.name, "w") as file:
yield file
Path(_tmpfile.name).unlink()
@pytest.fixture()
def tmpdir():
tmpdir = Path(tempfile.mkdtemp())
yield tmpdir
shutil.rmtree(tmpdir)
def test_get_config_raises_config_file_does_not_exist():
with pytest.raises(ConfigFileDoesNotExist):
get_config(Path("/foobar"))
def test_get_config_with_empty_file_raises_invalid_config_file():
with pytest.raises(ConfigFileIsInvalid):
with NamedTemporaryFile() as f:
get_config(Path(f.name))
def test_get_config_raises_invalid_config_file_00(tmpfile):
tmpfile.write("foobar")
with pytest.raises(ConfigFileIsInvalid):
get_config(Path(tmpfile.name))
def test_get_config_raises_invalid_config_file_01(tmpfile):
yaml.dump({"foo": "bar"}, tmpfile, Dumper=yaml.SafeDumper)
with pytest.raises(ConfigFileIsInvalid):
get_config(Path(tmpfile.name))
def test_get_config(tmpdir):
notes_root_path = Path(tmpdir / "notes")
notes_root_path.mkdir(exist_ok=True)
age_binary_path = Path(tmpdir / "age")
age_binary_path.touch(exist_ok=True)
encryption_keys_root_path = Path(tmpdir / "encryption_keys")
encryption_keys_root_path.mkdir(exist_ok=True)
public_key_path = Path(encryption_keys_root_path / "public.key")
public_key_path.touch(exist_ok=True)
private_key_path = Path(encryption_keys_root_path / "private.key")
private_key_path.touch(exist_ok=True)
config = Config(
notes_root_path=notes_root_path,
age_binary_path=age_binary_path,
encryption_keys=EncryptionKeysConfig(
public_key_path=public_key_path, private_key_path=private_key_path
),
)
assert config.notes_root_path == notes_root_path
assert config.age_binary_path == age_binary_path
assert config.encryption_keys.public_key_path == public_key_path
assert config.encryption_keys.private_key_path == private_key_path