tests: cover delete-all-data, which leaves the admin account (section 31 point 2)
This commit is contained in:
1 parent
a85d37060e
commit
2e43c1f00f
1 file changed
+51
-29
+51
-29
@@ -1,8 +1,10 @@
|
||||
"""Tests for deleting the stored history (section 31 point 2).
|
||||
"""Tests for deleting all app data (section 31 point 2).
|
||||
|
||||
The history, the export and the alerts are emptied, while the key setup, the
|
||||
multisig settings and the wallet software list stay. The acknowledgements have
|
||||
no foreign key, so they are deleted explicitly.
|
||||
The key setup, the history, the export, the alerts and the wallet software list
|
||||
are emptied, so the app is back at the first-run setup step, while the admin
|
||||
credentials and the session secret stay. The acknowledgements have no foreign
|
||||
key, so they are deleted explicitly, and the checks have to go before the keys
|
||||
because ``key_checks.key_id`` refuses to be orphaned.
|
||||
"""
|
||||
|
||||
from contextlib import redirect_stdout
|
||||
@@ -11,10 +13,12 @@ import os
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
import auth
|
||||
import maintenance
|
||||
from database import (
|
||||
DEFAULT_WALLET_SOFTWARE,
|
||||
delete_history,
|
||||
delete_all_data,
|
||||
get_app_settings,
|
||||
get_health_check,
|
||||
get_multisig_settings,
|
||||
initialize_database,
|
||||
@@ -86,7 +90,7 @@ def unavailable_payload(key):
|
||||
}
|
||||
|
||||
|
||||
class DeleteHistoryTests(unittest.TestCase):
|
||||
class DeleteAllDataTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp_dir = tempfile.TemporaryDirectory()
|
||||
self.database_path = os.path.join(self.temp_dir.name, "test.sqlite3")
|
||||
@@ -111,12 +115,16 @@ class DeleteHistoryTests(unittest.TestCase):
|
||||
"rotate",
|
||||
self.database_path,
|
||||
)
|
||||
# The admin account is the one thing that survives the reset, so it is
|
||||
# stored here to be checked.
|
||||
auth.set_admin("admin", "s3cret", self.database_path)
|
||||
self.session_secret = auth.session_secret(self.database_path)
|
||||
|
||||
def tearDown(self):
|
||||
self.temp_dir.cleanup()
|
||||
|
||||
def test_deleting_the_history_empties_the_checks_the_export_and_the_alerts(self):
|
||||
deleted = delete_history(self.database_path)
|
||||
def test_deleting_everything_empties_the_checks_the_export_and_the_alerts(self):
|
||||
deleted = delete_all_data(self.database_path)
|
||||
|
||||
self.assertEqual(deleted, 2)
|
||||
self.assertEqual(list_health_checks(self.database_path), [])
|
||||
@@ -126,28 +134,40 @@ class DeleteHistoryTests(unittest.TestCase):
|
||||
self.assertIsNone(get_health_check(self.first, self.database_path))
|
||||
self.assertIsNone(get_health_check(self.second, self.database_path))
|
||||
|
||||
def test_the_key_setup_and_the_software_list_survive(self):
|
||||
delete_history(self.database_path)
|
||||
def test_the_key_setup_goes_too(self):
|
||||
delete_all_data(self.database_path)
|
||||
|
||||
self.assertEqual(list_keys(self.database_path), [])
|
||||
|
||||
def test_the_app_is_left_the_way_a_new_database_starts(self):
|
||||
# The seeded wallet software entry comes back and the multisig shape
|
||||
# falls back to the 2-of-3 default, which is exactly what a database
|
||||
# without a setup reports (sections 25.4 and 31 point 7).
|
||||
delete_all_data(self.database_path)
|
||||
|
||||
keys = list_keys(self.database_path)
|
||||
self.assertEqual(
|
||||
[key["name"] for key in keys],
|
||||
["SeedSigner", "Blockstream Jade", "Nunchuk"],
|
||||
)
|
||||
self.assertEqual(
|
||||
[key["address_path"] for key in keys],
|
||||
[ADDRESS_PATH, SECOND_ADDRESS_PATH, THIRD_ADDRESS_PATH],
|
||||
)
|
||||
self.assertEqual(list_wallet_software(self.database_path), [DEFAULT_WALLET_SOFTWARE])
|
||||
self.assertEqual(
|
||||
get_multisig_settings(self.database_path),
|
||||
{"threshold": 2, "key_count": 3},
|
||||
)
|
||||
|
||||
def test_deleting_an_empty_history_reports_zero(self):
|
||||
delete_history(self.database_path)
|
||||
def test_the_admin_account_and_the_session_secret_survive(self):
|
||||
delete_all_data(self.database_path)
|
||||
|
||||
self.assertEqual(delete_history(self.database_path), 0)
|
||||
settings = get_app_settings(self.database_path)
|
||||
self.assertEqual(settings.get("admin_username"), "admin")
|
||||
self.assertTrue(settings.get("admin_password_hash", "").startswith("scrypt$"))
|
||||
self.assertEqual(settings.get("session_secret"), self.session_secret)
|
||||
# The multisig rows are gone, so the default applies again.
|
||||
self.assertNotIn("multisig_threshold", settings)
|
||||
self.assertNotIn("multisig_key_count", settings)
|
||||
# The credential still works after the reset.
|
||||
self.assertTrue(auth.authenticate("admin", "s3cret", self.database_path))
|
||||
|
||||
def test_deleting_an_empty_database_reports_zero(self):
|
||||
delete_all_data(self.database_path)
|
||||
|
||||
self.assertEqual(delete_all_data(self.database_path), 0)
|
||||
|
||||
|
||||
class MaintenanceCommandTests(unittest.TestCase):
|
||||
@@ -172,21 +192,23 @@ class MaintenanceCommandTests(unittest.TestCase):
|
||||
code = maintenance.main(argv)
|
||||
return code, buffer.getvalue()
|
||||
|
||||
def test_the_delete_history_command_reports_the_count(self):
|
||||
code, output = self.run_command(["delete-history", "--database", self.database_path])
|
||||
def test_the_delete_all_data_command_reports_the_count(self):
|
||||
code, output = self.run_command(["delete-all-data", "--database", self.database_path])
|
||||
|
||||
self.assertEqual(code, 0)
|
||||
self.assertEqual(
|
||||
output.strip(),
|
||||
"Deleted 1 saved health check, with the follow-up they closed.",
|
||||
"Deleted the key setup, 1 saved health check, the follow-up they closed, "
|
||||
"and the wallet software list. The admin account is kept.",
|
||||
)
|
||||
self.assertEqual(list_health_checks(self.database_path), [])
|
||||
self.assertEqual(list_keys(self.database_path), [])
|
||||
|
||||
def test_the_database_path_can_come_from_the_environment(self):
|
||||
previous = os.environ.get("DATABASE_PATH")
|
||||
os.environ["DATABASE_PATH"] = self.database_path
|
||||
try:
|
||||
code, output = self.run_command(["delete-history"])
|
||||
code, output = self.run_command(["delete-all-data"])
|
||||
finally:
|
||||
if previous is None:
|
||||
os.environ.pop("DATABASE_PATH", None)
|
||||
@@ -194,17 +216,17 @@ class MaintenanceCommandTests(unittest.TestCase):
|
||||
os.environ["DATABASE_PATH"] = previous
|
||||
|
||||
self.assertEqual(code, 0)
|
||||
self.assertIn("Deleted 1 saved health check", output)
|
||||
self.assertIn("Deleted the key setup, 1 saved health check", output)
|
||||
|
||||
def test_the_command_handles_a_database_that_was_never_opened(self):
|
||||
# The schema is created first, so the command is safe on a fresh path
|
||||
# (section 31 point 2).
|
||||
fresh = os.path.join(self.temp_dir.name, "fresh.sqlite3")
|
||||
|
||||
code, output = self.run_command(["delete-history", "--database", fresh])
|
||||
code, output = self.run_command(["delete-all-data", "--database", fresh])
|
||||
|
||||
self.assertEqual(code, 0)
|
||||
self.assertIn("Deleted 0 saved health checks", output)
|
||||
self.assertIn("Deleted the key setup, 0 saved health checks", output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user