1import json
2from pathlib import Path
3import runpy
4import shutil
5import subprocess
6from tempfile import TemporaryDirectory
7import unittest
8
9from document_model import EXPORTER
10
11ROOT = Path(__file__).resolve().parent.parent
12FIXTURE = ROOT / 'corpus/protected-edit'
13compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare']
14
15
16class 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
44if __name__ == '__main__':
45 unittest.main()