source: flexoentity/tests/test_persistance_integrity.py@ ca39274

Last change on this file since ca39274 was ca39274, checked in by Enrico Schwass <ennoausberlin@…>, 3 months ago

minor cleanups

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