source: flexoentity/tests/test_persistance_integrity.py@ bf30018

main unify_backends
Last change on this file since bf30018 was 8aa20c7, checked in by Enrico Schwass <ennoausberlin@…>, 4 months ago

full refactoring of FlexOID

  • Property mode set to 100644
File size: 2.8 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()
[ca39274]36 print("JSON", json_str)
[8aa20c7]37 loaded = SingleChoiceQuestion.from_json(json_str)
[59342ce]38
[ca39274]39 print("Approved", approved_question.text_seed)
40 print("Loaded", loaded.text_seed)
[8a238e2]41 # Fingerprint and state should match — integrity must pass
[8aa20c7]42 assert SingleChoiceQuestion.verify_integrity(loaded)
[59342ce]43
[8a238e2]44 # Metadata should be preserved exactly
[3bad43e]45 assert approved_question.fingerprint == loaded.fingerprint
[02d288d]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")
50def test_json_tampering_detection(approved_question):
[59342ce]51 """Tampering with content should invalidate fingerprint verification."""
[02d288d]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)
[59342ce]56
[8aa20c7]57 loaded = SingleChoiceQuestion.from_json(tampered_json)
58 assert not SingleChoiceQuestion.verify_integrity(loaded)
[8a238e2]59
60@pytest.mark.skip(reason="FlexOIDs regenerated on import; corruption detection not yet applicable")
[02d288d]61def test_json_file_corruption(approved_question, tmp_path):
[59342ce]62 """Simulate file corruption — integrity check must fail."""
[02d288d]63 file = tmp_path / "question.json"
64 json_str = approved_question.to_json()
[59342ce]65 file.write_text(json_str)
66
[02d288d]67 # Corrupt the file (simulate accidental byte modification)
[59342ce]68 corrupted = json_str.replace("Ohm’s", "Omm’s")
69 file.write_text(corrupted)
70
[8aa20c7]71 loaded = SingleChoiceQuestion.from_json(file.read_text())
72 assert not SingleChoiceQuestion.verify_integrity(loaded)
Note: See TracBrowser for help on using the repository browser.