Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Subuser/DeleteSubuserTest.php
14052 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Subuser;34use Ramsey\Uuid\Uuid;5use Pterodactyl\Models\User;6use Pterodactyl\Models\Subuser;7use Pterodactyl\Models\Permission;8use Illuminate\Support\Facades\Bus;9use Pterodactyl\Jobs\RevokeSftpAccessJob;10use PHPUnit\Framework\Attributes\TestWith;11use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;1213class DeleteSubuserTest extends ClientApiIntegrationTestCase14{15/**16* Guards against PHP's exciting behavior where a string can be cast to an int and only17* the first numeric digits are returned. This causes UUIDs to be returned as an int when18* looking up users, thus returning the wrong subusers (or no subuser at all).19*20* For example, 12aaaaaa-bbbb-cccc-ddddeeeeffff would be cast to "12" if you tried to cast21* it to an integer. Then, in the deep API middlewares you would end up trying to load a user22* with an ID of 12, which may or may not exist and be wrongly assigned to the model object.23*24* @see https://github.com/pterodactyl/panel/issues/235925*/26#[TestWith([null])]27#[TestWith(['18180000'])]28public function testCorrectSubuserIsDeletedFromServer(?string $prefix)29{30Bus::fake([RevokeSftpAccessJob::class]);3132[$user, $server] = $this->generateTestAccount();3334/** @var User $differentUser */35$differentUser = User::factory()->create();3637$real = Uuid::uuid4()->toString();38// Generate a UUID that lines up with a user in the database if it were to be cast to an int.39$uuid = ($prefix ?: $differentUser->id) . substr($real, strlen($prefix ?: (string) $differentUser->id));4041/** @var User $subuser */42$subuser = User::factory()->create(['uuid' => $uuid]);4344Subuser::query()->forceCreate([45'user_id' => $subuser->id,46'server_id' => $server->id,47'permissions' => [Permission::ACTION_WEBSOCKET_CONNECT],48]);4950$this->withoutExceptionHandling()51->actingAs($user)52->deleteJson($this->link($server) . "/users/$subuser->uuid")->assertNoContent();5354Bus::assertDispatchedTimes(function (RevokeSftpAccessJob $job) use ($subuser, $server) {55return $job->user === $subuser->uuid && $job->target->is($server);56});57}58}596061