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/CloudAdminAbuseAlertHistory...

89 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Controllers\CloudAdminAbuseMonitoringController;
use App\Services\Cloud\CloudError;
use Slim\Http\Environment;
use Slim\Http\Request;
use Slim\Http\Response;
ob_start();
require __DIR__ . '/CloudAdminAbuseAlertHistoryServiceTest.php';
ob_end_clean();
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$request = static function (array $actor, array $query = []): Request {
$uri = '/api/v1/admin/abuse-monitoring/alerts';
if ($query !== []) {
$uri .= '?' . http_build_query($query, '', '&', PHP_QUERY_RFC3986);
}
return Request::createFromEnvironment(Environment::mock([
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => $uri,
'QUERY_STRING' => parse_url($uri, PHP_URL_QUERY) ?: '',
]))->withAttribute('cloud_actor', $actor);
};
$controller = new CloudAdminAbuseMonitoringController();
$response = $controller->alerts($request([
'user_id' => 88, 'is_admin' => true,
'is_super_admin' => false, 'can_manage' => false,
], [
'event_id' => '1', 'instance_id' => '41',
'user_id' => '10', 'host_id' => '3',
]), new Response());
$body = (string) $response->getBody();
$decoded = json_decode($body, true, flags: JSON_THROW_ON_ERROR);
$assert($response->getStatusCode() === 200
&& $response->getHeaderLine('Cache-Control') === 'private, no-store'
&& ($decoded['pagination']['total'] ?? 0) === 1
&& ($decoded['items'][0]['event_id'] ?? '') === '1'
&& !str_contains($body, 'message_text')
&& !str_contains($body, 'chat_id')
&& !str_contains($body, 'binding_generation')
&& !str_contains($body, 'telegram_message_id')
&& !str_contains($body, 'content_hash')
&& !str_contains($body, 'evidence_json')
&& !str_contains($body, 'policy_hash')
&& !str_contains($body, 'sample_key')
&& !str_contains($body, 'finding_key')
&& !str_contains($body, 'item_key'),
'Read-only alert history API lost its cache, filter, or secrecy boundary.');
try {
$controller->alerts($request([
'user_id' => 89, 'is_admin' => false,
]), new Response());
throw new RuntimeException('Non-administrator alert history API succeeded.');
} catch (CloudError $error) {
$assert($error->httpStatus === 403,
'Non-administrator alert history API returned the wrong status.');
}
$routes = file_get_contents(dirname(__DIR__) . '/config/routes_cloud.php');
$controllerSource = file_get_contents(dirname(__DIR__)
. '/app/Controllers/CloudAdminAbuseMonitoringController.php');
$assert(is_string($routes)
&& str_contains($routes,
"\$this->get('/admin/abuse-monitoring/alerts'")
&& !str_contains($routes,
"\$this->post('/admin/abuse-monitoring/alerts'")
&& !str_contains($routes,
"\$this->put('/admin/abuse-monitoring/alerts'")
&& !str_contains($routes,
"\$this->patch('/admin/abuse-monitoring/alerts'")
&& !str_contains($routes,
"\$this->delete('/admin/abuse-monitoring/alerts'")
&& is_string($controllerSource)
&& str_contains($controllerSource,
'new CloudAdminAbuseAlertHistoryService()'),
'Alert history must expose exactly one administrator-only GET surface.');
echo "Administrator abuse-alert history API tests passed.\n";