Fix node update tests

This commit is contained in:
Dane Everitt 2020-06-24 21:54:56 -07:00
parent a5d9faf6b2
commit 83a59cdf4f
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
10 changed files with 227 additions and 363 deletions

View file

@ -1,90 +0,0 @@
<?php
namespace Tests\Unit\Services\Eggs;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Egg;
use Pterodactyl\Services\Eggs\EggConfigurationService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
class EggConfigurationServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Eggs\EggConfigurationService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp(): void
{
parent::setUp();
$this->repository = m::mock(EggRepositoryInterface::class);
$this->service = new EggConfigurationService($this->repository);
}
/**
* Test that the correct array is returned.
*/
public function testCorrectArrayIsReturned()
{
$egg = factory(Egg::class)->make([
'config_startup' => '{"test": "start"}',
'config_stop' => 'test',
'config_files' => '{"test": "file"}',
'config_logs' => '{"test": "logs"}',
]);
$response = $this->service->handle($egg);
$this->assertNotEmpty($response);
$this->assertTrue(is_array($response), 'Assert response is an array.');
$this->assertArrayHasKey('startup', $response);
$this->assertArrayHasKey('stop', $response);
$this->assertArrayHasKey('configs', $response);
$this->assertArrayHasKey('log', $response);
$this->assertArrayHasKey('query', $response);
$this->assertEquals('start', object_get($response['startup'], 'test'));
$this->assertEquals('test', 'test');
$this->assertEquals('file', object_get($response['configs'], 'test'));
$this->assertEquals('logs', object_get($response['log'], 'test'));
$this->assertEquals('none', $response['query']);
}
/**
* Test that an integer referencing a model can be passed in place of the model.
*/
public function testFunctionHandlesIntegerPassedInPlaceOfModel()
{
$egg = factory(Egg::class)->make([
'config_startup' => '{"test": "start"}',
'config_stop' => 'test',
'config_files' => '{"test": "file"}',
'config_logs' => '{"test": "logs"}',
]);
$this->repository->shouldReceive('getWithCopyAttributes')->with($egg->id)->once()->andReturn($egg);
$response = $this->service->handle($egg->id);
$this->assertNotEmpty($response);
$this->assertTrue(is_array($response), 'Assert response is an array.');
$this->assertArrayHasKey('startup', $response);
$this->assertArrayHasKey('stop', $response);
$this->assertArrayHasKey('configs', $response);
$this->assertArrayHasKey('log', $response);
$this->assertArrayHasKey('query', $response);
$this->assertEquals('start', object_get($response['startup'], 'test'));
$this->assertEquals('test', 'test');
$this->assertEquals('file', object_get($response['configs'], 'test'));
$this->assertEquals('logs', object_get($response['log'], 'test'));
$this->assertEquals('none', $response['query']);
}
}

View file

@ -9,6 +9,8 @@ use Pterodactyl\Models\EggVariable;
use Illuminate\Contracts\Validation\Factory;
use Pterodactyl\Services\Eggs\Variables\VariableCreationService;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException;
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
class VariableCreationServiceTest extends TestCase
{
@ -91,10 +93,11 @@ class VariableCreationServiceTest extends TestCase
* @param string $variable
*
* @dataProvider reservedNamesProvider
* @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
*/
public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames(string $variable)
{
$this->expectException(ReservedVariableNameException::class);
$this->getService()->handle(1, ['env_variable' => $variable]);
}
@ -114,12 +117,12 @@ class VariableCreationServiceTest extends TestCase
/**
* Test that validation errors due to invalid rules are caught and handled properly.
*
* @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException
* @expectedExceptionMessage The validation rule "hodor_door" is not a valid rule for this application.
*/
public function testInvalidValidationRulesResultInException()
{
$this->expectException(BadValidationRuleException::class);
$this->expectExceptionMessage('The validation rule "hodor_door" is not a valid rule for this application.');
$data = ['env_variable' => 'TEST_VAR_123', 'rules' => 'string|hodorDoor'];
$this->validator->shouldReceive('make')->once()
@ -135,12 +138,12 @@ class VariableCreationServiceTest extends TestCase
/**
* Test that an exception not stemming from a bad rule is not caught.
*
* @expectedException \BadMethodCallException
* @expectedExceptionMessage Received something, but no expectations were specified.
*/
public function testExceptionNotCausedByBadRuleIsNotCaught()
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Received something, but no expectations were specified.');
$data = ['env_variable' => 'TEST_VAR_123', 'rules' => 'string'];
$this->validator->shouldReceive('make')->once()

View file

@ -11,6 +11,8 @@ use Illuminate\Contracts\Validation\Factory;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Eggs\Variables\VariableUpdateService;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException;
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
class VariableUpdateServiceTest extends TestCase
{
@ -159,21 +161,22 @@ class VariableUpdateServiceTest extends TestCase
* Test that all of the reserved variables defined in the model trigger an exception.
*
* @dataProvider reservedNamesProvider
* @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
*/
public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames(string $variable)
{
$this->expectException(ReservedVariableNameException::class);
$this->getService()->handle($this->model, ['env_variable' => $variable]);
}
/**
* Test that validation errors due to invalid rules are caught and handled properly.
*
* @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException
* @expectedExceptionMessage The validation rule "hodor_door" is not a valid rule for this application.
*/
public function testInvalidValidationRulesResultInException()
{
$this->expectException(BadValidationRuleException::class);
$this->expectExceptionMessage('The validation rule "hodor_door" is not a valid rule for this application.');
$data = ['env_variable' => 'TEST_VAR_123', 'rules' => 'string|hodorDoor'];
$this->repository->shouldReceive('setColumns->findCountWhere')->once()->andReturn(0);
@ -191,12 +194,12 @@ class VariableUpdateServiceTest extends TestCase
/**
* Test that an exception not stemming from a bad rule is not caught.
*
* @expectedException \BadMethodCallException
* @expectedExceptionMessage Received something, but no expectations were specified.
*/
public function testExceptionNotCausedByBadRuleIsNotCaught()
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Received something, but no expectations were specified.');
$data = ['rules' => 'string'];
$this->validator->shouldReceive('make')->once()