More fixes for broken unit tests

This commit is contained in:
Dane Everitt 2020-10-05 21:54:29 -07:00
parent b9eb87deaa
commit 83efb2d7b6
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
10 changed files with 56 additions and 213 deletions

View file

@ -252,7 +252,7 @@ class ServersController extends Controller
*/ */
public function reinstallServer(Server $server) public function reinstallServer(Server $server)
{ {
$this->reinstallService->reinstall($server); $this->reinstallService->handle($server);
$this->alert->success(trans('admin/server.alerts.server_reinstalled'))->flash(); $this->alert->success(trans('admin/server.alerts.server_reinstalled'))->flash();
return redirect()->route('admin.servers.view.manage', $server->id); return redirect()->route('admin.servers.view.manage', $server->id);

View file

@ -82,7 +82,7 @@ class ServerManagementController extends ApplicationApiController
*/ */
public function reinstall(ServerWriteRequest $request, Server $server): Response public function reinstall(ServerWriteRequest $request, Server $server): Response
{ {
$this->reinstallServerService->reinstall($server); $this->reinstallServerService->handle($server);
return $this->returnNoContent(); return $this->returnNoContent();
} }

View file

@ -69,7 +69,7 @@ class SettingsController extends ClientApiController
*/ */
public function reinstall(ReinstallServerRequest $request, Server $server) public function reinstall(ReinstallServerRequest $request, Server $server)
{ {
$this->reinstallServerService->reinstall($server); $this->reinstallServerService->handle($server);
return new JsonResponse([], Response::HTTP_ACCEPTED); return new JsonResponse([], Response::HTTP_ACCEPTED);
} }

View file

@ -4,8 +4,6 @@ namespace Pterodactyl\Services\Servers;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Models\EggVariable; use Pterodactyl\Models\EggVariable;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class EnvironmentService class EnvironmentService
{ {
@ -14,28 +12,6 @@ class EnvironmentService
*/ */
private $additional = []; private $additional = [];
/**
* @var \Illuminate\Contracts\Config\Repository
*/
private $config;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
private $repository;
/**
* EnvironmentService constructor.
*
* @param \Illuminate\Contracts\Config\Repository $config
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
*/
public function __construct(ConfigRepository $config, ServerRepositoryInterface $repository)
{
$this->config = $config;
$this->repository = $repository;
}
/** /**
* Dynamically configure additional environment variables to be assigned * Dynamically configure additional environment variables to be assigned
* with a specific server. * with a specific server.
@ -79,7 +55,7 @@ class EnvironmentService
} }
// Process variables set in the configuration file. // Process variables set in the configuration file.
foreach ($this->config->get('pterodactyl.environment_variables', []) as $key => $object) { foreach (config('pterodactyl.environment_variables', []) as $key => $object) {
$variables->put( $variables->put(
$key, is_callable($object) ? call_user_func($object, $server) : object_get($server, $object) $key, is_callable($object) ? call_user_func($object, $server) : object_get($server, $object)
); );

View file

@ -19,26 +19,18 @@ class ReinstallServerService
*/ */
private $connection; private $connection;
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
*/
private $repository;
/** /**
* ReinstallService constructor. * ReinstallService constructor.
* *
* @param \Illuminate\Database\ConnectionInterface $connection * @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository * @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
*/ */
public function __construct( public function __construct(
ConnectionInterface $connection, ConnectionInterface $connection,
DaemonServerRepository $daemonServerRepository, DaemonServerRepository $daemonServerRepository
ServerRepository $repository
) { ) {
$this->daemonServerRepository = $daemonServerRepository; $this->daemonServerRepository = $daemonServerRepository;
$this->connection = $connection; $this->connection = $connection;
$this->repository = $repository;
} }
/** /**
@ -49,16 +41,14 @@ class ReinstallServerService
* *
* @throws \Throwable * @throws \Throwable
*/ */
public function reinstall(Server $server) public function handle(Server $server)
{ {
return $this->connection->transaction(function () use ($server) { return $this->connection->transaction(function () use ($server) {
$updated = $this->repository->update($server->id, [ $server->forceFill(['installed' => Server::STATUS_INSTALLING])->save();
'installed' => Server::STATUS_INSTALLING,
], true, true);
$this->daemonServerRepository->setServer($server)->reinstall(); $this->daemonServerRepository->setServer($server)->reinstall();
return $updated; return $server->refresh();
}); });
} }
} }

View file

@ -1,17 +1,9 @@
<?php <?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 Pterodactyl\Services\Servers; namespace Pterodactyl\Services\Servers;
use Pterodactyl\Models\Mount; use Pterodactyl\Models\Mount;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class ServerConfigurationStructureService class ServerConfigurationStructureService
{ {
@ -22,22 +14,13 @@ class ServerConfigurationStructureService
*/ */
private $environment; private $environment;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
private $repository;
/** /**
* ServerConfigurationStructureService constructor. * ServerConfigurationStructureService constructor.
* *
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
* @param \Pterodactyl\Services\Servers\EnvironmentService $environment * @param \Pterodactyl\Services\Servers\EnvironmentService $environment
*/ */
public function __construct( public function __construct(EnvironmentService $environment)
ServerRepositoryInterface $repository, {
EnvironmentService $environment
) {
$this->repository = $repository;
$this->environment = $environment; $this->environment = $environment;
} }
@ -112,8 +95,6 @@ class ServerConfigurationStructureService
* *
* @param \Pterodactyl\Models\Server $server * @param \Pterodactyl\Models\Server $server
* @return array * @return array
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
protected function returnLegacyFormat(Server $server) protected function returnLegacyFormat(Server $server)
{ {

View file

@ -6,19 +6,13 @@ use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Models\Location; use Pterodactyl\Models\Location;
use Illuminate\Contracts\Config\Repository; use Illuminate\Support\Collection;
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Services\Servers\EnvironmentService; use Pterodactyl\Services\Servers\EnvironmentService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class EnvironmentServiceTest extends TestCase class EnvironmentServiceTest extends TestCase
{ {
const CONFIG_MAPPING = 'pterodactyl.environment_variables';
/**
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
*/
private $config;
/** /**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
*/ */
@ -30,9 +24,7 @@ class EnvironmentServiceTest extends TestCase
public function setUp(): void public function setUp(): void
{ {
parent::setUp(); parent::setUp();
config()->set('pterodactyl.environment_variables', []);
$this->config = m::mock(Repository::class);
$this->repository = m::mock(ServerRepositoryInterface::class);
} }
/** /**
@ -55,15 +47,17 @@ class EnvironmentServiceTest extends TestCase
*/ */
public function testProcessShouldReturnDefaultEnvironmentVariablesForAServer() public function testProcessShouldReturnDefaultEnvironmentVariablesForAServer()
{ {
$model = $this->getServerModel(); $model = $this->getServerModel([
$this->config->shouldReceive('get')->with(self::CONFIG_MAPPING, [])->once()->andReturn([]); 'TEST_VARIABLE' => factory(EggVariable::class)->make([
$this->repository->shouldReceive('getVariablesWithValues')->with($model->id)->once()->andReturn([ 'id' => 987,
'TEST_VARIABLE' => 'Test Variable', 'env_variable' => 'TEST_VARIABLE',
'default_value' => 'Test Variable',
]),
]); ]);
$response = $this->getService()->handle($model); $response = $this->getService()->handle($model);
$this->assertNotEmpty($response); $this->assertNotEmpty($response);
$this->assertEquals(4, count($response)); $this->assertCount(4, $response);
$this->assertArrayHasKey('TEST_VARIABLE', $response); $this->assertArrayHasKey('TEST_VARIABLE', $response);
$this->assertSame('Test Variable', $response['TEST_VARIABLE']); $this->assertSame('Test Variable', $response['TEST_VARIABLE']);
} }
@ -73,10 +67,7 @@ class EnvironmentServiceTest extends TestCase
*/ */
public function testProcessShouldReturnKeySetAtRuntime() public function testProcessShouldReturnKeySetAtRuntime()
{ {
$model = $this->getServerModel(); $model = $this->getServerModel([]);
$this->config->shouldReceive('get')->with(self::CONFIG_MAPPING, [])->once()->andReturn([]);
$this->repository->shouldReceive('getVariablesWithValues')->with($model->id)->once()->andReturn([]);
$service = $this->getService(); $service = $this->getService();
$service->setEnvironmentKey('TEST_VARIABLE', function ($server) { $service->setEnvironmentKey('TEST_VARIABLE', function ($server) {
return $server->uuidShort; return $server->uuidShort;
@ -94,12 +85,11 @@ class EnvironmentServiceTest extends TestCase
*/ */
public function testProcessShouldAllowOverwritingVariablesWithConfigurationFile() public function testProcessShouldAllowOverwritingVariablesWithConfigurationFile()
{ {
$model = $this->getServerModel(); config()->set('pterodactyl.environment_variables', [
$this->repository->shouldReceive('getVariablesWithValues')->with($model->id)->once()->andReturn([]);
$this->config->shouldReceive('get')->with(self::CONFIG_MAPPING, [])->once()->andReturn([
'P_SERVER_UUID' => 'name', 'P_SERVER_UUID' => 'name',
]); ]);
$model = $this->getServerModel([]);
$response = $this->getService()->handle($model); $response = $this->getService()->handle($model);
$this->assertNotEmpty($response); $this->assertNotEmpty($response);
@ -113,14 +103,13 @@ class EnvironmentServiceTest extends TestCase
*/ */
public function testVariablesSetInConfigurationAllowForClosures() public function testVariablesSetInConfigurationAllowForClosures()
{ {
$model = $this->getServerModel(); config()->set('pterodactyl.environment_variables', [
$this->config->shouldReceive('get')->with(self::CONFIG_MAPPING, [])->once()->andReturn([
'P_SERVER_UUID' => function ($server) { 'P_SERVER_UUID' => function ($server) {
return $server->id * 2; return $server->id * 2;
}, },
]); ]);
$this->repository->shouldReceive('getVariablesWithValues')->with($model->id)->once()->andReturn([]);
$model = $this->getServerModel([]);
$response = $this->getService()->handle($model); $response = $this->getService()->handle($model);
$this->assertNotEmpty($response); $this->assertNotEmpty($response);
@ -135,12 +124,11 @@ class EnvironmentServiceTest extends TestCase
*/ */
public function testProcessShouldAllowOverwritingDefaultVariablesWithRuntimeProvided() public function testProcessShouldAllowOverwritingDefaultVariablesWithRuntimeProvided()
{ {
$model = $this->getServerModel(); config()->set('pterodactyl.environment_variables', [
$this->config->shouldReceive('get')->with(self::CONFIG_MAPPING, [])->once()->andReturn([
'P_SERVER_UUID' => 'overwritten-config', 'P_SERVER_UUID' => 'overwritten-config',
]); ]);
$this->repository->shouldReceive('getVariablesWithValues')->with($model->id)->once()->andReturn([]);
$model = $this->getServerModel([]);
$service = $this->getService(); $service = $this->getService();
$service->setEnvironmentKey('P_SERVER_UUID', function ($model) { $service->setEnvironmentKey('P_SERVER_UUID', function ($model) {
return 'overwritten'; return 'overwritten';
@ -161,18 +149,25 @@ class EnvironmentServiceTest extends TestCase
*/ */
private function getService(): EnvironmentService private function getService(): EnvironmentService
{ {
return new EnvironmentService($this->config, $this->repository); return new EnvironmentService;
} }
/** /**
* Return a server model with a location relationship to be used in the tests. * Return a server model with a location relationship to be used in the tests.
* *
* @param array $variables
* @return \Pterodactyl\Models\Server * @return \Pterodactyl\Models\Server
*/ */
private function getServerModel(): Server private function getServerModel(array $variables): Server
{ {
return factory(Server::class)->make([ /** @var \Pterodactyl\Models\Server $server */
$server = factory(Server::class)->make([
'id' => 123,
'location' => factory(Location::class)->make(), 'location' => factory(Location::class)->make(),
]); ]);
$server->setRelation('variables', Collection::make($variables));
return $server;
} }
} }

View file

@ -1,82 +0,0 @@
<?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\Servers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Server;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Services\Servers\ReinstallServerService;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
class ReinstallServerServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
*/
private $daemonServerRepository;
/**
* @var \Illuminate\Database\ConnectionInterface
*/
private $connection;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
private $repository;
/**
* Setup tests.
*/
public function setUp(): void
{
parent::setUp();
$this->repository = m::mock(ServerRepository::class);
$this->connection = m::mock(ConnectionInterface::class);
$this->daemonServerRepository = m::mock(DaemonServerRepository::class);
}
/**
* Test that a server is reinstalled when it's model is passed to the function.
*/
public function testServerShouldBeReinstalledWhenModelIsPassed()
{
/** @var \Pterodactyl\Models\Server $server */
$server = factory(Server::class)->make(['id' => 123]);
$updated = clone $server;
$updated->installed = Server::STATUS_INSTALLING;
$this->connection->expects('transaction')->with(m::on(function ($closure) use ($updated) {
return $closure() instanceof Server;
}))->andReturn($updated);
$this->repository->expects('update')->with($server->id, [
'installed' => Server::STATUS_INSTALLING,
])->andReturns($updated);
$this->daemonServerRepository->expects('setServer')->with($server)->andReturnSelf();
$this->daemonServerRepository->expects('reinstall')->withNoArgs();
$this->assertSame($updated, $this->getService()->reinstall($server));
}
/**
* @return \Pterodactyl\Services\Servers\ReinstallServerService
*/
private function getService()
{
return new ReinstallServerService(
$this->connection, $this->daemonServerRepository, $this->repository
);
}
}

View file

@ -57,8 +57,8 @@ class ServerConfigurationStructureServiceTest extends TestCase
$this->assertArrayHasKey('suspended', $response); $this->assertArrayHasKey('suspended', $response);
$this->assertArrayHasKey('environment', $response); $this->assertArrayHasKey('environment', $response);
$this->assertArrayHasKey('invocation', $response); $this->assertArrayHasKey('invocation', $response);
$this->assertArrayHasKey('skip_egg_scripts', $response);
$this->assertArrayHasKey('build', $response); $this->assertArrayHasKey('build', $response);
$this->assertArrayHasKey('service', $response);
$this->assertArrayHasKey('container', $response); $this->assertArrayHasKey('container', $response);
$this->assertArrayHasKey('allocations', $response); $this->assertArrayHasKey('allocations', $response);
@ -79,11 +79,6 @@ class ServerConfigurationStructureServiceTest extends TestCase
'disk_space' => $model->disk, 'disk_space' => $model->disk,
], $response['build']); ], $response['build']);
$this->assertSame([
'egg' => $model->egg->uuid,
'skip_scripts' => $model->skip_scripts,
], $response['service']);
$this->assertSame([ $this->assertSame([
'image' => $model->image, 'image' => $model->image,
'oom_disabled' => $model->oom_disabled, 'oom_disabled' => $model->oom_disabled,
@ -103,6 +98,6 @@ class ServerConfigurationStructureServiceTest extends TestCase
*/ */
private function getService(): ServerConfigurationStructureService private function getService(): ServerConfigurationStructureService
{ {
return new ServerConfigurationStructureService($this->repository, $this->environment); return new ServerConfigurationStructureService($this->environment);
} }
} }

View file

@ -4,9 +4,7 @@ namespace Tests\Unit\Services\Servers;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Illuminate\Support\Collection;
use Pterodactyl\Models\Allocation; use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\EggVariable; use Pterodactyl\Models\EggVariable;
use Pterodactyl\Services\Servers\StartupCommandService; use Pterodactyl\Services\Servers\StartupCommandService;
@ -34,43 +32,33 @@ class StartupCommandViewServiceTest extends TestCase
*/ */
public function testServiceResponse() public function testServiceResponse()
{ {
$allocation = factory(Allocation::class)->make();
$egg = factory(Egg::class)->make();
$server = factory(Server::class)->make([ $server = factory(Server::class)->make([
'id' => 123,
'startup' => 'example {{SERVER_MEMORY}} {{SERVER_IP}} {{SERVER_PORT}} {{TEST_VARIABLE}} {{TEST_VARIABLE_HIDDEN}} {{UNKNOWN}}', 'startup' => 'example {{SERVER_MEMORY}} {{SERVER_IP}} {{SERVER_PORT}} {{TEST_VARIABLE}} {{TEST_VARIABLE_HIDDEN}} {{UNKNOWN}}',
]); ]);
$variables = collect([ $variables = collect([
factory(EggVariable::class)->make(['env_variable' => 'TEST_VARIABLE', 'user_viewable' => 1]), factory(EggVariable::class)->make([
factory(EggVariable::class)->make(['env_variable' => 'TEST_VARIABLE_HIDDEN', 'user_viewable' => 0]), 'env_variable' => 'TEST_VARIABLE',
'server_value' => 'Test Value',
'user_viewable' => 1,
]),
factory(EggVariable::class)->make([
'env_variable' => 'TEST_VARIABLE_HIDDEN',
'server_value' => 'Hidden Value',
'user_viewable' => 0,
]),
]); ]);
$egg->setRelation('variables', $variables); $server->setRelation('variables', $variables);
$server->setRelation('allocation', $allocation); $server->setRelation('allocation', $allocation = factory(Allocation::class)->make());
$server->setRelation('egg', $egg);
$this->repository->shouldReceive('getVariablesWithValues')->once()->with($server->id, true)->andReturn((object) [
'data' => [
'TEST_VARIABLE' => 'Test Value',
'TEST_VARIABLE_HIDDEN' => 'Hidden Value',
],
'server' => $server,
]);
$this->repository->shouldReceive('getPrimaryAllocation')->once()->with($server)->andReturn($server);
$response = $this->getService()->handle($server->id);
$this->assertInstanceOf(Collection::class, $response);
$response = $this->getService()->handle($server);
$this->assertSame( $this->assertSame(
sprintf('example %s %s %s %s %s {{UNKNOWN}}', $server->memory, $allocation->ip, $allocation->port, 'Test Value', '[hidden]'), sprintf('example %s %s %s %s %s {{UNKNOWN}}', $server->memory, $allocation->ip, $allocation->port, 'Test Value', '[hidden]'),
$response->get('startup') $response
); );
$this->assertEquals($variables->only(0), $response->get('variables'));
$this->assertSame([
'TEST_VARIABLE' => 'Test Value',
'TEST_VARIABLE_HIDDEN' => 'Hidden Value',
], $response->get('server_values'));
} }
/** /**
@ -80,6 +68,6 @@ class StartupCommandViewServiceTest extends TestCase
*/ */
private function getService(): StartupCommandService private function getService(): StartupCommandService
{ {
return new StartupCommandService($this->repository); return new StartupCommandService;
} }
} }