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