| 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 EntityState, EntityType, Domain
|
|---|
| 9 |
|
|---|
| 10 | @pytest.fixture
|
|---|
| 11 | def approved_question():
|
|---|
| 12 | """Provide a fully approved and published SingleChoiceQuestion for persistence tests."""
|
|---|
| 13 | q = SingleChoiceQuestion(
|
|---|
| 14 | domain=Domain(domain="GEN", entity_type=EntityType.DOMAIN, state=EntityState.DRAFT),
|
|---|
| 15 | entity_type=None, # SingleChoiceQuestion sets this internally to EntityType.ITEM
|
|---|
| 16 | state=EntityState.DRAFT,
|
|---|
| 17 | text="What is Ohm’s law?",
|
|---|
| 18 | options=[
|
|---|
| 19 | AnswerOption(id="OP1", text="U = R × I", points=1),
|
|---|
| 20 | AnswerOption(id="OP2", text="U = I / R", points=0),
|
|---|
| 21 | AnswerOption(id="OP3", text="R = U × I", points=0),
|
|---|
| 22 | ],
|
|---|
| 23 | )
|
|---|
| 24 | q.approve()
|
|---|
| 25 | q.sign()
|
|---|
| 26 | q.publish()
|
|---|
| 27 | return q
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | @pytest.mark.skip(reason="FlexOIDs regenerated on import; enable once JSON format is stable")
|
|---|
| 31 | def test_json_roundtrip_preserves_integrity(approved_question):
|
|---|
| 32 | """
|
|---|
| 33 | Export to JSON and reload — ensure fingerprints and signatures remain valid.
|
|---|
| 34 | """
|
|---|
| 35 | json_str = approved_question.to_json()
|
|---|
| 36 | print("JSON", json_str)
|
|---|
| 37 | loaded = SingleChoiceQuestion.from_json(json_str)
|
|---|
| 38 |
|
|---|
| 39 | print("Approved", approved_question.text_seed)
|
|---|
| 40 | print("Loaded", loaded.text_seed)
|
|---|
| 41 | # Fingerprint and state should match — integrity must pass
|
|---|
| 42 | assert SingleChoiceQuestion.verify_integrity(loaded)
|
|---|
| 43 |
|
|---|
| 44 | # Metadata should be preserved exactly
|
|---|
| 45 | assert approved_question.fingerprint == loaded.fingerprint
|
|---|
| 46 | assert approved_question.flexo_id == loaded.flexo_id
|
|---|
| 47 | assert loaded.state == approved_question.state
|
|---|
| 48 |
|
|---|
| 49 | @pytest.mark.skip(reason="FlexOIDs regenerated on import; tampering detection not yet implemented")
|
|---|
| 50 | def test_json_tampering_detection(approved_question):
|
|---|
| 51 | """Tampering with content should invalidate fingerprint verification."""
|
|---|
| 52 | json_str = approved_question.to_json()
|
|---|
| 53 | tampered = json.loads(json_str)
|
|---|
| 54 | tampered["text"] = "Tampered content injection"
|
|---|
| 55 | tampered_json = json.dumps(tampered)
|
|---|
| 56 |
|
|---|
| 57 | loaded = SingleChoiceQuestion.from_json(tampered_json)
|
|---|
| 58 | assert not SingleChoiceQuestion.verify_integrity(loaded)
|
|---|
| 59 |
|
|---|
| 60 | @pytest.mark.skip(reason="FlexOIDs regenerated on import; corruption detection not yet applicable")
|
|---|
| 61 | def test_json_file_corruption(approved_question, tmp_path):
|
|---|
| 62 | """Simulate file corruption — integrity check must fail."""
|
|---|
| 63 | file = tmp_path / "question.json"
|
|---|
| 64 | json_str = approved_question.to_json()
|
|---|
| 65 | file.write_text(json_str)
|
|---|
| 66 |
|
|---|
| 67 | # Corrupt the file (simulate accidental byte modification)
|
|---|
| 68 | corrupted = json_str.replace("Ohm’s", "Omm’s")
|
|---|
| 69 | file.write_text(corrupted)
|
|---|
| 70 |
|
|---|
| 71 | loaded = SingleChoiceQuestion.from_json(file.read_text())
|
|---|
| 72 | assert not SingleChoiceQuestion.verify_integrity(loaded)
|
|---|