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.
117 lines
3.9 KiB
PHP
117 lines
3.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Controllers\CloudAdminMaintenanceNoticeController;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudMaintenanceNoticeService;
|
|
use Slim\Http\Environment;
|
|
use Slim\Http\Request;
|
|
use Slim\Http\Response;
|
|
|
|
ob_start();
|
|
require __DIR__ . '/CloudMaintenanceNoticeServiceTest.php';
|
|
ob_end_clean();
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$request = static function (string $method, string $path, array $actor,
|
|
array $body = [], array $headers = []): Request {
|
|
$request = Request::createFromEnvironment(Environment::mock([
|
|
'REQUEST_METHOD' => $method,
|
|
'REQUEST_URI' => $path,
|
|
]))->withAttribute('cloud_actor', $actor);
|
|
if ($body !== []) {
|
|
$request = $request->withParsedBody($body);
|
|
}
|
|
foreach ($headers as $name => $value) {
|
|
$request = $request->withHeader($name, $value);
|
|
}
|
|
return $request;
|
|
};
|
|
$controller = new CloudAdminMaintenanceNoticeController();
|
|
$readonly = [
|
|
'user_id' => 8, 'is_admin' => true,
|
|
'is_super_admin' => false, 'can_manage' => false,
|
|
];
|
|
$manager = [
|
|
'user_id' => 9, 'is_admin' => true,
|
|
'is_super_admin' => true, 'can_manage' => true,
|
|
];
|
|
|
|
$index = $controller->index(
|
|
$request('GET', '/api/v1/admin/maintenance-notices', $readonly),
|
|
new Response()
|
|
);
|
|
$assert($index->getStatusCode() === 200
|
|
&& $index->getHeaderLine('Cache-Control') === 'private, no-store',
|
|
'Read-only administrator could not list maintenance notices safely.');
|
|
$listed = json_decode((string) $index->getBody(), true);
|
|
$assert(($listed['total'] ?? 0) === 1
|
|
&& !isset($listed['items'][0]['recipients']),
|
|
'List endpoint leaked recipient details or lost its result.');
|
|
|
|
$detail = $controller->detail(
|
|
$request('GET', '/api/v1/admin/maintenance-notices/'
|
|
. $created['notice_key'], $readonly),
|
|
new Response(),
|
|
['notice_key' => $created['notice_key']]
|
|
);
|
|
$detailBody = (string) $detail->getBody();
|
|
$assert(!str_contains($detailBody, 'owner@example.com')
|
|
&& str_contains($detailBody, 'o***@example.com')
|
|
&& !str_contains($detailBody, 'host_placement_only'),
|
|
'Read-only administrator received raw email or network evidence.');
|
|
|
|
try {
|
|
$controller->index(
|
|
$request('GET', '/api/v1/admin/maintenance-notices', [
|
|
'user_id' => 7, 'is_admin' => false,
|
|
]),
|
|
new Response()
|
|
);
|
|
throw new RuntimeException('Non-admin maintenance access was accepted.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 403,
|
|
'Non-admin maintenance access returned the wrong status.');
|
|
}
|
|
|
|
$current = time();
|
|
$service = new CloudMaintenanceNoticeService();
|
|
$preview = $service->preview('host', 1);
|
|
$body = [
|
|
'scope_type' => 'host', 'scope_id' => 1,
|
|
'subject' => 'API maintenance', 'message_text' => 'Save your work.',
|
|
'impact' => 'instance_restart',
|
|
'maintenance_starts_at' => $current + 3600,
|
|
'maintenance_ends_at' => $current + 7200,
|
|
'scheduled_at' => $current + 60,
|
|
'audience_digest' => $preview['audience_digest'],
|
|
];
|
|
try {
|
|
$controller->create(
|
|
$request('POST', '/api/v1/admin/maintenance-notices', $manager, $body),
|
|
new Response()
|
|
);
|
|
throw new RuntimeException('Create accepted a missing idempotency key.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 400,
|
|
'Missing idempotency key returned the wrong status.');
|
|
}
|
|
$createdResponse = $controller->create(
|
|
$request('POST', '/api/v1/admin/maintenance-notices', $manager, $body, [
|
|
'Idempotency-Key' => 'maintenance-api-0001',
|
|
]),
|
|
new Response()
|
|
);
|
|
$assert($createdResponse->getStatusCode() === 201
|
|
&& $createdResponse->getHeaderLine('Cache-Control')
|
|
=== 'private, no-store',
|
|
'Reviewed maintenance notice was not created as a database resource.');
|
|
|
|
echo "Maintenance notice administrator API behavior passed.\n";
|