first version
Python Code Quality / python-code-quality (push) Successful in 11s
Python Test / python-test (push) Successful in 12s

This commit is contained in:
Brian Bjarke Jensen
2026-01-07 17:59:16 +01:00
parent f8e512e663
commit 422bad3fb8
45 changed files with 3682 additions and 12 deletions
+80
View File
@@ -0,0 +1,80 @@
"""Definition of integration test fixtures."""
import pytest
import json
from pathlib import Path
import tempfile
from collections.abc import Iterator
@pytest.fixture
def password() -> str:
"""Fixture that provides a test password.
Returns:
str: A test password.
"""
return "sZoOOTSY32x-4nzb_6XXtmfY2tA3R6KFcHLGS1hGA30=" # Base64-encoded 32-byte key
@pytest.fixture
def wrong_password() -> str:
"""Fixture that provides a wrong test password.
Returns:
str: A wrong test password.
"""
return "5mtlZEFgdvVyrcpF-__xoBNFuDaUjpqLOb-Y05AOdKY=" # Base64-encoded 32-byte key
@pytest.fixture
def aad() -> str:
"""Fixture that provides additional authenticated data (AAD) for tests.
Returns:
str: AAD JSON string.
"""
return json.dumps(
{
"TEST_KEY": "TEST_VALUE",
"MODULE_VERSION": "1.0.0",
},
)
@pytest.fixture
def wrong_aad() -> str:
"""Fixture that provides wrong additional authenticated data (AAD) for tests.
Returns:
str: Wrong AAD JSON string.
"""
return json.dumps(
{
"TEST_KEY": "TEST_VALUE",
"MODULE_VERSION": "1.1.0",
},
)
@pytest.fixture(scope="function")
def tmp_dir() -> Iterator[Path]:
"""Fixture that provides a temporary directory for tests.
Yields:
Path: A temporary directory path.
"""
with tempfile.TemporaryDirectory() as tmpdirname:
yield Path(tmpdirname)
@pytest.fixture
def simple_python_module() -> Path:
"""Path to a simple hello world Python module."""
return Path(__file__).parent.parent / "test_data" / "simple_python_module"
@pytest.fixture
def nested_python_module() -> Path:
"""Path to a complex Python module with internal dependencies."""
return Path(__file__).parent.parent / "test_data" / "nested_python_module"
+177
View File
@@ -0,0 +1,177 @@
"""Definition of integration tests for the CLI functionality."""
import subprocess
import os
import sys
from pathlib import Path
def test_generate_password_command() -> None:
"""Test the generate-password CLI command."""
generate_password_args = [
sys.executable,
"-m",
"python_encrypt_code",
"generate-password",
]
output = subprocess.run(
generate_password_args, check=True, capture_output=True, text=True
)
password = output.stdout.strip()
assert len(password) > 0, "Generated password should not be empty"
def test_encrypt_command(
password: str,
aad: str,
simple_python_module: Path,
tmp_dir: Path,
) -> None:
"""Test the encrypt CLI command."""
encrypted_file = tmp_dir / "encrypted.pec"
# Encrypt the simple python module
encrypt_args = [
sys.executable,
"-m",
"python_encrypt_code",
"encrypt",
str(simple_python_module),
"-o",
str(encrypted_file),
"-p",
password,
"-aad",
aad,
]
subprocess.run(encrypt_args, check=True)
assert encrypted_file.exists(), "Encrypted file should be created"
# Encrypt without optional AAD
encrypted_file_no_aad = tmp_dir / "encrypted_no_aad.pec"
encrypt_args_no_aad = [
sys.executable,
"-m",
"python_encrypt_code",
"encrypt",
str(simple_python_module),
"-o",
str(encrypted_file_no_aad),
"-p",
password,
]
subprocess.run(encrypt_args_no_aad, check=True)
assert encrypted_file_no_aad.exists(), "Encrypted file should be created"
def test_decrypt_command(
password: str,
aad: str,
simple_python_module: Path,
tmp_dir: Path,
) -> None:
"""Test the decrypt CLI command."""
encrypted_file = tmp_dir / "encrypted.pec"
decrypted_output_dir = tmp_dir / "decrypted_output"
# Prepare an encrypted file
encrypt_args = [
sys.executable,
"-m",
"python_encrypt_code",
"encrypt",
str(simple_python_module),
"-o",
str(encrypted_file),
"-p",
password,
"-aad",
aad,
]
subprocess.run(encrypt_args, check=True)
# The PasswordProviderExample reads from environment variables
# to mock getting data from external secret management systems.
env = {**os.environ}
env["PEC_PASSWORD"] = password
env["PEC_AAD"] = aad
# Decrypt the file
decrypt_args = [
sys.executable,
"-m",
"python_encrypt_code",
"decrypt",
str(encrypted_file),
"-o",
str(decrypted_output_dir),
]
subprocess.run(decrypt_args, check=True, env=env)
assert decrypted_output_dir.exists(), "Decrypted output directory should be created"
assert any(decrypted_output_dir.iterdir()), (
"Decrypted output directory should not be empty"
)
def test_run_insecure_command(
password: str,
aad: str,
simple_python_module: Path,
tmp_dir: Path,
) -> None:
"""Test the run-insecure CLI command."""
encrypted_file = tmp_dir / "encrypted_run.pec"
# Encrypt the simple python module
encrypt_args = [
sys.executable,
"-m",
"python_encrypt_code",
"encrypt",
str(simple_python_module),
"-o",
str(encrypted_file),
"-p",
password,
"-aad",
aad,
]
subprocess.run(
args=encrypt_args,
check=True,
)
# Verify that the encrypted file was created
assert encrypted_file.exists()
# Store password and AAD in environment variables for the subprocesses
env = dict(**os.environ)
env["PEC_PASSWORD"] = password
env["PEC_AAD"] = aad
# Step 2: Run the encrypted module
run_args = [
sys.executable,
"-m",
"python_encrypt_code",
"run-insecure",
str(encrypted_file),
"--script",
"hello_world.py",
]
result = subprocess.run(
args=run_args,
check=True,
capture_output=True,
text=True,
env=env,
)
# Verify output
assert "Hello, World!" in result.stdout
@@ -0,0 +1,72 @@
"""Test the encryption of complex python modules with multiple files and imports."""
import os
import sys
import subprocess
from pathlib import Path
def test_encrypt_and_run_nested_module(
nested_python_module: Path,
tmp_dir: Path,
password: str,
aad: str,
) -> None:
"""Test encrypting and running a module with nested imports.
Args:
nested_python_module (Path): Path to the nested module directory.
tmp_dir (Path): Temporary directory for test files.
password (str): The test password.
aad (str): Additional authenticated data.
"""
encrypted_file = tmp_dir / "encrypted_nested.pec"
# Encrypt the folder containing the complex module
encrypt_args = [
sys.executable,
"-m",
"python_encrypt_code",
"encrypt",
str(nested_python_module),
"-o",
str(encrypted_file),
"-p",
password,
"-aad",
aad,
]
subprocess.run(
args=encrypt_args,
check=True,
)
# Verify that the encrypted file was created
assert encrypted_file.exists()
# Prepare environment for the PasswordProviderExample
env = {**os.environ}
env["PEC_PASSWORD"] = password
env["PEC_AAD"] = aad
# Run the encrypted module with environment set
run_args = [
sys.executable,
"-m",
"python_encrypt_code",
"run-insecure",
str(encrypted_file),
"--script",
"main.py",
]
result = subprocess.run(
args=run_args,
check=True,
capture_output=True,
text=True,
env=env,
)
# Verify nested module functionality works
assert "Nested Python Module executed successfully." in result.stderr
@@ -0,0 +1,200 @@
"""Test the encryption of a simple python module and its execution."""
import os
import sys
import subprocess
from pathlib import Path
def test_encrypt_and_run_simple_module(
simple_python_module: Path,
tmp_dir: Path,
password: str,
aad: str,
) -> None:
"""Test encrypting a simple hello world module.
Args:
simple_python_module (Path): Path to the simple python module.
tmp_dir (Path): Temporary directory for test files.
password (str): The test password.
aad (str): Additional authenticated data.
"""
encrypted_file = tmp_dir / "encrypted.pec"
# Encrypt the simple python module
encrypt_args = [
sys.executable,
"-m",
"python_encrypt_code",
"encrypt",
str(simple_python_module),
"-o",
str(encrypted_file),
"-p",
password,
"-aad",
aad,
]
subprocess.run(
args=encrypt_args,
check=True,
)
# Verify that the encrypted file was created
assert encrypted_file.exists()
# Store password and AAD in environment variables
# for the PasswordProviderExample used by the subprocesses
env = {**os.environ}
env["PEC_PASSWORD"] = password
env["PEC_AAD"] = aad
# Run the encrypted module with environment set
run_args = [
sys.executable,
"-m",
"python_encrypt_code",
"run-insecure",
str(encrypted_file),
"--script",
"hello_world.py",
]
result = subprocess.run(
args=run_args,
check=True,
capture_output=True,
text=True,
env=env,
)
# Verify output
assert "Hello, World!" in result.stdout
def test_run_with_wrong_password(
simple_python_module: Path,
tmp_dir: Path,
password: str,
aad: str,
wrong_password: str,
) -> None:
"""Test encrypting a simple hello world module and attempting to run it with a wrong password.
Args:
simple_python_module (Path): Path to the simple python module.
tmp_dir (Path): Temporary directory for test files.
"""
encrypted_file = tmp_dir / "encrypted_wrong_password.pec"
assert password != wrong_password, "Test setup error: passwords should differ."
# Encrypt the folder containing the hello world module
encrypt_args = [
sys.executable,
"-m",
"python_encrypt_code",
"encrypt",
str(simple_python_module),
"-o",
str(encrypted_file),
"-p",
password,
"-aad",
aad,
]
subprocess.run(encrypt_args, check=True)
# Verify that the encrypted file was created
assert encrypted_file.exists()
# Store wrong password and AAD in environment variables for the subprocesses
env = {**os.environ}
env["PEC_PASSWORD"] = wrong_password
env["PEC_AAD"] = aad
# Run the encrypted module with the wrong password
run_args = [
sys.executable,
"-m",
"python_encrypt_code",
"run-insecure",
str(encrypted_file),
"--script",
"hello_world.py",
]
result = subprocess.run(
args=run_args,
check=False,
capture_output=True,
text=True,
)
# Verify that an error message is in the output
assert "Error" in result.stderr
def test_run_with_wrong_aad(
simple_python_module: Path,
tmp_dir: Path,
password: str,
aad: str,
wrong_aad: str,
) -> None:
"""Test encrypting a simple hello world module and attempting to run it with wrong AAD.
Args:
simple_python_module (Path): Path to the simple python module.
tmp_dir (Path): Temporary directory for test files.
"""
encrypted_file = tmp_dir / "encrypted_wrong_aad.pec"
assert aad != wrong_aad, "Test setup error: AADs should differ."
# Encrypt the folder containing the hello world module
encrypt_args = [
sys.executable,
"-m",
"python_encrypt_code",
"encrypt",
str(simple_python_module),
"-o",
str(encrypted_file),
"-p",
password,
"-aad",
aad,
]
subprocess.run(
args=encrypt_args,
check=True,
)
# Verify that the encrypted file was created
assert encrypted_file.exists()
# Store wrong password and AAD in environment variables for the subprocesses
env = {**os.environ}
env["PEC_PASSWORD"] = password
env["PEC_AAD"] = wrong_aad
# Run the encrypted module with the wrong AAD
run_args = [
sys.executable,
"-m",
"python_encrypt_code",
"run-insecure",
str(encrypted_file),
"--script",
"hello_world.py",
]
result = subprocess.run(
args=run_args,
check=False,
capture_output=True,
text=True,
)
# Verify that an error message is in the output
assert "Error" in result.stderr