Path: blob/1.0-develop/tests/Integration/Api/Client/Server/NetworkAllocationControllerTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server;34use Pterodactyl\Models\User;5use Illuminate\Http\Response;6use Pterodactyl\Models\Allocation;7use Pterodactyl\Models\Permission;8use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;910class NetworkAllocationControllerTest extends ClientApiIntegrationTestCase11{12/**13* Test that a servers allocations are returned in the expected format.14*/15public function testServerAllocationsAreReturned()16{17[$user, $server] = $this->generateTestAccount();1819$response = $this->actingAs($user)->getJson($this->link($server, '/network/allocations'));2021$response->assertOk();22$response->assertJsonPath('object', 'list');23$response->assertJsonCount(1, 'data');2425$this->assertJsonTransformedWith($response->json('data.0.attributes'), $server->allocation);26}2728/**29* Test that allocations cannot be returned without the required user permissions.30*/31public function testServerAllocationsAreNotReturnedWithoutPermission()32{33[$user, $server] = $this->generateTestAccount();34$user2 = User::factory()->create();3536$server->owner_id = $user2->id;37$server->save();3839$this->actingAs($user)->getJson($this->link($server, '/network/allocations'))40->assertNotFound();4142[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);4344$this->actingAs($user)->getJson($this->link($server, '/network/allocations'))45->assertForbidden();46}4748/**49* Tests that notes on an allocation can be set correctly.50*/51#[\PHPUnit\Framework\Attributes\DataProvider('updatePermissionsDataProvider')]52public function testAllocationNotesCanBeUpdated(array $permissions)53{54[$user, $server] = $this->generateTestAccount($permissions);55$allocation = $server->allocation;5657$this->assertNull($allocation->notes);5859$this->actingAs($user)->postJson($this->link($allocation), [])60->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)61->assertJsonPath('errors.0.meta.rule', 'present');6263$this->actingAs($user)->postJson($this->link($allocation), ['notes' => 'Test notes'])64->assertOk()65->assertJsonPath('object', Allocation::RESOURCE_NAME)66->assertJsonPath('attributes.notes', 'Test notes');6768$allocation = $allocation->refresh();6970$this->assertSame('Test notes', $allocation->notes);7172$this->actingAs($user)->postJson($this->link($allocation), ['notes' => null])73->assertOk()74->assertJsonPath('object', Allocation::RESOURCE_NAME)75->assertJsonPath('attributes.notes', null);7677$allocation = $allocation->refresh();7879$this->assertNull($allocation->notes);80}8182public function testAllocationNotesCannotBeUpdatedByInvalidUsers()83{84[$user, $server] = $this->generateTestAccount();85$user2 = User::factory()->create();8687$server->owner_id = $user2->id;88$server->save();8990$this->actingAs($user)->postJson($this->link($server->allocation))->assertNotFound();9192[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);9394$this->actingAs($user)->postJson($this->link($server->allocation))->assertForbidden();95}9697#[\PHPUnit\Framework\Attributes\DataProvider('updatePermissionsDataProvider')]98public function testPrimaryAllocationCanBeModified(array $permissions)99{100[$user, $server] = $this->generateTestAccount($permissions);101$allocation = $server->allocation;102$allocation2 = Allocation::factory()->create(['node_id' => $server->node_id, 'server_id' => $server->id]);103104$server->allocation_id = $allocation->id;105$server->save();106107$this->actingAs($user)->postJson($this->link($allocation2, '/primary'))108->assertOk();109110$server = $server->refresh();111112$this->assertSame($allocation2->id, $server->allocation_id);113}114115public function testPrimaryAllocationCannotBeModifiedByInvalidUser()116{117[$user, $server] = $this->generateTestAccount();118$user2 = User::factory()->create();119120$server->owner_id = $user2->id;121$server->save();122123$this->actingAs($user)->postJson($this->link($server->allocation, '/primary'))124->assertNotFound();125126[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);127128$this->actingAs($user)->postJson($this->link($server->allocation, '/primary'))129->assertForbidden();130}131132public static function updatePermissionsDataProvider(): array133{134return [[[]], [[Permission::ACTION_ALLOCATION_UPDATE]]];135}136}137138139