| 1 | import json |
| 2 | from pathlib import Path |
| 3 | import runpy |
| 4 | import shutil |
| 5 | import subprocess |
| 6 | from tempfile import TemporaryDirectory |
| 7 | import unittest |
| 8 | |
| 9 | from document_model import EXPORTER |
| 10 | |
| 11 | ROOT = Path(__file__).resolve().parent.parent |
| 12 | FIXTURE = ROOT / 'corpus/protected-edit' |
| 13 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 14 | |
| 15 | |
| 16 | class ProtectedEditTest(unittest.TestCase): |
| 17 | def setUp(self): |
| 18 | self.temporary = TemporaryDirectory() |
| 19 | self.root = Path(self.temporary.name) |
| 20 | self.password = self.root / 'password' |
| 21 | manifest = json.loads((ROOT / 'corpus/native-encrypted/manifest.json').read_text()) |
| 22 | self.password.write_text(manifest['password']) |
| 23 | |
| 24 | def tearDown(self): |
| 25 | self.temporary.cleanup() |
| 26 | |
| 27 | def test_onenote_unlocks_and_reads_the_page_saved_under_the_section_key(self): |
| 28 | native = self.root / 'read' |
| 29 | shutil.copytree(FIXTURE / 'candidate/read', native) |
| 30 | compare(FIXTURE / 'candidate/notebook', native, password_file=self.password) |
| 31 | page = (native / 'page-000.xml').read_text(encoding='utf-8-sig') |
| 32 | self.assertIn('and edited under its key', page) |
| 33 | self.assertIn('Written while protected', page) |
| 34 | |
| 35 | def test_the_native_edit_that_followed_unlocks_with_both_edits(self): |
| 36 | output = self.root / 'export' |
| 37 | subprocess.run([EXPORTER, FIXTURE / 'native-after/synthetic.one', output, |
| 38 | '--password-file', self.password], check=True, capture_output=True) |
| 39 | text = (output / 'text.json').read_text() |
| 40 | for expected in ['Native edit after Rust.', 'Written while protected', 'and edited under its key']: |
| 41 | self.assertIn(expected, text) |
| 42 | |
| 43 | |
| 44 | if __name__ == '__main__': |
| 45 | unittest.main() |