| 1 | """
|
|---|
| 2 | Persistence and integrity verification tests for Flex-O entities.
|
|---|
| 3 | Ensures fingerprints survive JSON export/import and detect tampering.
|
|---|
| 4 | """
|
|---|
| 5 | import json
|
|---|
| 6 | import pytest
|
|---|
| 7 |
|
|---|
| 8 | from flexoentity import FlexOID, EntityType, EntityState
|
|---|
| 9 | from tests.conftest import DummyEntity
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 | # ──────────────────────────────────────────────────────────────────────────────
|
|---|
| 13 | @pytest.fixture
|
|---|
| 14 | def approved_entity():
|
|---|
| 15 | """A fully published dummy entity for persistence tests."""
|
|---|
| 16 | e = DummyEntity(
|
|---|
| 17 | domain="AF",
|
|---|
| 18 | etype=EntityType.QUESTION,
|
|---|
| 19 | state=EntityState.DRAFT,
|
|---|
| 20 | seed="What is Ohm’s law?"
|
|---|
| 21 | )
|
|---|
| 22 | e.approve()
|
|---|
| 23 | e.sign()
|
|---|
| 24 | e.publish()
|
|---|
| 25 | return e
|
|---|
| 26 |
|
|---|
| 27 | @pytest.mark.skip(reason="FlexOIDs are regenerated on import; enable once JSON format is stable")
|
|---|
| 28 | def test_json_roundtrip_preserves_integrity(approved_entity):
|
|---|
| 29 | """
|
|---|
| 30 | Export to JSON and reload — ensure fingerprints remain valid.
|
|---|
| 31 | """
|
|---|
| 32 | json_str = approved_entity.to_json()
|
|---|
| 33 | loaded = approved_entity.__class__.from_json(json_str)
|
|---|
| 34 |
|
|---|
| 35 | # Fingerprint and state should match — integrity must pass
|
|---|
| 36 | assert approved_entity.__class__.verify_integrity(loaded)
|
|---|
| 37 |
|
|---|
| 38 | # Metadata should be preserved exactly
|
|---|
| 39 | assert approved_entity.flexo_id.signature == loaded.flexo_id.signature
|
|---|
| 40 | assert approved_entity.flexo_id == loaded.flexo_id
|
|---|
| 41 | assert loaded.state == approved_entity.state
|
|---|
| 42 |
|
|---|
| 43 | # ──────────────────────────────────────────────────────────────────────────────
|
|---|
| 44 |
|
|---|
| 45 | @pytest.mark.skip(reason="FlexOIDs regenerated on import; tampering detection not applicable yet")
|
|---|
| 46 | def test_json_tampering_detection(approved_entity):
|
|---|
| 47 | """Tampering with content should invalidate fingerprint verification."""
|
|---|
| 48 | json_str = approved_entity.to_json()
|
|---|
| 49 | tampered_data = json.loads(json_str)
|
|---|
| 50 | tampered_data["text_seed"] = "Tampered content injection"
|
|---|
| 51 | tampered_json = json.dumps(tampered_data)
|
|---|
| 52 |
|
|---|
| 53 | # We use DummyEntity.from_json to reconstruct (FlexoEntity is abstract)
|
|---|
| 54 | loaded = approved_entity.__class__.from_json(tampered_json)
|
|---|
| 55 | assert not approved_entity.__class__.verify_integrity(loaded)
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 | # ──────────────────────────────────────────────────────────────────────────────
|
|---|
| 59 |
|
|---|
| 60 | @pytest.mark.skip(reason="FlexOIDs regenerated on import; corruption detection not yet applicable")
|
|---|
| 61 | def test_json_file_corruption(approved_entity, tmp_path):
|
|---|
| 62 | """Simulate file corruption — integrity check must fail."""
|
|---|
| 63 | file = tmp_path / "entity.json"
|
|---|
| 64 | json_str = approved_entity.to_json()
|
|---|
| 65 | file.write_text(json_str)
|
|---|
| 66 |
|
|---|
| 67 | # Corrupt the file
|
|---|
| 68 | corrupted = json_str.replace("Ohm’s", "Omm’s")
|
|---|
| 69 | file.write_text(corrupted)
|
|---|
| 70 |
|
|---|
| 71 | loaded = approved_entity.__class__.from_json(file.read_text())
|
|---|
| 72 | assert not approved_entity.__class__.verify_integrity(loaded)
|
|---|