import importlib.util import json import shutil import subprocess from pathlib import Path import tempfile import unittest from unittest.mock import patch DIRECTORY = Path(__file__).resolve().parents[1] SPEC = importlib.util.spec_from_file_location("deploy", DIRECTORY / "deploy.py") deploy = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(deploy) def configuration(): return { "services": { "moyi-api": {"environment": { "SPRING_DATASOURCE_URL": "jdbc:postgresql://rds.internal:5432/moyi_control", "SPRING_DATASOURCE_USERNAME": "moyi_app", "SPRING_DATASOURCE_PASSWORD": "test-only-rds-password", "MOYI_REDIS_HOST": "redis.internal", "MOYI_REDIS_PASSWORD": "test-only-tair-password", "MOYI_REDIS_KEY_PREFIX": "moyi:test:", "MOYI_REDIS_SSL": "false", "AUTH_PASSWORD_PEPPER": "p" * 64, "CREDENTIAL_ENCRYPTION_KEY": "k" * 64, "ADMIN_PASSWORD": "test-only-admin-password", "AUTH_SECURE_COOKIE": "false" }}, "frontend": {"ports": [{"host_ip": "127.0.0.1", "published": "8080", "target": 80}], "environment": {"MOYI_DOMAIN": "moyi.soulofmodel.com"}} }, "networks": {"default": {"ipam": {"config": [{"subnet": "10.253.240.0/24"}]}}} } class DeploymentTests(unittest.TestCase): def test_tunnel_config(self): deploy.validate_config(configuration(), "tunnel") def test_reject_placeholder(self): cfg = configuration() cfg["services"]["moyi-api"]["environment"]["MOYI_REDIS_HOST"] = "CHANGE_ME_TAIR_HOST" with self.assertRaisesRegex(ValueError, "placeholder"): deploy.validate_config(cfg, "tunnel") def test_reject_mysql(self): cfg = configuration() cfg["services"]["moyi-api"]["environment"]["SPRING_DATASOURCE_URL"] = "jdbc:mysql://db/app" with self.assertRaisesRegex(ValueError, "PostgreSQL"): deploy.validate_config(cfg, "tunnel") def test_reject_public_http(self): cfg = configuration() cfg["services"]["frontend"]["ports"][0]["host_ip"] = "0.0.0.0" with self.assertRaisesRegex(ValueError, "127.0.0.1"): deploy.validate_config(cfg, "tunnel") def test_reject_public_backend(self): cfg = configuration() cfg["services"]["moyi-api"]["ports"] = [{"published": "8787"}] with self.assertRaisesRegex(ValueError, "Backend"): deploy.validate_config(cfg, "tunnel") def test_https_requires_secure_cookie(self): with self.assertRaisesRegex(ValueError, "secure cookies"): deploy.validate_config(configuration(), "https") def test_https_domain_and_cookie(self): cfg = configuration() cfg["services"]["moyi-api"]["environment"]["AUTH_SECURE_COOKIE"] = "true" deploy.validate_config(cfg, "https") cfg["services"]["frontend"]["environment"]["MOYI_DOMAIN"] = "example.com; return 200;" with self.assertRaisesRegex(ValueError, "hostname"): deploy.validate_config(cfg, "https") def test_initialization_never_overwrites_secrets(self): with tempfile.TemporaryDirectory() as temporary: directory = Path(temporary) (directory / ".env.cloud.example").write_text((DIRECTORY / ".env.cloud.example").read_text()) with patch.object(deploy, "HERE", directory), patch.object(deploy, "ROOT", directory): deploy.initialize() before = (directory / ".env.cloud").read_bytes() self.assertNotIn(b"GENERATE_AUTH_PEPPER", before) with self.assertRaisesRegex(ValueError, "overwrite"): deploy.initialize() self.assertEqual(before, (directory / ".env.cloud").read_bytes()) def test_manifest_detects_tampering(self): with tempfile.TemporaryDirectory() as temporary: directory = Path(temporary) (directory / "sample").write_bytes(b"original") digest = deploy.hashlib.sha256(b"original").hexdigest() (directory / "release-manifest.json").write_text(json.dumps({"release": "test", "sha256": {"sample": digest}})) with patch.object(deploy, "ROOT", directory): deploy.verify_release() (directory / "sample").write_bytes(b"changed") with self.assertRaisesRegex(ValueError, "checksum mismatch"): deploy.verify_release() def test_compose_paths_and_password_not_in_arguments(self): command = deploy.compose_command(Path("settings"), "https") self.assertIn("docker-compose.https.yml", command) self.assertNotIn("password", " ".join(command)) @unittest.skipUnless(shutil.which("docker"), "Docker CLI required, daemon not needed") def test_real_compose_interpolation_and_https_overlay(self): content = (DIRECTORY / ".env.cloud.example").read_text(encoding="utf-8") replacements = { "CHANGE_ME_RDS_HOST": "rds.internal", "CHANGE_ME_RDS_PASSWORD": "test-db-$value#literal", "CHANGE_ME_TAIR_HOST": "redis.internal", "CHANGE_ME_TAIR_PASSWORD": "test-cache-$value#literal", "GENERATE_AUTH_PEPPER": "p" * 64, "GENERATE_CREDENTIAL_KEY": "k" * 64, "GENERATE_ADMIN_PASSWORD": "test-only-admin-password", } for before, after in replacements.items(): content = content.replace(before, after) with tempfile.TemporaryDirectory() as temporary: path = Path(temporary) / ".env.cloud" path.write_text(content, encoding="utf-8") path.chmod(0o600) for mode in ("tunnel", "https"): cfg = deploy.load_config(path, mode) deploy.validate_config(cfg, mode) api = cfg["services"]["moyi-api"]["environment"] self.assertEqual(api["MOYI_REDIS_PASSWORD"], "test-cache-$value#literal") self.assertEqual(api["SPRING_DATASOURCE_PASSWORD"], "test-db-$value#literal") self.assertNotIn("MOYI_REDIS_PASSWORD", cfg["services"]["frontend"].get("environment", {})) self.assertNotIn("OSS_ACCESS_KEY_SECRET", api) if mode == "https": ports = cfg["services"]["frontend"]["ports"] self.assertEqual({str(p["published"]) for p in ports}, {"80", "443", "8080"}) defaults = [v for v in cfg["services"]["frontend"]["volumes"] if v["target"] == "/etc/nginx/conf.d/default.conf"] self.assertEqual(len(defaults), 1) self.assertTrue(defaults[0]["source"].endswith("https-fallback.conf")) if __name__ == "__main__": unittest.main()