2017-07-23 01:15:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Servers;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Models\Location;
|
2020-10-06 04:54:29 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Pterodactyl\Models\EggVariable;
|
2017-07-23 01:15:01 +00:00
|
|
|
use Pterodactyl\Services\Servers\EnvironmentService;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
|
|
|
|
|
|
|
class EnvironmentServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2017-10-27 04:49:54 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $repository;
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-07-23 01:15:01 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2020-10-06 04:54:29 +00:00
|
|
|
config()->set('pterodactyl.environment_variables', []);
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-13 15:05:52 +00:00
|
|
|
* Test that set environment key stores the key into a retrievable array.
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
public function testSettingEnvironmentKeyPersistsItInArray()
|
2017-07-23 01:15:01 +00:00
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
$service = $this->getService();
|
|
|
|
|
|
|
|
$service->setEnvironmentKey('TEST_KEY', function () {
|
2017-07-23 01:15:01 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->assertNotEmpty($service->getEnvironmentKeys());
|
|
|
|
$this->assertArrayHasKey('TEST_KEY', $service->getEnvironmentKeys());
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that environment defaults are returned by the process function.
|
|
|
|
*/
|
|
|
|
public function testProcessShouldReturnDefaultEnvironmentVariablesForAServer()
|
|
|
|
{
|
2020-10-06 04:54:29 +00:00
|
|
|
$model = $this->getServerModel([
|
|
|
|
'TEST_VARIABLE' => factory(EggVariable::class)->make([
|
|
|
|
'id' => 987,
|
|
|
|
'env_variable' => 'TEST_VARIABLE',
|
|
|
|
'default_value' => 'Test Variable',
|
|
|
|
]),
|
2017-07-23 01:15:01 +00:00
|
|
|
]);
|
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$response = $this->getService()->handle($model);
|
|
|
|
$this->assertNotEmpty($response);
|
2020-10-06 04:54:29 +00:00
|
|
|
$this->assertCount(4, $response);
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->assertArrayHasKey('TEST_VARIABLE', $response);
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->assertSame('Test Variable', $response['TEST_VARIABLE']);
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that variables included at run-time are also included.
|
|
|
|
*/
|
|
|
|
public function testProcessShouldReturnKeySetAtRuntime()
|
|
|
|
{
|
2020-10-06 04:54:29 +00:00
|
|
|
$model = $this->getServerModel([]);
|
2017-10-27 04:49:54 +00:00
|
|
|
$service = $this->getService();
|
|
|
|
$service->setEnvironmentKey('TEST_VARIABLE', function ($server) {
|
2017-07-23 01:15:01 +00:00
|
|
|
return $server->uuidShort;
|
2017-10-27 04:49:54 +00:00
|
|
|
});
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$response = $service->handle($model);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($response);
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->assertArrayHasKey('TEST_VARIABLE', $response);
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->assertSame($model->uuidShort, $response['TEST_VARIABLE']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that duplicate variables provided in config override the defaults.
|
|
|
|
*/
|
2018-05-13 15:05:52 +00:00
|
|
|
public function testProcessShouldAllowOverwritingVariablesWithConfigurationFile()
|
2017-10-27 04:49:54 +00:00
|
|
|
{
|
2020-10-06 04:54:29 +00:00
|
|
|
config()->set('pterodactyl.environment_variables', [
|
2017-10-27 04:49:54 +00:00
|
|
|
'P_SERVER_UUID' => 'name',
|
|
|
|
]);
|
|
|
|
|
2020-10-06 04:54:29 +00:00
|
|
|
$model = $this->getServerModel([]);
|
2017-10-27 04:49:54 +00:00
|
|
|
$response = $this->getService()->handle($model);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($response);
|
|
|
|
$this->assertSame(3, count($response));
|
|
|
|
$this->assertArrayHasKey('P_SERVER_UUID', $response);
|
|
|
|
$this->assertSame($model->name, $response['P_SERVER_UUID']);
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-27 04:49:54 +00:00
|
|
|
* Test that config based environment variables can be done using closures.
|
|
|
|
*/
|
|
|
|
public function testVariablesSetInConfigurationAllowForClosures()
|
|
|
|
{
|
2020-10-06 04:54:29 +00:00
|
|
|
config()->set('pterodactyl.environment_variables', [
|
2017-10-27 04:49:54 +00:00
|
|
|
'P_SERVER_UUID' => function ($server) {
|
|
|
|
return $server->id * 2;
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
2020-10-06 04:54:29 +00:00
|
|
|
$model = $this->getServerModel([]);
|
2017-10-27 04:49:54 +00:00
|
|
|
$response = $this->getService()->handle($model);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($response);
|
|
|
|
$this->assertSame(3, count($response));
|
|
|
|
$this->assertArrayHasKey('P_SERVER_UUID', $response);
|
|
|
|
$this->assertSame($model->id * 2, $response['P_SERVER_UUID']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that duplicate variables provided at run-time override the defaults and those
|
|
|
|
* that are defined in the configuration file.
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
|
|
|
public function testProcessShouldAllowOverwritingDefaultVariablesWithRuntimeProvided()
|
|
|
|
{
|
2020-10-06 04:54:29 +00:00
|
|
|
config()->set('pterodactyl.environment_variables', [
|
2017-10-27 04:49:54 +00:00
|
|
|
'P_SERVER_UUID' => 'overwritten-config',
|
|
|
|
]);
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2020-10-06 04:54:29 +00:00
|
|
|
$model = $this->getServerModel([]);
|
2017-10-27 04:49:54 +00:00
|
|
|
$service = $this->getService();
|
|
|
|
$service->setEnvironmentKey('P_SERVER_UUID', function ($model) {
|
2017-07-23 01:15:01 +00:00
|
|
|
return 'overwritten';
|
2017-10-27 04:49:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$response = $service->handle($model);
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->assertNotEmpty($response);
|
|
|
|
$this->assertSame(3, count($response));
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->assertArrayHasKey('P_SERVER_UUID', $response);
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->assertSame('overwritten', $response['P_SERVER_UUID']);
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-27 04:49:54 +00:00
|
|
|
* Return an instance of the service with mocked dependencies.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Servers\EnvironmentService
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private function getService(): EnvironmentService
|
2017-07-23 01:15:01 +00:00
|
|
|
{
|
2020-10-06 04:54:29 +00:00
|
|
|
return new EnvironmentService;
|
2017-10-27 04:49:54 +00:00
|
|
|
}
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
/**
|
|
|
|
* Return a server model with a location relationship to be used in the tests.
|
|
|
|
*
|
2020-10-06 04:54:29 +00:00
|
|
|
* @param array $variables
|
2017-10-27 04:49:54 +00:00
|
|
|
* @return \Pterodactyl\Models\Server
|
|
|
|
*/
|
2020-10-06 04:54:29 +00:00
|
|
|
private function getServerModel(array $variables): Server
|
2017-10-27 04:49:54 +00:00
|
|
|
{
|
2020-10-06 04:54:29 +00:00
|
|
|
/** @var \Pterodactyl\Models\Server $server */
|
|
|
|
$server = factory(Server::class)->make([
|
|
|
|
'id' => 123,
|
2017-10-27 04:49:54 +00:00
|
|
|
'location' => factory(Location::class)->make(),
|
|
|
|
]);
|
2020-10-06 04:54:29 +00:00
|
|
|
|
|
|
|
$server->setRelation('variables', Collection::make($variables));
|
|
|
|
|
|
|
|
return $server;
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|
|
|
|
}
|