Refactor startup modification and environment variable services

Better setup, more flexibility, more tests.
This commit is contained in:
Dane Everitt 2017-10-26 23:49:54 -05:00
parent 7022ec788f
commit fa62a0982e
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
19 changed files with 660 additions and 563 deletions

View file

@ -11,8 +11,11 @@ namespace Tests\Unit\Services\Servers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\User;
use Illuminate\Support\Collection;
use Pterodactyl\Models\EggVariable;
use Illuminate\Contracts\Validation\Factory;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Exceptions\DisplayValidationException;
use Pterodactyl\Services\Servers\VariableValidatorService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
@ -22,35 +25,25 @@ use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
class VariableValidatorServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
*/
protected $optionVariableRepository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
*/
protected $serverRepository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface|\Mockery\Mock
*/
protected $serverVariableRepository;
/**
* @var \Pterodactyl\Services\Servers\VariableValidatorService
*/
protected $service;
/**
* @var \Illuminate\Validation\Factory
* @var \Illuminate\Contracts\Validation\Factory|\Mockery\Mock
*/
protected $validator;
/**
* @var \Illuminate\Support\Collection
*/
protected $variables;
/**
* Setup tests.
*/
@ -58,56 +51,10 @@ class VariableValidatorServiceTest extends TestCase
{
parent::setUp();
$this->variables = collect(
[
factory(EggVariable::class)->states('editable', 'viewable')->make(),
factory(EggVariable::class)->states('viewable')->make(),
factory(EggVariable::class)->states('editable')->make(),
factory(EggVariable::class)->make(),
]
);
$this->optionVariableRepository = m::mock(EggVariableRepositoryInterface::class);
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
$this->serverVariableRepository = m::mock(ServerVariableRepositoryInterface::class);
$this->validator = m::mock(Factory::class);
$this->service = new VariableValidatorService(
$this->optionVariableRepository,
$this->serverRepository,
$this->serverVariableRepository,
$this->validator
);
}
/**
* Test that setting fields returns an instance of the class.
*/
public function testSettingFieldsShouldReturnInstanceOfSelf()
{
$response = $this->service->setFields([]);
$this->assertInstanceOf(VariableValidatorService::class, $response);
}
/**
* Test that setting administrator value returns an instance of the class.
*/
public function testSettingAdminShouldReturnInstanceOfSelf()
{
$response = $this->service->isAdmin();
$this->assertInstanceOf(VariableValidatorService::class, $response);
}
/**
* Test that getting the results returns an array of values.
*/
public function testGettingResultsReturnsAnArrayOfValues()
{
$response = $this->service->getResults();
$this->assertTrue(is_array($response));
}
/**
@ -115,13 +62,11 @@ class VariableValidatorServiceTest extends TestCase
*/
public function testEmptyResultSetShouldBeReturnedIfNoVariablesAreFound()
{
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn([]);
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn(collect([]));
$response = $this->service->validate(1);
$this->assertInstanceOf(VariableValidatorService::class, $response);
$this->assertTrue(is_array($response->getResults()));
$this->assertEmpty($response->getResults());
$response = $this->getService()->handle(1, []);
$this->assertEmpty($response);
$this->assertInstanceOf(Collection::class, $response);
}
/**
@ -129,31 +74,34 @@ class VariableValidatorServiceTest extends TestCase
*/
public function testValidatorShouldNotProcessVariablesSetAsNotUserEditableWhenAdminFlagIsNotPassed()
{
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($this->variables);
$variables = $this->getVariableCollection();
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($variables);
$this->validator->shouldReceive('make')->with([
'variable_value' => 'Test_SomeValue_0',
], [
'variable_value' => $this->variables[0]->rules,
])->once()->andReturnSelf()
->shouldReceive('fails')->withNoArgs()->once()->andReturn(false);
'variable_value' => $variables[0]->rules,
])->once()->andReturnSelf();
$this->validator->shouldReceive('fails')->withNoArgs()->once()->andReturn(false);
$response = $this->service->setFields([
$this->variables[0]->env_variable => 'Test_SomeValue_0',
$this->variables[1]->env_variable => 'Test_SomeValue_1',
$this->variables[2]->env_variable => 'Test_SomeValue_2',
$this->variables[3]->env_variable => 'Test_SomeValue_3',
])->validate(1)->getResults();
$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->assertEquals(1, count($response), 'Assert response has a single item in array.');
$this->assertArrayHasKey('0', $response);
$this->assertArrayHasKey('id', $response[0]);
$this->assertArrayHasKey('key', $response[0]);
$this->assertArrayHasKey('value', $response[0]);
$this->assertNotEmpty($response);
$this->assertInstanceOf(Collection::class, $response);
$this->assertEquals(1, $response->count(), 'Assert response has a single item in collection.');
$this->assertEquals($this->variables[0]->id, $response[0]['id']);
$this->assertEquals($this->variables[0]->env_variable, $response[0]['key']);
$this->assertEquals('Test_SomeValue_0', $response[0]['value']);
$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);
}
/**
@ -161,36 +109,39 @@ class VariableValidatorServiceTest extends TestCase
*/
public function testValidatorShouldProcessAllVariablesWhenAdminFlagIsSet()
{
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($this->variables);
$variables = $this->getVariableCollection();
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($variables);
foreach ($this->variables as $key => $variable) {
foreach ($variables as $key => $variable) {
$this->validator->shouldReceive('make')->with([
'variable_value' => 'Test_SomeValue_' . $key,
], [
'variable_value' => $this->variables[$key]->rules,
])->andReturnSelf()
->shouldReceive('fails')->withNoArgs()->once()->andReturn(false);
'variable_value' => $variables[$key]->rules,
])->once()->andReturnSelf();
$this->validator->shouldReceive('fails')->withNoArgs()->once()->andReturn(false);
}
$response = $this->service->isAdmin()->setFields([
$this->variables[0]->env_variable => 'Test_SomeValue_0',
$this->variables[1]->env_variable => 'Test_SomeValue_1',
$this->variables[2]->env_variable => 'Test_SomeValue_2',
$this->variables[3]->env_variable => 'Test_SomeValue_3',
])->validate(1)->getResults();
$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->assertEquals(4, count($response), 'Assert response has all four items in array.');
$this->assertNotEmpty($response);
$this->assertInstanceOf(Collection::class, $response);
$this->assertEquals(4, $response->count(), 'Assert response has all four items in collection.');
foreach ($response as $key => $values) {
$this->assertArrayHasKey($key, $response);
$this->assertArrayHasKey('id', $response[$key]);
$this->assertArrayHasKey('key', $response[$key]);
$this->assertArrayHasKey('value', $response[$key]);
$this->assertEquals($this->variables[$key]->id, $response[$key]['id']);
$this->assertEquals($this->variables[$key]->env_variable, $response[$key]['key']);
$this->assertEquals('Test_SomeValue_' . $key, $response[$key]['value']);
}
$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);
});
}
/**
@ -198,31 +149,63 @@ class VariableValidatorServiceTest extends TestCase
*/
public function testValidatorShouldThrowExceptionWhenAValidationErrorIsEncountered()
{
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($this->variables);
$variables = $this->getVariableCollection();
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($variables);
$this->validator->shouldReceive('make')->with([
'variable_value' => null,
], [
'variable_value' => $this->variables[0]->rules,
])->once()->andReturnSelf()
->shouldReceive('fails')->withNoArgs()->once()->andReturn(true);
'variable_value' => $variables[0]->rules,
])->once()->andReturnSelf();
$this->validator->shouldReceive('fails')->withNoArgs()->once()->andReturn(true);
$this->validator->shouldReceive('errors')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('toArray')->withNoArgs()->once()->andReturn([]);
$this->validator->shouldReceive('errors')->withNoArgs()->once()->andReturnSelf();
$this->validator->shouldReceive('toArray')->withNoArgs()->once()->andReturn([]);
try {
$this->service->setFields([
$this->variables[0]->env_variable => null,
])->validate(1);
} catch (DisplayValidationException $exception) {
$decoded = json_decode($exception->getMessage());
$this->getService()->handle(1, [$variables[0]->env_variable => null]);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DisplayValidationException::class, $exception);
$decoded = json_decode($exception->getMessage());
$this->assertEquals(0, json_last_error(), 'Assert that response is decodable JSON.');
$this->assertObjectHasAttribute('notice', $decoded);
$this->assertEquals(
trans('admin/server.exceptions.bad_variable', ['name' => $this->variables[0]->name]),
trans('admin/server.exceptions.bad_variable', ['name' => $variables[0]->name]),
$decoded->notice[0]
);
}
}
/**
* 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('viewable')->make(),
factory(EggVariable::class)->states('editable')->make(),
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,
$this->validator
);
}
}