2017-07-23 01:15:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Servers;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
2017-10-27 04:49:54 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Illuminate\Support\Collection;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Models\EggVariable;
|
2017-07-23 01:15:01 +00:00
|
|
|
use Illuminate\Contracts\Validation\Factory;
|
2017-11-25 19:15:46 +00:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2017-07-23 01:15:01 +00:00
|
|
|
use Pterodactyl\Services\Servers\VariableValidatorService;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
2017-07-23 01:15:01 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
|
|
|
|
|
|
|
class VariableValidatorServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2017-10-27 04:49:54 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-11-25 19:15:46 +00:00
|
|
|
private $optionVariableRepository;
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-27 04:49:54 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-11-25 19:15:46 +00:00
|
|
|
private $serverRepository;
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-27 04:49:54 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-11-25 19:15:46 +00:00
|
|
|
private $serverVariableRepository;
|
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();
|
|
|
|
|
2017-10-07 04:57:53 +00:00
|
|
|
$this->optionVariableRepository = m::mock(EggVariableRepositoryInterface::class);
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
|
|
|
|
$this->serverVariableRepository = m::mock(ServerVariableRepositoryInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that when no variables are found for an option no data is returned.
|
|
|
|
*/
|
|
|
|
public function testEmptyResultSetShouldBeReturnedIfNoVariablesAreFound()
|
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn(collect([]));
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$response = $this->getService()->handle(1, []);
|
|
|
|
$this->assertEmpty($response);
|
|
|
|
$this->assertInstanceOf(Collection::class, $response);
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that variables set as user_editable=0 and/or user_viewable=0 are skipped when admin flag is not set.
|
|
|
|
*/
|
|
|
|
public function testValidatorShouldNotProcessVariablesSetAsNotUserEditableWhenAdminFlagIsNotPassed()
|
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
$variables = $this->getVariableCollection();
|
|
|
|
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($variables);
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$response = $this->getService()->handle(1, [
|
|
|
|
$variables[0]->env_variable => 'Test_SomeValue_0',
|
|
|
|
$variables[1]->env_variable => 'Test_SomeValue_1',
|
|
|
|
$variables[2]->env_variable => 'Test_SomeValue_2',
|
|
|
|
$variables[3]->env_variable => 'Test_SomeValue_3',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($response);
|
|
|
|
$this->assertInstanceOf(Collection::class, $response);
|
|
|
|
$this->assertEquals(1, $response->count(), 'Assert response has a single item in collection.');
|
|
|
|
|
|
|
|
$variable = $response->first();
|
|
|
|
$this->assertObjectHasAttribute('id', $variable);
|
|
|
|
$this->assertObjectHasAttribute('key', $variable);
|
|
|
|
$this->assertObjectHasAttribute('value', $variable);
|
|
|
|
$this->assertSame($variables[0]->id, $variable->id);
|
|
|
|
$this->assertSame($variables[0]->env_variable, $variable->key);
|
|
|
|
$this->assertSame('Test_SomeValue_0', $variable->value);
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that all variables are processed correctly if admin flag is set.
|
|
|
|
*/
|
|
|
|
public function testValidatorShouldProcessAllVariablesWhenAdminFlagIsSet()
|
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
$variables = $this->getVariableCollection();
|
|
|
|
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($variables);
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$service = $this->getService();
|
|
|
|
$service->setUserLevel(User::USER_LEVEL_ADMIN);
|
|
|
|
$response = $service->handle(1, [
|
|
|
|
$variables[0]->env_variable => 'Test_SomeValue_0',
|
|
|
|
$variables[1]->env_variable => 'Test_SomeValue_1',
|
|
|
|
$variables[2]->env_variable => 'Test_SomeValue_2',
|
|
|
|
$variables[3]->env_variable => 'Test_SomeValue_3',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($response);
|
|
|
|
$this->assertInstanceOf(Collection::class, $response);
|
|
|
|
$this->assertEquals(4, $response->count(), 'Assert response has all four items in collection.');
|
|
|
|
|
|
|
|
$response->each(function ($variable, $key) use ($variables) {
|
|
|
|
$this->assertObjectHasAttribute('id', $variable);
|
|
|
|
$this->assertObjectHasAttribute('key', $variable);
|
|
|
|
$this->assertObjectHasAttribute('value', $variable);
|
|
|
|
$this->assertSame($variables[$key]->id, $variable->id);
|
|
|
|
$this->assertSame($variables[$key]->env_variable, $variable->key);
|
|
|
|
$this->assertSame('Test_SomeValue_' . $key, $variable->value);
|
|
|
|
});
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a DisplayValidationError is thrown when a variable is not validated.
|
|
|
|
*/
|
|
|
|
public function testValidatorShouldThrowExceptionWhenAValidationErrorIsEncountered()
|
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
$variables = $this->getVariableCollection();
|
|
|
|
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($variables);
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
try {
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->getService()->handle(1, [$variables[0]->env_variable => null]);
|
2017-11-25 19:15:46 +00:00
|
|
|
} catch (ValidationException $exception) {
|
|
|
|
$messages = $exception->validator->getMessageBag()->all();
|
|
|
|
|
|
|
|
$this->assertNotEmpty($messages);
|
2018-02-16 02:58:51 +00:00
|
|
|
$this->assertSame(2, count($messages));
|
2018-01-28 23:14:14 +00:00
|
|
|
|
2018-02-16 02:58:51 +00:00
|
|
|
// We only expect to get the first two variables form the getVariableCollection
|
|
|
|
// function here since those are the only two that are editable, and the others
|
|
|
|
// should be discarded and not validated.
|
|
|
|
for ($i = 0; $i < 2; $i++) {
|
2018-01-28 23:14:14 +00:00
|
|
|
$this->assertSame(trans('validation.required', [
|
|
|
|
'attribute' => trans('validation.internal.variable_value', ['env' => $variables[$i]->name]),
|
|
|
|
]), $messages[$i]);
|
|
|
|
}
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-27 04:49:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a collection of fake variables to use for testing.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
private function getVariableCollection(): Collection
|
|
|
|
{
|
|
|
|
return collect(
|
|
|
|
[
|
|
|
|
factory(EggVariable::class)->states('editable', 'viewable')->make(),
|
|
|
|
factory(EggVariable::class)->states('editable')->make(),
|
2018-02-16 02:58:51 +00:00
|
|
|
factory(EggVariable::class)->states('viewable')->make(),
|
2017-10-27 04:49:54 +00:00
|
|
|
factory(EggVariable::class)->make(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the service with mocked dependencies.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Servers\VariableValidatorService
|
|
|
|
*/
|
|
|
|
private function getService(): VariableValidatorService
|
|
|
|
{
|
|
|
|
return new VariableValidatorService(
|
|
|
|
$this->optionVariableRepository,
|
|
|
|
$this->serverRepository,
|
|
|
|
$this->serverVariableRepository,
|
2017-11-25 19:15:46 +00:00
|
|
|
$this->app->make(Factory::class)
|
2017-10-27 04:49:54 +00:00
|
|
|
);
|
|
|
|
}
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|