Skip to content

Commit a430ebb

Browse files
committed
Add test coverage for allocation routes
1 parent d040b3a commit a430ebb

File tree

3 files changed

+220
-2
lines changed

3 files changed

+220
-2
lines changed

app/Http/Requests/Api/Client/Servers/Network/UpdateAllocationRequest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ public function permission(): string
2121
*/
2222
public function rules(): array
2323
{
24+
$rules = Allocation::getRules();
25+
2426
return [
25-
'notes' => Allocation::$validationRules['notes'],
27+
'notes' => array_merge($rules['notes'], ['present']),
2628
];
2729
}
2830
}

tests/Integration/Api/Client/ClientApiIntegrationTestCase.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Pterodactyl\Models\Location;
1616
use Pterodactyl\Models\Schedule;
1717
use Illuminate\Support\Collection;
18+
use Pterodactyl\Models\Allocation;
1819
use Pterodactyl\Tests\Integration\IntegrationTestCase;
1920
use Pterodactyl\Transformers\Api\Client\BaseClientTransformer;
2021

@@ -53,7 +54,7 @@ public function setUp(): void
5354
*/
5455
protected function link($model, $append = null): string
5556
{
56-
Assert::isInstanceOfAny($model, [Server::class, Schedule::class, Task::class]);
57+
Assert::isInstanceOfAny($model, [Server::class, Schedule::class, Task::class, Allocation::class]);
5758

5859
$link = '';
5960
switch (get_class($model)) {
@@ -66,6 +67,9 @@ protected function link($model, $append = null): string
6667
case Task::class:
6768
$link = "/api/client/servers/{$model->schedule->server->uuid}/schedules/{$model->schedule->id}/tasks/{$model->id}";
6869
break;
70+
case Allocation::class:
71+
$link = "/api/client/servers/{$model->server->uuid}/network/allocations/{$model->id}";
72+
break;
6973
}
7074

7175
return $link . ($append ? '/' . ltrim($append, '/') : '');
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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\Server;
8+
use Pterodactyl\Models\Allocation;
9+
use Pterodactyl\Models\Permission;
10+
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
11+
12+
class NetworkAllocationControllerTest extends ClientApiIntegrationTestCase
13+
{
14+
/**
15+
* Test that a servers allocations are returned in the expected format.
16+
*/
17+
public function testServerAllocationsAreReturned()
18+
{
19+
[$user, $server] = $this->generateTestAccount();
20+
$allocation = $this->getAllocation($server);
21+
22+
$response = $this->actingAs($user)->getJson($this->link($server, '/network/allocations'));
23+
24+
$response->assertOk();
25+
$response->assertJsonPath('object', 'list');
26+
$response->assertJsonCount(1, 'data');
27+
28+
$this->assertJsonTransformedWith($response->json('data.0.attributes'), $allocation);
29+
}
30+
31+
/**
32+
* Test that allocations cannot be returned without the required user permissions.
33+
*/
34+
public function testServerAllocationsAreNotReturnedWithoutPermission()
35+
{
36+
[$user, $server] = $this->generateTestAccount();
37+
$user2 = factory(User::class)->create();
38+
39+
$server->owner_id = $user2->id;
40+
$server->save();
41+
42+
$this->actingAs($user)->getJson($this->link($server, '/network/allocations'))
43+
->assertNotFound();
44+
45+
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);
46+
47+
$this->actingAs($user)->getJson($this->link($server, '/network/allocations'))
48+
->assertForbidden();
49+
}
50+
51+
/**
52+
* Tests that notes on an allocation can be set correctly.
53+
*
54+
* @param array $permissions
55+
* @dataProvider updatePermissionsDataProvider
56+
*/
57+
public function testAllocationNotesCanBeUpdated(array $permissions)
58+
{
59+
[$user, $server] = $this->generateTestAccount($permissions);
60+
$allocation = $this->getAllocation($server);
61+
62+
$this->assertNull($allocation->notes);
63+
64+
$this->actingAs($user)->postJson($this->link($allocation), [])
65+
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
66+
->assertJsonPath('errors.0.code', 'present');
67+
68+
$this->actingAs($user)->postJson($this->link($allocation), ['notes' => 'Test notes'])
69+
->assertOk()
70+
->assertJsonPath('object', Allocation::RESOURCE_NAME)
71+
->assertJsonPath('attributes.notes', 'Test notes');
72+
73+
$allocation = $allocation->refresh();
74+
75+
$this->assertSame('Test notes', $allocation->notes);
76+
77+
$this->actingAs($user)->postJson($this->link($allocation), ['notes' => null])
78+
->assertOk()
79+
->assertJsonPath('object', Allocation::RESOURCE_NAME)
80+
->assertJsonPath('attributes.notes', null);
81+
82+
$allocation = $allocation->refresh();
83+
84+
$this->assertNull($allocation->notes);
85+
}
86+
87+
public function testAllocationNotesCannotBeUpdatedByInvalidUsers()
88+
{
89+
[$user, $server] = $this->generateTestAccount();
90+
$user2 = factory(User::class)->create();
91+
92+
$server->owner_id = $user2->id;
93+
$server->save();
94+
95+
$this->actingAs($user)->postJson($this->link($this->getAllocation($server)))
96+
->assertNotFound();
97+
98+
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);
99+
100+
$this->actingAs($user)->postJson($this->link($this->getAllocation($server)))
101+
->assertForbidden();
102+
}
103+
104+
/**
105+
* @param array $permissions
106+
* @dataProvider updatePermissionsDataProvider
107+
*/
108+
public function testPrimaryAllocationCanBeModified(array $permissions)
109+
{
110+
[$user, $server] = $this->generateTestAccount($permissions);
111+
$allocation = $this->getAllocation($server);
112+
$allocation2 = $this->getAllocation($server);
113+
114+
$server->allocation_id = $allocation->id;
115+
$server->save();
116+
117+
$this->actingAs($user)->postJson($this->link($allocation2, '/primary'))
118+
->assertOk();
119+
120+
$server = $server->refresh();
121+
122+
$this->assertSame($allocation2->id, $server->allocation_id);
123+
}
124+
125+
public function testPrimaryAllocationCannotBeModifiedByInvalidUser()
126+
{
127+
[$user, $server] = $this->generateTestAccount();
128+
$user2 = factory(User::class)->create();
129+
130+
$server->owner_id = $user2->id;
131+
$server->save();
132+
133+
$this->actingAs($user)->postJson($this->link($this->getAllocation($server), '/primary'))
134+
->assertNotFound();
135+
136+
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);
137+
138+
$this->actingAs($user)->postJson($this->link($this->getAllocation($server), '/primary'))
139+
->assertForbidden();
140+
}
141+
142+
/**
143+
* @param array $permissions
144+
* @dataProvider deletePermissionsDataProvider
145+
*/
146+
public function testAllocationCanBeDeleted(array $permissions)
147+
{
148+
[$user, $server] = $this->generateTestAccount($permissions);
149+
$allocation = $this->getAllocation($server);
150+
$allocation2 = $this->getAllocation($server);
151+
152+
$allocation2->notes = 'Filled notes';
153+
$allocation2->save();
154+
155+
$server->allocation_id = $allocation->id;
156+
$server->save();
157+
158+
$this->actingAs($user)->deleteJson($this->link($allocation))
159+
->assertStatus(Response::HTTP_BAD_REQUEST)
160+
->assertJsonPath('errors.0.code', 'DisplayException')
161+
->assertJsonPath('errors.0.detail', 'Cannot delete the primary allocation for a server.');
162+
163+
$this->actingAs($user)->deleteJson($this->link($allocation2))
164+
->assertStatus(Response::HTTP_NO_CONTENT);
165+
166+
$server = $server->refresh();
167+
$allocation2 = $allocation2->refresh();
168+
169+
$this->assertSame($allocation->id, $server->allocation_id);
170+
$this->assertNull($allocation2->server_id);
171+
$this->assertNull($allocation2->notes);
172+
}
173+
174+
public function testAllocationCannotBeDeletedByInvalidUser()
175+
{
176+
[$user, $server] = $this->generateTestAccount();
177+
$user2 = factory(User::class)->create();
178+
179+
$server->owner_id = $user2->id;
180+
$server->save();
181+
182+
$this->actingAs($user)->deleteJson($this->link($this->getAllocation($server)))
183+
->assertNotFound();
184+
185+
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);
186+
187+
$this->actingAs($user)->deleteJson($this->link($this->getAllocation($server)))
188+
->assertForbidden();
189+
}
190+
191+
public function updatePermissionsDataProvider()
192+
{
193+
return [[[]], [[Permission::ACTION_ALLOCATION_UPDATE]]];
194+
}
195+
196+
public function deletePermissionsDataProvider()
197+
{
198+
return [[[]], [[Permission::ACTION_ALLOCATION_DELETE]]];
199+
}
200+
201+
/**
202+
* @param \Pterodactyl\Models\Server $server
203+
* @return \Pterodactyl\Models\Allocation
204+
*/
205+
protected function getAllocation(Server $server): Allocation
206+
{
207+
return factory(Allocation::class)->create([
208+
'server_id' => $server->id,
209+
'node_id' => $server->node_id,
210+
]);
211+
}
212+
}

0 commit comments

Comments
 (0)