2020-10-31 21:57:27 -07:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
namespace Pterodactyl\Tests\Pest\Integration\Api\Client\Server\Allocation\CreateNewAllocationTest ;
2020-10-31 21:57:27 -07:00
use Illuminate\Http\Response ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Enum\Permissions ;
2020-10-31 21:57:27 -07:00
use Pterodactyl\Models\Allocation ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Server ;
2020-10-31 21:57:27 -07:00
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase ;
2026-10-05 18:10:18 -04:00
uses ( ClientApiIntegrationTestCase :: class );
/**
* Setup tests.
*/
beforeEach ( function () : void {
config () -> set ( 'pterodactyl.client_features.allocations.enabled' , true );
config () -> set ( 'pterodactyl.client_features.allocations.range_start' , 5000 );
config () -> set ( 'pterodactyl.client_features.allocations.range_end' , 5050 );
});
dataset ( 'permissionDataProvider' , fn () : array => [[[ Permissions :: AllocationCreate -> value ]], [[]]]);
test ( 'new allocation can be assigned to server' , function ( array $permission ) : void {
/** @var Server $server */
[ $user , $server ] = $this -> generateTestAccount ( $permission );
$server -> update ([ 'allocation_limit' => 2 ]);
$response = $this -> actingAs ( $user ) -> postJson ( $this -> link ( $server , '/network/allocations' ));
$response -> assertJsonPath ( 'object' , Allocation :: RESOURCE_NAME );
$matched = Allocation :: query () -> findOrFail ( $response -> json ( 'attributes.id' ));
expect ( $matched -> server_id ) -> toBe ( $server -> id );
expect ( $response -> json ( 'attributes' )) -> toBe ([
'id' => $matched -> id ,
'ip' => $matched -> ip ,
'ip_alias' => $matched -> ip_alias ,
'port' => $matched -> port ,
'notes' => null ,
'is_default' => false ,
]);
}) -> with ( 'permissionDataProvider' );
test ( 'allocation cannot be created if user does not have permission' , function () : void {
/** @var Server $server */
[ $user , $server ] = $this -> generateTestAccount ([ Permissions :: AllocationUpdate -> value ]);
$server -> update ([ 'allocation_limit' => 2 ]);
$this -> actingAs ( $user ) -> postJson ( $this -> link ( $server , '/network/allocations' )) -> assertForbidden ();
});
test ( 'allocation cannot be created if not enabled' , function () : void {
config () -> set ( 'pterodactyl.client_features.allocations.enabled' , false );
/** @var Server $server */
[ $user , $server ] = $this -> generateTestAccount ();
$server -> update ([ 'allocation_limit' => 2 ]);
$this -> actingAs ( $user ) -> postJson ( $this -> link ( $server , '/network/allocations' )) -> assertStatus ( Response :: HTTP_BAD_REQUEST ) -> assertJsonPath ( 'errors.0.code' , 'AutoAllocationNotEnabledException' ) -> assertJsonPath ( 'errors.0.detail' , 'Server auto-allocation is not enabled for this instance.' );
});
test ( 'allocation cannot be created if server is at limit' , function () : void {
/** @var Server $server */
[ $user , $server ] = $this -> generateTestAccount ();
$server -> update ([ 'allocation_limit' => 1 ]);
$this -> actingAs ( $user ) -> postJson ( $this -> link ( $server , '/network/allocations' )) -> assertStatus ( Response :: HTTP_BAD_REQUEST ) -> assertJsonPath ( 'errors.0.code' , 'DisplayException' ) -> assertJsonPath ( 'errors.0.detail' , 'Cannot assign additional allocations to this server: limit has been reached.' );
});