| 1 | import pytest
|
|---|
| 2 | import platform
|
|---|
| 3 | from pathlib import Path
|
|---|
| 4 | from datetime import datetime
|
|---|
| 5 | from flexoentity import Domain, FlexoSignature
|
|---|
| 6 | from flexoentity import get_signing_backend, CertificateReference
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 | @pytest.fixture
|
|---|
| 10 | def fixed_datetime(monkeypatch):
|
|---|
| 11 | class FixedDate(datetime):
|
|---|
| 12 | @classmethod
|
|---|
| 13 | def now(cls, tz=None):
|
|---|
| 14 | return datetime(2025, 11, 1, tzinfo=tz)
|
|---|
| 15 | monkeypatch.setattr("flexoentity.id_factory.datetime", FixedDate)
|
|---|
| 16 | return FixedDate
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 | @pytest.fixture
|
|---|
| 20 | def sample_domain():
|
|---|
| 21 | domain_id = "PY_ARITHM"
|
|---|
| 22 | return Domain.with_domain_id(domain_id=domain_id,
|
|---|
| 23 | fullname="PYTHON_ARITHMETIC",
|
|---|
| 24 | description="ALL ABOUT ARITHMETIC IN PYTHON")
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 | SYSTEM = platform.system()
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | # ─────────────────────────────────────────────────────────────
|
|---|
| 31 | # Basic test data directory + PEM test files
|
|---|
| 32 | # ─────────────────────────────────────────────────────────────
|
|---|
| 33 |
|
|---|
| 34 | @pytest.fixture(scope="session")
|
|---|
| 35 | def test_data_dir():
|
|---|
| 36 | return Path(__file__).parent / "data"
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | @pytest.fixture(scope="session")
|
|---|
| 40 | def test_cert(test_data_dir):
|
|---|
| 41 | return test_data_dir / "testcert.pem"
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | @pytest.fixture(scope="session")
|
|---|
| 45 | def test_key(test_data_dir):
|
|---|
| 46 | return test_data_dir / "testkey.pem"
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | # ─────────────────────────────────────────────────────────────
|
|---|
| 50 | # CertificateReference fixtures for each platform
|
|---|
| 51 | # ─────────────────────────────────────────────────────────────
|
|---|
| 52 |
|
|---|
| 53 | @pytest.fixture(scope="session")
|
|---|
| 54 | def cert_ref_linux(test_cert, test_key):
|
|---|
| 55 | """Linux: Uses OpenSSL CMS with PEM cert + PEM private key."""
|
|---|
| 56 | return CertificateReference(
|
|---|
| 57 | platform="LINUX",
|
|---|
| 58 | identifier=str(test_cert),
|
|---|
| 59 | private_key_path=str(test_key),
|
|---|
| 60 | public_cert_path=str(test_cert),
|
|---|
| 61 | )
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 | @pytest.fixture(scope="session")
|
|---|
| 65 | def cert_ref_macos(test_cert):
|
|---|
| 66 | """
|
|---|
| 67 | macOS: Uses Keychain identity with Common Name (CN).
|
|---|
| 68 | The test cert must be imported into the login keychain with CN=FlexOSignerTest.
|
|---|
| 69 | """
|
|---|
| 70 | return CertificateReference(
|
|---|
| 71 | platform="MACOS",
|
|---|
| 72 | identifier="FlexOSignerTest",
|
|---|
| 73 | public_cert_path=str(test_cert),
|
|---|
| 74 | )
|
|---|
| 75 |
|
|---|
| 76 | @pytest.fixture(scope="session")
|
|---|
| 77 | def backend(test_cert, test_key):
|
|---|
| 78 | """Return the correct backend for the current platform."""
|
|---|
| 79 |
|
|---|
| 80 | if SYSTEM == "Linux":
|
|---|
| 81 | cert_ref = CertificateReference(
|
|---|
| 82 | platform="LINUX",
|
|---|
| 83 | identifier=str(test_cert),
|
|---|
| 84 | private_key_path=str(test_key),
|
|---|
| 85 | public_cert_path=str(test_cert),
|
|---|
| 86 | )
|
|---|
| 87 |
|
|---|
| 88 | elif SYSTEM == "Darwin":
|
|---|
| 89 | cert_ref = CertificateReference(
|
|---|
| 90 | platform="MACOS",
|
|---|
| 91 | identifier="FlexOSignerTest",
|
|---|
| 92 | public_cert_path=str(test_cert),
|
|---|
| 93 | )
|
|---|
| 94 |
|
|---|
| 95 | elif SYSTEM == "Windows":
|
|---|
| 96 | pytest.skip("Windows signing tests not implemented yet")
|
|---|
| 97 |
|
|---|
| 98 | else:
|
|---|
| 99 | pytest.skip(f"Unsupported platform: {SYSTEM}")
|
|---|
| 100 |
|
|---|
| 101 | try:
|
|---|
| 102 | backend = get_signing_backend(cert_ref)
|
|---|
| 103 | # sanity check: ensures cert exists and command is available
|
|---|
| 104 | _ = backend.certificate_thumbprint
|
|---|
| 105 | return backend
|
|---|
| 106 | except Exception as e:
|
|---|
| 107 | pytest.skip(f"Backend unavailable or misconfigured: {e}")
|
|---|
| 108 |
|
|---|
| 109 | @pytest.fixture
|
|---|
| 110 | def sample_signature(sample_domain, cert_ref_linux):
|
|---|
| 111 | return FlexoSignature.with_domain_id(domain_id="SIG", signed_entity=sample_domain,
|
|---|
| 112 | certificate_reference=cert_ref_linux,
|
|---|
| 113 | comment="This is a mock signature")
|
|---|
| 114 |
|
|---|