You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudAdminAbusePolicyApiCon...

50 lines
2.2 KiB
PHP

<?php
$root = dirname(__DIR__);
$read = static function (string $path) use ($root): string {
$source = file_get_contents($root . '/' . $path);
if (!is_string($source)) {
throw new RuntimeException('Unable to read ' . $path);
}
return $source;
};
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$routes = $read('config/routes_cloud.php');
$middleware = $read('app/Middleware/CloudApi.php');
$controller = $read('app/Controllers/CloudAdminAbusePolicyController.php');
$service = $read('app/Services/Cloud/CloudAdminAbusePolicyService.php');
$assert(str_contains($routes,
"get('/admin/abuse-policies',\n 'App\\Controllers\\CloudAdminAbusePolicyController:index')")
&& str_contains($routes,
"post('/admin/abuse-policies/preview',\n 'App\\Controllers\\CloudAdminAbusePolicyController:preview')")
&& str_contains($routes,
"put('/admin/abuse-policies/active',\n 'App\\Controllers\\CloudAdminAbusePolicyController:update')"),
'Administrator policy API routes are incomplete.');
$assert(!preg_match(
"#(?:post|put|patch|delete|map)\\([^\\n]*'/admin/abuse-monitoring#i",
$routes
),
'Policy writes were incorrectly added to the read-only monitoring API.');
$assert(str_contains($middleware,
"#^/api/v1/admin/abuse-policies(?:/|$)#D")
&& str_contains($middleware, 'CloudAccess::actor(!$databaseOnly)'),
'Database-owned policy API still requires an Agent session.');
$assert(str_contains($controller,
"CloudInput::only(\$body, ['expected_revision', 'definition'])")
&& substr_count($controller, 'self::actor($request, true)') === 2
&& str_contains($controller, "'Cache-Control', 'private, no-store'")
&& str_contains($service, 'CloudAccess::manage($actor)')
&& str_contains($service, '->activeSnapshot()')
&& str_contains($service, '->publish(')
&& !preg_match('/->(?:insert|update|delete|lockForUpdate)\s*\(/',
$controller . $service),
'Policy API lost its authorization, exact-input, cache or service boundary.');
echo "Administrator abuse-policy API contracts passed.\n";