source: flexoentity/tests/conftest.py@ ea28ca0

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

use InMemoryBackend as read cache for CompositeBackends as default

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