feat: move project from poetry to pdm, rewrite from scratch and add
basic `notebooks`, `edit` and `show` commands
This commit is contained in:
parent
d9eb99b72e
commit
d3ad87211e
35 changed files with 1309 additions and 1434 deletions
0
tests/commands/__init__.py
Normal file
0
tests/commands/__init__.py
Normal file
98
tests/conftest.py
Normal file
98
tests/conftest.py
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
from pyrage.ssh import Identity, Recipient
|
||||
|
||||
from halig.encryption import Encryptor
|
||||
from halig.settings import Settings
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def halig_ssh_public_key():
|
||||
return "ssh-ed25519 " \
|
||||
"AAAAC3NzaC1lZDI1NTE5AAAAIGjHhIF/DlVCb2dRFMlKia7nij1Aq+zRDCaMIwe/VKDh" \
|
||||
" foo@bar"
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def halig_ssh_private_key():
|
||||
return """-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
||||
QyNTUxOQAAACBox4SBfw5VQm9nURTJSomu54o9QKvs0QwmjCMHv1Sg4QAAAJhvD2Jxbw9i
|
||||
cQAAAAtzc2gtZWQyNTUxOQAAACBox4SBfw5VQm9nURTJSomu54o9QKvs0QwmjCMHv1Sg4Q
|
||||
AAAEAZANW15ieou1ds73BlM1nqzyZ2A0454JnB3QirZycGv2jHhIF/DlVCb2dRFMlKia7n
|
||||
ij1Aq+zRDCaMIwe/VKDhAAAAEXJvb3RANGNjNWUxOWYyYThiAQIDBA==
|
||||
-----END OPENSSH PRIVATE KEY-----
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def ssh_identity(halig_ssh_private_key: str) -> Identity:
|
||||
return Identity.from_buffer(halig_ssh_private_key.encode())
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def ssh_recipient(halig_ssh_public_key: str) -> Recipient:
|
||||
return Recipient.from_str(halig_ssh_public_key)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def halig_path(fs, halig_ssh_public_key, halig_ssh_private_key) -> Path:
|
||||
ssh_path = Path("~/.ssh").expanduser()
|
||||
ssh_path.mkdir(parents=True)
|
||||
|
||||
with (ssh_path / "id_ed25519").open("w") as f:
|
||||
f.write(halig_ssh_private_key)
|
||||
|
||||
with (ssh_path / "id_ed25519.pub").open("w") as f:
|
||||
f.write(halig_ssh_public_key)
|
||||
|
||||
halig_path = Path("~/.config/halig").expanduser()
|
||||
halig_path.mkdir(parents=True)
|
||||
return halig_path
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def notebooks_path(halig_path) -> Path:
|
||||
notebooks_path = Path("~/Notebooks").expanduser()
|
||||
notebooks_path.mkdir(parents=True)
|
||||
return notebooks_path
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def settings(notebooks_path: Path) -> Settings:
|
||||
return Settings(notebooks_root_path=notebooks_path)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def settings_file_path(halig_path: Path, notebooks_path: Path) -> Path:
|
||||
yaml_file = halig_path / "halig.yml"
|
||||
yaml_file.touch()
|
||||
s = Settings(notebooks_root_path=notebooks_path)
|
||||
# `.dict()` doesn't serialize some fields that yaml doesn't understand
|
||||
serialized = json.loads(s.json())
|
||||
with yaml_file.open("w") as f:
|
||||
yaml.safe_dump(serialized, f)
|
||||
return yaml_file
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def empty_file_path(halig_path: Path) -> Path:
|
||||
empty_path = halig_path / "empty"
|
||||
empty_path.touch()
|
||||
return empty_path
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def notebooks_root_path_envvar(notebooks_path: Path):
|
||||
os.environ["HALIG_NOTEBOOKS_ROOT_PATH"] = str(notebooks_path)
|
||||
yield notebooks_path
|
||||
del os.environ["HALIG_NOTEBOOKS_ROOT_PATH"]
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def encryptor(settings: Settings) -> Encryptor:
|
||||
return Encryptor(settings)
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
import shutil
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from tempfile import NamedTemporaryFile
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@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)
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
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"])
|
||||
)
|
||||
|
|
@ -1,79 +0,0 @@
|
|||
from pathlib import Path
|
||||
from subprocess import CalledProcessError
|
||||
|
||||
import pytest
|
||||
|
||||
from halig.config import Config, EncryptionKeysConfig
|
||||
from halig.edit_note import edit_note
|
||||
from halig.exceptions import CouldNotEditTempfile
|
||||
from tests.file_fixtures import tmpfile, tempfile # noqa: F401
|
||||
|
||||
from sh import age, ssh_keygen, yes
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def notes_path(tmpdir): # noqa: F811
|
||||
notes_path = Path(tmpdir.dirname) / "notes"
|
||||
notes_path.mkdir(exist_ok=True)
|
||||
|
||||
yield notes_path
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def config(notes_path): # noqa: F811
|
||||
keys_path = Path(notes_path.parent / ".ssh")
|
||||
keys_path.mkdir(exist_ok=True)
|
||||
private_key_path = keys_path / "key"
|
||||
public_key_path = keys_path / "key.pub"
|
||||
|
||||
ssh_keygen(yes(_piped=True), t="ed25519", f=str(private_key_path))
|
||||
|
||||
config = Config(
|
||||
notes_root_path=notes_path,
|
||||
encryption_keys=EncryptionKeysConfig(
|
||||
public_key_path=public_key_path,
|
||||
private_key_path=private_key_path,
|
||||
),
|
||||
)
|
||||
|
||||
note_path = notes_path / "note"
|
||||
note_path.touch(exist_ok=True)
|
||||
with open(note_path, "w") as f:
|
||||
f.write("this is a test")
|
||||
|
||||
age(
|
||||
"-R",
|
||||
str(config.encryption_keys.public_key_path),
|
||||
str(note_path),
|
||||
_out=f"{note_path}.age",
|
||||
)
|
||||
|
||||
yield config
|
||||
|
||||
|
||||
def mock_subprocess(args: list):
|
||||
with open(args[1], "w") as f:
|
||||
f.write("mocked")
|
||||
|
||||
|
||||
def test_edit_note(config: Config, mocker): # noqa: F811
|
||||
mocker.patch("subprocess.call", side_effect=mock_subprocess)
|
||||
edit_note(config.notes_root_path / "note.age", config)
|
||||
assert (
|
||||
age(
|
||||
"-d",
|
||||
"-i",
|
||||
config.encryption_keys.private_key_path,
|
||||
config.notes_root_path / "note.age",
|
||||
)
|
||||
== "mocked"
|
||||
)
|
||||
|
||||
|
||||
def test_edit_not_raises_could_not_edit_tempfile(config: Config, mocker): # noqa: F811
|
||||
mocker.patch(
|
||||
"subprocess.call",
|
||||
side_effect=CalledProcessError(returncode=1, cmd="foo", output="mocked error"),
|
||||
)
|
||||
with pytest.raises(CouldNotEditTempfile):
|
||||
edit_note(config.notes_root_path / "note.age", config)
|
||||
54
tests/test_encryption.py
Normal file
54
tests/test_encryption.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
from pyrage import decrypt, encrypt, x25519
|
||||
|
||||
from halig.encryption import Encryptor
|
||||
from halig.settings import Settings
|
||||
|
||||
|
||||
def test_instance_encryptor_from_age_keys(halig_path, notebooks_path):
|
||||
identity = x25519.Identity.generate()
|
||||
identity_path = halig_path / "identity.key"
|
||||
identity_path.touch()
|
||||
recipient_path = halig_path / "recipient.key"
|
||||
recipient_path.touch()
|
||||
with identity_path.open("w") as f:
|
||||
f.write(str(identity))
|
||||
|
||||
with recipient_path.open("w") as f:
|
||||
f.write(str(identity.to_public()))
|
||||
|
||||
settings = Settings(
|
||||
notebooks_root_path=notebooks_path,
|
||||
identity_path=identity_path,
|
||||
recipient_path=recipient_path,
|
||||
)
|
||||
assert Encryptor(settings)
|
||||
|
||||
|
||||
def test_encrypt(encryptor: Encryptor, ssh_identity):
|
||||
unencrypted_data = "foo"
|
||||
encrypted_data = encryptor.encrypt(unencrypted_data)
|
||||
|
||||
assert isinstance(encrypted_data, bytes)
|
||||
assert unencrypted_data == decrypt(encrypted_data, [ssh_identity]).decode()
|
||||
|
||||
|
||||
def test_encrypt_bytes(encryptor: Encryptor, ssh_identity):
|
||||
unencrypted_data = b"foo"
|
||||
encrypted_data = encryptor.encrypt(unencrypted_data)
|
||||
|
||||
assert isinstance(encrypted_data, bytes)
|
||||
assert unencrypted_data == decrypt(encrypted_data, [ssh_identity])
|
||||
|
||||
|
||||
def test_decrypt(encryptor: Encryptor, ssh_recipient):
|
||||
unencrypted_data = "foo"
|
||||
encrypted_data = encrypt(unencrypted_data.encode(), [ssh_recipient])
|
||||
decrypted_data = encryptor.decrypt(encrypted_data)
|
||||
assert decrypted_data.decode() == unencrypted_data
|
||||
|
||||
|
||||
def test_decrypt_bytes(encryptor: Encryptor, ssh_recipient):
|
||||
unencrypted_data = b"foo"
|
||||
encrypted_data = encrypt(unencrypted_data, [ssh_recipient])
|
||||
decrypted_data = encryptor.decrypt(encrypted_data)
|
||||
assert decrypted_data == unencrypted_data
|
||||
|
|
@ -1,95 +0,0 @@
|
|||
from pathlib import Path
|
||||
from subprocess import CalledProcessError
|
||||
|
||||
import pytest
|
||||
|
||||
from halig.config import Config, EncryptionKeysConfig
|
||||
from halig.edit_note import edit_note
|
||||
from halig.exceptions import CouldNotEditTempfile
|
||||
from halig.new_note import new_note
|
||||
from tests.file_fixtures import tmpfile, tempfile # noqa: F401
|
||||
|
||||
from sh import age, ssh_keygen, yes
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def notes_path(tmpdir): # noqa: F811
|
||||
notes_path = Path(tmpdir.dirname) / "notes"
|
||||
notes_path.mkdir(exist_ok=True)
|
||||
|
||||
yield notes_path
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def config(notes_path): # noqa: F811
|
||||
keys_path = Path(notes_path.parent / ".ssh")
|
||||
keys_path.mkdir(exist_ok=True)
|
||||
private_key_path = keys_path / "key"
|
||||
public_key_path = keys_path / "key.pub"
|
||||
|
||||
ssh_keygen(yes(_piped=True), t="ed25519", f=str(private_key_path))
|
||||
|
||||
config = Config(
|
||||
notes_root_path=notes_path,
|
||||
encryption_keys=EncryptionKeysConfig(
|
||||
public_key_path=public_key_path,
|
||||
private_key_path=private_key_path,
|
||||
),
|
||||
)
|
||||
|
||||
note_path = notes_path / "note"
|
||||
note_path.touch(exist_ok=True)
|
||||
with open(note_path, "w") as f:
|
||||
f.write("this is a test")
|
||||
|
||||
age(
|
||||
"-R",
|
||||
str(config.encryption_keys.public_key_path),
|
||||
str(note_path),
|
||||
_out=f"{note_path}.age",
|
||||
)
|
||||
|
||||
yield config
|
||||
|
||||
|
||||
def mock_subprocess(args: list):
|
||||
with open(args[1], "a") as f:
|
||||
f.write(" mocked")
|
||||
|
||||
|
||||
def test_new_note_default_template_data(config: Config, mocker): # noqa: F811
|
||||
mocker.patch("subprocess.call", side_effect=mock_subprocess)
|
||||
new_note(config.notes_root_path / "new_default_note.age", config)
|
||||
note_contents = age(
|
||||
"-d",
|
||||
"-i",
|
||||
config.encryption_keys.private_key_path,
|
||||
config.notes_root_path / "new_default_note.age",
|
||||
)
|
||||
assert note_contents == f"{config._default_template_data} mocked"
|
||||
|
||||
|
||||
def test_new_note_custom_template_data(config: Config, mocker): # noqa: F811
|
||||
mocker.patch("subprocess.call", side_effect=mock_subprocess)
|
||||
template_path = config.notes_root_path / "template.halig"
|
||||
template_path.touch(exist_ok=True)
|
||||
with open(template_path, "w") as f:
|
||||
f.write("template string")
|
||||
new_note(config.notes_root_path / "new_note.age", config)
|
||||
template_path.unlink()
|
||||
note_contents = age(
|
||||
"-d",
|
||||
"-i",
|
||||
config.encryption_keys.private_key_path,
|
||||
config.notes_root_path / "new_note.age",
|
||||
)
|
||||
assert note_contents == "template string mocked"
|
||||
|
||||
|
||||
def test_new_not_raises_could_not_edit_tempfile(config: Config, mocker): # noqa: F811
|
||||
mocker.patch(
|
||||
"subprocess.call",
|
||||
side_effect=CalledProcessError(returncode=1, cmd="foo", output="mocked error"),
|
||||
)
|
||||
with pytest.raises(CouldNotEditTempfile):
|
||||
edit_note(config.notes_root_path / "note.age", config)
|
||||
42
tests/test_settings.py
Normal file
42
tests/test_settings.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from halig.settings import Settings, load_from_file
|
||||
|
||||
|
||||
def test_settings_from_env(settings: Settings, notebooks_root_path_envvar):
|
||||
from_env_settings = Settings() # type: ignore[call-arg]
|
||||
assert from_env_settings.notebooks_root_path == settings.notebooks_root_path
|
||||
|
||||
|
||||
def test_settings_from_non_existing_file_raises_value_error():
|
||||
with pytest.raises(ValueError, match="field required"):
|
||||
Settings() # type: ignore[call-arg]
|
||||
|
||||
|
||||
def test_load_from_file(notebooks_path: Path, settings_file_path: Path):
|
||||
settings = load_from_file(settings_file_path)
|
||||
assert settings.notebooks_root_path == notebooks_path
|
||||
|
||||
|
||||
def test_load_from_non_xdg_home_config_raises_file_not_found_error(fs):
|
||||
path = Path("~/.config").expanduser()
|
||||
with pytest.raises(FileNotFoundError, match=f"File {path} does not exist"):
|
||||
load_from_file()
|
||||
|
||||
|
||||
def test_load_from_existing_standard_file(settings_file_path: Path, settings: Settings):
|
||||
standard_settings = load_from_file()
|
||||
assert standard_settings.notebooks_root_path == settings.notebooks_root_path
|
||||
|
||||
|
||||
def test_load_from_empty_file_raises_value_error(empty_file_path: Path):
|
||||
with pytest.raises(ValueError, match=f"File {empty_file_path} is empty"):
|
||||
load_from_file(empty_file_path)
|
||||
|
||||
|
||||
def test_load_from_non_existing_file_path_raises_file_not_found_error(halig_path: Path):
|
||||
file = halig_path / "some_invalid_file.yml"
|
||||
with pytest.raises(FileNotFoundError, match=f"File {file} does not exist"):
|
||||
load_from_file(file)
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from halig.utils import resolve_path
|
||||
|
||||
|
||||
def test_resolve_absolute_path():
|
||||
path = Path("/foo/bar/baz")
|
||||
assert resolve_path(path) == path
|
||||
|
||||
|
||||
def test_resolve_user_path():
|
||||
path = Path("~/foo/bar/baz")
|
||||
assert resolve_path(path) == path.expanduser()
|
||||
|
||||
|
||||
def test_resolve_path_with_envvars():
|
||||
os.environ["FOO"] = "foo"
|
||||
os.environ["BAR"] = "bar"
|
||||
path = Path("/${FOO}/${BAR}")
|
||||
assert resolve_path(path) == Path("/foo/bar")
|
||||
|
||||
|
||||
def test_resolve_relative_path():
|
||||
path = Path("foo/bar/baz")
|
||||
assert resolve_path(path) == path.resolve()
|
||||
|
||||
|
||||
def test_resolve_path_all():
|
||||
os.environ["FOO"] = "foo"
|
||||
os.environ["BAR"] = "bar"
|
||||
path = Path("foo/bar/$FOO/../$BAR")
|
||||
assert resolve_path(path) == Path(os.path.expandvars(path)).resolve().expanduser()
|
||||
Loading…
Add table
Add a link
Reference in a new issue