Section 31 point 4: key status carries the address path

This commit is contained in:
ai-bot committed 2026-10-13 08:12:10 +00:00
1 parent 8462eedc10
commit e04d6c7651
1 file changed
+121
+121
View File
@@ -0,0 +1,121 @@
"""Tests for the address path on the key status and the key card.
The dashboard card shows the address path of each key, so ``GET /api/key-status``
has to carry it, and an empty path -- which a migrated key can have -- is left
out instead of rendering an empty line (section 31 point 4).
"""
from http.server import ThreadingHTTPServer
import json
import os
from pathlib import Path
import tempfile
import threading
import unittest
from urllib.request import urlopen
from app import AppHandler
from database import initialize_database, save_key_setup
ROOT = Path(__file__).resolve().parents[1]
ADDRESS_PATH = "m/48'/0'/0'/2'"
SECOND_ADDRESS_PATH = "m/48'/1'/0'/2'"
THIRD_ADDRESS_PATH = "m/48'/2'/0'/2'"
DEFAULT_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},
]
# A setup as an older database leaves it: the address paths were added by the
# migration and are still empty (section 25.1).
MIGRATED_SETUP = [
{"key_number": 1, "name": "SeedSigner", "type": "hardware", "address_path": ""},
{"key_number": 2, "name": "Blockstream Jade", "type": "hardware", "address_path": ""},
{"key_number": 3, "name": "Nunchuk", "type": "cloud", "address_path": ""},
]
def read_static(name):
"""Return the text of one static asset."""
return (ROOT / "static" / name).read_text(encoding="utf-8")
class KeyStatusAddressPathTests(unittest.TestCase):
"""The address path across the key status and the dashboard card."""
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(DEFAULT_SETUP, self.database_path)
self.previous_database_path = os.environ.get("DATABASE_PATH")
os.environ["DATABASE_PATH"] = self.database_path
self.server = ThreadingHTTPServer(("127.0.0.1", 0), AppHandler)
self.thread = threading.Thread(target=self.server.serve_forever, daemon=True)
self.thread.start()
self.base_url = f"http://127.0.0.1:{self.server.server_port}"
def tearDown(self):
self.server.shutdown()
self.server.server_close()
self.thread.join(timeout=2)
if self.previous_database_path is None:
os.environ.pop("DATABASE_PATH", None)
else:
os.environ["DATABASE_PATH"] = self.previous_database_path
self.temp_dir.cleanup()
def get_json(self, path):
with urlopen(f"{self.base_url}{path}") as response:
self.assertEqual(response.status, 200)
return json.load(response)
def test_the_key_status_carries_the_address_path(self):
# The card reads the path from the key status, so the endpoint has to
# give it, in key order (section 31 point 4).
status = self.get_json("/api/key-status")
self.assertEqual([entry["key_number"] for entry in status], [1, 2, 3])
self.assertEqual(
[entry["address_path"] for entry in status],
[ADDRESS_PATH, SECOND_ADDRESS_PATH, THIRD_ADDRESS_PATH],
)
def test_an_empty_address_path_is_passed_through_as_it_stands(self):
# A key the migration left without a path answers with an empty value,
# which the card leaves out (sections 25.1 and 31.4).
migrated = os.path.join(self.temp_dir.name, "migrated.sqlite3")
initialize_database(migrated)
save_key_setup(MIGRATED_SETUP, migrated)
os.environ["DATABASE_PATH"] = migrated
status = self.get_json("/api/key-status")
self.assertEqual([entry["address_path"] for entry in status], ["", "", ""])
def test_the_key_status_still_carries_the_dates_with_the_path(self):
# The path is an addition, not a replacement: the dates stay in the same
# answer (section 31 point 4).
status = self.get_json("/api/key-status")
self.assertEqual([entry["last_checked_at"] for entry in status], [None, None, None])
self.assertEqual([entry["next_check_due"] for entry in status], [None, None, None])
def test_the_dashboard_card_shows_the_path_between_the_name_and_the_type(self):
# createKeyCard() renders the path between the name and the type, and an
# empty path is skipped (section 31 point 4).
script = read_static("app.js")
self.assertIn("entry.address_path", script)
name_index = script.index("card.append(top, name);")
path_index = script.index("card.append(path);")
type_index = script.index("card.append(type);")
self.assertLess(name_index, path_index)
self.assertLess(path_index, type_index)
self.assertIn("if (entry.address_path) {", script)
if __name__ == "__main__":
unittest.main()