2017-09-27 03:54:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Servers;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
2018-01-31 02:36:59 +00:00
|
|
|
use Pterodactyl\Models\Egg;
|
2017-10-27 04:49:54 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2018-01-06 00:27:47 +00:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
2017-09-27 03:54:34 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
use Pterodactyl\Services\Servers\EnvironmentService;
|
|
|
|
use Pterodactyl\Services\Servers\VariableValidatorService;
|
2018-01-31 02:36:59 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
2017-09-27 03:54:34 +00:00
|
|
|
use Pterodactyl\Services\Servers\StartupModificationService;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepository;
|
|
|
|
|
|
|
|
class StartupModificationServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $daemonServerRepository;
|
2017-09-27 03:54:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $connection;
|
2017-09-27 03:54:34 +00:00
|
|
|
|
2018-01-31 02:36:59 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $eggRepository;
|
|
|
|
|
2017-09-27 03:54:34 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\EnvironmentService|\Mockery\Mock
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $environmentService;
|
2017-09-27 03:54:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $repository;
|
2017-09-27 03:54:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface|\Mockery\Mock
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $serverVariableRepository;
|
2017-09-27 03:54:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\VariableValidatorService|\Mockery\Mock
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $validatorService;
|
2017-09-27 03:54:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->daemonServerRepository = m::mock(DaemonServerRepository::class);
|
|
|
|
$this->connection = m::mock(ConnectionInterface::class);
|
2018-01-31 02:36:59 +00:00
|
|
|
$this->eggRepository = m::mock(EggRepositoryInterface::class);
|
2017-09-27 03:54:34 +00:00
|
|
|
$this->environmentService = m::mock(EnvironmentService::class);
|
|
|
|
$this->repository = m::mock(ServerRepositoryInterface::class);
|
|
|
|
$this->serverVariableRepository = m::mock(ServerVariableRepositoryInterface::class);
|
|
|
|
$this->validatorService = m::mock(VariableValidatorService::class);
|
2017-10-27 04:49:54 +00:00
|
|
|
}
|
2017-09-27 03:54:34 +00:00
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
/**
|
|
|
|
* Test startup modification as a non-admin user.
|
|
|
|
*/
|
|
|
|
public function testStartupModifiedAsNormalUser()
|
|
|
|
{
|
|
|
|
$model = factory(Server::class)->make();
|
|
|
|
|
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->validatorService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_USER)->once()->andReturnNull();
|
|
|
|
$this->validatorService->shouldReceive('handle')->with(123, ['test' => 'abcd1234'])->once()->andReturn(
|
|
|
|
collect([(object) ['id' => 1, 'value' => 'stored-value']])
|
2017-09-27 03:54:34 +00:00
|
|
|
);
|
2017-10-27 04:49:54 +00:00
|
|
|
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->serverVariableRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->serverVariableRepository->shouldReceive('updateOrCreate')->with([
|
|
|
|
'server_id' => $model->id,
|
|
|
|
'variable_id' => 1,
|
|
|
|
], ['variable_value' => 'stored-value'])->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->environmentService->shouldReceive('handle')->with($model)->once()->andReturn(['env']);
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf();
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('update')->with([
|
|
|
|
'build' => ['env|overwrite' => ['env']],
|
2018-01-06 00:27:47 +00:00
|
|
|
])->once()->andReturn(new Response);
|
2017-10-27 04:49:54 +00:00
|
|
|
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
2018-01-31 02:36:59 +00:00
|
|
|
$response = $this->getService()->handle($model, ['egg_id' => 123, 'environment' => ['test' => 'abcd1234']]);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Server::class, $response);
|
|
|
|
$this->assertSame($model, $response);
|
2017-09-27 03:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-27 04:49:54 +00:00
|
|
|
* Test startup modification as an admin user.
|
2017-09-27 03:54:34 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
public function testStartupModificationAsAdminUser()
|
2017-09-27 03:54:34 +00:00
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
$model = factory(Server::class)->make([
|
|
|
|
'egg_id' => 123,
|
2017-11-11 21:07:01 +00:00
|
|
|
'image' => 'docker:image',
|
2017-10-27 04:49:54 +00:00
|
|
|
]);
|
|
|
|
|
2018-01-31 02:36:59 +00:00
|
|
|
$eggModel = factory(Egg::class)->make([
|
|
|
|
'id' => 456,
|
|
|
|
'nest_id' => 12345,
|
|
|
|
]);
|
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->validatorService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_ADMIN)->once()->andReturnNull();
|
|
|
|
$this->validatorService->shouldReceive('handle')->with(456, ['test' => 'abcd1234'])->once()->andReturn(
|
|
|
|
collect([(object) ['id' => 1, 'value' => 'stored-value']])
|
|
|
|
);
|
|
|
|
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->serverVariableRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->serverVariableRepository->shouldReceive('updateOrCreate')->with([
|
|
|
|
'server_id' => $model->id,
|
|
|
|
'variable_id' => 1,
|
|
|
|
], ['variable_value' => 'stored-value'])->once()->andReturnNull();
|
|
|
|
|
2018-01-31 02:36:59 +00:00
|
|
|
$this->eggRepository->shouldReceive('setColumns->find')->once()->with($eggModel->id)->andReturn($eggModel);
|
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->repository->shouldReceive('update')->with($model->id, m::subset([
|
|
|
|
'installed' => 0,
|
2018-01-31 02:36:59 +00:00
|
|
|
'nest_id' => $eggModel->nest_id,
|
|
|
|
'egg_id' => $eggModel->id,
|
2017-10-27 04:49:54 +00:00
|
|
|
'pack_id' => 789,
|
2017-11-11 21:07:01 +00:00
|
|
|
'image' => 'docker:image',
|
2017-10-27 04:49:54 +00:00
|
|
|
]))->once()->andReturn($model);
|
2017-11-11 21:07:01 +00:00
|
|
|
$this->repository->shouldReceive('getDaemonServiceData')->with($model, true)->once()->andReturn([
|
|
|
|
'egg' => 'abcd1234',
|
|
|
|
'pack' => 'xyz987',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->environmentService->shouldReceive('handle')->with($model)->once()->andReturn(['env']);
|
2017-09-27 03:54:34 +00:00
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf();
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('update')->with([
|
|
|
|
'build' => [
|
|
|
|
'env|overwrite' => ['env'],
|
2017-11-11 21:07:01 +00:00
|
|
|
'image' => $model->image,
|
2017-10-27 04:49:54 +00:00
|
|
|
],
|
|
|
|
'service' => [
|
2017-11-11 21:07:01 +00:00
|
|
|
'egg' => 'abcd1234',
|
|
|
|
'pack' => 'xyz987',
|
2017-10-27 04:49:54 +00:00
|
|
|
'skip_scripts' => false,
|
|
|
|
],
|
2018-01-06 00:27:47 +00:00
|
|
|
])->once()->andReturn(new Response);
|
2017-10-27 04:49:54 +00:00
|
|
|
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
$service = $this->getService();
|
|
|
|
$service->setUserLevel(User::USER_LEVEL_ADMIN);
|
2018-01-31 02:36:59 +00:00
|
|
|
$response = $service->handle($model, [
|
|
|
|
'docker_image' => 'docker:image',
|
|
|
|
'egg_id' => $eggModel->id,
|
|
|
|
'pack_id' => 789,
|
|
|
|
'environment' => ['test' => 'abcd1234'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Server::class, $response);
|
|
|
|
$this->assertSame($model, $response);
|
2017-09-27 03:54:34 +00:00
|
|
|
}
|
2017-10-27 04:49:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the service with mocked dependencies.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Servers\StartupModificationService
|
|
|
|
*/
|
|
|
|
private function getService(): StartupModificationService
|
|
|
|
{
|
|
|
|
return new StartupModificationService(
|
|
|
|
$this->connection,
|
|
|
|
$this->daemonServerRepository,
|
2018-01-31 02:36:59 +00:00
|
|
|
$this->eggRepository,
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->environmentService,
|
|
|
|
$this->repository,
|
|
|
|
$this->serverVariableRepository,
|
|
|
|
$this->validatorService
|
|
|
|
);
|
|
|
|
}
|
2017-09-27 03:54:34 +00:00
|
|
|
}
|