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

430 lines
19 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\User;
use App\Services\UserDataDeletionService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$System_Config = ['enable_log' => 'false'];
$assert = static function ($condition, $message) {
if (!$condition) {
throw new RuntimeException($message);
}
};
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->getConnection('default')->getSchemaBuilder();
$schema->create('user', static function (Blueprint $table) {
$table->increments('id');
$table->string('email')->unique();
$table->boolean('is_admin')->default(false);
$table->integer('login_session_version')->default(0);
$table->integer('ref_by')->default(0);
});
$schema->create('ticket', static function (Blueprint $table) {
$table->increments('id');
$table->integer('userid');
$table->integer('rootid')->default(0);
});
$schema->create('yipay_orders', static function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('order_no')->unique();
});
foreach (['yipay_callback_events', 'yipay_refunds'] as $tableName) {
$schema->create($tableName, static function (Blueprint $table) {
$table->increments('id');
$table->string('order_no');
});
}
$schema->create('cloud_instances', static function (Blueprint $table) {
$table->increments('id'); $table->integer('user_id'); $table->string('status');
});
$schema->create('cloud_orders', static function (Blueprint $table) {
$table->increments('id'); $table->integer('user_id'); $table->string('status');
});
$schema->create('cloud_jobs', static function (Blueprint $table) {
$table->increments('id'); $table->integer('user_id');
$table->integer('instance_id')->nullable();
$table->integer('snapshot_id')->nullable();
$table->text('payload_cipher')->nullable();
});
$schema->create('cloud_instance_snapshots', static function (Blueprint $table) {
$table->increments('id'); $table->integer('user_id');
$table->integer('instance_id');
});
$schema->create('cloud_instance_firewalls', static function (Blueprint $table) {
$table->integer('instance_id')->primary(); $table->integer('user_id');
});
$schema->create('cloud_instance_firewall_versions', static function (Blueprint $table) {
$table->increments('id'); $table->integer('instance_id');
});
$schema->create('cloud_instance_firewall_rules', static function (Blueprint $table) {
$table->increments('id'); $table->integer('version_id');
});
foreach (['cloud_service_periods', 'cloud_renewals'] as $tableName) {
$schema->create($tableName, static function (Blueprint $table) {
$table->increments('id'); $table->integer('user_id');
});
}
$schema->create('cloud_invoices', static function (Blueprint $table) {
$table->increments('id'); $table->integer('user_id');
});
$schema->create('cloud_invoice_payments', static function (Blueprint $table) {
$table->increments('id'); $table->integer('user_id'); $table->string('status');
});
$schema->create('cloud_mail_outbox', static function (Blueprint $table) {
$table->increments('id'); $table->integer('invoice_id');
});
$schema->create('cloud_private_cidr_claims', static function (Blueprint $table) {
$table->increments('id'); $table->string('allocation_key', 32);
});
$schema->create('cloud_private_networks', static function (Blueprint $table) {
$table->increments('id'); $table->string('vpc_key', 32);
$table->string('allocation_key', 32); $table->integer('user_id');
$table->string('state', 16); $table->string('active_cidr', 18)->nullable();
});
$schema->create('cloud_vpcs', static function (Blueprint $table) {
$table->increments('id'); $table->string('vpc_key', 32);
$table->integer('user_id'); $table->integer('private_network_id');
$table->string('state', 16);
});
foreach (['cloud_vpc_routes', 'cloud_vpc_acl_rules', 'cloud_vpc_private_ips',
'cloud_vpc_members', 'cloud_vpc_subnets', 'cloud_vpc_host_deployments',
'cloud_vpc_operations'] as $tableName) {
$schema->create($tableName, static function (Blueprint $table): void {
$table->increments('id'); $table->integer('vpc_id');
});
}
$schema->create('email_verify', static function (Blueprint $table) {
$table->increments('id');
$table->string('email');
});
$schema->create('configurations', static function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->text('value');
});
Capsule::table('user')->insert([
['id' => 1, 'email' => 'deleted@example.com', 'is_admin' => 0, 'ref_by' => 0],
['id' => 2, 'email' => 'admin@example.com', 'is_admin' => 1, 'ref_by' => 0],
['id' => 3, 'email' => 'other@example.com', 'is_admin' => 0, 'ref_by' => 1],
]);
Capsule::table('ticket')->insert([
['id' => 10, 'userid' => 1, 'rootid' => 0],
['id' => 11, 'userid' => 2, 'rootid' => 10],
['id' => 20, 'userid' => 3, 'rootid' => 0],
]);
Capsule::table('yipay_orders')->insert([
['user_id' => 1, 'order_no' => 'deleted-order'],
['user_id' => 3, 'order_no' => 'retained-order'],
]);
foreach (['yipay_callback_events', 'yipay_refunds'] as $tableName) {
Capsule::table($tableName)->insert([
['order_no' => 'deleted-order'], ['order_no' => 'retained-order'],
]);
}
Capsule::table('cloud_instances')->insert([
['id' => 101, 'user_id' => 1, 'status' => 'deleted'],
['id' => 301, 'user_id' => 3, 'status' => 'active'],
]);
Capsule::table('cloud_orders')->insert([
['id' => 102, 'user_id' => 1, 'status' => 'completed'],
['id' => 302, 'user_id' => 3, 'status' => 'completed'],
]);
Capsule::table('cloud_instance_snapshots')->insert([
['id' => 104, 'user_id' => 1, 'instance_id' => 101],
['id' => 304, 'user_id' => 3, 'instance_id' => 301],
]);
Capsule::table('cloud_instance_firewalls')->insert([
['instance_id' => 101, 'user_id' => 1],
['instance_id' => 301, 'user_id' => 3],
]);
Capsule::table('cloud_instance_firewall_versions')->insert([
['id' => 106, 'instance_id' => 101],
['id' => 306, 'instance_id' => 301],
]);
Capsule::table('cloud_instance_firewall_rules')->insert([
['id' => 107, 'version_id' => 106],
['id' => 307, 'version_id' => 306],
]);
Capsule::table('cloud_jobs')->insert([
['id' => 105, 'user_id' => 1, 'instance_id' => 101,
'snapshot_id' => 104, 'payload_cipher' => 'owner-job'],
['id' => 205, 'user_id' => 2, 'instance_id' => 101,
'snapshot_id' => 104, 'payload_cipher' => 'delegated-admin-job'],
['id' => 206, 'user_id' => 2, 'instance_id' => null,
'snapshot_id' => null, 'payload_cipher' => 'unrelated-admin-job'],
['id' => 305, 'user_id' => 3, 'instance_id' => 301,
'snapshot_id' => 304, 'payload_cipher' => 'other-user-job'],
]);
foreach (['cloud_service_periods', 'cloud_renewals'] as $tableName) {
Capsule::table($tableName)->insert([
['user_id' => 1], ['user_id' => 3],
]);
}
Capsule::table('cloud_invoices')->insert([
['id' => 103, 'user_id' => 1], ['id' => 303, 'user_id' => 3],
]);
Capsule::table('cloud_invoice_payments')->insert([
['user_id' => 1, 'status' => 'paid_invoice'],
['user_id' => 3, 'status' => 'paid_invoice'],
]);
Capsule::table('cloud_mail_outbox')->insert([
['invoice_id' => 103], ['invoice_id' => 303],
]);
$deletedVpcKey = str_repeat('a', 32);
$deletedAllocationKey = str_repeat('b', 32);
Capsule::table('cloud_private_networks')->insert([
'id' => 1001, 'vpc_key' => $deletedVpcKey,
'allocation_key' => $deletedAllocationKey, 'user_id' => 1,
'state' => 'released', 'active_cidr' => null,
]);
Capsule::table('cloud_vpcs')->insert([
'id' => 1002, 'vpc_key' => $deletedVpcKey, 'user_id' => 1,
'private_network_id' => 1001, 'state' => 'deleted',
]);
foreach (['cloud_vpc_routes', 'cloud_vpc_acl_rules', 'cloud_vpc_private_ips',
'cloud_vpc_members', 'cloud_vpc_subnets', 'cloud_vpc_host_deployments',
'cloud_vpc_operations'] as $tableName) {
Capsule::table($tableName)->insert(['vpc_id' => 1002]);
}
Capsule::table('email_verify')->insert([
['email' => 'deleted@example.com'], ['email' => 'other@example.com'],
]);
Capsule::table('configurations')->insert([
'name' => 'email_blacklist', 'value' => "deleted@example.com\n@blocked.example",
]);
$result = (new UserDataDeletionService())->deleteUsers([1, '2', 999], false);
$assert($result['deleted_user_ids'] === [1], 'Expected only the ordinary selected account to be deleted.');
$assert($result['skipped_user_ids'] === [2, 999], 'Administrator or missing account was not excluded.');
$assert(User::find(1) === null && User::find(2) !== null && User::find(3) !== null, 'User deletion was not isolated.');
$assert((int) User::find(3)->ref_by === 0, 'Deleted referrer was not cleared.');
$assert(Capsule::table('ticket')->pluck('id')->all() === [20], 'Ticket replies from the deleted thread were retained.');
foreach (['yipay_orders', 'yipay_callback_events', 'yipay_refunds'] as $tableName) {
$assert(Capsule::table($tableName)->pluck('order_no')->all() === ['retained-order'], 'Payment relation deletion was not isolated.');
}
foreach (['cloud_instances', 'cloud_orders', 'cloud_instance_snapshots',
'cloud_instance_firewalls', 'cloud_service_periods', 'cloud_renewals',
'cloud_invoices', 'cloud_invoice_payments'] as $tableName) {
$assert(Capsule::table($tableName)->pluck('user_id')->all() === [3],
'Cloud account history deletion was not isolated: ' . $tableName);
}
$assert(Capsule::table('cloud_instance_firewall_versions')->pluck('id')->all()
=== [306]
&& Capsule::table('cloud_instance_firewall_rules')->pluck('id')->all()
=== [307],
'Tenant deletion orphaned or over-deleted firewall policy history.');
$assert(Capsule::table('cloud_jobs')->orderBy('id')->pluck('id')->all()
=== [206, 305],
'Tenant deletion retained delegated jobs or removed unrelated administrator jobs.');
$assert(Capsule::table('cloud_mail_outbox')->pluck('invoice_id')->all() === [303],
'Cloud invoice mail history deletion was not isolated.');
$assert(Capsule::table('cloud_vpcs')->count() === 0
&& Capsule::table('cloud_private_networks')->count() === 0,
'Terminal VPC roots or released private-network history survived account deletion.');
foreach (['cloud_vpc_routes', 'cloud_vpc_acl_rules', 'cloud_vpc_private_ips',
'cloud_vpc_members', 'cloud_vpc_subnets', 'cloud_vpc_host_deployments',
'cloud_vpc_operations'] as $tableName) {
$assert(Capsule::table($tableName)->count() === 0,
'Terminal VPC child history survived account deletion: ' . $tableName);
}
$assert(Capsule::table('email_verify')->pluck('email')->all() === ['other@example.com'], 'Email verification cleanup was not isolated.');
$assert(Capsule::table('configurations')->where('name', 'email_blacklist')->value('value') === "deleted@example.com\n@blocked.example", 'Manual blacklist was changed.');
$assert($result['cache_error'] === false && $result['log_rows_deleted'] === 0, 'Disabled optional services were accessed.');
Capsule::table('user')->insert([
'id' => 4, 'email' => 'payment-pending@example.com', 'is_admin' => 0, 'ref_by' => 0,
]);
Capsule::table('cloud_invoice_payments')->insert([
'user_id' => 4, 'status' => 'failed',
]);
try {
(new UserDataDeletionService())->deleteUsers([4], false);
throw new RuntimeException('Account with an unresolved payment was deleted.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), '未核对的支付订单'),
'Unresolved-payment deletion returned the wrong error.');
}
$assert(User::find(4) !== null, 'Unresolved-payment deletion was not rolled back.');
Capsule::table('user')->insert([
'id' => 5, 'email' => 'provision-invoice@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
Capsule::table('cloud_orders')->insert([
'id' => 502, 'user_id' => 5, 'status' => 'awaiting_payment',
]);
try {
(new UserDataDeletionService())->deleteUsers([5], false);
throw new RuntimeException('Account with an unpaid provisioning invoice was deleted.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), '开通中的订单'),
'Unpaid-provisioning deletion returned the wrong error.');
}
$assert(User::find(5) !== null
&& Capsule::table('cloud_orders')->where('id', 502)->exists(),
'Unpaid-provisioning deletion was not rolled back.');
Capsule::table('user')->insert([
'id' => 6, 'email' => 'active-vpc@example.com', 'is_admin' => 0, 'ref_by' => 0,
]);
$activeVpcKey = str_repeat('c', 32);
Capsule::table('cloud_private_networks')->insert([
'id' => 6001, 'vpc_key' => $activeVpcKey,
'allocation_key' => str_repeat('d', 32), 'user_id' => 6,
'state' => 'active', 'active_cidr' => '10.60.0.0/24',
]);
Capsule::table('cloud_vpcs')->insert([
'id' => 6002, 'vpc_key' => $activeVpcKey, 'user_id' => 6,
'private_network_id' => 6001, 'state' => 'active',
]);
try {
(new UserDataDeletionService())->deleteUsers([6], false);
throw new RuntimeException('Account with a live VPC was deleted.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), '仍有 VPC 私网'),
'Live-VPC account deletion returned the wrong error.');
}
$assert(User::find(6) !== null, 'Live-VPC account deletion was not rolled back.');
Capsule::table('user')->insert([
'id' => 7, 'email' => 'claimed-vpc@example.com', 'is_admin' => 0, 'ref_by' => 0,
]);
$claimedVpcKey = str_repeat('e', 32);
$claimedAllocationKey = str_repeat('f', 32);
Capsule::table('cloud_private_networks')->insert([
'id' => 7001, 'vpc_key' => $claimedVpcKey,
'allocation_key' => $claimedAllocationKey, 'user_id' => 7,
'state' => 'released', 'active_cidr' => null,
]);
Capsule::table('cloud_vpcs')->insert([
'id' => 7002, 'vpc_key' => $claimedVpcKey, 'user_id' => 7,
'private_network_id' => 7001, 'state' => 'deleted',
]);
Capsule::table('cloud_private_cidr_claims')->insert([
'allocation_key' => $claimedAllocationKey,
]);
try {
(new UserDataDeletionService())->deleteUsers([7], false);
throw new RuntimeException('Account with a residual private CIDR claim was deleted.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), '仍有 CIDR 声明'),
'Residual-claim account deletion returned the wrong error.');
}
$assert(User::find(7) !== null
&& Capsule::table('cloud_vpcs')->where('id', 7002)->exists(),
'Residual-claim account deletion discarded fail-closed VPC evidence.');
Capsule::table('user')->insert([
'id' => 8, 'email' => 'missing-vpc-table@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
$missingTableVpcKey = str_repeat('1', 32);
Capsule::table('cloud_private_networks')->insert([
'id' => 8001, 'vpc_key' => $missingTableVpcKey,
'allocation_key' => str_repeat('2', 32), 'user_id' => 8,
'state' => 'released', 'active_cidr' => null,
]);
Capsule::table('cloud_vpcs')->insert([
'id' => 8002, 'vpc_key' => $missingTableVpcKey, 'user_id' => 8,
'private_network_id' => 8001, 'state' => 'deleted',
]);
$schema->drop('cloud_vpc_operations');
try {
(new UserDataDeletionService())->deleteUsers([8], false);
throw new RuntimeException('Account cleanup ignored a missing VPC child ledger.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), '子账本不完整'),
'Missing VPC child ledger returned the wrong error.');
}
$assert(User::find(8) !== null
&& Capsule::table('cloud_vpcs')->where('id', 8002)->exists()
&& Capsule::table('cloud_private_networks')->where('id', 8001)->exists(),
'Missing child schema did not roll back account/VPC cleanup.');
$schema->create('cloud_vpc_operations', static function (Blueprint $table): void {
$table->increments('id');
$table->string('sensitive_payload');
});
Capsule::table('cloud_vpc_operations')->insert([
'sensitive_payload' => 'must-not-be-orphaned',
]);
Capsule::table('user')->insert([
'id' => 9, 'email' => 'missing-vpc-column@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
$missingColumnVpcKey = str_repeat('3', 32);
Capsule::table('cloud_private_networks')->insert([
'id' => 9001, 'vpc_key' => $missingColumnVpcKey,
'allocation_key' => str_repeat('4', 32), 'user_id' => 9,
'state' => 'released', 'active_cidr' => null,
]);
Capsule::table('cloud_vpcs')->insert([
'id' => 9002, 'vpc_key' => $missingColumnVpcKey, 'user_id' => 9,
'private_network_id' => 9001, 'state' => 'deleted',
]);
try {
(new UserDataDeletionService())->deleteUsers([9], false);
throw new RuntimeException('Account cleanup ignored a malformed VPC child ledger.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), '子账本不完整'),
'Malformed VPC child ledger returned the wrong error.');
}
$assert(User::find(9) !== null
&& Capsule::table('cloud_vpcs')->where('id', 9002)->exists()
&& Capsule::table('cloud_vpc_operations')->where(
'sensitive_payload', 'must-not-be-orphaned'
)->exists(),
'Malformed child schema orphaned VPC or sensitive operation history.');
// Both core tables existing with incomplete ownership columns must never be
// confused with a deployment where the VPC feature is wholly absent. This
// reproduces the dangerous false/false hasColumns branch that used to delete
// the account and leave an unverifiable CIDR claim/history relationship.
$schema->drop('cloud_vpcs');
$schema->drop('cloud_private_networks');
$schema->create('cloud_private_networks', static function (Blueprint $table): void {
$table->increments('id');
$table->string('vpc_key', 32);
$table->integer('user_id');
$table->string('state', 16);
});
$schema->create('cloud_vpcs', static function (Blueprint $table): void {
$table->increments('id');
$table->string('vpc_key', 32);
$table->integer('user_id');
$table->string('state', 16);
});
Capsule::table('user')->insert([
'id' => 10, 'email' => 'malformed-vpc-core@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
$malformedCoreVpcKey = str_repeat('5', 32);
Capsule::table('cloud_private_networks')->insert([
'id' => 10001, 'vpc_key' => $malformedCoreVpcKey,
'user_id' => 10, 'state' => 'released',
]);
Capsule::table('cloud_vpcs')->insert([
'id' => 10002, 'vpc_key' => $malformedCoreVpcKey,
'user_id' => 10, 'state' => 'deleted',
]);
try {
(new UserDataDeletionService())->deleteUsers([10], false);
throw new RuntimeException('Account cleanup ignored malformed VPC core ledgers.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), '删除账本不完整'),
'Malformed VPC core ledgers returned the wrong error.');
}
$assert(User::find(10) !== null
&& Capsule::table('cloud_vpcs')->where('id', 10002)->exists()
&& Capsule::table('cloud_private_networks')->where('id', 10001)->exists(),
'Malformed VPC core ledgers did not roll back account cleanup.');
echo 'Manual account deletion SQLite checks passed.' . PHP_EOL;