Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raddessi committed Jan 27, 2024
1 parent b04ccba commit 1da60f1
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 80 deletions.
61 changes: 61 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
"""Configuration for the pytest test suite."""

import functools
import os
import shutil
import sys
from pathlib import Path
from typing import Iterator

import gnupg
import pytest
from _pytest.fixtures import FixtureRequest
from _pytest.tmpdir import TempPathFactory
from click.testing import CliRunner

SALT_PILLAR_DATADIR = "./tests/data/salt_pillar"


@pytest.fixture
def runner() -> Iterator[CliRunner]:
Expand Down Expand Up @@ -48,3 +56,56 @@ def wrapper(*args, **kwargs): # type: ignore
cli_runner = class_()

yield cli_runner


@pytest.fixture(name="pytest_gnupg_keyring_dirpath")
def gnupg_keyring_dirpath() -> str:
"""A fixture to return the path to the pytest gnupg keyring directory.
Returns:
str: dirpath of the keyring
"""
return "./tests/data/gnupg"


@pytest.fixture(
name="salt_pillar_fpath",
scope="session",
params=[
"encrypted_file.gpg",
"multiple_keys_in_yaml.sls",
"one_key_in_yaml.sls",
"nonconforming_file_type.txt",
],
)
def salt_pillar_fpath_fixture(
tmp_path_factory: TempPathFactory, request: FixtureRequest
) -> str:
"""A fixture returning the path to a temp directory to use for pillar data.
Args:
tmp_path_factory: pytest-tmpdir fixture
request: pytest request fixture
Returns:
str: Of the temp dir
"""
temp_fpath = os.path.join(tmp_path_factory.mktemp("data"), request.param)
shutil.copy(os.path.join(SALT_PILLAR_DATADIR, request.param), temp_fpath)
return temp_fpath


@pytest.fixture(name="new_gnupg_homedir", scope="session")
def new_gnupg_homedir_fixture(tmp_path_factory: TempPathFactory) -> Path:
"""A fixture returning a temp dir path with a gnupg keyring inside.
Args:
tmp_path_factory: pytest-tmpdir fixture
Returns:
Path: Of the temp dir
"""
temp_fpath = tmp_path_factory.mktemp("gnupg")
gpg = gnupg.GPG(gnupghome=temp_fpath)
gpg.gen_key_input(key_type="RSA", key_length=1024)
return temp_fpath
28 changes: 24 additions & 4 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,59 @@
from salt_gnupg_rotate.main import main


def test_main_return_value(mocker: MockerFixture) -> None:
def test_main_return_value(
mocker: MockerFixture, pytest_gnupg_keyring_dirpath: str
) -> None:
"""Verify that main returns as expected.
Args:
mocker: pytest-mock mocker fixture
pytest_gnupg_keyring_dirpath: pytest fixture
"""
mocked_process_directory = mocker.patch(
"salt_gnupg_rotate.main.process_directory",
return_value=2,
)
main(
dirpath="./tests/data/salt_pillar",
decryption_gpg_homedir=pytest_gnupg_keyring_dirpath,
encryption_gpg_homedir=pytest_gnupg_keyring_dirpath,
recipient="pytest",
)
mocked_process_directory.assert_called()


def test_main_return_value_on_write(mocker: MockerFixture) -> None:
def test_main_return_value_on_write(
mocker: MockerFixture, pytest_gnupg_keyring_dirpath: str
) -> None:
"""Verify that main returns as expected when write=True.
Args:
mocker: pytest-mock mocker fixture
pytest_gnupg_keyring_dirpath: pytest fixture
"""
mocked_process_directory = mocker.patch(
"salt_gnupg_rotate.main.process_directory",
return_value=2,
)
main(
dirpath="./tests/data/salt_pillar",
decryption_gpg_homedir=pytest_gnupg_keyring_dirpath,
encryption_gpg_homedir=pytest_gnupg_keyring_dirpath,
recipient="pytest",
write=True,
)
mocked_process_directory.assert_called()


def test_main_gpg_keyring_missing_secret_key(mocker: MockerFixture) -> None:
def test_main_gpg_keyring_missing_secret_key(
mocker: MockerFixture, pytest_gnupg_keyring_dirpath: str
) -> None:
"""Verify that main raises as expected when a secret key is missing.
Args:
mocker: pytest-mock mocker fixture
pytest_gnupg_keyring_dirpath: pytest fixture
"""
mocked_gpg = mocker.patch("salt_gnupg_rotate.main.gnupg.GPG")
mocked_gpg.side_effect = [
Expand All @@ -56,22 +69,29 @@ def test_main_gpg_keyring_missing_secret_key(mocker: MockerFixture) -> None:
with pytest.raises(NameError):
main(
dirpath="./tests/data/salt_pillar",
decryption_gpg_homedir=pytest_gnupg_keyring_dirpath,
encryption_gpg_homedir=pytest_gnupg_keyring_dirpath,
recipient="pytest",
)


def test_main_decryption_error(mocker: MockerFixture) -> None:
def test_main_decryption_error(
mocker: MockerFixture, pytest_gnupg_keyring_dirpath: str
) -> None:
"""Verify that main raises as expected on a decryption error.
Args:
mocker: pytest-mock mocker fixture
pytest_gnupg_keyring_dirpath: pytest fixture
"""
mocked_process_directory = mocker.patch(
"salt_gnupg_rotate.main.process_directory",
side_effect=DecryptionError,
)
main(
dirpath="./tests/data/salt_pillar",
decryption_gpg_homedir=pytest_gnupg_keyring_dirpath,
encryption_gpg_homedir=pytest_gnupg_keyring_dirpath,
recipient="pytest",
)
mocked_process_directory.assert_called()
Loading

0 comments on commit 1da60f1

Please sign in to comment.