Update a batch of failing tests

This commit is contained in:
Dane Everitt 2017-10-07 23:29:08 -05:00
parent c19c423568
commit 787346525b
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
13 changed files with 218 additions and 195 deletions

View file

@ -0,0 +1,113 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Services\Options;
use Exception;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Egg;
use Pterodactyl\Services\Eggs\Scripts\InstallScriptService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException;
class InstallScriptServiceTest extends TestCase
{
/**
* @var array
*/
protected $data = [
'script_install' => 'test-script',
'script_is_privileged' => true,
'script_entry' => '/bin/bash',
'script_container' => 'ubuntu',
'copy_script_from' => null,
];
/**
* @var \Pterodactyl\Models\Egg
*/
protected $model;
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Eggs\Scripts\InstallScriptService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->model = factory(Egg::class)->make();
$this->repository = m::mock(EggRepositoryInterface::class);
$this->service = new InstallScriptService($this->repository);
}
/**
* Test that passing a new copy_script_from attribute works properly.
*/
public function testUpdateWithValidCopyScriptFromAttribute()
{
$this->data['copy_script_from'] = 1;
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(true);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
$this->service->handle($this->model, $this->data);
}
/**
* Test that an exception gets raised when the script is not copiable.
*/
public function testUpdateWithInvalidCopyScriptFromAttribute()
{
$this->data['copy_script_from'] = 1;
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(false);
try {
$this->service->handle($this->model, $this->data);
} catch (Exception $exception) {
$this->assertInstanceOf(InvalidCopyFromException::class, $exception);
$this->assertEquals(trans('exceptions.nest.egg.invalid_copy_id'), $exception->getMessage());
}
}
/**
* Test standard functionality.
*/
public function testUpdateWithoutNewCopyScriptFromAttribute()
{
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
$this->service->handle($this->model, $this->data);
}
/**
* Test that an integer can be passed in place of a model.
*/
public function testFunctionAcceptsIntegerInPlaceOfModel()
{
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
$this->service->handle($this->model->id, $this->data);
}
}

View file

@ -0,0 +1,83 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Eggs\Sharing;
use Mockery as m;
use Carbon\Carbon;
use Tests\TestCase;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\EggVariable;
use Tests\Assertions\NestedObjectAssertionsTrait;
use Pterodactyl\Services\Eggs\Sharing\EggExporterService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
class EggExporterServiceTest extends TestCase
{
use NestedObjectAssertionsTrait;
/**
* @var \Carbon\Carbon
*/
protected $carbon;
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Eggs\Sharing\EggExporterService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
Carbon::setTestNow(Carbon::now());
$this->carbon = new Carbon();
$this->repository = m::mock(EggRepositoryInterface::class);
$this->service = new EggExporterService($this->repository);
}
/**
* Test that a JSON structure is returned.
*/
public function testJsonStructureIsExported()
{
$egg = factory(Egg::class)->make();
$egg->variables = collect([$variable = factory(EggVariable::class)->make()]);
$this->repository->shouldReceive('getWithExportAttributes')->with($egg->id)->once()->andReturn($egg);
$response = $this->service->handle($egg->id);
$this->assertNotEmpty($response);
$data = json_decode($response);
$this->assertEquals(JSON_ERROR_NONE, json_last_error());
$this->assertObjectHasNestedAttribute('meta.version', $data);
$this->assertObjectNestedValueEquals('meta.version', 'PTDL_v1', $data);
$this->assertObjectHasNestedAttribute('author', $data);
$this->assertObjectNestedValueEquals('author', $egg->author, $data);
$this->assertObjectHasNestedAttribute('exported_at', $data);
$this->assertObjectNestedValueEquals('exported_at', Carbon::now()->toIso8601String(), $data);
$this->assertObjectHasNestedAttribute('scripts.installation.script', $data);
$this->assertObjectHasNestedAttribute('scripts.installation.container', $data);
$this->assertObjectHasNestedAttribute('scripts.installation.entrypoint', $data);
$this->assertObjectHasAttribute('variables', $data);
$this->assertArrayHasKey('0', $data->variables);
$this->assertObjectHasAttribute('name', $data->variables[0]);
$this->assertObjectNestedValueEquals('name', $variable->name, $data->variables[0]);
}
}

View file

@ -0,0 +1,168 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Services\Sharing;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Egg;
use Tests\Traits\KnownUuid;
use Pterodactyl\Models\Nest;
use Illuminate\Http\UploadedFile;
use Pterodactyl\Models\EggVariable;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Services\Eggs\Sharing\EggImporterService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Exceptions\Service\Pack\InvalidFileUploadException;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
class EggImporterServiceTest extends TestCase
{
use KnownUuid;
/**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/
protected $connection;
/**
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
*/
protected $eggVariableRepository;
/**
* @var \Illuminate\Http\UploadedFile|\Mockery\Mock
*/
protected $file;
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock
*/
protected $nestRepository;
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Eggs\Sharing\EggImporterService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->eggVariableRepository = m::mock(EggVariableRepositoryInterface::class);
$this->file = m::mock(UploadedFile::class);
$this->nestRepository = m::mock(NestRepositoryInterface::class);
$this->repository = m::mock(EggRepositoryInterface::class);
$this->service = new EggImporterService(
$this->connection, $this->repository, $this->eggVariableRepository, $this->nestRepository
);
}
/**
* Test that a service option can be successfully imported.
*/
public function testEggConfigurationIsImported()
{
$egg = factory(Egg::class)->make();
$nest = factory(Nest::class)->make();
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100);
$this->file->shouldReceive('openFile->fread')->with(100)->once()->andReturn(json_encode([
'meta' => ['version' => 'PTDL_v1'],
'name' => $egg->name,
'tag' => $egg->tag,
'variables' => [
$variable = factory(EggVariable::class)->make(),
],
]));
$this->nestRepository->shouldReceive('getWithEggs')->with($nest->id)->once()->andReturn($nest);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with(m::subset([
'uuid' => $this->getKnownUuid(),
'nest_id' => $nest->id,
'name' => $egg->name,
]), true, true)->once()->andReturn($egg);
$this->eggVariableRepository->shouldReceive('create')->with(m::subset([
'egg_id' => $egg->id,
'env_variable' => $variable->env_variable,
]))->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($this->file, $nest->id);
$this->assertNotEmpty($response);
$this->assertInstanceOf(Egg::class, $response);
$this->assertSame($egg, $response);
}
/**
* Test that an exception is thrown if the file is invalid.
*/
public function testExceptionIsThrownIfFileIsInvalid()
{
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(false);
try {
$this->service->handle($this->file, 1234);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(InvalidFileUploadException::class, $exception);
$this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage());
}
}
/**
* Test that an exception is thrown if the file is not a file.
*/
public function testExceptionIsThrownIfFileIsNotAFile()
{
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(false);
try {
$this->service->handle($this->file, 1234);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(InvalidFileUploadException::class, $exception);
$this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage());
}
}
/**
* Test that an exception is thrown if the JSON metadata is invalid.
*/
public function testExceptionIsThrownIfJsonMetaDataIsInvalid()
{
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100);
$this->file->shouldReceive('openFile->fread')->with(100)->once()->andReturn(json_encode([
'meta' => ['version' => 'hodor'],
]));
try {
$this->service->handle($this->file, 1234);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(InvalidFileUploadException::class, $exception);
$this->assertEquals(trans('exceptions.nest.importer.invalid_json_provided'), $exception->getMessage());
}
}
}

View file

@ -0,0 +1,119 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Eggs\Variables;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Services\Eggs\Variables\VariableCreationService;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
class VariableCreationServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Eggs\Variables\VariableCreationService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->repository = m::mock(EggVariableRepositoryInterface::class);
$this->service = new VariableCreationService($this->repository);
}
/**
* Test basic functionality, data should be stored in the database.
*/
public function testVariableIsCreatedAndStored()
{
$data = ['env_variable' => 'TEST_VAR_123'];
$this->repository->shouldReceive('create')->with([
'egg_id' => 1,
'user_viewable' => false,
'user_editable' => false,
'env_variable' => 'TEST_VAR_123',
])->once()->andReturn(new EggVariable);
$this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data));
}
/**
* Test that the option key in the data array is properly parsed.
*/
public function testOptionsPassedInArrayKeyAreParsedProperly()
{
$data = ['env_variable' => 'TEST_VAR_123', 'options' => ['user_viewable', 'user_editable']];
$this->repository->shouldReceive('create')->with([
'egg_id' => 1,
'user_viewable' => true,
'user_editable' => true,
'env_variable' => 'TEST_VAR_123',
'options' => ['user_viewable', 'user_editable'],
])->once()->andReturn(new EggVariable);
$this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data));
}
/**
* 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->service->handle(1, ['env_variable' => $variable]);
}
/**
* Test that the egg ID applied in the function takes higher priority than an
* ID passed into the handler.
*/
public function testEggIdPassedInDataIsNotApplied()
{
$data = ['egg_id' => 123456, 'env_variable' => 'TEST_VAR_123'];
$this->repository->shouldReceive('create')->with([
'egg_id' => 1,
'user_viewable' => false,
'user_editable' => false,
'env_variable' => 'TEST_VAR_123',
])->once()->andReturn(new EggVariable);
$this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data));
}
/**
* Provides the data to be used in the tests.
*
* @return array
*/
public function reservedNamesProvider()
{
$data = [];
$exploded = explode(',', EggVariable::RESERVED_ENV_NAMES);
foreach ($exploded as $e) {
$data[] = [$e];
}
return $data;
}
}

View file

@ -0,0 +1,173 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Eggs\Variables;
use Exception;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Eggs\Variables\VariableUpdateService;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
class VariableUpdateServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Models\EggVariable|\Mockery\Mock
*/
protected $model;
/**
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Eggs\Variables\VariableUpdateService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->model = factory(EggVariable::class)->make();
$this->repository = m::mock(EggVariableRepositoryInterface::class);
$this->service = new VariableUpdateService($this->repository);
}
/**
* Test the function when no env_variable key is passed into the function.
*/
public function testVariableIsUpdatedWhenNoEnvironmentVariableIsPassed()
{
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, [
'user_viewable' => false,
'user_editable' => false,
'test-data' => 'test-value',
])->once()->andReturn(true);
$this->assertTrue($this->service->handle($this->model, ['test-data' => 'test-value']));
}
/**
* Test that a service variable ID can be passed in place of the model.
*/
public function testVariableIdCanBePassedInPlaceOfModel()
{
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, [
'user_viewable' => false,
'user_editable' => false,
'test-data' => 'test-value',
])->once()->andReturn(true);
$this->assertTrue($this->service->handle($this->model->id, ['test-data' => 'test-value']));
}
/**
* Test the function when a valid env_variable key is passed into the function.
*/
public function testVariableIsUpdatedWhenValidEnvironmentVariableIsPassed()
{
$this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([
['env_variable', '=', 'TEST_VAR_123'],
['egg_id', '=', $this->model->option_id],
['id', '!=', $this->model->id],
])->once()->andReturn(0);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, [
'user_viewable' => false,
'user_editable' => false,
'env_variable' => 'TEST_VAR_123',
])->once()->andReturn(true);
$this->assertTrue($this->service->handle($this->model, ['env_variable' => 'TEST_VAR_123']));
}
/**
* Test that data passed into the handler is overwritten inside the handler.
*/
public function testDataPassedIntoHandlerTakesLowerPriorityThanDataSet()
{
$this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([
['env_variable', '=', 'TEST_VAR_123'],
['egg_id', '=', $this->model->option_id],
['id', '!=', $this->model->id],
])->once()->andReturn(0);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, [
'user_viewable' => false,
'user_editable' => false,
'env_variable' => 'TEST_VAR_123',
])->once()->andReturn(true);
$this->assertTrue($this->service->handle($this->model, ['user_viewable' => 123456, 'env_variable' => 'TEST_VAR_123']));
}
/**
* Test that a non-unique environment variable triggers an exception.
*/
public function testExceptionIsThrownIfEnvironmentVariableIsNotUnique()
{
$this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([
['env_variable', '=', 'TEST_VAR_123'],
['egg_id', '=', $this->model->option_id],
['id', '!=', $this->model->id],
])->once()->andReturn(1);
try {
$this->service->handle($this->model, ['env_variable' => 'TEST_VAR_123']);
} catch (Exception $exception) {
$this->assertInstanceOf(DisplayException::class, $exception);
$this->assertEquals(trans('exceptions.service.variables.env_not_unique', [
'name' => 'TEST_VAR_123',
]), $exception->getMessage());
}
}
/**
* 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->service->handle($this->model, ['env_variable' => $variable]);
}
/**
* Provides the data to be used in the tests.
*
* @return array
*/
public function reservedNamesProvider()
{
$data = [];
$exploded = explode(',', EggVariable::RESERVED_ENV_NAMES);
foreach ($exploded as $e) {
$data[] = [$e];
}
return $data;
}
}