Changeset ef964d8 in flexoentity for tests/test_persistance_integrity.py
- Timestamp:
- 11/27/25 18:12:23 (7 weeks ago)
- Branches:
- master
- Children:
- 4e11d58
- Parents:
- 9a50e0b
- File:
-
- 1 edited
-
tests/test_persistance_integrity.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
tests/test_persistance_integrity.py
r9a50e0b ref964d8 6 6 import pytest 7 7 8 from flexoentity import EntityState, EntityType, Domain 8 from flexoentity import Domain 9 9 10 10 11 @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 12 def approved_domain(sample_domain): 13 """Provide a fully approved and published Domain for persistence tests.""" 14 sample_domain.approve() 15 sample_domain.sign() 16 sample_domain.publish() 17 return sample_domain 28 18 29 19 30 @pytest.mark.skip(reason="FlexOIDs regenerated on import; enable once JSON format is stable") 31 def test_json_roundtrip_preserves_integrity(approved_question): 20 def test_json_roundtrip_preserves_integrity(approved_domain): 32 21 """ 33 22 Export to JSON and reload — ensure fingerprints and signatures remain valid. 34 23 """ 35 json_str = approved_ question.to_json()36 loaded = SingleChoiceQuestion.from_json(json_str)24 json_str = approved_domain.to_json() 25 loaded = Domain.from_json(json_str) 37 26 38 27 # Fingerprint and state should match — integrity must pass 39 assert SingleChoiceQuestion.verify_integrity(loaded)28 assert Domain.verify_integrity(loaded) 40 29 41 30 # Metadata should be preserved exactly 42 assert approved_ question.fingerprint == loaded.fingerprint43 assert approved_ question.flexo_id == loaded.flexo_id44 assert loaded.state == approved_ question.state31 assert approved_domain.fingerprint == loaded.fingerprint 32 assert approved_domain.flexo_id == loaded.flexo_id 33 assert loaded.state == approved_domain.state 45 34 46 @pytest.mark.skip(reason="FlexOIDs regenerated on import; tampering detection not yet implemented") 47 def test_json_tampering_detection(approved_ question):35 36 def test_json_tampering_detection(approved_domain): 48 37 """Tampering with content should invalidate fingerprint verification.""" 49 json_str = approved_ question.to_json()38 json_str = approved_domain.to_json() 50 39 tampered = json.loads(json_str) 51 tampered[" text"] = "Tampered content injection"40 tampered["content"]["fullname"] = "Tampered content injection" 52 41 tampered_json = json.dumps(tampered) 53 42 54 loaded = SingleChoiceQuestion.from_json(tampered_json)55 assert not SingleChoiceQuestion.verify_integrity(loaded)43 loaded = Domain.from_json(tampered_json) 44 assert not Domain.verify_integrity(loaded) 56 45 57 @pytest.mark.skip(reason="FlexOIDs regenerated on import; corruption detection not yet applicable") 58 def test_json_file_corruption(approved_ question, tmp_path):46 47 def test_json_file_corruption(approved_domain, tmp_path): 59 48 """Simulate file corruption — integrity check must fail.""" 60 49 file = tmp_path / "question.json" 61 json_str = approved_question.to_json() 50 json_str = approved_domain.to_json() 51 print("JSON", json_str) 62 52 file.write_text(json_str) 63 53 64 54 # Corrupt the file (simulate accidental byte modification) 65 corrupted = json_str.replace(" Ohm’s", "Omm’s")55 corrupted = json_str.replace("ARITHMETIC", "ARITHM") 66 56 file.write_text(corrupted) 67 57 68 loaded = SingleChoiceQuestion.from_json(file.read_text())69 assert not SingleChoiceQuestion.verify_integrity(loaded)58 loaded = Domain.from_json(file.read_text()) 59 assert not Domain.verify_integrity(loaded)
Note:
See TracChangeset
for help on using the changeset viewer.
