Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Client/Server/NetworkAllocationControllerTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client\Server;
4
5
use Pterodactyl\Models\User;
6
use Illuminate\Http\Response;
7
use Pterodactyl\Models\Allocation;
8
use Pterodactyl\Models\Permission;
9
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
10
11
class NetworkAllocationControllerTest extends ClientApiIntegrationTestCase
12
{
13
/**
14
* Test that a servers allocations are returned in the expected format.
15
*/
16
public function testServerAllocationsAreReturned()
17
{
18
[$user, $server] = $this->generateTestAccount();
19
20
$response = $this->actingAs($user)->getJson($this->link($server, '/network/allocations'));
21
22
$response->assertOk();
23
$response->assertJsonPath('object', 'list');
24
$response->assertJsonCount(1, 'data');
25
26
$this->assertJsonTransformedWith($response->json('data.0.attributes'), $server->allocation);
27
}
28
29
/**
30
* Test that allocations cannot be returned without the required user permissions.
31
*/
32
public function testServerAllocationsAreNotReturnedWithoutPermission()
33
{
34
[$user, $server] = $this->generateTestAccount();
35
$user2 = User::factory()->create();
36
37
$server->owner_id = $user2->id;
38
$server->save();
39
40
$this->actingAs($user)->getJson($this->link($server, '/network/allocations'))
41
->assertNotFound();
42
43
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);
44
45
$this->actingAs($user)->getJson($this->link($server, '/network/allocations'))
46
->assertForbidden();
47
}
48
49
/**
50
* Tests that notes on an allocation can be set correctly.
51
*/
52
#[\PHPUnit\Framework\Attributes\DataProvider('updatePermissionsDataProvider')]
53
public function testAllocationNotesCanBeUpdated(array $permissions)
54
{
55
[$user, $server] = $this->generateTestAccount($permissions);
56
$allocation = $server->allocation;
57
58
$this->assertNull($allocation->notes);
59
60
$this->actingAs($user)->postJson($this->link($allocation), [])
61
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
62
->assertJsonPath('errors.0.meta.rule', 'present');
63
64
$this->actingAs($user)->postJson($this->link($allocation), ['notes' => 'Test notes'])
65
->assertOk()
66
->assertJsonPath('object', Allocation::RESOURCE_NAME)
67
->assertJsonPath('attributes.notes', 'Test notes');
68
69
$allocation = $allocation->refresh();
70
71
$this->assertSame('Test notes', $allocation->notes);
72
73
$this->actingAs($user)->postJson($this->link($allocation), ['notes' => null])
74
->assertOk()
75
->assertJsonPath('object', Allocation::RESOURCE_NAME)
76
->assertJsonPath('attributes.notes', null);
77
78
$allocation = $allocation->refresh();
79
80
$this->assertNull($allocation->notes);
81
}
82
83
public function testAllocationNotesCannotBeUpdatedByInvalidUsers()
84
{
85
[$user, $server] = $this->generateTestAccount();
86
$user2 = User::factory()->create();
87
88
$server->owner_id = $user2->id;
89
$server->save();
90
91
$this->actingAs($user)->postJson($this->link($server->allocation))->assertNotFound();
92
93
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);
94
95
$this->actingAs($user)->postJson($this->link($server->allocation))->assertForbidden();
96
}
97
98
#[\PHPUnit\Framework\Attributes\DataProvider('updatePermissionsDataProvider')]
99
public function testPrimaryAllocationCanBeModified(array $permissions)
100
{
101
[$user, $server] = $this->generateTestAccount($permissions);
102
$allocation = $server->allocation;
103
$allocation2 = Allocation::factory()->create(['node_id' => $server->node_id, 'server_id' => $server->id]);
104
105
$server->allocation_id = $allocation->id;
106
$server->save();
107
108
$this->actingAs($user)->postJson($this->link($allocation2, '/primary'))
109
->assertOk();
110
111
$server = $server->refresh();
112
113
$this->assertSame($allocation2->id, $server->allocation_id);
114
}
115
116
public function testPrimaryAllocationCannotBeModifiedByInvalidUser()
117
{
118
[$user, $server] = $this->generateTestAccount();
119
$user2 = User::factory()->create();
120
121
$server->owner_id = $user2->id;
122
$server->save();
123
124
$this->actingAs($user)->postJson($this->link($server->allocation, '/primary'))
125
->assertNotFound();
126
127
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);
128
129
$this->actingAs($user)->postJson($this->link($server->allocation, '/primary'))
130
->assertForbidden();
131
}
132
133
public static function updatePermissionsDataProvider(): array
134
{
135
return [[[]], [[Permission::ACTION_ALLOCATION_UPDATE]]];
136
}
137
}
138
139