From 429a76eca94dc5710d015e042e47319f03556410 Mon Sep 17 00:00:00 2001 From: ai-bot Date: Sat, 24 Oct 2026 10:38:41 +0000 Subject: [PATCH] Cover the exported closures and the frozen snapshot (section 32.8 points 5 and 6) --- tests/test_export.py | 144 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 141 insertions(+), 3 deletions(-) diff --git a/tests/test_export.py b/tests/test_export.py index 08f44cf..6cfb4c4 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -2,9 +2,21 @@ import csv import io +import os +import tempfile import unittest +from database import ( + initialize_database, + list_export_rows, + list_keys, + rotate_key, + save_alert_acknowledgement, + save_health_check, + save_key_setup, +) from export import COLUMNS, FLAG_KEYS, HEADERS, build_csv +from validation import validate_health_check def parse(text): @@ -12,6 +24,53 @@ def parse(text): return list(csv.reader(io.StringIO(text))) +# The address path each key slot is configured with. Every key has its own one, +# so the setup the API would accept is the setup the export reads back +# (sections 25.1 and 26.2). +ADDRESS_PATH = "m/48'/0'/0'/2'" +SECOND_ADDRESS_PATH = "m/48'/1'/0'/2'" +THIRD_ADDRESS_PATH = "m/48'/2'/0'/2'" +SETUP = ( + {"key_number": 1, "name": "SeedSigner", "type": "hardware", "address_path": ADDRESS_PATH}, + {"key_number": 2, "name": "Blockstream Jade", "type": "hardware", "address_path": SECOND_ADDRESS_PATH}, + {"key_number": 3, "name": "Nunchuk", "type": "cloud", "address_path": THIRD_ADDRESS_PATH}, +) +# The wallet software list the settings hold, and that the app passes to the +# validator (section 25.4). +WALLET_SOFTWARE = ("Sparrow Wallet",) + + +def sample_payload(key): + """Return a valid payload for one key, dated in the past (section 32.8 point 3).""" + wallet_software = key["name"] if key["type"] == "cloud" else "Sparrow Wallet" + labels = list(dict.fromkeys((key["name"], wallet_software))) + entry = { + "key_id": key["id"], + "available": 1, + "address_path": key["address_path"], + "comments": "", + } + if key["type"] == "hardware": + entry.update({ + "hardware_works": 1, + "firmware_version": "1.0.0", + "seed_backup_verified": "OK", + }) + return { + "checked_at": "2026-10-01", + "wallet_software": wallet_software, + "software_version": "1.8.0", + "software_compatibility_status": "OK", + "software_comments": "", + "contact_info_verified": "Confirmed current", + "comments": "Annual review", + "key_check": entry, + "ai_security_checks": [ + {"provider": label, "status": "SECURE", "comments": ""} for label in labels + ], + } + + class ExportTests(unittest.TestCase): def test_empty_export_has_only_the_header_row(self): self.assertEqual(parse(build_csv([])), [list(HEADERS)]) @@ -69,9 +128,9 @@ class ExportTests(unittest.TestCase): self.assertEqual(values["Contact details confirmed"], value) def test_the_follow_up_closed_column_is_last_and_reads_back(self): - # The closures that belong to a key are joined into the last column, - # like the security statuses (section 27). - joined = "key_unavailable: rotate (2026-10-05); hardware_failure: replace_hardware (2026-10-06)" + # Every closure of a check is joined into the last column, like the + # security statuses (sections 27 and 32.8 point 6). + joined = "key_unavailable: rotate (2026-10-05); software_incompatible: remove_software (2026-10-06)" rows = parse(build_csv([{"closed_alerts": joined}])) values = dict(zip(HEADERS, rows[1])) @@ -108,6 +167,85 @@ class ExportTests(unittest.TestCase): self.assertEqual(values["Software compatibility"], "NOT_ASSESSED") +class ExportHistoryTests(unittest.TestCase): + """The exported rows are read from the stored history. + + The key name and the key type come from the snapshot the check carries, so a + rotation cannot rewrite an older row, and every closure of a check is joined + into the last column, whether it belongs to a key or to a software entry in + the settings list (sections 10, 27 and 32.8 points 5 and 6). + """ + + def setUp(self): + self.temp_dir = tempfile.TemporaryDirectory() + self.database_path = os.path.join(self.temp_dir.name, "test.sqlite3") + initialize_database(self.database_path) + save_key_setup(SETUP, self.database_path) + + def tearDown(self): + self.temp_dir.cleanup() + + def keys(self): + """Return the active key profiles of the stored setup.""" + return list_keys(self.database_path) + + def store_check(self, key): + """Store one valid check for a key and return its id.""" + return save_health_check( + validate_health_check(sample_payload(key), self.keys(), WALLET_SOFTWARE), + self.database_path, + ) + + def export_rows(self): + """Return the exported CSV as a list of header-keyed data rows.""" + rows = parse(build_csv(list_export_rows(self.database_path))) + self.assertEqual(rows[0], list(HEADERS)) + return [dict(zip(HEADERS, row)) for row in rows[1:]] + + def test_a_closed_software_alert_is_written_into_the_last_column(self): + # A software alert raised by the settings list belongs to no key, but its + # closure still hangs on the check it closed through its check_id, so it + # is listed as well (sections 10, 27 and 32.8 point 6). + key = self.keys()[0] + saved_id = self.store_check(key) + save_alert_acknowledgement( + "software_incompatible", + "Sparrow Wallet", + None, + saved_id, + "remove_software", + self.database_path, + ) + + rows = self.export_rows() + + self.assertEqual(len(rows), 1) + self.assertEqual(HEADERS[-1], "Follow-up closed") + self.assertRegex( + rows[0]["Follow-up closed"], + r"^software_incompatible: remove_software \(\d{4}-\d{2}-\d{2}\)$", + ) + + def test_an_older_row_keeps_the_name_and_the_type_of_its_snapshot(self): + # A rotation retires the key row and inserts a new one for the same + # multisig slot, so the export row that was already written keeps the name + # and the type it was recorded with (section 32.8 point 5). + key = self.keys()[0] + self.store_check(key) + + rotate_key(key["id"], "ColdCard", "m/48'/7'/0'/2'", self.database_path) + + rows = self.export_rows() + + self.assertEqual(len(rows), 1) + self.assertEqual(rows[0]["Key number"], "1") + self.assertEqual(rows[0]["Key name"], "SeedSigner") + self.assertEqual(rows[0]["Key type"], "hardware") + # The active row carries the new name, which the next check records. + self.assertEqual(self.keys()[0]["name"], "ColdCard") + self.assertEqual(self.keys()[0]["address_path"], "m/48'/7'/0'/2'") + + class FormulaInjectionTests(unittest.TestCase): """A user-controlled text cell is never written as a spreadsheet formula.