diff --git a/tests/test_app.py b/tests/test_app.py index fcbfd6c..7fce411 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -2,17 +2,27 @@ from http.server import ThreadingHTTPServer import json +import os +import tempfile import threading import unittest from urllib.error import HTTPError from urllib.request import urlopen from app import AppHandler +from database import initialize_database, seed_fixed_keys class AppHandlerTests(unittest.TestCase): @classmethod 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.thread = threading.Thread(target=cls.server.serve_forever, daemon=True) cls.thread.start() @@ -23,6 +33,11 @@ class AppHandlerTests(unittest.TestCase): cls.server.shutdown() cls.server.server_close() 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): 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(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: body = response.read().decode("utf-8") self.assertEqual(response.status, 200) self.assertIn('', body) - self.assertIn("Start annual health check", body) - self.assertNotIn("Ã…rleg", body) + self.assertIn('id="key-list"', body) + self.assertIn('', body) def test_unknown_path_returns_not_found(self): try: