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/CreateNewAllocationTest.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 CreateNewAllocationTest extends ClientApiIntegrationTestCase
11
{
12
/**
13
* Setup tests.
14
*/
15
public function setUp(): void
16
{
17
parent::setUp();
18
19
config()->set('pterodactyl.client_features.allocations.enabled', true);
20
config()->set('pterodactyl.client_features.allocations.range_start', 5000);
21
config()->set('pterodactyl.client_features.allocations.range_end', 5050);
22
}
23
24
/**
25
* Tests that a new allocation can be properly assigned to a server.
26
*/
27
#[\PHPUnit\Framework\Attributes\DataProvider('permissionDataProvider')]
28
public function testNewAllocationCanBeAssignedToServer(array $permission)
29
{
30
/** @var \Pterodactyl\Models\Server $server */
31
[$user, $server] = $this->generateTestAccount($permission);
32
$server->update(['allocation_limit' => 2]);
33
34
$response = $this->actingAs($user)->postJson($this->link($server, '/network/allocations'));
35
$response->assertJsonPath('object', Allocation::RESOURCE_NAME);
36
37
$matched = Allocation::query()->findOrFail($response->json('attributes.id'));
38
39
$this->assertSame($server->id, $matched->server_id);
40
$this->assertJsonTransformedWith($response->json('attributes'), $matched);
41
}
42
43
/**
44
* Test that a user without the required permissions cannot create an allocation for
45
* the server instance.
46
*/
47
public function testAllocationCannotBeCreatedIfUserDoesNotHavePermission()
48
{
49
/** @var \Pterodactyl\Models\Server $server */
50
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_UPDATE]);
51
$server->update(['allocation_limit' => 2]);
52
53
$this->actingAs($user)->postJson($this->link($server, '/network/allocations'))->assertForbidden();
54
}
55
56
/**
57
* Test that an error is returned to the user if this feature is not enabled on the system.
58
*/
59
public function testAllocationCannotBeCreatedIfNotEnabled()
60
{
61
config()->set('pterodactyl.client_features.allocations.enabled', false);
62
63
/** @var \Pterodactyl\Models\Server $server */
64
[$user, $server] = $this->generateTestAccount();
65
$server->update(['allocation_limit' => 2]);
66
67
$this->actingAs($user)->postJson($this->link($server, '/network/allocations'))
68
->assertStatus(Response::HTTP_BAD_REQUEST)
69
->assertJsonPath('errors.0.code', 'AutoAllocationNotEnabledException')
70
->assertJsonPath('errors.0.detail', 'Server auto-allocation is not enabled for this instance.');
71
}
72
73
/**
74
* Test that an allocation cannot be created if the server has reached its allocation limit.
75
*/
76
public function testAllocationCannotBeCreatedIfServerIsAtLimit()
77
{
78
/** @var \Pterodactyl\Models\Server $server */
79
[$user, $server] = $this->generateTestAccount();
80
$server->update(['allocation_limit' => 1]);
81
82
$this->actingAs($user)->postJson($this->link($server, '/network/allocations'))
83
->assertStatus(Response::HTTP_BAD_REQUEST)
84
->assertJsonPath('errors.0.code', 'DisplayException')
85
->assertJsonPath('errors.0.detail', 'Cannot assign additional allocations to this server: limit has been reached.');
86
}
87
88
public static function permissionDataProvider(): array
89
{
90
return [[[Permission::ACTION_ALLOCATION_CREATE]], [[]]];
91
}
92
}
93
94