source: flexoentity/tests/conftest.py@ c296f76

Last change on this file since c296f76 was c296f76, checked in by Enrico Schwass <ennoausberlin@…>, 6 weeks ago

add support for persistance backends

  • Property mode set to 100644
File size: 4.0 KB
Line 
1import pytest
2import platform
3from pathlib import Path
4from datetime import datetime
5from flexoentity import Domain, FlexoSignature, DomainManager, EntityRegistry, CompositeBackend
6from flexoentity import get_signing_backend, CertificateReference
7
8
9@pytest.fixture
10def 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
20def 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
27SYSTEM = platform.system()
28
29
30@pytest.fixture
31def 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")
40def test_data_dir():
41 return Path(__file__).parent / "data"
42
43
44@pytest.fixture(scope="session")
45def test_cert(test_data_dir):
46 return test_data_dir / "testcert.pem"
47
48
49@pytest.fixture(scope="session")
50def 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")
59def 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")
70def 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")
83def 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
117def 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")
Note: See TracBrowser for help on using the repository browser.