feat: move edit_note function to a separate file
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
cătălin 2022-08-28 12:12:31 +02:00
commit 75d623a124
Signed by: catalin
GPG key ID: 0178DF42F43E5FD2
10 changed files with 191 additions and 42 deletions

21
tests/file_fixtures.py Normal file
View file

@ -0,0 +1,21 @@
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)

View file

@ -1,5 +1,3 @@
import shutil
import tempfile
from pathlib import Path
from tempfile import NamedTemporaryFile
@ -8,21 +6,7 @@ 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)
from tests.file_fixtures import tmpfile, tmpdir # noqa: 401
def test_get_config_raises_config_file_does_not_exist():
@ -36,19 +20,19 @@ def test_get_config_with_empty_file_raises_invalid_config_file():
get_config(Path(f.name))
def test_get_config_raises_invalid_config_file_00(tmpfile):
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_invalid_config_file_01(tmpfile):
def test_get_config_raises_invalid_config_file_01(tmpfile): # noqa: F811
yaml.dump({"foo": "bar"}, tmpfile, Dumper=yaml.SafeDumper)
with pytest.raises(ConfigFileIsInvalid):
get_config(Path(tmpfile.name))
def test_get_config(tmpdir):
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")

79
tests/test_edit.py Normal file
View file

@ -0,0 +1,79 @@
from pathlib import Path
from subprocess import CalledProcessError
import pytest
from halig.config import Config, EncryptionKeysConfig
from halig.edit 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)