2020-06-27 17:35:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Api\Client;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Models\Subuser;
|
2020-10-16 04:52:26 +00:00
|
|
|
use Pterodactyl\Models\Allocation;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Pterodactyl\Models\Permission;
|
2020-06-27 17:35:02 +00:00
|
|
|
|
2020-06-27 19:04:41 +00:00
|
|
|
class ClientControllerTest extends ClientApiIntegrationTestCase
|
2020-06-27 17:35:02 +00:00
|
|
|
{
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Test that only the servers a logged-in user is assigned to are returned by the
|
2020-06-27 17:35:02 +00:00
|
|
|
* API endpoint. Obviously there are cases such as being an administrator or being
|
|
|
|
* a subuser, but for this test we just want to test a basic scenario and pretend
|
|
|
|
* subusers do not exist at all.
|
|
|
|
*/
|
|
|
|
public function testOnlyLoggedInUsersServersAreReturned()
|
|
|
|
{
|
2022-11-25 20:29:04 +00:00
|
|
|
/** @var User[] $users */
|
2021-01-23 20:09:16 +00:00
|
|
|
$users = User::factory()->times(3)->create();
|
2020-06-27 17:35:02 +00:00
|
|
|
|
2022-11-25 20:29:04 +00:00
|
|
|
/** @var Server[] $servers */
|
2020-06-27 17:35:02 +00:00
|
|
|
$servers = [
|
|
|
|
$this->createServerModel(['user_id' => $users[0]->id]),
|
|
|
|
$this->createServerModel(['user_id' => $users[1]->id]),
|
|
|
|
$this->createServerModel(['user_id' => $users[2]->id]),
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->actingAs($users[0])->getJson('/api/client');
|
|
|
|
|
|
|
|
$response->assertOk();
|
|
|
|
$response->assertJsonPath('object', 'list');
|
|
|
|
$response->assertJsonPath('data.0.object', Server::RESOURCE_NAME);
|
|
|
|
$response->assertJsonPath('data.0.attributes.identifier', $servers[0]->uuidShort);
|
|
|
|
$response->assertJsonPath('data.0.attributes.server_owner', true);
|
|
|
|
$response->assertJsonPath('meta.pagination.total', 1);
|
2020-07-10 04:32:31 +00:00
|
|
|
$response->assertJsonPath('meta.pagination.per_page', 50);
|
2020-06-27 17:35:02 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 04:52:26 +00:00
|
|
|
/**
|
|
|
|
* Test that using ?filter[*]=name|uuid returns any server matching that name or UUID
|
|
|
|
* with the search filters.
|
|
|
|
*/
|
|
|
|
public function testServersAreFilteredUsingNameAndUuidInformation()
|
|
|
|
{
|
2022-11-25 20:29:04 +00:00
|
|
|
/** @var User[] $users */
|
2021-01-23 20:09:16 +00:00
|
|
|
$users = User::factory()->times(2)->create();
|
2020-10-16 04:52:26 +00:00
|
|
|
$users[0]->update(['root_admin' => true]);
|
|
|
|
|
2022-11-25 20:29:04 +00:00
|
|
|
/** @var Server[] $servers */
|
2020-10-16 04:52:26 +00:00
|
|
|
$servers = [
|
|
|
|
$this->createServerModel(['user_id' => $users[0]->id, 'name' => 'Julia']),
|
|
|
|
$this->createServerModel(['user_id' => $users[1]->id, 'uuidShort' => '12121212', 'name' => 'Janice']),
|
|
|
|
$this->createServerModel(['user_id' => $users[1]->id, 'uuid' => '88788878-12356789', 'external_id' => 'ext123', 'name' => 'Julia']),
|
|
|
|
$this->createServerModel(['user_id' => $users[1]->id, 'uuid' => '88788878-abcdefgh', 'name' => 'Jennifer']),
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->actingAs($users[1])->getJson('/api/client?filter[*]=Julia')
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonCount(1, 'data')
|
|
|
|
->assertJsonPath('data.0.attributes.identifier', $servers[2]->uuidShort);
|
|
|
|
|
|
|
|
$this->actingAs($users[1])->getJson('/api/client?filter[*]=ext123')
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonCount(1, 'data')
|
|
|
|
->assertJsonPath('data.0.attributes.identifier', $servers[2]->uuidShort);
|
|
|
|
|
|
|
|
$this->actingAs($users[1])->getJson('/api/client?filter[*]=ext123')
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonCount(1, 'data')
|
|
|
|
->assertJsonPath('data.0.attributes.identifier', $servers[2]->uuidShort);
|
|
|
|
|
|
|
|
$this->actingAs($users[1])->getJson('/api/client?filter[*]=12121212')
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonCount(1, 'data')
|
|
|
|
->assertJsonPath('data.0.attributes.identifier', $servers[1]->uuidShort);
|
|
|
|
|
|
|
|
$this->actingAs($users[1])->getJson('/api/client?filter[*]=88788878')
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonCount(2, 'data')
|
|
|
|
->assertJsonPath('data.0.attributes.identifier', $servers[2]->uuidShort)
|
|
|
|
->assertJsonPath('data.1.attributes.identifier', $servers[3]->uuidShort);
|
|
|
|
|
|
|
|
$this->actingAs($users[1])->getJson('/api/client?filter[*]=88788878-abcd')
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonCount(1, 'data')
|
|
|
|
->assertJsonPath('data.0.attributes.identifier', $servers[3]->uuidShort);
|
|
|
|
|
|
|
|
$this->actingAs($users[0])->getJson('/api/client?filter[*]=Julia&type=admin-all')
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonCount(2, 'data')
|
|
|
|
->assertJsonPath('data.0.attributes.identifier', $servers[0]->uuidShort)
|
|
|
|
->assertJsonPath('data.1.attributes.identifier', $servers[2]->uuidShort);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that using ?filter[*]=:25565 or ?filter[*]=192.168.1.1:25565 returns only those servers
|
|
|
|
* with the same allocation for the given user.
|
|
|
|
*/
|
|
|
|
public function testServersAreFilteredUsingAllocationInformation()
|
|
|
|
{
|
2022-11-25 20:29:04 +00:00
|
|
|
/** @var User $user */
|
|
|
|
/** @var Server $server */
|
2020-10-16 04:52:26 +00:00
|
|
|
[$user, $server] = $this->generateTestAccount();
|
|
|
|
$server2 = $this->createServerModel(['user_id' => $user->id, 'node_id' => $server->node_id]);
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$allocation = Allocation::factory()->create(['node_id' => $server->node_id, 'server_id' => $server->id, 'ip' => '192.168.1.1', 'port' => 25565]);
|
|
|
|
$allocation2 = Allocation::factory()->create(['node_id' => $server->node_id, 'server_id' => $server2->id, 'ip' => '192.168.1.1', 'port' => 25570]);
|
2020-10-16 04:52:26 +00:00
|
|
|
|
|
|
|
$server->update(['allocation_id' => $allocation->id]);
|
|
|
|
$server2->update(['allocation_id' => $allocation2->id]);
|
|
|
|
|
|
|
|
$server->refresh();
|
|
|
|
$server2->refresh();
|
|
|
|
|
|
|
|
$this->actingAs($user)->getJson('/api/client?filter[*]=192.168.1.1')
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonCount(2, 'data')
|
|
|
|
->assertJsonPath('data.0.attributes.identifier', $server->uuidShort)
|
|
|
|
->assertJsonPath('data.1.attributes.identifier', $server2->uuidShort);
|
|
|
|
|
|
|
|
$this->actingAs($user)->getJson('/api/client?filter[*]=192.168.1.1:25565')
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonCount(1, 'data')
|
|
|
|
->assertJsonPath('data.0.attributes.identifier', $server->uuidShort);
|
|
|
|
|
|
|
|
$this->actingAs($user)->getJson('/api/client?filter[*]=:25570')
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonCount(1, 'data')
|
|
|
|
->assertJsonPath('data.0.attributes.identifier', $server2->uuidShort);
|
|
|
|
|
|
|
|
$this->actingAs($user)->getJson('/api/client?filter[*]=:255')
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonCount(2, 'data')
|
|
|
|
->assertJsonPath('data.0.attributes.identifier', $server->uuidShort)
|
|
|
|
->assertJsonPath('data.1.attributes.identifier', $server2->uuidShort);
|
|
|
|
}
|
|
|
|
|
2020-06-27 17:35:02 +00:00
|
|
|
/**
|
|
|
|
* Test that servers where the user is a subuser are returned by default in the API call.
|
|
|
|
*/
|
|
|
|
public function testServersUserIsASubuserOfAreReturned()
|
|
|
|
{
|
2022-11-25 20:29:04 +00:00
|
|
|
/** @var User[] $users */
|
2021-01-23 20:09:16 +00:00
|
|
|
$users = User::factory()->times(3)->create();
|
2020-06-27 17:35:02 +00:00
|
|
|
$servers = [
|
|
|
|
$this->createServerModel(['user_id' => $users[0]->id]),
|
|
|
|
$this->createServerModel(['user_id' => $users[1]->id]),
|
|
|
|
$this->createServerModel(['user_id' => $users[2]->id]),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Set user 0 as a subuser of server 1. Thus, we should get two servers
|
|
|
|
// back in the response when making the API call as user 0.
|
|
|
|
Subuser::query()->create([
|
|
|
|
'user_id' => $users[0]->id,
|
|
|
|
'server_id' => $servers[1]->id,
|
|
|
|
'permissions' => [Permission::ACTION_WEBSOCKET_CONNECT],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response = $this->actingAs($users[0])->getJson('/api/client');
|
|
|
|
|
|
|
|
$response->assertOk();
|
|
|
|
$response->assertJsonCount(2, 'data');
|
|
|
|
$response->assertJsonPath('data.0.attributes.server_owner', true);
|
|
|
|
$response->assertJsonPath('data.0.attributes.identifier', $servers[0]->uuidShort);
|
|
|
|
$response->assertJsonPath('data.1.attributes.server_owner', false);
|
|
|
|
$response->assertJsonPath('data.1.attributes.identifier', $servers[1]->uuidShort);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns only servers that the user owns, not servers they are a subuser of.
|
|
|
|
*/
|
|
|
|
public function testFilterOnlyOwnerServers()
|
|
|
|
{
|
2022-11-25 20:29:04 +00:00
|
|
|
/** @var User[] $users */
|
2021-01-23 20:09:16 +00:00
|
|
|
$users = User::factory()->times(3)->create();
|
2020-06-27 17:35:02 +00:00
|
|
|
$servers = [
|
|
|
|
$this->createServerModel(['user_id' => $users[0]->id]),
|
|
|
|
$this->createServerModel(['user_id' => $users[1]->id]),
|
|
|
|
$this->createServerModel(['user_id' => $users[2]->id]),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Set user 0 as a subuser of server 1. Thus, we should get two servers
|
|
|
|
// back in the response when making the API call as user 0.
|
|
|
|
Subuser::query()->create([
|
|
|
|
'user_id' => $users[0]->id,
|
|
|
|
'server_id' => $servers[1]->id,
|
|
|
|
'permissions' => [Permission::ACTION_WEBSOCKET_CONNECT],
|
|
|
|
]);
|
|
|
|
|
2020-07-10 04:32:31 +00:00
|
|
|
$response = $this->actingAs($users[0])->getJson('/api/client?type=owner');
|
2020-06-27 17:35:02 +00:00
|
|
|
|
|
|
|
$response->assertOk();
|
|
|
|
$response->assertJsonCount(1, 'data');
|
|
|
|
$response->assertJsonPath('data.0.attributes.server_owner', true);
|
|
|
|
$response->assertJsonPath('data.0.attributes.identifier', $servers[0]->uuidShort);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that the permissions from the Panel are returned correctly.
|
|
|
|
*/
|
|
|
|
public function testPermissionsAreReturned()
|
|
|
|
{
|
2022-11-25 20:29:04 +00:00
|
|
|
/** @var User $user */
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2020-06-27 17:35:02 +00:00
|
|
|
|
|
|
|
$this->actingAs($user)
|
|
|
|
->getJson('/api/client/permissions')
|
|
|
|
->assertOk()
|
|
|
|
->assertJson([
|
|
|
|
'object' => 'system_permissions',
|
|
|
|
'attributes' => [
|
|
|
|
'permissions' => Permission::permissions()->toArray(),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
2020-07-26 17:43:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that only servers a user can access because they are an administrator are returned. This
|
|
|
|
* will always exclude any servers they can see because they're the owner or a subuser of the server.
|
|
|
|
*/
|
|
|
|
public function testOnlyAdminLevelServersAreReturned()
|
|
|
|
{
|
2022-11-25 20:29:04 +00:00
|
|
|
/** @var User[] $users */
|
2021-01-23 20:09:16 +00:00
|
|
|
$users = User::factory()->times(4)->create();
|
2020-07-26 17:43:46 +00:00
|
|
|
$users[0]->update(['root_admin' => true]);
|
|
|
|
|
|
|
|
$servers = [
|
|
|
|
$this->createServerModel(['user_id' => $users[0]->id]),
|
|
|
|
$this->createServerModel(['user_id' => $users[1]->id]),
|
|
|
|
$this->createServerModel(['user_id' => $users[2]->id]),
|
|
|
|
$this->createServerModel(['user_id' => $users[3]->id]),
|
|
|
|
];
|
|
|
|
|
|
|
|
Subuser::query()->create([
|
|
|
|
'user_id' => $users[0]->id,
|
|
|
|
'server_id' => $servers[1]->id,
|
|
|
|
'permissions' => [Permission::ACTION_WEBSOCKET_CONNECT],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Only servers 2 & 3 (0 indexed) should be returned by the API at this point. The user making
|
2022-10-14 16:59:20 +00:00
|
|
|
// the request is the owner of server 0, and a subuser of server 1, so they should be excluded.
|
2020-07-26 17:43:46 +00:00
|
|
|
$response = $this->actingAs($users[0])->getJson('/api/client?type=admin');
|
|
|
|
|
|
|
|
$response->assertOk();
|
|
|
|
$response->assertJsonCount(2, 'data');
|
|
|
|
|
|
|
|
$response->assertJsonPath('data.0.attributes.server_owner', false);
|
|
|
|
$response->assertJsonPath('data.0.attributes.identifier', $servers[2]->uuidShort);
|
|
|
|
$response->assertJsonPath('data.1.attributes.server_owner', false);
|
|
|
|
$response->assertJsonPath('data.1.attributes.identifier', $servers[3]->uuidShort);
|
|
|
|
}
|
|
|
|
|
2020-10-16 04:32:49 +00:00
|
|
|
/**
|
|
|
|
* Test that all servers a user can access as an admin are returned if using ?filter=admin-all.
|
|
|
|
*/
|
|
|
|
public function testAllServersAreReturnedToAdmin()
|
|
|
|
{
|
2022-11-25 20:29:04 +00:00
|
|
|
/** @var User[] $users */
|
2021-01-23 20:09:16 +00:00
|
|
|
$users = User::factory()->times(4)->create();
|
2020-10-16 04:32:49 +00:00
|
|
|
$users[0]->update(['root_admin' => true]);
|
|
|
|
|
|
|
|
$servers = [
|
|
|
|
$this->createServerModel(['user_id' => $users[0]->id]),
|
|
|
|
$this->createServerModel(['user_id' => $users[1]->id]),
|
|
|
|
$this->createServerModel(['user_id' => $users[2]->id]),
|
|
|
|
$this->createServerModel(['user_id' => $users[3]->id]),
|
|
|
|
];
|
|
|
|
|
|
|
|
Subuser::query()->create([
|
|
|
|
'user_id' => $users[0]->id,
|
|
|
|
'server_id' => $servers[1]->id,
|
|
|
|
'permissions' => [Permission::ACTION_WEBSOCKET_CONNECT],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// All servers should be returned.
|
|
|
|
$response = $this->actingAs($users[0])->getJson('/api/client?type=admin-all');
|
|
|
|
|
|
|
|
$response->assertOk();
|
|
|
|
$response->assertJsonCount(4, 'data');
|
|
|
|
}
|
|
|
|
|
2020-07-26 17:43:46 +00:00
|
|
|
/**
|
|
|
|
* Test that no servers get returned if the user requests all admin level servers by using
|
2020-10-16 04:32:49 +00:00
|
|
|
* ?type=admin or ?type=admin-all in the request.
|
|
|
|
*
|
|
|
|
* @dataProvider filterTypeDataProvider
|
2020-07-26 17:43:46 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function testNoServersAreReturnedIfAdminFilterIsPassedByRegularUser(string $type)
|
2020-07-26 17:43:46 +00:00
|
|
|
{
|
2022-11-25 20:29:04 +00:00
|
|
|
/** @var User[] $users */
|
2021-01-23 20:09:16 +00:00
|
|
|
$users = User::factory()->times(3)->create();
|
2020-07-26 17:43:46 +00:00
|
|
|
|
|
|
|
$this->createServerModel(['user_id' => $users[0]->id]);
|
|
|
|
$this->createServerModel(['user_id' => $users[1]->id]);
|
|
|
|
$this->createServerModel(['user_id' => $users[2]->id]);
|
|
|
|
|
2020-10-16 04:32:49 +00:00
|
|
|
$response = $this->actingAs($users[0])->getJson('/api/client?type=' . $type);
|
2020-07-26 17:43:46 +00:00
|
|
|
|
|
|
|
$response->assertOk();
|
|
|
|
$response->assertJsonCount(0, 'data');
|
|
|
|
}
|
2020-10-16 04:32:49 +00:00
|
|
|
|
2020-11-07 17:57:53 +00:00
|
|
|
/**
|
|
|
|
* Test that a subuser without the allocation.read permission is only able to see the primary
|
|
|
|
* allocation for the server.
|
|
|
|
*/
|
|
|
|
public function testOnlyPrimaryAllocationIsReturnedToSubuser()
|
|
|
|
{
|
2022-11-25 20:29:04 +00:00
|
|
|
/** @var Server $server */
|
2020-11-07 17:57:53 +00:00
|
|
|
[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);
|
|
|
|
$server->allocation->notes = 'Test notes';
|
|
|
|
$server->allocation->save();
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
Allocation::factory()->times(2)->create([
|
2020-11-07 17:57:53 +00:00
|
|
|
'node_id' => $server->node_id,
|
|
|
|
'server_id' => $server->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$server->refresh();
|
|
|
|
$response = $this->actingAs($user)->getJson('/api/client');
|
|
|
|
|
|
|
|
$response->assertOk();
|
|
|
|
$response->assertJsonCount(1, 'data');
|
|
|
|
$response->assertJsonPath('data.0.attributes.server_owner', false);
|
|
|
|
$response->assertJsonPath('data.0.attributes.uuid', $server->uuid);
|
|
|
|
$response->assertJsonCount(1, 'data.0.attributes.relationships.allocations.data');
|
|
|
|
$response->assertJsonPath('data.0.attributes.relationships.allocations.data.0.attributes.id', $server->allocation->id);
|
|
|
|
$response->assertJsonPath('data.0.attributes.relationships.allocations.data.0.attributes.notes', null);
|
|
|
|
}
|
|
|
|
|
2023-02-23 19:30:16 +00:00
|
|
|
public static function filterTypeDataProvider(): array
|
2020-10-16 04:32:49 +00:00
|
|
|
{
|
|
|
|
return [['admin'], ['admin-all']];
|
|
|
|
}
|
2020-06-27 17:35:02 +00:00
|
|
|
}
|