2017-09-27 01:49:05 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\DaemonKeys;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Tests\TestCase;
|
2017-11-05 18:38:39 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Pterodactyl\Models\Server;
|
2018-03-04 03:31:44 +00:00
|
|
|
use Pterodactyl\Models\Subuser;
|
2017-09-27 01:49:05 +00:00
|
|
|
use Pterodactyl\Models\DaemonKey;
|
|
|
|
use Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService;
|
2017-11-05 22:07:50 +00:00
|
|
|
use Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService;
|
2017-09-27 01:49:05 +00:00
|
|
|
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
2017-11-05 22:07:50 +00:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2018-03-04 03:31:44 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
|
2017-09-27 01:49:05 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
|
|
|
|
|
|
|
class DaemonKeyProviderServiceTest extends TestCase
|
|
|
|
{
|
2017-11-05 22:07:50 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $keyCreationService;
|
|
|
|
|
2017-09-27 01:49:05 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService|\Mockery\Mock
|
|
|
|
*/
|
2017-11-05 18:38:39 +00:00
|
|
|
private $keyUpdateService;
|
2017-09-27 01:49:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
|
|
|
|
*/
|
2017-11-05 18:38:39 +00:00
|
|
|
private $repository;
|
2017-09-27 01:49:05 +00:00
|
|
|
|
2018-03-04 03:31:44 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $subuserRepository;
|
|
|
|
|
2017-09-27 01:49:05 +00:00
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-09-27 01:49:05 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2017-11-18 18:35:33 +00:00
|
|
|
Carbon::setTestNow(Carbon::now());
|
2017-09-27 03:16:26 +00:00
|
|
|
|
2017-11-05 22:07:50 +00:00
|
|
|
$this->keyCreationService = m::mock(DaemonKeyCreationService::class);
|
2017-09-27 01:49:05 +00:00
|
|
|
$this->keyUpdateService = m::mock(DaemonKeyUpdateService::class);
|
|
|
|
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
|
2018-03-04 03:31:44 +00:00
|
|
|
$this->subuserRepository = m::mock(SubuserRepositoryInterface::class);
|
2017-09-27 01:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-05 18:38:39 +00:00
|
|
|
* Test that a key is returned correctly as a non-admin.
|
2017-09-27 01:49:05 +00:00
|
|
|
*/
|
|
|
|
public function testKeyIsReturned()
|
|
|
|
{
|
2017-11-05 18:38:39 +00:00
|
|
|
$server = factory(Server::class)->make();
|
2017-11-05 22:07:50 +00:00
|
|
|
$user = factory(User::class)->make();
|
2017-09-27 01:49:05 +00:00
|
|
|
$key = factory(DaemonKey::class)->make();
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('findFirstWhere')->with([
|
2017-11-05 18:38:39 +00:00
|
|
|
['user_id', '=', $user->id],
|
|
|
|
['server_id', '=', $server->id],
|
2017-09-27 01:49:05 +00:00
|
|
|
])->once()->andReturn($key);
|
|
|
|
|
2017-11-05 18:38:39 +00:00
|
|
|
$response = $this->getService()->handle($server, $user);
|
|
|
|
$this->assertNotEmpty($response);
|
|
|
|
$this->assertEquals($key->secret, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-05 22:07:50 +00:00
|
|
|
* Test that an expired key is updated and then returned.
|
2017-11-05 18:38:39 +00:00
|
|
|
*/
|
2017-11-05 22:07:50 +00:00
|
|
|
public function testExpiredKeyIsUpdated()
|
2017-11-05 18:38:39 +00:00
|
|
|
{
|
|
|
|
$server = factory(Server::class)->make();
|
2017-11-05 22:07:50 +00:00
|
|
|
$user = factory(User::class)->make(['root_admin' => 0]);
|
|
|
|
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
|
2017-11-05 18:38:39 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('findFirstWhere')->with([
|
2017-11-05 22:07:50 +00:00
|
|
|
['user_id', '=', $user->id],
|
2017-11-05 18:38:39 +00:00
|
|
|
['server_id', '=', $server->id],
|
|
|
|
])->once()->andReturn($key);
|
|
|
|
|
2017-11-05 22:07:50 +00:00
|
|
|
$this->keyUpdateService->shouldReceive('handle')->with($key->id)->once()->andReturn('abc123');
|
|
|
|
|
2017-11-05 18:38:39 +00:00
|
|
|
$response = $this->getService()->handle($server, $user);
|
2017-09-27 01:49:05 +00:00
|
|
|
$this->assertNotEmpty($response);
|
2017-11-05 22:07:50 +00:00
|
|
|
$this->assertEquals('abc123', $response);
|
2017-09-27 01:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-05 22:07:50 +00:00
|
|
|
* Test that an expired key is not updated and the expired key is returned.
|
2017-09-27 01:49:05 +00:00
|
|
|
*/
|
2017-11-05 22:07:50 +00:00
|
|
|
public function testExpiredKeyIsNotUpdated()
|
2017-09-27 01:49:05 +00:00
|
|
|
{
|
2017-11-05 18:38:39 +00:00
|
|
|
$server = factory(Server::class)->make();
|
|
|
|
$user = factory(User::class)->make(['root_admin' => 0]);
|
|
|
|
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
|
2017-09-27 01:49:05 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('findFirstWhere')->with([
|
2017-11-05 18:38:39 +00:00
|
|
|
['user_id', '=', $user->id],
|
|
|
|
['server_id', '=', $server->id],
|
2017-09-27 01:49:05 +00:00
|
|
|
])->once()->andReturn($key);
|
|
|
|
|
2017-11-05 22:07:50 +00:00
|
|
|
$response = $this->getService()->handle($server, $user, false);
|
2017-09-27 01:49:05 +00:00
|
|
|
$this->assertNotEmpty($response);
|
2017-11-05 22:07:50 +00:00
|
|
|
$this->assertEquals($key->secret, $response);
|
2017-09-27 01:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-05 22:07:50 +00:00
|
|
|
* Test that a key is created if it is missing and the user is a
|
|
|
|
* root administrator.
|
2017-09-27 01:49:05 +00:00
|
|
|
*/
|
2017-11-05 22:07:50 +00:00
|
|
|
public function testMissingKeyIsCreatedIfRootAdmin()
|
2017-09-27 01:49:05 +00:00
|
|
|
{
|
2017-11-05 18:38:39 +00:00
|
|
|
$server = factory(Server::class)->make();
|
2017-11-05 22:07:50 +00:00
|
|
|
$user = factory(User::class)->make(['root_admin' => 1]);
|
|
|
|
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('findFirstWhere')->with([
|
|
|
|
['user_id', '=', $user->id],
|
|
|
|
['server_id', '=', $server->id],
|
|
|
|
])->once()->andThrow(new RecordNotFoundException);
|
|
|
|
|
|
|
|
$this->keyCreationService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn($key->secret);
|
|
|
|
|
|
|
|
$response = $this->getService()->handle($server, $user, false);
|
|
|
|
$this->assertNotEmpty($response);
|
|
|
|
$this->assertEquals($key->secret, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a key is created if it is missing and the user is the
|
|
|
|
* server owner.
|
|
|
|
*/
|
|
|
|
public function testMissingKeyIsCreatedIfUserIsServerOwner()
|
|
|
|
{
|
2017-11-05 18:38:39 +00:00
|
|
|
$user = factory(User::class)->make(['root_admin' => 0]);
|
2017-11-05 22:07:50 +00:00
|
|
|
$server = factory(Server::class)->make(['owner_id' => $user->id]);
|
2017-11-05 18:38:39 +00:00
|
|
|
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
|
2017-09-27 01:49:05 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('findFirstWhere')->with([
|
2017-11-05 18:38:39 +00:00
|
|
|
['user_id', '=', $user->id],
|
|
|
|
['server_id', '=', $server->id],
|
2017-11-05 22:07:50 +00:00
|
|
|
])->once()->andThrow(new RecordNotFoundException);
|
|
|
|
|
|
|
|
$this->keyCreationService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn($key->secret);
|
2017-09-27 01:49:05 +00:00
|
|
|
|
2017-11-05 18:38:39 +00:00
|
|
|
$response = $this->getService()->handle($server, $user, false);
|
2017-09-27 01:49:05 +00:00
|
|
|
$this->assertNotEmpty($response);
|
|
|
|
$this->assertEquals($key->secret, $response);
|
|
|
|
}
|
2017-11-05 18:38:39 +00:00
|
|
|
|
2018-03-04 03:31:44 +00:00
|
|
|
/**
|
|
|
|
* Test that a missing key is created for a subuser.
|
|
|
|
*/
|
|
|
|
public function testMissingKeyIsCreatedForSubuser()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->make(['root_admin' => 0]);
|
|
|
|
$server = factory(Server::class)->make();
|
|
|
|
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
|
|
|
|
$subuser = factory(Subuser::class)->make(['user_id' => $user->id, 'server_id' => $server->id]);
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('findFirstWhere')->with([
|
|
|
|
['user_id', '=', $user->id],
|
|
|
|
['server_id', '=', $server->id],
|
|
|
|
])->once()->andThrow(new RecordNotFoundException);
|
|
|
|
|
|
|
|
$this->subuserRepository->shouldReceive('findFirstWhere')->once()->with([
|
|
|
|
['user_id', '=', $user->id],
|
|
|
|
['server_id', '=', $server->id],
|
|
|
|
])->andReturn($subuser);
|
|
|
|
|
|
|
|
$this->keyCreationService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn($key->secret);
|
|
|
|
|
|
|
|
$response = $this->getService()->handle($server, $user, false);
|
|
|
|
$this->assertNotEmpty($response);
|
|
|
|
$this->assertEquals($key->secret, $response);
|
|
|
|
}
|
|
|
|
|
2017-11-05 22:07:50 +00:00
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if the user should not get a key.
|
|
|
|
*
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfUserDoesNotDeserveKey()
|
|
|
|
{
|
|
|
|
$server = factory(Server::class)->make();
|
|
|
|
$user = factory(User::class)->make(['root_admin' => 0]);
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('findFirstWhere')->with([
|
|
|
|
['user_id', '=', $user->id],
|
|
|
|
['server_id', '=', $server->id],
|
|
|
|
])->once()->andThrow(new RecordNotFoundException);
|
|
|
|
|
2018-03-04 03:31:44 +00:00
|
|
|
$this->subuserRepository->shouldReceive('findFirstWhere')->once()->with([
|
|
|
|
['user_id', '=', $user->id],
|
|
|
|
['server_id', '=', $server->id],
|
|
|
|
])->andThrow(new RecordNotFoundException);
|
|
|
|
|
2017-11-05 22:07:50 +00:00
|
|
|
$this->getService()->handle($server, $user, false);
|
|
|
|
}
|
|
|
|
|
2017-11-05 18:38:39 +00:00
|
|
|
/**
|
|
|
|
* Return an instance of the service with mocked dependencies.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
|
|
|
|
*/
|
|
|
|
private function getService(): DaemonKeyProviderService
|
|
|
|
{
|
2018-03-04 03:31:44 +00:00
|
|
|
return new DaemonKeyProviderService(
|
|
|
|
$this->keyCreationService,
|
|
|
|
$this->repository,
|
|
|
|
$this->keyUpdateService,
|
|
|
|
$this->subuserRepository
|
|
|
|
);
|
2017-11-05 18:38:39 +00:00
|
|
|
}
|
2017-09-27 01:49:05 +00:00
|
|
|
}
|