halig/tests/test_config.py
cătălin d9eb99b72e
Some checks failed
continuous-integration/drone/push Build is failing
tests: add utils.py tests
2022-08-28 22:24:16 +02:00

70 lines
2.4 KiB
Python

from pathlib import Path
from tempfile import NamedTemporaryFile
import pytest
import yaml
from pydantic import ValidationError
from halig.config import get_config, Config, EncryptionKeysConfig
from halig.exceptions import ConfigFileDoesNotExist, ConfigFileIsInvalid
from tests.file_fixtures import tmpfile, tmpdir # noqa: 401
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): # noqa: F811
tmpfile.write("foobar")
with pytest.raises(ConfigFileIsInvalid):
get_config(Path(tmpfile.name))
def test_get_config_raises_validation_error(tmpfile): # noqa: F811
yaml.dump({"foo": "bar"}, tmpfile, Dumper=yaml.SafeDumper)
with pytest.raises(ValidationError):
get_config(Path(tmpfile.name))
def test_get_config(tmpdir): # noqa: F811
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
),
)
raw_config = config.dict()
assert (
config.notes_root_path == notes_root_path == Path(raw_config["notes_root_path"])
)
assert (
config.age_binary_path == age_binary_path == Path(raw_config["age_binary_path"])
)
assert (
config.encryption_keys.public_key_path
== public_key_path
== Path(raw_config["encryption_keys"]["public_key_path"])
)
assert (
config.encryption_keys.private_key_path
== private_key_path
== Path(raw_config["encryption_keys"]["private_key_path"])
)