Implement fix to allow root admins to view all servers.

closes #722
This commit is contained in:
Dane Everitt 2017-11-05 12:38:39 -06:00
parent fb2909a1c7
commit 6409fffdad
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
22 changed files with 143 additions and 166 deletions

View file

@ -12,6 +12,8 @@ namespace Tests\Unit\Services\DaemonKeys;
use Mockery as m;
use Carbon\Carbon;
use Tests\TestCase;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\DaemonKey;
use Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService;
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
@ -19,25 +21,15 @@ use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
class DaemonKeyProviderServiceTest extends TestCase
{
/**
* @var \Carbon\Carbon|\Mockery\Mock
*/
protected $carbon;
/**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService|\Mockery\Mock
*/
protected $keyUpdateService;
private $keyUpdateService;
/**
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
*/
protected $service;
private $repository;
/**
* Setup tests.
@ -45,29 +37,46 @@ class DaemonKeyProviderServiceTest extends TestCase
public function setUp()
{
parent::setUp();
$this->carbon = new Carbon();
$this->carbon->setTestNow();
Carbon::setTestNow();
$this->keyUpdateService = m::mock(DaemonKeyUpdateService::class);
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
$this->service = new DaemonKeyProviderService($this->carbon, $this->keyUpdateService, $this->repository);
}
/**
* Test that a key is returned.
* Test that a key is returned correctly as a non-admin.
*/
public function testKeyIsReturned()
{
$server = factory(Server::class)->make();
$user = factory(User::class)->make(['root_admin' => 0]);
$key = factory(DaemonKey::class)->make();
$this->repository->shouldReceive('findFirstWhere')->with([
['user_id', '=', $key->user_id],
['server_id', '=', $key->server_id],
['user_id', '=', $user->id],
['server_id', '=', $server->id],
])->once()->andReturn($key);
$response = $this->service->handle($key->server_id, $key->user_id);
$response = $this->getService()->handle($server, $user);
$this->assertNotEmpty($response);
$this->assertEquals($key->secret, $response);
}
/**
* Test that an admin user gets the server owner's key as the response.
*/
public function testServerOwnerKeyIsReturnedIfUserIsAdministrator()
{
$server = factory(Server::class)->make();
$user = factory(User::class)->make(['root_admin' => 1]);
$key = factory(DaemonKey::class)->make();
$this->repository->shouldReceive('findFirstWhere')->with([
['user_id', '=', $server->owner_id],
['server_id', '=', $server->id],
])->once()->andReturn($key);
$response = $this->getService()->handle($server, $user);
$this->assertNotEmpty($response);
$this->assertEquals($key->secret, $response);
}
@ -77,20 +86,20 @@ class DaemonKeyProviderServiceTest extends TestCase
*/
public function testExpiredKeyIsUpdated()
{
$key = factory(DaemonKey::class)->make([
'expires_at' => $this->carbon->subHour(),
]);
$server = factory(Server::class)->make();
$user = factory(User::class)->make(['root_admin' => 0]);
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
$this->repository->shouldReceive('findFirstWhere')->with([
['user_id', '=', $key->user_id],
['server_id', '=', $key->server_id],
['user_id', '=', $user->id],
['server_id', '=', $server->id],
])->once()->andReturn($key);
$this->keyUpdateService->shouldReceive('handle')->with($key->id)->once()->andReturn(true);
$this->keyUpdateService->shouldReceive('handle')->with($key->id)->once()->andReturn('abc123');
$response = $this->service->handle($key->server_id, $key->user_id);
$response = $this->getService()->handle($server, $user);
$this->assertNotEmpty($response);
$this->assertTrue($response);
$this->assertEquals('abc123', $response);
}
/**
@ -98,17 +107,27 @@ class DaemonKeyProviderServiceTest extends TestCase
*/
public function testExpiredKeyIsNotUpdated()
{
$key = factory(DaemonKey::class)->make([
'expires_at' => $this->carbon->subHour(),
]);
$server = factory(Server::class)->make();
$user = factory(User::class)->make(['root_admin' => 0]);
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
$this->repository->shouldReceive('findFirstWhere')->with([
['user_id', '=', $key->user_id],
['server_id', '=', $key->server_id],
['user_id', '=', $user->id],
['server_id', '=', $server->id],
])->once()->andReturn($key);
$response = $this->service->handle($key->server_id, $key->user_id, false);
$response = $this->getService()->handle($server, $user, false);
$this->assertNotEmpty($response);
$this->assertEquals($key->secret, $response);
}
/**
* Return an instance of the service with mocked dependencies.
*
* @return \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
*/
private function getService(): DaemonKeyProviderService
{
return new DaemonKeyProviderService($this->keyUpdateService, $this->repository);
}
}

View file

@ -55,7 +55,7 @@ class AuthenticateUsingPasswordServiceTest extends TestCase
$this->repository->shouldReceive('withColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
$this->repository->shouldReceive('getByUuid')->with($server->uuidShort)->once()->andReturn($server);
$this->keyProviderService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn('server_token');
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('server_token');
$response = $this->getService()->handle($user->username, 'password', 1, $server->uuidShort);
$this->assertNotEmpty($response);
@ -80,7 +80,7 @@ class AuthenticateUsingPasswordServiceTest extends TestCase
$this->repository->shouldReceive('withColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
$this->repository->shouldReceive('getByUuid')->with($server->uuidShort)->once()->andReturn($server);
$this->keyProviderService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn('server_token');
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('server_token');
$response = $this->getService()->handle($user->username, 'password', 1, $server->uuidShort);
$this->assertNotEmpty($response);

View file

@ -11,6 +11,7 @@ namespace Tests\Unit\Services\Subusers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser;
use Tests\Traits\MocksRequestException;
@ -81,13 +82,14 @@ class SubuserUpdateServiceTest extends TestCase
{
$subuser = factory(Subuser::class)->make();
$subuser->setRelation('server', factory(Server::class)->make());
$subuser->setRelation('user', factory(User::class)->make());
$this->repository->shouldReceive('getWithServer')->with($subuser)->once()->andReturn($subuser);
$this->repository->shouldReceive('loadServerAndUserRelations')->with($subuser)->once()->andReturn($subuser);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturnNull();
$this->permissionService->shouldReceive('handle')->with($subuser->id, ['some-permission'])->once()->andReturnNull();
$this->keyProviderService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id, false)->once()->andReturn('test123');
$this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturnNull();
@ -106,13 +108,14 @@ class SubuserUpdateServiceTest extends TestCase
$subuser = factory(Subuser::class)->make();
$subuser->setRelation('server', factory(Server::class)->make());
$subuser->setRelation('user', factory(User::class)->make());
$this->repository->shouldReceive('getWithServer')->with($subuser)->once()->andReturn($subuser);
$this->repository->shouldReceive('loadServerAndUserRelations')->with($subuser)->once()->andReturn($subuser);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturnNull();
$this->permissionService->shouldReceive('handle')->with($subuser->id, [])->once()->andReturnNull();
$this->keyProviderService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id, false)->once()->andReturn('test123');
$this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andThrow($this->getExceptionMock());
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();