tests: sign in before the model tests (section 31 point 1)

This commit is contained in:
ai-bot committed 2026-10-13 16:12:32 +00:00
1 parent 0488dcea7d
commit a325021fd1
1 file changed
+16 -20
+16 -20
View File
@@ -12,10 +12,10 @@ import tempfile
import threading import threading
import unittest import unittest
from urllib.error import HTTPError from urllib.error import HTTPError
from urllib.request import Request, urlopen
from app import AppHandler from app import AppHandler
from database import initialize_database, save_key_setup from database import initialize_database, save_key_setup
from server_session import SessionMixin
ADDRESS_PATH = "m/48'/0'/0'/2'" ADDRESS_PATH = "m/48'/0'/0'/2'"
@@ -42,7 +42,7 @@ FRESH_SETUP = {
} }
class KeyModelApiTests(unittest.TestCase): class KeyModelApiTests(SessionMixin, unittest.TestCase):
"""The model label across the key endpoints and the settings.""" """The model label across the key endpoints and the settings."""
def setUp(self): def setUp(self):
@@ -57,6 +57,8 @@ class KeyModelApiTests(unittest.TestCase):
self.thread = threading.Thread(target=self.server.serve_forever, daemon=True) self.thread = threading.Thread(target=self.server.serve_forever, daemon=True)
self.thread.start() self.thread.start()
self.base_url = f"http://127.0.0.1:{self.server.server_port}" self.base_url = f"http://127.0.0.1:{self.server.server_port}"
# Every route except /health needs a session (section 31 point 1).
self.authenticate()
def tearDown(self): def tearDown(self):
self.server.shutdown() self.server.shutdown()
@@ -68,19 +70,17 @@ class KeyModelApiTests(unittest.TestCase):
os.environ["DATABASE_PATH"] = self.previous_database_path os.environ["DATABASE_PATH"] = self.previous_database_path
self.temp_dir.cleanup() self.temp_dir.cleanup()
def get_json(self, path): def switch_to_a_fresh_database(self):
with urlopen(f"{self.base_url}{path}") as response: """Point the server at a new, empty database and sign in to it.
self.assertEqual(response.status, 200)
return json.load(response)
def post_json(self, path, payload): The session secret lives in the database, so the sign-in is repeated for
request = Request( the new file, or its cookie would be signed with the wrong secret.
f"{self.base_url}{path}", """
data=json.dumps(payload).encode("utf-8"), fresh = os.path.join(self.temp_dir.name, "fresh.sqlite3")
headers={"Content-Type": "application/json"}, initialize_database(fresh)
method="POST", os.environ["DATABASE_PATH"] = fresh
) self.authenticate()
return urlopen(request) return fresh
def assert_rejected_at(self, path, payload, status=400): def assert_rejected_at(self, path, payload, status=400):
try: try:
@@ -108,9 +108,7 @@ class KeyModelApiTests(unittest.TestCase):
def test_the_setup_endpoint_stores_the_model(self): def test_the_setup_endpoint_stores_the_model(self):
# A new database has no setup yet, so the first-run step writes the model # A new database has no setup yet, so the first-run step writes the model
# with the rest of the key (section 31 point 5). # with the rest of the key (section 31 point 5).
fresh = os.path.join(self.temp_dir.name, "fresh.sqlite3") self.switch_to_a_fresh_database()
initialize_database(fresh)
os.environ["DATABASE_PATH"] = fresh
with self.post_json("/api/setup", FRESH_SETUP) as response: with self.post_json("/api/setup", FRESH_SETUP) as response:
self.assertEqual(response.status, 201) self.assertEqual(response.status, 201)
@@ -121,9 +119,7 @@ class KeyModelApiTests(unittest.TestCase):
self.assertEqual([key["model"] for key in keys], ["Ledger Nano S", None, "Casa"]) self.assertEqual([key["model"] for key in keys], ["Ledger Nano S", None, "Casa"])
def test_an_over_long_model_is_rejected_by_the_setup(self): def test_an_over_long_model_is_rejected_by_the_setup(self):
fresh = os.path.join(self.temp_dir.name, "fresh.sqlite3") self.switch_to_a_fresh_database()
initialize_database(fresh)
os.environ["DATABASE_PATH"] = fresh
payload = json.loads(json.dumps(FRESH_SETUP)) payload = json.loads(json.dumps(FRESH_SETUP))
payload["keys"][0]["model"] = "x" * 61 payload["keys"][0]["model"] = "x" * 61