From b83c583dc534fcb2d269382f217fdfa51193c843 Mon Sep 17 00:00:00 2001 From: ai-bot Date: Thu, 1 Oct 2026 22:22:54 +0000 Subject: [PATCH] Update file --- tests/test_database.py | 180 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 162 insertions(+), 18 deletions(-) diff --git a/tests/test_database.py b/tests/test_database.py index 929ed58..ffbfaf2 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -1,4 +1,4 @@ -"""Tests for SQLite schema initialization and constraints.""" +"""Tests for SQLite schema, storage, and constraints.""" from contextlib import contextmanager import sqlite3 @@ -6,7 +6,66 @@ import tempfile import unittest from pathlib import Path -from database import SCHEMA_VERSION, initialize_database, list_keys, seed_fixed_keys +from database import ( + SCHEMA_VERSION, + get_health_check, + initialize_database, + list_health_checks, + list_keys, + save_health_check, + seed_fixed_keys, +) +from validation import validate_health_check + + +PRODUCTS = ("SeedSigner", "Blockstream Jade", "Nunchuk", "Sparrow Wallet") + + +def sample_payload(keys): + """Return a valid health check payload matching the given key profiles.""" + return { + "checked_at": "2026-10-01", + "next_check_due": "2027-10-01", + "software_compatibility_status": "OK", + "sparrow_version": "1.8.0", + "nunchuk_version": "", + "software_comments": "", + "contact_info_verified": 1, + "comments": "Annual review", + "key_checks": [ + { + "key_id": key["id"], + "available": 1, + "hardware_works": 1 if key["type"] == "hardware" else None, + "firmware_version": "1.0.0" if key["type"] == "hardware" else "", + "seed_backup_status": "OK" if key["type"] == "hardware" else None, + "seed_backup_verified": 1 if key["type"] == "hardware" else None, + "cloud_health_status": "OK" if key["type"] == "cloud" else None, + "cloud_provider_operational": 1 if key["type"] == "cloud" else None, + "address_verified": 1, + "derivation_height": "m/48'/0'/0'/2'", + "address_path_verified": 1, + "address_path": "m/48'/0'/0'/2'/0/0", + "comments": "", + } + for key in keys + ], + "tool_checks": [ + { + "product": product, + "bip327_status": "IKKJE_VURDERT", + "slip39_status": "IKKJE_VURDERT", + "version": "", + "checked_at": "", + "notes": "", + } + for product in PRODUCTS + ], + "ai_security_checks": [ + {"provider": product, "status": "TRYGG", "comments": ""} + for product in PRODUCTS + ], + } class DatabaseInitializationTests(unittest.TestCase): @@ -46,33 +105,98 @@ class DatabaseInitializationTests(unittest.TestCase): <= tables ) - def test_v1_database_migrates_to_current_schema_without_losing_data(self): + def test_partial_schema_with_zero_version_is_repaired_idempotently(self): self.database_path.parent.mkdir(parents=True, exist_ok=True) - connection = sqlite3.connect(self.database_path) - try: + with sqlite3.connect(self.database_path) as connection: connection.executescript( """ CREATE TABLE key_checks ( id INTEGER PRIMARY KEY, - address_path_verified INTEGER, - address_path TEXT + address_verified INTEGER, + derivation_height TEXT, + seed_backup_verified INTEGER ); - INSERT INTO key_checks (address_path_verified, address_path) VALUES (1, "m/48'/0'/0'/2'"); + CREATE TABLE health_checks ( + id INTEGER PRIMARY KEY, + software_comments TEXT + ); + INSERT INTO key_checks (address_verified, derivation_height) VALUES (1, '0'); + PRAGMA user_version = 0; + """ + ) + + initialize_database(self.database_path) + initialize_database(self.database_path) + + with self.connect() as connection: + version = connection.execute("PRAGMA user_version").fetchone()[0] + row = connection.execute( + "SELECT address_verified, derivation_height FROM key_checks" + ).fetchone() + columns = { + row[1] for row in connection.execute("PRAGMA table_info(key_checks)") + } + + self.assertEqual(version, SCHEMA_VERSION) + self.assertEqual(row, (1, "0")) + self.assertIn("seed_backup_verified", columns) + + def test_legacy_database_migrates_without_losing_existing_values(self): + self.database_path.parent.mkdir(parents=True, exist_ok=True) + with sqlite3.connect(self.database_path) as connection: + connection.executescript( + """ + CREATE TABLE keys ( + id INTEGER PRIMARY KEY, + key_number INTEGER NOT NULL UNIQUE, + name TEXT NOT NULL, + type TEXT NOT NULL, + device TEXT NOT NULL, + created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TABLE health_checks ( + id INTEGER PRIMARY KEY, + checked_at TEXT NOT NULL, + next_check_due TEXT NOT NULL, + software_compatibility_status TEXT NOT NULL DEFAULT 'IKKJE_VURDERT', + sparrow_version TEXT, + nunchuk_version TEXT, + contact_info_verified INTEGER, + comments TEXT + ); + CREATE TABLE key_checks ( + id INTEGER PRIMARY KEY, + health_check_id INTEGER NOT NULL, + key_id INTEGER NOT NULL, + available INTEGER, + hardware_works INTEGER, + firmware_version TEXT, + seed_backup_status TEXT, + cloud_health_status TEXT, + cloud_provider_operational INTEGER, + address_path_verified INTEGER, + address_path TEXT, + comments TEXT, + UNIQUE (health_check_id, key_id) + ); + INSERT INTO health_checks (checked_at, next_check_due) VALUES ('2026-01-01', '2027-01-01'); PRAGMA user_version = 1; """ ) - finally: - connection.close() initialize_database(self.database_path) with self.connect() as connection: version = connection.execute("PRAGMA user_version").fetchone()[0] - row = connection.execute( - "SELECT address_path_verified, address_path, address_verified, derivation_height FROM key_checks" - ).fetchone() + health_check_id = connection.execute("SELECT id FROM health_checks").fetchone()[0] + columns = { + row[1] for row in connection.execute("PRAGMA table_info(key_checks)") + } self.assertEqual(version, SCHEMA_VERSION) - self.assertEqual(row, (1, "m/48'/0'/0'/2'", None, None)) + self.assertEqual(health_check_id, 1) + self.assertTrue( + {"address_verified", "derivation_height", "seed_backup_verified"} <= columns + ) def test_seed_fixed_keys_is_idempotent_and_preserves_custom_names(self): initialize_database(self.database_path) @@ -95,6 +219,29 @@ class DatabaseInitializationTests(unittest.TestCase): seed_fixed_keys(self.database_path) self.assertEqual(list_keys(self.database_path)[1]["name"], "My Jade") + def test_saved_health_checks_are_stored_and_never_overwritten(self): + initialize_database(self.database_path) + seed_fixed_keys(self.database_path) + keys = list_keys(self.database_path) + data = validate_health_check(sample_payload(keys), {key["id"] for key in keys}) + + first_id = save_health_check(data, self.database_path) + second_id = save_health_check(data, self.database_path) + + self.assertNotEqual(first_id, second_id) + summaries = list_health_checks(self.database_path) + self.assertEqual([entry["id"] for entry in summaries], [second_id, first_id]) + + record = get_health_check(first_id, self.database_path) + self.assertEqual(record["checked_at"], "2026-10-01") + self.assertEqual(record["comments"], "Annual review") + self.assertEqual(len(record["key_checks"]), 3) + self.assertEqual(len(record["tool_checks"]), 4) + self.assertEqual(len(record["ai_security_checks"]), 4) + self.assertEqual(record["key_checks"][0]["key_number"], 1) + self.assertEqual(record["ai_security_checks"][0]["status"], "TRYGG") + self.assertIsNone(get_health_check(9999, self.database_path)) + def test_invalid_status_and_duplicate_key_numbers_are_rejected(self): initialize_database(self.database_path) with self.connect() as connection: @@ -132,11 +279,8 @@ class DatabaseInitializationTests(unittest.TestCase): def test_unknown_future_schema_version_is_rejected(self): self.database_path.parent.mkdir(parents=True, exist_ok=True) - connection = sqlite3.connect(self.database_path) - try: + with sqlite3.connect(self.database_path) as connection: connection.execute(f"PRAGMA user_version = {SCHEMA_VERSION + 1}") - finally: - connection.close() with self.assertRaises(RuntimeError): initialize_database(self.database_path)