source: flexoentity/tests/conftest.py@ e230c40

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

fix some tests to mirror new entity manager

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