Add test coverage for allocation assignment endpoint

This commit is contained in:
Dane Everitt 2020-10-31 21:57:27 -07:00
parent b2be067f38
commit d493685518
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
6 changed files with 204 additions and 79 deletions

View file

@ -4,7 +4,6 @@ namespace Pterodactyl\Tests\Integration\Api\Client\Server;
use Pterodactyl\Models\User;
use Illuminate\Http\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Permission;
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
@ -17,7 +16,6 @@ class NetworkAllocationControllerTest extends ClientApiIntegrationTestCase
public function testServerAllocationsAreReturned()
{
[$user, $server] = $this->generateTestAccount();
$allocation = $this->getAllocation($server);
$response = $this->actingAs($user)->getJson($this->link($server, '/network/allocations'));
@ -25,7 +23,7 @@ class NetworkAllocationControllerTest extends ClientApiIntegrationTestCase
$response->assertJsonPath('object', 'list');
$response->assertJsonCount(1, 'data');
$this->assertJsonTransformedWith($response->json('data.0.attributes'), $allocation);
$this->assertJsonTransformedWith($response->json('data.0.attributes'), $server->allocation);
}
/**
@ -57,7 +55,7 @@ class NetworkAllocationControllerTest extends ClientApiIntegrationTestCase
public function testAllocationNotesCanBeUpdated(array $permissions)
{
[$user, $server] = $this->generateTestAccount($permissions);
$allocation = $this->getAllocation($server);
$allocation = $server->allocation;
$this->assertNull($allocation->notes);
@ -92,13 +90,11 @@ class NetworkAllocationControllerTest extends ClientApiIntegrationTestCase
$server->owner_id = $user2->id;
$server->save();
$this->actingAs($user)->postJson($this->link($this->getAllocation($server)))
->assertNotFound();
$this->actingAs($user)->postJson($this->link($server->allocation))->assertNotFound();
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);
$this->actingAs($user)->postJson($this->link($this->getAllocation($server)))
->assertForbidden();
$this->actingAs($user)->postJson($this->link($server->allocation))->assertForbidden();
}
/**
@ -108,8 +104,8 @@ class NetworkAllocationControllerTest extends ClientApiIntegrationTestCase
public function testPrimaryAllocationCanBeModified(array $permissions)
{
[$user, $server] = $this->generateTestAccount($permissions);
$allocation = $this->getAllocation($server);
$allocation2 = $this->getAllocation($server);
$allocation = $server->allocation;
$allocation2 = factory(Allocation::class)->create(['node_id' => $server->node_id, 'server_id' => $server->id]);
$server->allocation_id = $allocation->id;
$server->save();
@ -130,61 +126,12 @@ class NetworkAllocationControllerTest extends ClientApiIntegrationTestCase
$server->owner_id = $user2->id;
$server->save();
$this->actingAs($user)->postJson($this->link($this->getAllocation($server), '/primary'))
$this->actingAs($user)->postJson($this->link($server->allocation, '/primary'))
->assertNotFound();
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);
$this->actingAs($user)->postJson($this->link($this->getAllocation($server), '/primary'))
->assertForbidden();
}
/**
* @param array $permissions
* @dataProvider deletePermissionsDataProvider
*/
public function testAllocationCanBeDeleted(array $permissions)
{
[$user, $server] = $this->generateTestAccount($permissions);
$allocation = $this->getAllocation($server);
$allocation2 = $this->getAllocation($server);
$allocation2->notes = 'Filled notes';
$allocation2->save();
$server->allocation_id = $allocation->id;
$server->save();
$this->actingAs($user)->deleteJson($this->link($allocation))
->assertStatus(Response::HTTP_BAD_REQUEST)
->assertJsonPath('errors.0.code', 'DisplayException')
->assertJsonPath('errors.0.detail', 'Cannot delete the primary allocation for a server.');
$this->actingAs($user)->deleteJson($this->link($allocation2))
->assertStatus(Response::HTTP_NO_CONTENT);
$server = $server->refresh();
$allocation2 = $allocation2->refresh();
$this->assertSame($allocation->id, $server->allocation_id);
$this->assertNull($allocation2->server_id);
$this->assertNull($allocation2->notes);
}
public function testAllocationCannotBeDeletedByInvalidUser()
{
[$user, $server] = $this->generateTestAccount();
$user2 = factory(User::class)->create();
$server->owner_id = $user2->id;
$server->save();
$this->actingAs($user)->deleteJson($this->link($this->getAllocation($server)))
->assertNotFound();
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);
$this->actingAs($user)->deleteJson($this->link($this->getAllocation($server)))
$this->actingAs($user)->postJson($this->link($server->allocation, '/primary'))
->assertForbidden();
}
@ -197,16 +144,4 @@ class NetworkAllocationControllerTest extends ClientApiIntegrationTestCase
{
return [[[]], [[Permission::ACTION_ALLOCATION_DELETE]]];
}
/**
* @param \Pterodactyl\Models\Server $server
* @return \Pterodactyl\Models\Allocation
*/
protected function getAllocation(Server $server): Allocation
{
return factory(Allocation::class)->create([
'server_id' => $server->id,
'node_id' => $server->node_id,
]);
}
}