Move CORM settings and histories to dedicated PostgreSQL database
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
"""Validate operator deployment decisions without production access."""
|
||||
|
||||
import io
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import redirect_stderr, redirect_stdout
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import deploy_corm as deployment
|
||||
from history_store import normalize_record
|
||||
from settings_store import HEADER, SettingsNotInitialized
|
||||
|
||||
|
||||
ROWS = [HEADER, ["APP_SETTINGS", "shipping_fee", "3500"]]
|
||||
HISTORY = {"kind": "corm-history", "sms": [], "manual": []}
|
||||
|
||||
|
||||
class DeploymentTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.output = io.StringIO()
|
||||
|
||||
def test_environment_switch_preserves_existing_secret_and_database_keys(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
path = Path(directory) / ".env"
|
||||
path.write_text("POSTGRES_PASSWORD='keep-$-secret'\nITEMCODE_DB=itemcode_db\n# preserve comment\n", encoding="utf-8")
|
||||
with patch.object(os, "chown", create=True), redirect_stdout(self.output):
|
||||
deployment.switch_environment(path)
|
||||
text = path.read_text(encoding="utf-8")
|
||||
self.assertIn("POSTGRES_PASSWORD='keep-$-secret'", text)
|
||||
self.assertIn("ITEMCODE_DB=itemcode_db", text)
|
||||
self.assertIn("# preserve comment", text)
|
||||
self.assertIn("SETTINGS_DB=corm_db", text)
|
||||
self.assertIn("HISTORY_BACKEND=postgres", text)
|
||||
|
||||
def test_existing_settings_difference_is_never_overwritten(self):
|
||||
store = MagicMock()
|
||||
store.read.return_value = ([HEADER, ["APP_SETTINGS", "shipping_fee", "9999"]], 2)
|
||||
with patch.object(deployment, "load_snapshot", return_value=(ROWS, "test")), \
|
||||
patch.object(deployment, "load_backup", return_value=(HISTORY, "test")), \
|
||||
patch.object(deployment, "PostgresSettingsStore", return_value=store), \
|
||||
self.assertRaises(RuntimeError):
|
||||
deployment.migrate("unused")
|
||||
store.import_rows.assert_not_called()
|
||||
|
||||
def test_incomplete_import_can_resume_without_reimporting_settings(self):
|
||||
settings = MagicMock()
|
||||
settings.read.return_value = (ROWS, 1)
|
||||
histories = MagicMock()
|
||||
histories.check_ready.side_effect = SettingsNotInitialized()
|
||||
histories.export_all.return_value = HISTORY
|
||||
with patch.object(deployment, "load_snapshot", return_value=(ROWS, "test")), \
|
||||
patch.object(deployment, "load_backup", return_value=(HISTORY, "test")), \
|
||||
patch.object(deployment, "PostgresSettingsStore", return_value=settings), \
|
||||
patch.object(deployment, "PostgresHistoryStore", return_value=histories), redirect_stdout(self.output):
|
||||
deployment.migrate("unused")
|
||||
settings.import_rows.assert_not_called()
|
||||
histories.import_all.assert_called_once_with(HISTORY, "test")
|
||||
|
||||
def test_snapshot_uses_existing_database_when_already_converted(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
settings = MagicMock()
|
||||
settings.read.return_value = (ROWS, 1)
|
||||
histories = MagicMock()
|
||||
histories.export_all.return_value = HISTORY
|
||||
with patch.object(deployment, "PostgresSettingsStore", return_value=settings), \
|
||||
patch.object(deployment, "PostgresHistoryStore", return_value=histories), \
|
||||
patch.object(deployment, "read_google_rows") as google, \
|
||||
patch.object(os, "chown", create=True), redirect_stdout(self.output):
|
||||
deployment.snapshot(directory, "postgres", "postgres")
|
||||
google.assert_not_called()
|
||||
self.assertTrue((Path(directory) / "settings-source.json").exists())
|
||||
self.assertTrue((Path(directory) / "history-source.json").exists())
|
||||
|
||||
def test_database_creation_does_not_modify_an_existing_database(self):
|
||||
connection = MagicMock()
|
||||
cursor = connection.cursor.return_value.__enter__.return_value
|
||||
cursor.fetchone.return_value = (1,)
|
||||
with patch.object(deployment, "settings_db_config", return_value={"dbname": "corm_db", "user": "app"}), \
|
||||
patch.object(deployment.psycopg2, "connect", return_value=connection), redirect_stdout(self.output):
|
||||
deployment.ensure_database()
|
||||
cursor.execute.assert_called_once()
|
||||
|
||||
def test_deployment_error_output_does_not_reveal_credentials(self):
|
||||
with patch.object(deployment, "ensure_database", side_effect=RuntimeError("password=do-not-print")), \
|
||||
redirect_stderr(self.output):
|
||||
self.assertEqual(deployment.main(["database"]), 1)
|
||||
self.assertNotIn("do-not-print", self.output.getvalue())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user