256 lines
7.2 KiB
Python
256 lines
7.2 KiB
Python
import copy
|
|
import unittest
|
|
|
|
from pathlib import Path
|
|
|
|
from core.configuration import load_config
|
|
from core.site_policy import (
|
|
SitePolicyError,
|
|
validate_site,
|
|
validate_registry,
|
|
)
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
class SitePolicyTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.config = load_config(
|
|
ROOT / "config/site.example.json"
|
|
)
|
|
|
|
self.other = copy.deepcopy(self.config)
|
|
|
|
self.other["site"].update({
|
|
"identifier": "other-absdev-net",
|
|
"domain": "other.absdev.net",
|
|
"root": "/opt/laravel-sites/other-absdev-net",
|
|
"origin_port": 8198,
|
|
})
|
|
|
|
self.other["docker"]["project"] = (
|
|
"site-other-absdev-net"
|
|
)
|
|
|
|
self.other["database"]["name"] = "other_db"
|
|
self.other["database"]["username"] = "other_app"
|
|
|
|
def test_valid_site(self):
|
|
result = validate_site(self.config)
|
|
self.assertEqual(result["status"], "VALID")
|
|
|
|
def test_root_must_match_identifier(self):
|
|
self.config["site"]["root"] = (
|
|
"/opt/laravel-sites/different"
|
|
)
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
def test_untrusted_root_rejected(self):
|
|
self.config["site"]["root"] = "/etc"
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
def test_nested_root_rejected(self):
|
|
self.config["site"]["root"] = (
|
|
"/opt/laravel-sites/nested/example-absdev-net"
|
|
)
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
def test_double_slash_rejected(self):
|
|
self.config["site"]["root"] = (
|
|
"//opt/laravel-sites/example-absdev-net"
|
|
)
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
def test_project_must_match_site(self):
|
|
self.config["docker"]["project"] = "other-project"
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
def test_root_database_user_rejected(self):
|
|
self.config["database"]["username"] = "root"
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
def test_postgres_admin_rejected(self):
|
|
self.config["database"]["username"] = "postgres"
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
def test_mysql_system_database_rejected(self):
|
|
self.config["database"]["name"] = "mysql"
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
def test_postgres_system_database_rejected(self):
|
|
self.config["database"]["name"] = "template1"
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
def test_distinct_services_required(self):
|
|
self.config["docker"]["queue_service"] = (
|
|
self.config["docker"]["php_service"]
|
|
)
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
def test_distinct_networks_required(self):
|
|
self.config["docker"]["proxy_network"] = (
|
|
self.config["docker"]["backend_network"]
|
|
)
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
def test_reserved_origin_port_rejected(self):
|
|
self.config["site"]["origin_port"] = 443
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
def test_valid_registry(self):
|
|
result = validate_registry([
|
|
self.config, self.other,
|
|
])
|
|
|
|
self.assertEqual(result["site_count"], 2)
|
|
self.assertEqual(result["status"], "VALID")
|
|
|
|
def test_duplicate_site_rejected(self):
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_registry([
|
|
self.config, self.config,
|
|
])
|
|
|
|
def test_duplicate_domain_rejected(self):
|
|
self.other["site"]["domain"] = (
|
|
self.config["site"]["domain"]
|
|
)
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_registry([
|
|
self.config, self.other,
|
|
])
|
|
|
|
def test_duplicate_port_rejected(self):
|
|
self.other["site"]["origin_port"] = (
|
|
self.config["site"]["origin_port"]
|
|
)
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_registry([
|
|
self.config, self.other,
|
|
])
|
|
|
|
def test_duplicate_database_rejected(self):
|
|
self.other["database"]["name"] = (
|
|
self.config["database"]["name"]
|
|
)
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_registry([
|
|
self.config, self.other,
|
|
])
|
|
|
|
def test_duplicate_database_user_rejected(self):
|
|
self.other["database"]["username"] = (
|
|
self.config["database"]["username"]
|
|
)
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_registry([
|
|
self.config, self.other,
|
|
])
|
|
|
|
def test_case_insensitive_database_conflict(self):
|
|
self.other["database"]["name"] = (
|
|
self.config["database"]["name"].upper()
|
|
)
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_registry([
|
|
self.config, self.other,
|
|
])
|
|
|
|
def test_shared_database_container_permitted(self):
|
|
self.assertEqual(
|
|
self.config["database"]["container"],
|
|
self.other["database"]["container"],
|
|
)
|
|
|
|
validate_registry([
|
|
self.config, self.other,
|
|
])
|
|
|
|
def test_registry_order_independent(self):
|
|
self.assertEqual(
|
|
validate_registry([
|
|
self.config, self.other,
|
|
]),
|
|
validate_registry([
|
|
self.other, self.config,
|
|
]),
|
|
)
|
|
|
|
def test_empty_registry_rejected(self):
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_registry([])
|
|
|
|
def test_invalid_registry_type_rejected(self):
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_registry("invalid")
|
|
|
|
def test_input_not_mutated(self):
|
|
before = copy.deepcopy(self.config)
|
|
validate_site(self.config)
|
|
self.assertEqual(self.config, before)
|
|
|
|
def test_execution_remains_disabled(self):
|
|
result = validate_site(self.config)
|
|
|
|
self.assertFalse(result["execution_permitted"])
|
|
self.assertFalse(result["runtime_verified"])
|
|
|
|
def test_registry_not_runtime_verification(self):
|
|
result = validate_registry([self.config])
|
|
|
|
self.assertFalse(result["runtime_verified"])
|
|
self.assertFalse(result["execution_permitted"])
|
|
|
|
def test_fingerprint_deterministic(self):
|
|
self.assertEqual(
|
|
validate_site(self.config),
|
|
validate_site(self.config),
|
|
)
|
|
|
|
def test_invalid_configuration_wrapped(self):
|
|
del self.config["database"]
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
def test_reserved_username_case_insensitive(self):
|
|
self.config["database"]["username"] = "ROOT"
|
|
|
|
with self.assertRaises(SitePolicyError):
|
|
validate_site(self.config)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|