Test API response for fixed keys

This commit is contained in:
ai-bot committed 2026-10-01 13:06:07 +00:00
1 parent 1684bc589b
commit eb54355594
1 file changed
+26 -3
+26 -3
View File
@@ -2,17 +2,27 @@
from http.server import ThreadingHTTPServer from http.server import ThreadingHTTPServer
import json import json
import os
import tempfile
import threading import threading
import unittest import unittest
from urllib.error import HTTPError from urllib.error import HTTPError
from urllib.request import urlopen from urllib.request import urlopen
from app import AppHandler from app import AppHandler
from database import initialize_database, seed_fixed_keys
class AppHandlerTests(unittest.TestCase): class AppHandlerTests(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.temp_dir = tempfile.TemporaryDirectory()
cls.database_path = os.path.join(cls.temp_dir.name, "test.sqlite3")
initialize_database(cls.database_path)
seed_fixed_keys(cls.database_path)
cls.previous_database_path = os.environ.get("DATABASE_PATH")
os.environ["DATABASE_PATH"] = cls.database_path
cls.server = ThreadingHTTPServer(("127.0.0.1", 0), AppHandler) cls.server = ThreadingHTTPServer(("127.0.0.1", 0), AppHandler)
cls.thread = threading.Thread(target=cls.server.serve_forever, daemon=True) cls.thread = threading.Thread(target=cls.server.serve_forever, daemon=True)
cls.thread.start() cls.thread.start()
@@ -23,6 +33,11 @@ class AppHandlerTests(unittest.TestCase):
cls.server.shutdown() cls.server.shutdown()
cls.server.server_close() cls.server.server_close()
cls.thread.join(timeout=2) cls.thread.join(timeout=2)
if cls.previous_database_path is None:
os.environ.pop("DATABASE_PATH", None)
else:
os.environ["DATABASE_PATH"] = cls.previous_database_path
cls.temp_dir.cleanup()
def test_health_endpoint_returns_json(self): def test_health_endpoint_returns_json(self):
with urlopen(f"{self.base_url}/health") as response: with urlopen(f"{self.base_url}/health") as response:
@@ -30,14 +45,22 @@ class AppHandlerTests(unittest.TestCase):
self.assertEqual(json.load(response), {"status": "ok"}) self.assertEqual(json.load(response), {"status": "ok"})
self.assertEqual(response.headers["X-Content-Type-Options"], "nosniff") self.assertEqual(response.headers["X-Content-Type-Options"], "nosniff")
def test_dashboard_is_english(self): def test_key_api_returns_the_three_fixed_profiles(self):
with urlopen(f"{self.base_url}/api/keys") as response:
keys = json.load(response)
self.assertEqual(response.status, 200)
self.assertEqual([key["device"] for key in keys], ["SeedSigner", "Blockstream Jade", "Nunchuk"])
self.assertEqual([key["key_number"] for key in keys], [1, 2, 3])
def test_dashboard_loads_key_profiles_from_api(self):
with urlopen(self.base_url) as response: with urlopen(self.base_url) as response:
body = response.read().decode("utf-8") body = response.read().decode("utf-8")
self.assertEqual(response.status, 200) self.assertEqual(response.status, 200)
self.assertIn('<html lang="en">', body) self.assertIn('<html lang="en">', body)
self.assertIn("Start annual health check", body) self.assertIn('id="key-list"', body)
self.assertNotIn("Årleg", body) self.assertIn('<script src="/app.js" defer></script>', body)
def test_unknown_path_returns_not_found(self): def test_unknown_path_returns_not_found(self):
try: try: