source: flexoentity/tests/test_persistance_integrity.py@ 2fd0536

Last change on this file since 2fd0536 was 4af65b0, checked in by Enrico Schwass <ennoausberlin@…>, 2 months ago

initial logging support

  • Property mode set to 100644
File size: 2.7 KB
RevLine 
[59342ce]1"""
2Persistence and integrity verification tests for Flex-O entities.
3Ensures fingerprints survive JSON export/import and detect tampering.
4"""
5import json
6import pytest
7
[ca39274]8from flexoentity import EntityState, EntityType, Domain
[59342ce]9
10@pytest.fixture
[ca39274]11def approved_question():
[8aa20c7]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
[59342ce]16 state=EntityState.DRAFT,
[02d288d]17 text="What is Ohm’s law?",
18 options=[
[ca39274]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),
[02d288d]22 ],
[59342ce]23 )
[02d288d]24 q.approve()
25 q.sign()
26 q.publish()
27 return q
[59342ce]28
[02d288d]29
30@pytest.mark.skip(reason="FlexOIDs regenerated on import; enable once JSON format is stable")
31def test_json_roundtrip_preserves_integrity(approved_question):
[59342ce]32 """
[02d288d]33 Export to JSON and reload — ensure fingerprints and signatures remain valid.
[59342ce]34 """
[02d288d]35 json_str = approved_question.to_json()
[8aa20c7]36 loaded = SingleChoiceQuestion.from_json(json_str)
[59342ce]37
[8a238e2]38 # Fingerprint and state should match — integrity must pass
[8aa20c7]39 assert SingleChoiceQuestion.verify_integrity(loaded)
[59342ce]40
[8a238e2]41 # Metadata should be preserved exactly
[3bad43e]42 assert approved_question.fingerprint == loaded.fingerprint
[02d288d]43 assert approved_question.flexo_id == loaded.flexo_id
44 assert loaded.state == approved_question.state
45
46@pytest.mark.skip(reason="FlexOIDs regenerated on import; tampering detection not yet implemented")
47def test_json_tampering_detection(approved_question):
[59342ce]48 """Tampering with content should invalidate fingerprint verification."""
[02d288d]49 json_str = approved_question.to_json()
50 tampered = json.loads(json_str)
51 tampered["text"] = "Tampered content injection"
52 tampered_json = json.dumps(tampered)
[59342ce]53
[8aa20c7]54 loaded = SingleChoiceQuestion.from_json(tampered_json)
55 assert not SingleChoiceQuestion.verify_integrity(loaded)
[8a238e2]56
57@pytest.mark.skip(reason="FlexOIDs regenerated on import; corruption detection not yet applicable")
[02d288d]58def test_json_file_corruption(approved_question, tmp_path):
[59342ce]59 """Simulate file corruption — integrity check must fail."""
[02d288d]60 file = tmp_path / "question.json"
61 json_str = approved_question.to_json()
[59342ce]62 file.write_text(json_str)
63
[02d288d]64 # Corrupt the file (simulate accidental byte modification)
[59342ce]65 corrupted = json_str.replace("Ohm’s", "Omm’s")
66 file.write_text(corrupted)
67
[8aa20c7]68 loaded = SingleChoiceQuestion.from_json(file.read_text())
69 assert not SingleChoiceQuestion.verify_integrity(loaded)
Note: See TracBrowser for help on using the repository browser.