Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Subuser/DeleteSubuserTest.php
10259 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Subuser;34use Ramsey\Uuid\Uuid;5use Mockery\MockInterface;6use Pterodactyl\Models\User;7use Pterodactyl\Models\Subuser;8use Pterodactyl\Models\Permission;9use PHPUnit\Framework\Attributes\TestWith;10use Pterodactyl\Repositories\Wings\DaemonRevocationRepository;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{30[$user, $server] = $this->generateTestAccount();3132/** @var User $differentUser */33$differentUser = User::factory()->create();3435$real = Uuid::uuid4()->toString();36// Generate a UUID that lines up with a user in the database if it were to be cast to an int.37$uuid = ($prefix ?: $differentUser->id) . substr($real, strlen($prefix ?: (string) $differentUser->id));3839/** @var User $subuser */40$subuser = User::factory()->create(['uuid' => $uuid]);4142Subuser::query()->forceCreate([43'user_id' => $subuser->id,44'server_id' => $server->id,45'permissions' => [Permission::ACTION_WEBSOCKET_CONNECT],46]);4748$this->mock(DaemonRevocationRepository::class, function (MockInterface $mock) use ($subuser, $server) {49$mock->expects('setNode')50->with(\Mockery::on(fn ($value) => $value->is($server->node)))51->andReturnSelf();5253$mock->expects('deauthorize')54->with($subuser->uuid, [$server->uuid])55->andReturnUndefined();56});5758$this->withoutExceptionHandling()59->actingAs($user)60->deleteJson($this->link($server) . "/users/$subuser->uuid")->assertNoContent();61}62}636465