Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Allocation/CreateNewAllocationTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Allocation;34use Illuminate\Http\Response;5use Pterodactyl\Models\Allocation;6use Pterodactyl\Models\Permission;7use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;89class CreateNewAllocationTest extends ClientApiIntegrationTestCase10{11/**12* Setup tests.13*/14public function setUp(): void15{16parent::setUp();1718config()->set('pterodactyl.client_features.allocations.enabled', true);19config()->set('pterodactyl.client_features.allocations.range_start', 5000);20config()->set('pterodactyl.client_features.allocations.range_end', 5050);21}2223/**24* Tests that a new allocation can be properly assigned to a server.25*/26#[\PHPUnit\Framework\Attributes\DataProvider('permissionDataProvider')]27public function testNewAllocationCanBeAssignedToServer(array $permission)28{29/** @var \Pterodactyl\Models\Server $server */30[$user, $server] = $this->generateTestAccount($permission);31$server->update(['allocation_limit' => 2]);3233$response = $this->actingAs($user)->postJson($this->link($server, '/network/allocations'));34$response->assertJsonPath('object', Allocation::RESOURCE_NAME);3536$matched = Allocation::query()->findOrFail($response->json('attributes.id'));3738$this->assertSame($server->id, $matched->server_id);39$this->assertJsonTransformedWith($response->json('attributes'), $matched);40}4142/**43* Test that a user without the required permissions cannot create an allocation for44* the server instance.45*/46public function testAllocationCannotBeCreatedIfUserDoesNotHavePermission()47{48/** @var \Pterodactyl\Models\Server $server */49[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_UPDATE]);50$server->update(['allocation_limit' => 2]);5152$this->actingAs($user)->postJson($this->link($server, '/network/allocations'))->assertForbidden();53}5455/**56* Test that an error is returned to the user if this feature is not enabled on the system.57*/58public function testAllocationCannotBeCreatedIfNotEnabled()59{60config()->set('pterodactyl.client_features.allocations.enabled', false);6162/** @var \Pterodactyl\Models\Server $server */63[$user, $server] = $this->generateTestAccount();64$server->update(['allocation_limit' => 2]);6566$this->actingAs($user)->postJson($this->link($server, '/network/allocations'))67->assertStatus(Response::HTTP_BAD_REQUEST)68->assertJsonPath('errors.0.code', 'AutoAllocationNotEnabledException')69->assertJsonPath('errors.0.detail', 'Server auto-allocation is not enabled for this instance.');70}7172/**73* Test that an allocation cannot be created if the server has reached its allocation limit.74*/75public function testAllocationCannotBeCreatedIfServerIsAtLimit()76{77/** @var \Pterodactyl\Models\Server $server */78[$user, $server] = $this->generateTestAccount();79$server->update(['allocation_limit' => 1]);8081$this->actingAs($user)->postJson($this->link($server, '/network/allocations'))82->assertStatus(Response::HTTP_BAD_REQUEST)83->assertJsonPath('errors.0.code', 'DisplayException')84->assertJsonPath('errors.0.detail', 'Cannot assign additional allocations to this server: limit has been reached.');85}8687public static function permissionDataProvider(): array88{89return [[[Permission::ACTION_ALLOCATION_CREATE]], [[]]];90}91}929394