import importlib.util import json import shutil import subprocess from pathlib import Path import tempfile from types import SimpleNamespace 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_execute_detaches_stdin_when_no_payload_is_explicitly_supplied(self): completed = SimpleNamespace(returncode=0, stdout="") with patch.object(deploy.subprocess, "run", return_value=completed) as runner: deploy.execute(["docker", "compose", "config"], capture=True) self.assertEqual(runner.call_args.kwargs["stdin"], subprocess.DEVNULL) self.assertNotIn("input", runner.call_args.kwargs) def test_execute_passes_only_explicit_payload_to_stdin(self): completed = SimpleNamespace(returncode=0, stdout="") with patch.object(deploy.subprocess, "run", return_value=completed) as runner: deploy.execute(["psql"], data="SELECT 1", capture=True) self.assertEqual(runner.call_args.kwargs["input"], "SELECT 1") self.assertNotIn("stdin", runner.call_args.kwargs) def test_duplicate_empty_secret_is_rejected_before_compose(self): with tempfile.TemporaryDirectory() as directory: env_file = Path(directory) / ".env.cloud" env_file.write_text("TRIPO_API_KEY=private-test-value\nTRIPO_API_KEY=\n", encoding="utf-8") env_file.chmod(0o600) with patch.object(deploy, "execute") as execute: with self.assertRaisesRegex(ValueError, "Duplicate environment setting: TRIPO_API_KEY") as error: deploy.load_config(env_file, "https") self.assertNotIn("private-test-value", str(error.exception)) execute.assert_not_called() def test_commented_assignment_does_not_override_secret(self): with tempfile.TemporaryDirectory() as directory: env_file = Path(directory) / ".env.cloud" env_file.write_text("# TRIPO_API_KEY=\nTRIPO_API_KEY=private-test-value\n", encoding="utf-8") env_file.chmod(0o600) with patch.object(deploy, "execute", return_value=json.dumps(configuration())): self.assertEqual(deploy.load_config(env_file, "https"), configuration()) def test_proxy_replaces_forwarding_headers_for_verification_limits(self): routes = (DIRECTORY / "nginx" / "routes.conf").read_text(encoding="utf-8") self.assertEqual(routes.count('proxy_set_header Forwarded "";'), 2) self.assertEqual(routes.count('proxy_set_header X-Forwarded-For $remote_addr;'), 2) self.assertNotIn('$proxy_add_x_forwarded_for', routes) def test_login_smoke_requires_super_admin_and_checks_management(self): cfg = configuration() cfg["services"]["moyi-api"]["environment"]["ADMIN_ACCOUNT"] = "adimin" for role in ("super_admin", "admin", "user"): paths = [] def response(request, timeout): path = deploy.urllib.parse.urlsplit(request.full_url).path paths.append(path) if path == "/": code, body = 200, b"" elif path == "/health": code, body = 200, b'{"status":"ok"}' elif path == "/api/auth/me" and paths.count(path) == 2: code, body = 200, json.dumps({"user": {"role": role}}).encode() elif path in ("/api/auth/me", "/v1/models"): code, body = 401, b"{}" elif path == "/actuator/health": code, body = 404, b"{}" elif path == "/api/auth/logout": code, body = 204, b"" else: code, body = 200, b"{}" return SimpleNamespace(code=code, read=lambda: body) with self.subTest(role=role), patch.object(deploy.urllib.request, "build_opener", return_value=SimpleNamespace(open=response)): if role == "super_admin": deploy.smoke(cfg, "tunnel", True) self.assertIn("/api/admin/users", paths) self.assertEqual(paths[-1], "/api/auth/logout") else: with self.assertRaisesRegex(ValueError, "Super administrator"): deploy.smoke(cfg, "tunnel", True) def test_tunnel_config(self): deploy.validate_config(configuration(), "tunnel") def test_admin_password_has_no_minimum_length(self): for mode in ("tunnel", "https"): for password in ("x", "Test7!", "Qa-Test42!"): with self.subTest(mode=mode, length=len(password)): cfg = configuration() api = cfg["services"]["moyi-api"]["environment"] api["ADMIN_PASSWORD"] = password api["AUTH_SECURE_COOKIE"] = "true" if mode == "https" else "false" deploy.validate_config(cfg, mode) def test_admin_password_still_rejects_empty_blank_and_placeholder(self): for password in ("", " ", "\t\r\n", "GENERATE_ADMIN_PASSWORD"): with self.subTest(password=password): cfg = configuration() cfg["services"]["moyi-api"]["environment"]["ADMIN_PASSWORD"] = password with self.assertRaisesRegex(ValueError, "ADMIN_PASSWORD"): deploy.validate_config(cfg, "tunnel") def test_cryptographic_secrets_keep_minimum_length(self): for key in ("AUTH_PASSWORD_PEPPER", "CREDENTIAL_ENCRYPTION_KEY"): with self.subTest(key=key): cfg = configuration() cfg["services"]["moyi-api"]["environment"][key] = "s" * 31 with self.assertRaisesRegex(ValueError, key + " is too short"): deploy.validate_config(cfg, "tunnel") cfg["services"]["moyi-api"]["environment"][key] = "s" * 32 deploy.validate_config(cfg, "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": "Qa-Test42!", } 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.assertEqual(api["ADMIN_PASSWORD"], "Qa-Test42!") self.assertNotIn("MOYI_REDIS_PASSWORD", cfg["services"]["frontend"].get("environment", {})) self.assertFalse(api.get("OSS_ACCESS_KEY_SECRET")) self.assertNotIn("OSS_ACCESS_KEY_SECRET", cfg["services"]["frontend"].get("environment", {})) 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()