Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Allocation/DeleteAllocationTest.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 DeleteAllocationTest extends ClientApiIntegrationTestCase10{11/**12* Test that an allocation is deleted from the server and the notes are properly reset13* to an empty value on assignment.14*/15#[\PHPUnit\Framework\Attributes\DataProvider('permissionDataProvider')]16public function testAllocationCanBeDeletedFromServer(array $permission)17{18/** @var \Pterodactyl\Models\Server $server */19[$user, $server] = $this->generateTestAccount($permission);20$server->update(['allocation_limit' => 2]);2122/** @var Allocation $allocation */23$allocation = Allocation::factory()->create([24'server_id' => $server->id,25'node_id' => $server->node_id,26'notes' => 'hodor',27]);2829$this->actingAs($user)->deleteJson($this->link($allocation))->assertStatus(Response::HTTP_NO_CONTENT);3031$this->assertDatabaseHas('allocations', ['id' => $allocation->id, 'server_id' => null, 'notes' => null]);32}3334/**35* Test that an error is returned if the user does not have permissiont to delete an allocation.36*/37public function testErrorIsReturnedIfUserDoesNotHavePermission()38{39/** @var \Pterodactyl\Models\Server $server */40[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);4142/** @var Allocation $allocation */43$allocation = Allocation::factory()->create([44'server_id' => $server->id,45'node_id' => $server->node_id,46'notes' => 'hodor',47]);4849$this->actingAs($user)->deleteJson($this->link($allocation))->assertForbidden();5051$this->assertDatabaseHas('allocations', ['id' => $allocation->id, 'server_id' => $server->id]);52}5354/**55* Test that an allocation is not deleted if it is currently marked as the primary allocation56* for the server.57*/58public function testErrorIsReturnedIfAllocationIsPrimary()59{60/** @var \Pterodactyl\Models\Server $server */61[$user, $server] = $this->generateTestAccount();62$server->update(['allocation_limit' => 2]);6364$this->actingAs($user)->deleteJson($this->link($server->allocation))65->assertStatus(Response::HTTP_BAD_REQUEST)66->assertJsonPath('errors.0.code', 'DisplayException')67->assertJsonPath('errors.0.detail', 'You cannot delete the primary allocation for this server.');68}6970public function testAllocationCannotBeDeletedIfServerLimitIsNotDefined()71{72[$user, $server] = $this->generateTestAccount();7374/** @var Allocation $allocation */75$allocation = Allocation::factory()->forServer($server)->create(['notes' => 'Test notes']);7677$this->actingAs($user)->deleteJson($this->link($allocation))78->assertStatus(400)79->assertJsonPath('errors.0.detail', 'You cannot delete allocations for this server: no allocation limit is set.');8081$allocation->refresh();82$this->assertNotNull($allocation->notes);83$this->assertEquals($server->id, $allocation->server_id);84}8586/**87* Test that an allocation cannot be deleted if it does not belong to the server instance.88*/89public function testErrorIsReturnedIfAllocationDoesNotBelongToServer()90{91/** @var \Pterodactyl\Models\Server $server */92[$user, $server] = $this->generateTestAccount();93[, $server2] = $this->generateTestAccount();9495$this->actingAs($user)->deleteJson($this->link($server2->allocation))->assertNotFound();96$this->actingAs($user)->deleteJson($this->link($server, "/network/allocations/{$server2->allocation_id}"))->assertNotFound();97}9899public static function permissionDataProvider(): array100{101return [[[Permission::ACTION_ALLOCATION_DELETE]], [[]]];102}103}104105106