Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Allocation/DeleteAllocationTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Allocation;
4
5
use Illuminate\Http\Response;
6
use Pterodactyl\Models\Allocation;
7
use Pterodactyl\Models\Permission;
8
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
9
10
class DeleteAllocationTest extends ClientApiIntegrationTestCase
11
{
12
/**
13
* Test that an allocation is deleted from the server and the notes are properly reset
14
* to an empty value on assignment.
15
*/
16
#[\PHPUnit\Framework\Attributes\DataProvider('permissionDataProvider')]
17
public function testAllocationCanBeDeletedFromServer(array $permission)
18
{
19
/** @var \Pterodactyl\Models\Server $server */
20
[$user, $server] = $this->generateTestAccount($permission);
21
$server->update(['allocation_limit' => 2]);
22
23
/** @var Allocation $allocation */
24
$allocation = Allocation::factory()->create([
25
'server_id' => $server->id,
26
'node_id' => $server->node_id,
27
'notes' => 'hodor',
28
]);
29
30
$this->actingAs($user)->deleteJson($this->link($allocation))->assertStatus(Response::HTTP_NO_CONTENT);
31
32
$this->assertDatabaseHas('allocations', ['id' => $allocation->id, 'server_id' => null, 'notes' => null]);
33
}
34
35
/**
36
* Test that an error is returned if the user does not have permissiont to delete an allocation.
37
*/
38
public function testErrorIsReturnedIfUserDoesNotHavePermission()
39
{
40
/** @var \Pterodactyl\Models\Server $server */
41
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);
42
43
/** @var Allocation $allocation */
44
$allocation = Allocation::factory()->create([
45
'server_id' => $server->id,
46
'node_id' => $server->node_id,
47
'notes' => 'hodor',
48
]);
49
50
$this->actingAs($user)->deleteJson($this->link($allocation))->assertForbidden();
51
52
$this->assertDatabaseHas('allocations', ['id' => $allocation->id, 'server_id' => $server->id]);
53
}
54
55
/**
56
* Test that an allocation is not deleted if it is currently marked as the primary allocation
57
* for the server.
58
*/
59
public function testErrorIsReturnedIfAllocationIsPrimary()
60
{
61
/** @var \Pterodactyl\Models\Server $server */
62
[$user, $server] = $this->generateTestAccount();
63
$server->update(['allocation_limit' => 2]);
64
65
$this->actingAs($user)->deleteJson($this->link($server->allocation))
66
->assertStatus(Response::HTTP_BAD_REQUEST)
67
->assertJsonPath('errors.0.code', 'DisplayException')
68
->assertJsonPath('errors.0.detail', 'You cannot delete the primary allocation for this server.');
69
}
70
71
public function testAllocationCannotBeDeletedIfServerLimitIsNotDefined()
72
{
73
[$user, $server] = $this->generateTestAccount();
74
75
/** @var Allocation $allocation */
76
$allocation = Allocation::factory()->forServer($server)->create(['notes' => 'Test notes']);
77
78
$this->actingAs($user)->deleteJson($this->link($allocation))
79
->assertStatus(400)
80
->assertJsonPath('errors.0.detail', 'You cannot delete allocations for this server: no allocation limit is set.');
81
82
$allocation->refresh();
83
$this->assertNotNull($allocation->notes);
84
$this->assertEquals($server->id, $allocation->server_id);
85
}
86
87
/**
88
* Test that an allocation cannot be deleted if it does not belong to the server instance.
89
*/
90
public function testErrorIsReturnedIfAllocationDoesNotBelongToServer()
91
{
92
/** @var \Pterodactyl\Models\Server $server */
93
[$user, $server] = $this->generateTestAccount();
94
[, $server2] = $this->generateTestAccount();
95
96
$this->actingAs($user)->deleteJson($this->link($server2->allocation))->assertNotFound();
97
$this->actingAs($user)->deleteJson($this->link($server, "/network/allocations/{$server2->allocation_id}"))->assertNotFound();
98
}
99
100
public static function permissionDataProvider(): array
101
{
102
return [[[Permission::ACTION_ALLOCATION_DELETE]], [[]]];
103
}
104
}
105
106