From e28a377efc52e3db2b54cbdf89a218d218b2f0d2 Mon Sep 17 00:00:00 2001 From: Eli Kogan-Wang Date: Tue, 15 Oct 2024 05:03:35 +0200 Subject: [PATCH] Add test for decrypt_to_stdout --- .../{encrypted.cfg => encrypted.cfg.gpg} | 0 src/batou/secrets/tests/test_manage.py | 21 ++++++++++++++++++- src/batou/secrets/tests/test_secrets.py | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) rename src/batou/secrets/tests/fixture/{encrypted.cfg => encrypted.cfg.gpg} (100%) diff --git a/src/batou/secrets/tests/fixture/encrypted.cfg b/src/batou/secrets/tests/fixture/encrypted.cfg.gpg similarity index 100% rename from src/batou/secrets/tests/fixture/encrypted.cfg rename to src/batou/secrets/tests/fixture/encrypted.cfg.gpg diff --git a/src/batou/secrets/tests/test_manage.py b/src/batou/secrets/tests/test_manage.py index 3afa90fd6..2112da54c 100644 --- a/src/batou/secrets/tests/test_manage.py +++ b/src/batou/secrets/tests/test_manage.py @@ -8,7 +8,15 @@ from batou.environment import UnknownEnvironmentError -from ..manage import add_user, reencrypt, remove_user, summary +from ..encryption import GPGEncryptedFile +from ..manage import ( + add_user, + decrypt_to_stdout, + reencrypt, + remove_user, + summary, +) +from .test_secrets import cleartext_file, encrypted_file @pytest.mark.parametrize("func", (add_user, remove_user)) @@ -136,3 +144,14 @@ def test_manage__reencrypt__1(tmp_path, monkeypatch, capsys): assert old[path] != new[path] assert set(old) == set(new) + + +def test_manage__decrypt_to_stdout__1(encrypted_file, capsys): + """It decrypts a file and writes the content to stdout.""" + with GPGEncryptedFile(encrypted_file) as secret: + with open(cleartext_file) as cleartext: + # assert cleartext.read().strip() == secret.cleartext.strip() + assert decrypt_to_stdout(str(encrypted_file)) == 0 + out, err = capsys.readouterr() + assert out == cleartext.read() + assert err == "" diff --git a/src/batou/secrets/tests/test_secrets.py b/src/batou/secrets/tests/test_secrets.py index 7e8ec2e0e..a263708e1 100644 --- a/src/batou/secrets/tests/test_secrets.py +++ b/src/batou/secrets/tests/test_secrets.py @@ -19,7 +19,7 @@ FIXTURE = pathlib.Path(__file__).parent / "fixture" cleartext_file = FIXTURE / "cleartext.cfg" -FIXTURE_ENCRYPTED_CONFIG = FIXTURE / "encrypted.cfg" +FIXTURE_ENCRYPTED_CONFIG = FIXTURE / "encrypted.cfg.gpg" @pytest.fixture(scope="function")