2017-07-23 01:15:01 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Servers;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
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;
|
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException;
|
|
|
|
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-07 04:57:53 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
|
|
|
protected $optionVariableRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $serverRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $serverVariableRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\VariableValidatorService
|
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Validation\Factory
|
|
|
|
*/
|
|
|
|
protected $validator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
protected $variables;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->variables = collect(
|
|
|
|
[
|
2017-10-07 04:57:53 +00:00
|
|
|
factory(EggVariable::class)->states('editable', 'viewable')->make(),
|
|
|
|
factory(EggVariable::class)->states('viewable')->make(),
|
|
|
|
factory(EggVariable::class)->states('editable')->make(),
|
|
|
|
factory(EggVariable::class)->make(),
|
2017-07-23 01:15:01 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
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);
|
|
|
|
$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()
|
|
|
|
{
|
2017-07-25 02:34:10 +00:00
|
|
|
$response = $this->service->isAdmin();
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
$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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that when no variables are found for an option no data is returned.
|
|
|
|
*/
|
|
|
|
public function testEmptyResultSetShouldBeReturnedIfNoVariablesAreFound()
|
|
|
|
{
|
|
|
|
$this->optionVariableRepository->shouldReceive('findWhere')->with([['option_id', '=', 1]])->andReturn([]);
|
|
|
|
|
|
|
|
$response = $this->service->validate(1);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(VariableValidatorService::class, $response);
|
|
|
|
$this->assertTrue(is_array($response->getResults()));
|
|
|
|
$this->assertEmpty($response->getResults());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that variables set as user_editable=0 and/or user_viewable=0 are skipped when admin flag is not set.
|
|
|
|
*/
|
|
|
|
public function testValidatorShouldNotProcessVariablesSetAsNotUserEditableWhenAdminFlagIsNotPassed()
|
|
|
|
{
|
|
|
|
$this->optionVariableRepository->shouldReceive('findWhere')->with([['option_id', '=', 1]])->andReturn($this->variables);
|
|
|
|
|
|
|
|
$this->validator->shouldReceive('make')->with([
|
|
|
|
'variable_value' => 'Test_SomeValue_0',
|
|
|
|
], [
|
2017-08-05 22:26:30 +00:00
|
|
|
'variable_value' => $this->variables[0]->rules,
|
2017-07-23 01:15:01 +00:00
|
|
|
])->once()->andReturnSelf()
|
|
|
|
->shouldReceive('fails')->withNoArgs()->once()->andReturn(false);
|
|
|
|
|
|
|
|
$response = $this->service->setFields([
|
2017-08-05 22:26:30 +00:00
|
|
|
$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',
|
2017-07-23 01:15:01 +00:00
|
|
|
])->validate(1)->getResults();
|
|
|
|
|
|
|
|
$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]);
|
|
|
|
|
2017-08-05 22:26:30 +00:00
|
|
|
$this->assertEquals($this->variables[0]->id, $response[0]['id']);
|
|
|
|
$this->assertEquals($this->variables[0]->env_variable, $response[0]['key']);
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->assertEquals('Test_SomeValue_0', $response[0]['value']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that all variables are processed correctly if admin flag is set.
|
|
|
|
*/
|
|
|
|
public function testValidatorShouldProcessAllVariablesWhenAdminFlagIsSet()
|
|
|
|
{
|
|
|
|
$this->optionVariableRepository->shouldReceive('findWhere')->with([['option_id', '=', 1]])->andReturn($this->variables);
|
|
|
|
|
2017-08-05 22:26:30 +00:00
|
|
|
foreach ($this->variables as $key => $variable) {
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->validator->shouldReceive('make')->with([
|
|
|
|
'variable_value' => 'Test_SomeValue_' . $key,
|
|
|
|
], [
|
2017-08-05 22:26:30 +00:00
|
|
|
'variable_value' => $this->variables[$key]->rules,
|
2017-07-23 01:15:01 +00:00
|
|
|
])->andReturnSelf()
|
|
|
|
->shouldReceive('fails')->withNoArgs()->once()->andReturn(false);
|
|
|
|
}
|
|
|
|
|
2017-07-25 02:34:10 +00:00
|
|
|
$response = $this->service->isAdmin()->setFields([
|
2017-08-05 22:26:30 +00:00
|
|
|
$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',
|
2017-07-23 01:15:01 +00:00
|
|
|
])->validate(1)->getResults();
|
|
|
|
|
|
|
|
$this->assertEquals(4, count($response), 'Assert response has all four items in array.');
|
|
|
|
|
2017-08-05 22:26:30 +00:00
|
|
|
foreach ($response as $key => $values) {
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->assertArrayHasKey($key, $response);
|
|
|
|
$this->assertArrayHasKey('id', $response[$key]);
|
|
|
|
$this->assertArrayHasKey('key', $response[$key]);
|
|
|
|
$this->assertArrayHasKey('value', $response[$key]);
|
|
|
|
|
2017-08-05 22:26:30 +00:00
|
|
|
$this->assertEquals($this->variables[$key]->id, $response[$key]['id']);
|
|
|
|
$this->assertEquals($this->variables[$key]->env_variable, $response[$key]['key']);
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->assertEquals('Test_SomeValue_' . $key, $response[$key]['value']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a DisplayValidationError is thrown when a variable is not validated.
|
|
|
|
*/
|
|
|
|
public function testValidatorShouldThrowExceptionWhenAValidationErrorIsEncountered()
|
|
|
|
{
|
|
|
|
$this->optionVariableRepository->shouldReceive('findWhere')->with([['option_id', '=', 1]])->andReturn($this->variables);
|
|
|
|
|
|
|
|
$this->validator->shouldReceive('make')->with([
|
|
|
|
'variable_value' => null,
|
|
|
|
], [
|
2017-08-05 22:26:30 +00:00
|
|
|
'variable_value' => $this->variables[0]->rules,
|
2017-07-23 01:15:01 +00:00
|
|
|
])->once()->andReturnSelf()
|
|
|
|
->shouldReceive('fails')->withNoArgs()->once()->andReturn(true);
|
|
|
|
|
|
|
|
$this->validator->shouldReceive('errors')->withNoArgs()->once()->andReturnSelf()
|
|
|
|
->shouldReceive('toArray')->withNoArgs()->once()->andReturn([]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->service->setFields([
|
2017-08-05 22:26:30 +00:00
|
|
|
$this->variables[0]->env_variable => null,
|
2017-07-23 01:15:01 +00:00
|
|
|
])->validate(1);
|
|
|
|
} catch (DisplayValidationException $exception) {
|
|
|
|
$decoded = json_decode($exception->getMessage());
|
|
|
|
|
|
|
|
$this->assertEquals(0, json_last_error(), 'Assert that response is decodable JSON.');
|
|
|
|
$this->assertObjectHasAttribute('notice', $decoded);
|
|
|
|
$this->assertEquals(
|
2017-08-05 22:26:30 +00:00
|
|
|
trans('admin/server.exceptions.bad_variable', ['name' => $this->variables[0]->name]),
|
2017-07-23 01:15:01 +00:00
|
|
|
$decoded->notice[0]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|