Changeset 02d288d in flexoentity for tests/test_persistance_integrity.py


Ignore:
Timestamp:
10/23/25 13:27:08 (3 months ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
master
Children:
4ceca57
Parents:
6a7dec1
Message:

improve hash generation and collision handler - move signature from FlexOID to FlexoEntity

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/test_persistance_integrity.py

    r6a7dec1 r02d288d  
    66import pytest
    77
    8 from flexoentity import FlexOID, EntityType, EntityState
    9 from tests.conftest import DummyEntity
     8from flexoentity import EntityState
     9from builder.questions import RadioQuestion, AnswerOption
    1010
    1111
    1212# ──────────────────────────────────────────────────────────────────────────────
    1313@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,
     14def approved_question(domain):
     15    """Provide a fully approved and published RadioQuestion for persistence tests."""
     16    q = RadioQuestion(
     17        domain=domain,
     18        etype=None,  # RadioQuestion sets this internally to EntityType.QUESTION
    1919        state=EntityState.DRAFT,
    20         seed="What is Ohm’s law?"
     20        text="What is Ohm’s law?",
     21        options=[
     22            AnswerOption(text="U = R × I", points=1),
     23            AnswerOption(text="U = I / R", points=0),
     24            AnswerOption(text="R = U × I", points=0),
     25        ],
    2126    )
    22     e.approve()
    23     e.sign()
    24     e.publish()
    25     return e
     27    q.approve()
     28    q.sign()
     29    q.publish()
     30    return q
    2631
    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):
     32
     33@pytest.mark.skip(reason="FlexOIDs regenerated on import; enable once JSON format is stable")
     34def test_json_roundtrip_preserves_integrity(approved_question):
    2935    """
    30     Export to JSON and reload — ensure fingerprints remain valid.
     36    Export to JSON and reload — ensure fingerprints and signatures remain valid.
    3137    """
    32     json_str = approved_entity.to_json()
    33     loaded = approved_entity.__class__.from_json(json_str)
     38    json_str = approved_question.to_json()
     39    loaded = RadioQuestion.from_json(json_str)
    3440
    3541    # Fingerprint and state should match — integrity must pass
    36     assert approved_entity.__class__.verify_integrity(loaded)
     42    assert RadioQuestion.verify_integrity(loaded)
    3743
    3844    # 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
     45    assert approved_question.signature == loaded.signature
     46    assert approved_question.flexo_id == loaded.flexo_id
     47    assert loaded.state == approved_question.state
     48
    4249
    4350# ──────────────────────────────────────────────────────────────────────────────
    4451
    45 @pytest.mark.skip(reason="FlexOIDs regenerated on import; tampering detection not applicable yet")
    46 def test_json_tampering_detection(approved_entity):
     52@pytest.mark.skip(reason="FlexOIDs regenerated on import; tampering detection not yet implemented")
     53def test_json_tampering_detection(approved_question):
    4754    """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)
     55    json_str = approved_question.to_json()
     56    tampered = json.loads(json_str)
     57    tampered["text"] = "Tampered content injection"
     58    tampered_json = json.dumps(tampered)
    5259
    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)
     60    loaded = RadioQuestion.from_json(tampered_json)
     61    assert not RadioQuestion.verify_integrity(loaded)
    5662
    5763
     
    5965
    6066@pytest.mark.skip(reason="FlexOIDs regenerated on import; corruption detection not yet applicable")
    61 def test_json_file_corruption(approved_entity, tmp_path):
     67def test_json_file_corruption(approved_question, tmp_path):
    6268    """Simulate file corruption — integrity check must fail."""
    63     file = tmp_path / "entity.json"
    64     json_str = approved_entity.to_json()
     69    file = tmp_path / "question.json"
     70    json_str = approved_question.to_json()
    6571    file.write_text(json_str)
    6672
    67     # Corrupt the file
     73    # Corrupt the file (simulate accidental byte modification)
    6874    corrupted = json_str.replace("Ohm’s", "Omm’s")
    6975    file.write_text(corrupted)
    7076
    71     loaded = approved_entity.__class__.from_json(file.read_text())
    72     assert not approved_entity.__class__.verify_integrity(loaded)
     77    loaded = RadioQuestion.from_json(file.read_text())
     78    assert not RadioQuestion.verify_integrity(loaded)
Note: See TracChangeset for help on using the changeset viewer.