source: flexoentity/tests/conftest.py@ 3d598d5

main unify_backends
Last change on this file since 3d598d5 was e230c40, checked in by Enrico Schwass <ennoausberlin@…>, 3 months ago

fix some tests to mirror new entity manager

  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[8a238e2]1import pytest
[d7499ca]2import platform
3from pathlib import Path
[8aa20c7]4from datetime import datetime
[753855a]5from flexoentity import Domain, FlexoSignature, DomainManager, EntityRegistry, CompositeBackend, InMemoryBackend
[ef964d8]6from flexoentity import get_signing_backend, CertificateReference
[d7499ca]7
[8a238e2]8
[8aa20c7]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
[02d288d]17
[8aa20c7]18
19@pytest.fixture
[6ad031b]20def sample_domain():
21 domain_id = "PY_ARITHM"
[9592936]22 return Domain.with_domain_id(domain_id=domain_id,
23 fullname="PYTHON_ARITHMETIC",
24 description="ALL ABOUT ARITHMETIC IN PYTHON")
[3d16c35]25
[d7499ca]26
27SYSTEM = platform.system()
28
[753855a]29@pytest.fixture
[e230c40]30def local_backend():
[753855a]31 return InMemoryBackend(Domain)
32
[e230c40]33@pytest.fixture
34def staging_backend():
35 return InMemoryBackend(Domain)
36
37@pytest.fixture
38def permanent_backend():
39 return InMemoryBackend(Domain)
[d7499ca]40
[df459f7]41@pytest.fixture
[e230c40]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())
[df459f7]48
[d7499ca]49# ─────────────────────────────────────────────────────────────
50# Basic test data directory + PEM test files
51# ─────────────────────────────────────────────────────────────
52
[df459f7]53
[d7499ca]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
[100c1d2]96
[d7499ca]97@pytest.fixture(scope="session")
[753855a]98def signing_backend(test_cert, test_key):
[d7499ca]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}")
[ef964d8]129
[100c1d2]130
[ef964d8]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.