Skip to content

Commit

Permalink
refactor: make the example_age_key fixture reusable in all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuller committed Apr 30, 2024
1 parent 45dc834 commit 19ae926
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 31 deletions.
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pathlib import Path

import pytest

ROOT = Path(__file__).parent.parent
EXAMPLE_PATH = ROOT / "example"


@pytest.fixture()
def example_age_key() -> str:
return str(EXAMPLE_PATH / "config" / "age.key")
10 changes: 2 additions & 8 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import os
from pathlib import Path

import pytest
from saltfactories.cli.call import SaltCall
from saltfactories.daemons.minion import SaltMinion

ROOT = Path(__file__).parent.parent.parent
EXAMPLE_PATH = ROOT / "example"
from tests.conftest import EXAMPLE_PATH, ROOT

MINION_CONFIG = {
"file_client": "local",
"master_type": "disable",
Expand All @@ -31,8 +30,3 @@ def salt_factories_config() -> dict[str, str | int | bool | None]:
@pytest.fixture()
def salt_call_cli(minion: SaltMinion) -> SaltCall:
return minion.salt_call_cli()


@pytest.fixture()
def example_age_key() -> str:
return str(EXAMPLE_PATH / "config" / "age.key")
9 changes: 6 additions & 3 deletions tests/integration/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ def test_encrypt__passphrase(caplog: pytest.LogCaptureFixture) -> None:
assert secure_value.decrypt("woah that is so secret") == "another secret"


def test_encrypt__single_recipient(caplog: pytest.LogCaptureFixture) -> None:
def test_encrypt__single_recipient(
caplog: pytest.LogCaptureFixture,
example_age_key: str,
) -> None:
# Only keep INFO log records
caplog.set_level(logging.INFO)
# Run the CLI tool
main(["-i", "example/config/age.key", "enc", "foo"])
main(["-i", example_age_key, "enc", "foo"])
# Ensure we get an identity secure value string
secure_value_string = caplog.record_tuples[0][2]
secure_value = parse_secure_value(secure_value_string)
assert isinstance(secure_value, IdentitySecureValue)
# Ensure we can decrypt it using the same identity
assert secure_value.decrypt(read_identity_file("example/config/age.key")) == "foo"
assert secure_value.decrypt(read_identity_file(example_age_key)) == "foo"


def test_encrypt__multiple_recipients(
Expand Down
16 changes: 0 additions & 16 deletions tests/unit/renderers/conftest.py

This file was deleted.

4 changes: 2 additions & 2 deletions tests/unit/renderers/test_identity_from_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@


@pytest.fixture()
def config_get(age_identity_path: str) -> Callable[[str], str]:
def config_get(example_age_key: str) -> Callable[[str], str]:
def _config_get(key: str) -> str:
assert key == "age_identity_file"
return age_identity_path
return example_age_key

return _config_get

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/renderers/test_identity_from_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ def configure_loader_modules() -> dict[ModuleType, Any]:
return {age: {"__salt__": {"config.get": lambda _key: None}}}


def test(monkeypatch: pytest.MonkeyPatch, age_identity_path: str) -> None:
monkeypatch.setenv("AGE_IDENTITY_FILE", age_identity_path)
def test(monkeypatch: pytest.MonkeyPatch, example_age_key: str) -> None:
monkeypatch.setenv("AGE_IDENTITY_FILE", example_age_key)
_test_identity.test()

0 comments on commit 19ae926

Please sign in to comment.