Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 4, 08:00 UTC
test(mapping-xlsx): reproduce hidden sheet, row and column ingestion bypass
davoodmehraban89 committed
Oct 30, 2026 at 17:25 UTC
e266906686a265d32dfdf8032c0cafec32ff3a73
1 file changed
+55
backend/tests/test_mapping_excel.py
+55
@@ -144,3 +144,58 @@ def test_reject_duplicate_export():
144
def test_reject_empty_export():
145
with pytest.raises(MappingValidationError, match="1 to 10000"):
146
export_mapping_workbook(())
147
+
148
+
149
+def workbook_with_hidden_mapping(kind, note_id=1):
150
+ book = Workbook()
151
+ sheet = book.active
152
+ sheet.title = "Mapping"
153
+ sheet.append(("rule_key", "account_code", "target_note_id", "mapping_type", "version"))
154
+ sheet.append(("cash", "001", note_id, "direct", 1))
155
+ if kind == "row":
156
+ sheet.row_dimensions[2].hidden = True
157
+ elif kind == "header":
158
+ sheet.row_dimensions[1].hidden = True
159
+ elif kind == "column":
160
+ sheet.column_dimensions["B"].hidden = True
161
+ elif kind == "last_column":
162
+ sheet.column_dimensions["E"].hidden = True
163
+ elif kind != "sheet":
164
+ raise AssertionError("unknown test variant")
165
+ stream = BytesIO()
166
+ book.save(stream)
167
+ if kind != "sheet":
168
+ return stream.getvalue()
169
+
170
+ # Excel/openpyxl forbids saving a workbook with its *only* sheet hidden.
171
+ # Mutate workbook metadata in a valid single-worksheet ZIP to reproduce
172
+ # an adversarial file without adding an extra worksheet.
173
+ altered = BytesIO()
174
+ with ZipFile(BytesIO(stream.getvalue()), "r") as source:
175
+ with ZipFile(altered, "w") as dest:
176
+ for item in source.infolist():
177
+ content = source.read(item.filename)
178
+ if item.filename == "xl/workbook.xml":
179
+ assert b'state="visible"' in content
180
+ content = content.replace(b'state="visible"', b'state="hidden"', 1)
181
+ dest.writestr(item, content)
182
+ return altered.getvalue()
183
+
184
+
185
+@pytest.mark.parametrize("kind", ["sheet", "row", "header", "column", "last_column"])
186
+def test_reject_hidden_mapping_sheet_or_data(kind):
187
+ with pytest.raises(MappingValidationError, match="[Hh]idden"):
188
+ preview_mapping_workbook(workbook_with_hidden_mapping(kind))
189
+
190
+
191
+@pytest.mark.parametrize("kind", ["sheet", "row", "column"])
192
+def test_hidden_mapping_import_never_stages_rules(session, kind):
193
+ year, note = make_year(session, "HIDDEN-" + kind)
194
+ content = workbook_with_hidden_mapping(kind, note.id)
195
+ with pytest.raises(MappingValidationError, match="[Hh]idden"):
196
+ import_mapping_workbook(session, year_id=year.id, content=content)
197
+ assert session.scalars(select(MappingRule)).all() == []
198
+
199
+def test_visible_mapping_workbook_still_accepted():
200
+ assert preview_mapping_workbook(xlsx([("cash", "001", 1, "direct", 1)])) == (
201
+ entry(1),)