Spaces:
Sleeping
Sleeping
solidprivacy-nl commited on
Commit ·
9078630
1
Parent(s): 43b2999
Add WP37 DOCX hidden content extraction tests
Browse files
tests/test_docx_hidden_content_extractor.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from zipfile import ZipFile
|
| 5 |
+
|
| 6 |
+
from docx_hidden_content_extractor import inspect_docx_hidden_content
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
W_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def _minimal_docx(parts: dict[str, str]) -> bytes:
|
| 13 |
+
content_types = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
| 14 |
+
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
|
| 15 |
+
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
|
| 16 |
+
<Default Extension="xml" ContentType="application/xml"/>
|
| 17 |
+
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
|
| 18 |
+
</Types>"""
|
| 19 |
+
rels = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
| 20 |
+
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
| 21 |
+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
|
| 22 |
+
</Relationships>"""
|
| 23 |
+
output = BytesIO()
|
| 24 |
+
with ZipFile(output, "w") as docx:
|
| 25 |
+
docx.writestr("[Content_Types].xml", content_types)
|
| 26 |
+
docx.writestr("_rels/.rels", rels)
|
| 27 |
+
for part_name, xml in parts.items():
|
| 28 |
+
docx.writestr(part_name, xml)
|
| 29 |
+
return output.getvalue()
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def _text_part(text: str) -> str:
|
| 33 |
+
return f"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
| 34 |
+
<w:document xmlns:w="{W_NS}"><w:body><w:p><w:r><w:t>{text}</w:t></w:r></w:p></w:body></w:document>"""
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _header_or_footer(text: str) -> str:
|
| 38 |
+
return f"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
| 39 |
+
<w:hdr xmlns:w="{W_NS}"><w:p><w:r><w:t>{text}</w:t></w:r></w:p></w:hdr>"""
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def _comments(text: str, author: str = "SYNTHETIC-REVIEWER") -> str:
|
| 43 |
+
return f"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
| 44 |
+
<w:comments xmlns:w="{W_NS}"><w:comment w:id="0" w:author="{author}" w:initials="SR"><w:p><w:r><w:t>{text}</w:t></w:r></w:p></w:comment></w:comments>"""
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def _tracked_changes() -> str:
|
| 48 |
+
return f"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
| 49 |
+
<w:document xmlns:w="{W_NS}"><w:body><w:p><w:del><w:r><w:delText>OUDE-SYNTHETISCHE-WAARDE</w:delText></w:r></w:del><w:ins><w:r><w:t>NIEUWE-SYNTHETISCHE-WAARDE</w:t></w:r></w:ins></w:p></w:body></w:document>"""
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def test_extracts_headers_footers_comments_and_tracked_changes_without_cleaning():
|
| 53 |
+
docx_bytes = _minimal_docx({
|
| 54 |
+
"word/document.xml": _tracked_changes(),
|
| 55 |
+
"word/header1.xml": _header_or_footer("SYNTHETIC-HEADER-DOSSIER-001"),
|
| 56 |
+
"word/footer1.xml": _header_or_footer("SYNTHETIC-FOOTER-CLIENT-001"),
|
| 57 |
+
"word/comments.xml": _comments("SYNTHETIC-COMMENT-RISK-001"),
|
| 58 |
+
})
|
| 59 |
+
|
| 60 |
+
result = inspect_docx_hidden_content(docx_bytes)
|
| 61 |
+
|
| 62 |
+
assert result["valid_docx"] is True
|
| 63 |
+
assert result["local_only"] is True
|
| 64 |
+
assert result["ai_processing"] is False
|
| 65 |
+
assert result["cloud_processing"] is False
|
| 66 |
+
assert result["extraction_only"] is True
|
| 67 |
+
assert result["cleaning_applied"] is False
|
| 68 |
+
assert result["export_blocking"] is False
|
| 69 |
+
assert result["detected"] == {
|
| 70 |
+
"headers_detected": True,
|
| 71 |
+
"footers_detected": True,
|
| 72 |
+
"comments_detected": True,
|
| 73 |
+
"tracked_changes_detected": True,
|
| 74 |
+
}
|
| 75 |
+
assert result["headers"][0]["text"] == "SYNTHETIC-HEADER-DOSSIER-001"
|
| 76 |
+
assert result["footers"][0]["text"] == "SYNTHETIC-FOOTER-CLIENT-001"
|
| 77 |
+
assert result["comments"][0]["text"] == "SYNTHETIC-COMMENT-RISK-001"
|
| 78 |
+
assert result["comments"][0]["authors"] == ["SR", "SYNTHETIC-REVIEWER"]
|
| 79 |
+
assert {change["element"] for change in result["tracked_changes"]} >= {"del", "delText", "ins"}
|
| 80 |
+
assert any("OUDE-SYNTHETISCHE-WAARDE" in change["text"] for change in result["tracked_changes"])
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def test_header_footer_comment_absence_is_reported_without_warnings():
|
| 84 |
+
docx_bytes = _minimal_docx({"word/document.xml": _text_part("Alleen synthetische bodytekst.")})
|
| 85 |
+
|
| 86 |
+
result = inspect_docx_hidden_content(docx_bytes)
|
| 87 |
+
|
| 88 |
+
assert result["detected"] == {
|
| 89 |
+
"headers_detected": False,
|
| 90 |
+
"footers_detected": False,
|
| 91 |
+
"comments_detected": False,
|
| 92 |
+
"tracked_changes_detected": False,
|
| 93 |
+
}
|
| 94 |
+
assert result["headers"] == []
|
| 95 |
+
assert result["footers"] == []
|
| 96 |
+
assert result["comments"] == []
|
| 97 |
+
assert result["tracked_changes"] == []
|
| 98 |
+
assert result["warnings"] == []
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def test_invalid_docx_is_reported_without_side_effects():
|
| 102 |
+
result = inspect_docx_hidden_content(b"not a zip")
|
| 103 |
+
|
| 104 |
+
assert result["valid_docx"] is False
|
| 105 |
+
assert result["validation_issues"] == ["DOCX content is not a valid OOXML package."]
|
| 106 |
+
assert result["local_only"] is True
|
| 107 |
+
assert result["ai_processing"] is False
|
| 108 |
+
assert result["cloud_processing"] is False
|
| 109 |
+
assert result["cleaning_applied"] is False
|
| 110 |
+
assert result["export_blocking"] is False
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def test_non_bytes_input_is_rejected_safely():
|
| 114 |
+
result = inspect_docx_hidden_content("not bytes")
|
| 115 |
+
|
| 116 |
+
assert result["valid_docx"] is False
|
| 117 |
+
assert result["validation_issues"] == ["DOCX content must be bytes."]
|
| 118 |
+
assert result["headers"] == []
|
| 119 |
+
assert result["footers"] == []
|
| 120 |
+
assert result["comments"] == []
|
| 121 |
+
assert result["tracked_changes"] == []
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def test_extractor_uses_synthetic_values_only():
|
| 125 |
+
docx_bytes = _minimal_docx({
|
| 126 |
+
"word/document.xml": _text_part("SYNTHETIC-DOCX-INSPECTION-001"),
|
| 127 |
+
"word/comments.xml": _comments("SYNTHETIC-COMMENT-ONLY"),
|
| 128 |
+
})
|
| 129 |
+
|
| 130 |
+
result = inspect_docx_hidden_content(docx_bytes)
|
| 131 |
+
rendered = repr(result)
|
| 132 |
+
|
| 133 |
+
assert "SYNTHETIC" in rendered
|
| 134 |
+
assert "Jan Jansen" not in rendered
|
| 135 |
+
assert "Piet de Vries" not in rendered
|
| 136 |
+
assert "123456782" not in rendered
|