| 1 | import pytest
|
|---|
| 2 | import platform
|
|---|
| 3 | from pathlib import Path
|
|---|
| 4 | from datetime import datetime
|
|---|
| 5 | from flexoentity import Domain, FlexoSignature, DomainManager, EntityRegistry, CompositeBackend
|
|---|
| 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 | @pytest.fixture
|
|---|
| 31 | def sample_domain_manager():
|
|---|
| 32 | return DomainManager(CompositeBackend(Domain), EntityRegistry())
|
|---|
| 33 |
|
|---|
| 34 | # ─────────────────────────────────────────────────────────────
|
|---|
| 35 | # Basic test data directory + PEM test files
|
|---|
| 36 | # ─────────────────────────────────────────────────────────────
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | @pytest.fixture(scope="session")
|
|---|
| 40 | def test_data_dir():
|
|---|
| 41 | return Path(__file__).parent / "data"
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | @pytest.fixture(scope="session")
|
|---|
| 45 | def test_cert(test_data_dir):
|
|---|
| 46 | return test_data_dir / "testcert.pem"
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | @pytest.fixture(scope="session")
|
|---|
| 50 | def test_key(test_data_dir):
|
|---|
| 51 | return test_data_dir / "testkey.pem"
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | # ─────────────────────────────────────────────────────────────
|
|---|
| 55 | # CertificateReference fixtures for each platform
|
|---|
| 56 | # ─────────────────────────────────────────────────────────────
|
|---|
| 57 |
|
|---|
| 58 | @pytest.fixture(scope="session")
|
|---|
| 59 | def cert_ref_linux(test_cert, test_key):
|
|---|
| 60 | """Linux: Uses OpenSSL CMS with PEM cert + PEM private key."""
|
|---|
| 61 | return CertificateReference(
|
|---|
| 62 | platform="LINUX",
|
|---|
| 63 | identifier=str(test_cert),
|
|---|
| 64 | private_key_path=str(test_key),
|
|---|
| 65 | public_cert_path=str(test_cert),
|
|---|
| 66 | )
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 | @pytest.fixture(scope="session")
|
|---|
| 70 | def cert_ref_macos(test_cert):
|
|---|
| 71 | """
|
|---|
| 72 | macOS: Uses Keychain identity with Common Name (CN).
|
|---|
| 73 | The test cert must be imported into the login keychain with CN=FlexOSignerTest.
|
|---|
| 74 | """
|
|---|
| 75 | return CertificateReference(
|
|---|
| 76 | platform="MACOS",
|
|---|
| 77 | identifier="FlexOSignerTest",
|
|---|
| 78 | public_cert_path=str(test_cert),
|
|---|
| 79 | )
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | @pytest.fixture(scope="session")
|
|---|
| 83 | def backend(test_cert, test_key):
|
|---|
| 84 | """Return the correct backend for the current platform."""
|
|---|
| 85 |
|
|---|
| 86 | if SYSTEM == "Linux":
|
|---|
| 87 | cert_ref = CertificateReference(
|
|---|
| 88 | platform="LINUX",
|
|---|
| 89 | identifier=str(test_cert),
|
|---|
| 90 | private_key_path=str(test_key),
|
|---|
| 91 | public_cert_path=str(test_cert),
|
|---|
| 92 | )
|
|---|
| 93 |
|
|---|
| 94 | elif SYSTEM == "Darwin":
|
|---|
| 95 | cert_ref = CertificateReference(
|
|---|
| 96 | platform="MACOS",
|
|---|
| 97 | identifier="FlexOSignerTest",
|
|---|
| 98 | public_cert_path=str(test_cert),
|
|---|
| 99 | )
|
|---|
| 100 |
|
|---|
| 101 | elif SYSTEM == "Windows":
|
|---|
| 102 | pytest.skip("Windows signing tests not implemented yet")
|
|---|
| 103 |
|
|---|
| 104 | else:
|
|---|
| 105 | pytest.skip(f"Unsupported platform: {SYSTEM}")
|
|---|
| 106 |
|
|---|
| 107 | try:
|
|---|
| 108 | backend = get_signing_backend(cert_ref)
|
|---|
| 109 | # sanity check: ensures cert exists and command is available
|
|---|
| 110 | _ = backend.certificate_thumbprint
|
|---|
| 111 | return backend
|
|---|
| 112 | except Exception as e:
|
|---|
| 113 | pytest.skip(f"Backend unavailable or misconfigured: {e}")
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 | @pytest.fixture
|
|---|
| 117 | def sample_signature(sample_domain, cert_ref_linux):
|
|---|
| 118 | return FlexoSignature.with_domain_id(domain_id="SIG", signed_entity=sample_domain,
|
|---|
| 119 | certificate_reference=cert_ref_linux,
|
|---|
| 120 | comment="This is a mock signature")
|
|---|