Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Allocation/AllocationAuthorizationTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Allocation;34use Pterodactyl\Models\Subuser;5use Pterodactyl\Models\Allocation;6use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;78class AllocationAuthorizationTest extends ClientApiIntegrationTestCase9{10#[\PHPUnit\Framework\Attributes\DataProvider('methodDataProvider')]11public function testAccessToAServersAllocationsIsRestrictedProperly(string $method, string $endpoint)12{13// The API $user is the owner of $server1.14[$user, $server1] = $this->generateTestAccount();15// Will be a subuser of $server2.16$server2 = $this->createServerModel();17// And as no access to $server3.18$server3 = $this->createServerModel();1920// Set the API $user as a subuser of server 2, but with no permissions21// to do anything with the allocations for that server.22Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $user->id]);2324$allocation1 = Allocation::factory()->create(['server_id' => $server1->id, 'node_id' => $server1->node_id]);25$allocation2 = Allocation::factory()->create(['server_id' => $server2->id, 'node_id' => $server2->node_id]);26$allocation3 = Allocation::factory()->create(['server_id' => $server3->id, 'node_id' => $server3->node_id]);2728// This is the only valid call for this test, accessing the allocation for the same29// server that the API user is the owner of.30$response = $this->actingAs($user)->json($method, $this->link($server1, '/network/allocations/' . $allocation1->id . $endpoint));31$this->assertTrue($response->status() <= 204 || $response->status() === 400 || $response->status() === 422);3233// This request fails because the allocation is valid for that server but the user34// making the request is not authorized to perform that action.35$this->actingAs($user)->json($method, $this->link($server2, '/network/allocations/' . $allocation2->id . $endpoint))->assertForbidden();3637// Both of these should report a 404 error due to the allocations being linked to38// servers that are not the same as the server in the request, or are assigned39// to a server for which the user making the request has no access to.40$this->actingAs($user)->json($method, $this->link($server1, '/network/allocations/' . $allocation2->id . $endpoint))->assertNotFound();41$this->actingAs($user)->json($method, $this->link($server1, '/network/allocations/' . $allocation3->id . $endpoint))->assertNotFound();42$this->actingAs($user)->json($method, $this->link($server2, '/network/allocations/' . $allocation3->id . $endpoint))->assertNotFound();43$this->actingAs($user)->json($method, $this->link($server3, '/network/allocations/' . $allocation3->id . $endpoint))->assertNotFound();44}4546public static function methodDataProvider(): array47{48return [49['POST', ''],50['DELETE', ''],51['POST', '/primary'],52];53}54}555657