| 1 | from pathlib import Path |
| 2 | import hashlib |
| 3 | import json |
| 4 | import runpy |
| 5 | import shutil |
| 6 | from tempfile import TemporaryDirectory |
| 7 | import unittest |
| 8 | import xml.etree.ElementTree as ET |
| 9 | |
| 10 | from native_xml import ns |
| 11 | |
| 12 | ROOT = Path(__file__).resolve().parent.parent |
| 13 | FIXTURE = ROOT / 'corpus/attachment-floating' |
| 14 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 15 | |
| 16 | |
| 17 | class AttachmentFloatingTest(unittest.TestCase): |
| 18 | def test_files_the_editor_put_on_the_page_open_in_onenote_where_they_were_placed(self): |
| 19 | read = FIXTURE / 'cold/read' |
| 20 | with TemporaryDirectory() as temporary: |
| 21 | copy = Path(temporary) / 'read' |
| 22 | shutil.copytree(read, copy) |
| 23 | compare(FIXTURE / 'candidate', copy) |
| 24 | page, = (ET.parse(path).getroot() for path in sorted(read.glob('page-*.xml'))) |
| 25 | self.assertEqual(page.findall('.//one:OE/one:InsertedFile', ns), []) |
| 26 | placed = {} |
| 27 | for inserted in page.findall('one:InsertedFile', ns): |
| 28 | position = inserted.find('one:Position', ns) |
| 29 | placed[inserted.get('preferredName')] = ( |
| 30 | round(float(position.get('x')), 2), round(float(position.get('y')), 2)) |
| 31 | # Attached at the caret a click on blank page left, the second then dragged. |
| 32 | self.assertEqual(placed, {'float 🦀.txt': (342.0, 284.4), 'large.bin': (288.0, 140.4)}) |
| 33 | # The bytes OneNote hands an application when the file is opened. |
| 34 | payloads = json.loads((read / 'payloads.json').read_text(encoding='utf-8-sig')) |
| 35 | self.assertEqual(sorted((p['name'], p['bytes']) for p in payloads), |
| 36 | [('float 🦀.txt', 37), ('large.bin', 2 << 20)]) |
| 37 | for payload in payloads: |
| 38 | data = (read / (payload['sha256'] + '.attachment')).read_bytes() |
| 39 | self.assertEqual(hashlib.sha256(data).hexdigest(), payload['sha256']) |
| 40 | |
| 41 | |
| 42 | if __name__ == '__main__': |
| 43 | unittest.main() |