Add test coverage for allocation assignment endpoint
This commit is contained in:
parent
b2be067f38
commit
d493685518
6 changed files with 204 additions and 79 deletions
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Allocation;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
use Pterodactyl\Models\Permission;
|
||||
use Pterodactyl\Models\Allocation;
|
||||
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
|
||||
|
||||
class CreateNewAllocationTest extends ClientApiIntegrationTestCase
|
||||
{
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that a new allocation can be properly assigned to a server.
|
||||
*
|
||||
* @param array $permission
|
||||
* @dataProvider permissionDataProvider
|
||||
*/
|
||||
public function testNewAllocationCanBeAssignedToServer(array $permission)
|
||||
{
|
||||
/** @var \Pterodactyl\Models\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'));
|
||||
|
||||
$this->assertSame($server->id, $matched->server_id);
|
||||
$this->assertJsonTransformedWith($response->json('attributes'), $matched);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a user without the required permissions cannot create an allocation for
|
||||
* the server instance.
|
||||
*/
|
||||
public function testAllocationCannotBeCreatedIfUserDoesNotHavePermission()
|
||||
{
|
||||
/** @var \Pterodactyl\Models\Server $server */
|
||||
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_UPDATE]);
|
||||
$server->update(['allocation_limit' => 2]);
|
||||
|
||||
$this->actingAs($user)->postJson($this->link($server, "/network/allocations"))->assertForbidden();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an error is returned to the user if this feature is not enabled on the system.
|
||||
*/
|
||||
public function testAllocationCannotBeCreatedIfNotEnabled()
|
||||
{
|
||||
config()->set('pterodactyl.client_features.allocations.enabled', false);
|
||||
|
||||
/** @var \Pterodactyl\Models\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 that an allocation cannot be created if the server has reached it's allocation limit.
|
||||
*/
|
||||
public function testAllocationCannotBeCreatedIfServerIsAtLimit()
|
||||
{
|
||||
/** @var \Pterodactyl\Models\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.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function permissionDataProvider()
|
||||
{
|
||||
return [[[Permission::ACTION_ALLOCATION_CREATE]], [[]]];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Allocation;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
use Pterodactyl\Models\Permission;
|
||||
use Pterodactyl\Models\Allocation;
|
||||
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
|
||||
|
||||
class DeleteAllocationTest extends ClientApiIntegrationTestCase
|
||||
{
|
||||
/**
|
||||
* Test that an allocation is deleted from the server and the notes are properly reset
|
||||
* to an empty value on assignment.
|
||||
*
|
||||
* @param array $permission
|
||||
* @dataProvider permissionDataProvider
|
||||
*/
|
||||
public function testAllocationCanBeDeletedFromServer(array $permission)
|
||||
{
|
||||
/** @var \Pterodactyl\Models\Server $server */
|
||||
[$user, $server] = $this->generateTestAccount($permission);
|
||||
|
||||
/** @var \Pterodactyl\Models\Allocation $allocation */
|
||||
$allocation = factory(Allocation::class)->create([
|
||||
'server_id' => $server->id,
|
||||
'node_id' => $server->node_id,
|
||||
'notes' => 'hodor',
|
||||
]);
|
||||
|
||||
$this->actingAs($user)->deleteJson($this->link($allocation))->assertStatus(Response::HTTP_NO_CONTENT);
|
||||
|
||||
$this->assertDatabaseHas('allocations', ['id' => $allocation->id, 'server_id' => null, 'notes' => null]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an error is returned if the user does not have permissiont to delete an allocation.
|
||||
*/
|
||||
public function testErrorIsReturnedIfUserDoesNotHavePermission()
|
||||
{
|
||||
/** @var \Pterodactyl\Models\Server $server */
|
||||
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);
|
||||
|
||||
/** @var \Pterodactyl\Models\Allocation $allocation */
|
||||
$allocation = factory(Allocation::class)->create([
|
||||
'server_id' => $server->id,
|
||||
'node_id' => $server->node_id,
|
||||
'notes' => 'hodor',
|
||||
]);
|
||||
|
||||
$this->actingAs($user)->deleteJson($this->link($allocation))->assertForbidden();
|
||||
|
||||
$this->assertDatabaseHas('allocations', ['id' => $allocation->id, 'server_id' => $server->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an allocation is not deleted if it is currently marked as the primary allocation
|
||||
* for the server.
|
||||
*/
|
||||
public function testErrorIsReturnedIfAllocationIsPrimary()
|
||||
{
|
||||
/** @var \Pterodactyl\Models\Server $server */
|
||||
[$user, $server] = $this->generateTestAccount();
|
||||
|
||||
$this->actingAs($user)->deleteJson($this->link($server->allocation))
|
||||
->assertStatus(Response::HTTP_BAD_REQUEST)
|
||||
->assertJsonPath('errors.0.code', 'DisplayException')
|
||||
->assertJsonPath('errors.0.detail', 'You cannot delete the primary allocation for this server.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an allocation cannot be deleted if it does not belong to the server instance.
|
||||
*/
|
||||
public function testErrorIsReturnedIfAllocationDoesNotBelongToServer()
|
||||
{
|
||||
/** @var \Pterodactyl\Models\Server $server */
|
||||
[$user, $server] = $this->generateTestAccount();
|
||||
[, $server2] = $this->generateTestAccount();
|
||||
|
||||
$this->actingAs($user)->deleteJson($this->link($server2->allocation))->assertNotFound();
|
||||
$this->actingAs($user)->deleteJson($this->link($server, "/network/allocations/{$server2->allocation_id}"))->assertNotFound();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function permissionDataProvider()
|
||||
{
|
||||
return [[[Permission::ACTION_ALLOCATION_DELETE]], [[]]];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue