unprepared('DROP TRIGGER abort_abuse_policy_rule'); $assert = static function (bool $condition, string $message): void { if (!$condition) { throw new RuntimeException($message); } }; $expectCloudError = static function ( callable $callback, int $status, string $message ) use ($assert): void { try { $callback(); } catch (CloudError $error) { $assert($error->httpStatus === $status, $message . ' Unexpected status: ' . $error->httpStatus); return; } throw new RuntimeException($message); }; $request = static function ( string $method, string $path, array $actor, array $body = [] ): Request { $request = Request::createFromEnvironment(Environment::mock([ 'REQUEST_METHOD' => $method, 'REQUEST_URI' => $path, ]))->withAttribute('cloud_actor', $actor); if ($body !== []) { $request = $request->withParsedBody($body); } return $request; }; $decode = static fn ($response): array => json_decode( (string) $response->getBody(), true, 32, JSON_THROW_ON_ERROR ); $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, ]; $controller = new CloudAdminAbusePolicyController(); $index = $controller->index( $request('GET', '/api/v1/admin/abuse-policies', $readonly), new Response() ); $listed = $decode($index); $assert($index->getStatusCode() === 200 && $index->getHeaderLine('Cache-Control') === 'private, no-store' && ($listed['active']['revision'] ?? null) === 2 && ($listed['can_manage'] ?? true) === false, 'Read-only administrator could not read the immutable active policy.'); $expectCloudError( static fn () => $controller->index( $request('GET', '/api/v1/admin/abuse-policies', [ 'user_id' => 7, 'is_admin' => false, ]), new Response() ), 403, 'A non-administrator read the network-abuse policy.' ); $definition = $listed['active']['definition']; $expectCloudError( static fn () => $controller->preview( $request('POST', '/api/v1/admin/abuse-policies/preview', $readonly, []), new Response() ), 403, 'Read-only administrator reached policy body validation.' ); $unchanged = $controller->preview( $request('POST', '/api/v1/admin/abuse-policies/preview', $manager, [ 'expected_revision' => 2, 'definition' => $definition, ]), new Response() ); $unchangedBody = $decode($unchanged); $assert($unchanged->getHeaderLine('Cache-Control') === 'private, no-store' && ($unchangedBody['changed'] ?? true) === false && ($unchangedBody['result_revision'] ?? null) === 2, 'Unchanged policy preview invented a new revision.'); $candidate = $definition; $candidate['windows']['clear'] = 6; $preview = $controller->preview( $request('POST', '/api/v1/admin/abuse-policies/preview', $manager, [ 'expected_revision' => 2, 'definition' => $candidate, ]), new Response() ); $previewBody = $decode($preview); $assert(($previewBody['changed'] ?? false) === true && ($previewBody['result_revision'] ?? null) === 3 && $connection->table('cloud_abuse_policy_revisions')->count() === 2, 'Policy preview mutated storage or lost the proposed revision.'); $expectCloudError( static fn () => $controller->preview( $request('POST', '/api/v1/admin/abuse-policies/preview', $manager, [ 'expected_revision' => 1, 'definition' => $candidate, ]), new Response() ), 409, 'A stale policy preview was compared with the wrong active revision.' ); $invalid = $candidate; $invalid['rules']['outbound_tcp_syn_sweep']['high']['tcp_syn_flows'] = 1; $expectCloudError( static fn () => $controller->preview( $request('POST', '/api/v1/admin/abuse-policies/preview', $manager, [ 'expected_revision' => 2, 'definition' => $invalid, ]), new Response() ), 422, 'Preview accepted a high threshold below its suspicious threshold.' ); $updated = $controller->update( $request('PUT', '/api/v1/admin/abuse-policies/active', $manager, [ 'expected_revision' => 2, 'definition' => $candidate, ]), new Response() ); $updatedBody = $decode($updated); $assert($updated->getHeaderLine('Cache-Control') === 'private, no-store' && ($updatedBody['published'] ?? false) === true && ($updatedBody['active']['revision'] ?? null) === 3 && $connection->table('cloud_abuse_policy_revisions')->count() === 3, 'Whole-policy publication did not activate exactly one revision.'); $expectCloudError( static fn () => $controller->update( $request('PUT', '/api/v1/admin/abuse-policies/active', $manager, [ 'expected_revision' => 2, 'definition' => $definition, ]), new Response() ), 409, 'A stale administrator overwrote the active policy.' ); $expectCloudError( static fn () => $controller->update( $request('PUT', '/api/v1/admin/abuse-policies/active', $manager, [ 'expected_revision' => 3, 'definition' => $candidate, 'active_slot' => 1, ]), new Response() ), 422, 'Policy publication accepted an unapproved request field.' ); $same = $controller->update( $request('PUT', '/api/v1/admin/abuse-policies/active', $manager, [ 'expected_revision' => 3, 'definition' => $candidate, ]), new Response() ); $sameBody = $decode($same); $assert(($sameBody['published'] ?? true) === false && ($sameBody['active']['revision'] ?? null) === 3 && $connection->table('cloud_abuse_policy_revisions')->count() === 3, 'Idempotent whole-policy PUT created a redundant immutable revision.'); echo "Administrator abuse-policy API behavior passed.\n";