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

@ -30,34 +30,34 @@ class EggImporterService
*/ */
protected $eggVariableRepository; protected $eggVariableRepository;
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
*/
protected $nestRepository;
/** /**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
*/ */
protected $repository; protected $repository;
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
*/
protected $serviceRepository;
/** /**
* EggImporterService constructor. * EggImporterService constructor.
* *
* @param \Illuminate\Database\ConnectionInterface $connection * @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $repository * @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $repository
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $eggVariableRepository * @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $eggVariableRepository
* @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $serviceRepository * @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $nestRepository
*/ */
public function __construct( public function __construct(
ConnectionInterface $connection, ConnectionInterface $connection,
EggRepositoryInterface $repository, EggRepositoryInterface $repository,
EggVariableRepositoryInterface $eggVariableRepository, EggVariableRepositoryInterface $eggVariableRepository,
NestRepositoryInterface $serviceRepository NestRepositoryInterface $nestRepository
) { ) {
$this->connection = $connection; $this->connection = $connection;
$this->repository = $repository;
$this->serviceRepository = $serviceRepository;
$this->eggVariableRepository = $eggVariableRepository; $this->eggVariableRepository = $eggVariableRepository;
$this->repository = $repository;
$this->nestRepository = $nestRepository;
} }
/** /**
@ -74,16 +74,16 @@ class EggImporterService
public function handle(UploadedFile $file, int $nest): Egg public function handle(UploadedFile $file, int $nest): Egg
{ {
if (! $file->isValid() || ! $file->isFile()) { if (! $file->isValid() || ! $file->isFile()) {
throw new InvalidFileUploadException(trans('exceptions.egg.importer.file_error')); throw new InvalidFileUploadException(trans('exceptions.nest.importer.file_error'));
} }
$parsed = json_decode($file->openFile()->fread($file->getSize())); $parsed = json_decode($file->openFile()->fread($file->getSize()));
if (object_get($parsed, 'meta.version') !== 'PTDL_v1') { if (object_get($parsed, 'meta.version') !== 'PTDL_v1') {
throw new InvalidFileUploadException(trans('exceptions.egg.importer.invalid_json_provided')); throw new InvalidFileUploadException(trans('exceptions.nest.importer.invalid_json_provided'));
} }
$nest = $this->serviceRepository->getWithEggs($nest); $nest = $this->nestRepository->getWithEggs($nest);
$this->connection->beginTransaction(); $this->connection->beginTransaction();
$egg = $this->repository->create([ $egg = $this->repository->create([

View file

@ -10,34 +10,24 @@
namespace Pterodactyl\Services\Eggs\Variables; namespace Pterodactyl\Services\Eggs\Variables;
use Pterodactyl\Models\EggVariable; use Pterodactyl\Models\EggVariable;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface; use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException; use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
class VariableCreationService class VariableCreationService
{ {
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
*/
protected $eggRepository;
/** /**
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface * @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
*/ */
protected $variableRepository; protected $repository;
/** /**
* VariableCreationService constructor. * VariableCreationService constructor.
* *
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $eggRepository * @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $repository
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $variableRepository
*/ */
public function __construct( public function __construct(EggVariableRepositoryInterface $repository)
EggRepositoryInterface $eggRepository, {
EggVariableRepositoryInterface $variableRepository $this->repository = $repository;
) {
$this->eggRepository = $eggRepository;
$this->variableRepository = $variableRepository;
} }
/** /**
@ -61,10 +51,10 @@ class VariableCreationService
$options = array_get($data, 'options', []); $options = array_get($data, 'options', []);
return $this->variableRepository->create(array_merge([ return $this->repository->create(array_merge($data, [
'egg_id' => $egg, 'egg_id' => $egg,
'user_viewable' => in_array('user_viewable', $options), 'user_viewable' => in_array('user_viewable', $options),
'user_editable' => in_array('user_editable', $options), 'user_editable' => in_array('user_editable', $options),
], $data)); ]));
} }
} }

View file

@ -32,10 +32,8 @@ class NestCreationService
* @param \Illuminate\Contracts\Config\Repository $config * @param \Illuminate\Contracts\Config\Repository $config
* @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $repository * @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $repository
*/ */
public function __construct( public function __construct(ConfigRepository $config, NestRepositoryInterface $repository)
ConfigRepository $config, {
NestRepositoryInterface $repository
) {
$this->config = $config; $this->config = $config;
$this->repository = $repository; $this->repository = $repository;
} }

View file

@ -8,8 +8,22 @@ abstract class TestCase extends BaseTestCase
{ {
use CreatesApplication; use CreatesApplication;
/**
* Setup tests.
*/
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->setKnownUuidFactory();
}
/**
* Handles the known UUID handling in certain unit tests. Use the "KnownUuid" trait
* in order to enable this ability.
*/
public function setKnownUuidFactory()
{
// do nothing
} }
} }

View file

@ -0,0 +1,47 @@
<?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\Traits;
use Mockery as m;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidFactory;
trait KnownUuid
{
/**
* The known UUID string.
*
* @var string
*/
protected $knownUuid = 'ffb5c3a6-ab17-43ab-97f0-8ff37ccd7f5f';
/**
* Setup a factory mock to produce the same UUID whenever called.
*/
public function setKnownUuidFactory()
{
$uuid = Uuid::fromString($this->getKnownUuid());
$factoryMock = m::mock(UuidFactory::class . '[uuid4]', [
'uuid4' => $uuid,
]);
Uuid::setFactory($factoryMock);
}
/**
* Returns the known UUID for tests to use.
*
* @return string
*/
public function getKnownUuid(): string
{
return $this->knownUuid;
}
}

View file

@ -13,11 +13,11 @@ use Exception;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Pterodactyl\Models\Egg; use Pterodactyl\Models\Egg;
use Pterodactyl\Services\Eggs\Scripts\InstallScriptService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Services\Services\Options\InstallScriptService; use Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException;
use Pterodactyl\Exceptions\Service\ServiceOption\InvalidCopyFromException;
class InstallScriptUpdateServiceTest extends TestCase class InstallScriptServiceTest extends TestCase
{ {
/** /**
* @var array * @var array
@ -36,12 +36,12 @@ class InstallScriptUpdateServiceTest extends TestCase
protected $model; protected $model;
/** /**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
*/ */
protected $repository; protected $repository;
/** /**
* @var \Pterodactyl\Services\Services\Options\InstallScriptService * @var \Pterodactyl\Services\Eggs\Scripts\InstallScriptService
*/ */
protected $service; protected $service;
@ -65,7 +65,7 @@ class InstallScriptUpdateServiceTest extends TestCase
{ {
$this->data['copy_script_from'] = 1; $this->data['copy_script_from'] = 1;
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->service_id)->once()->andReturn(true); $this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(true);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull(); ->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
@ -79,12 +79,12 @@ class InstallScriptUpdateServiceTest extends TestCase
{ {
$this->data['copy_script_from'] = 1; $this->data['copy_script_from'] = 1;
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->service_id)->once()->andReturn(false); $this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(false);
try { try {
$this->service->handle($this->model, $this->data); $this->service->handle($this->model, $this->data);
} catch (Exception $exception) { } catch (Exception $exception) {
$this->assertInstanceOf(InvalidCopyFromException::class, $exception); $this->assertInstanceOf(InvalidCopyFromException::class, $exception);
$this->assertEquals(trans('exceptions.service.options.invalid_copy_id'), $exception->getMessage()); $this->assertEquals(trans('exceptions.nest.egg.invalid_copy_id'), $exception->getMessage());
} }
} }

View file

@ -7,7 +7,7 @@
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
namespace Tests\Unit\Services\Services\Sharing; namespace Tests\Unit\Services\Eggs\Sharing;
use Mockery as m; use Mockery as m;
use Carbon\Carbon; use Carbon\Carbon;
@ -15,10 +15,10 @@ use Tests\TestCase;
use Pterodactyl\Models\Egg; use Pterodactyl\Models\Egg;
use Pterodactyl\Models\EggVariable; use Pterodactyl\Models\EggVariable;
use Tests\Assertions\NestedObjectAssertionsTrait; use Tests\Assertions\NestedObjectAssertionsTrait;
use Pterodactyl\Services\Eggs\Sharing\EggExporterService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Services\Services\Sharing\ServiceOptionExporterService;
class ServiceOptionExporterServiceTest extends TestCase class EggExporterServiceTest extends TestCase
{ {
use NestedObjectAssertionsTrait; use NestedObjectAssertionsTrait;
@ -33,7 +33,7 @@ class ServiceOptionExporterServiceTest extends TestCase
protected $repository; protected $repository;
/** /**
* @var \Pterodactyl\Services\Services\Sharing\ServiceOptionExporterService * @var \Pterodactyl\Services\Eggs\Sharing\EggExporterService
*/ */
protected $service; protected $service;
@ -48,7 +48,7 @@ class ServiceOptionExporterServiceTest extends TestCase
$this->carbon = new Carbon(); $this->carbon = new Carbon();
$this->repository = m::mock(EggRepositoryInterface::class); $this->repository = m::mock(EggRepositoryInterface::class);
$this->service = new ServiceOptionExporterService($this->carbon, $this->repository); $this->service = new EggExporterService($this->repository);
} }
/** /**
@ -56,18 +56,20 @@ class ServiceOptionExporterServiceTest extends TestCase
*/ */
public function testJsonStructureIsExported() public function testJsonStructureIsExported()
{ {
$option = factory(Egg::class)->make(); $egg = factory(Egg::class)->make();
$option->variables = collect([$variable = factory(EggVariable::class)->make()]); $egg->variables = collect([$variable = factory(EggVariable::class)->make()]);
$this->repository->shouldReceive('getWithExportAttributes')->with($option->id)->once()->andReturn($option); $this->repository->shouldReceive('getWithExportAttributes')->with($egg->id)->once()->andReturn($egg);
$response = $this->service->handle($option->id); $response = $this->service->handle($egg->id);
$this->assertNotEmpty($response); $this->assertNotEmpty($response);
$data = json_decode($response); $data = json_decode($response);
$this->assertEquals(JSON_ERROR_NONE, json_last_error()); $this->assertEquals(JSON_ERROR_NONE, json_last_error());
$this->assertObjectHasNestedAttribute('meta.version', $data); $this->assertObjectHasNestedAttribute('meta.version', $data);
$this->assertObjectNestedValueEquals('meta.version', 'PTDL_v1', $data); $this->assertObjectNestedValueEquals('meta.version', 'PTDL_v1', $data);
$this->assertObjectHasNestedAttribute('author', $data);
$this->assertObjectNestedValueEquals('author', $egg->author, $data);
$this->assertObjectHasNestedAttribute('exported_at', $data); $this->assertObjectHasNestedAttribute('exported_at', $data);
$this->assertObjectNestedValueEquals('exported_at', Carbon::now()->toIso8601String(), $data); $this->assertObjectNestedValueEquals('exported_at', Carbon::now()->toIso8601String(), $data);
$this->assertObjectHasNestedAttribute('scripts.installation.script', $data); $this->assertObjectHasNestedAttribute('scripts.installation.script', $data);

View file

@ -11,57 +11,53 @@ namespace Tests\Unit\Services\Services\Sharing;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Ramsey\Uuid\Uuid;
use Pterodactyl\Models\Egg; use Pterodactyl\Models\Egg;
use Tests\Traits\KnownUuid;
use Pterodactyl\Models\Nest; use Pterodactyl\Models\Nest;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Pterodactyl\Models\EggVariable; use Pterodactyl\Models\EggVariable;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\PterodactylException; use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Services\Eggs\Sharing\EggImporterService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Services\Services\Sharing\EggImporterService;
use Pterodactyl\Exceptions\Service\Pack\InvalidFileUploadException; use Pterodactyl\Exceptions\Service\Pack\InvalidFileUploadException;
use Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface; use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
use Pterodactyl\Exceptions\Service\ServiceOption\DuplicateOptionTagException;
class ServiceOptionImporterServiceTest extends TestCase class EggImporterServiceTest extends TestCase
{ {
use KnownUuid;
/** /**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock * @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/ */
protected $connection; protected $connection;
/**
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
*/
protected $eggVariableRepository;
/** /**
* @var \Illuminate\Http\UploadedFile|\Mockery\Mock * @var \Illuminate\Http\UploadedFile|\Mockery\Mock
*/ */
protected $file; protected $file;
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock
*/
protected $nestRepository;
/** /**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
*/ */
protected $repository; protected $repository;
/** /**
* @var \Pterodactyl\Services\Services\Sharing\EggImporterService * @var \Pterodactyl\Services\Eggs\Sharing\EggImporterService
*/ */
protected $service; protected $service;
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock
*/
protected $serviceRepository;
/**
* @var \Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface|\Mockery\Mock
*/
protected $serviceVariableRepository;
/**
* @var \Ramsey\Uuid\Uuid|\Mockery\Mock
*/
protected $uuid;
/** /**
* Setup tests. * Setup tests.
*/ */
@ -70,58 +66,54 @@ class ServiceOptionImporterServiceTest extends TestCase
parent::setUp(); parent::setUp();
$this->connection = m::mock(ConnectionInterface::class); $this->connection = m::mock(ConnectionInterface::class);
$this->eggVariableRepository = m::mock(EggVariableRepositoryInterface::class);
$this->file = m::mock(UploadedFile::class); $this->file = m::mock(UploadedFile::class);
$this->nestRepository = m::mock(NestRepositoryInterface::class);
$this->repository = m::mock(EggRepositoryInterface::class); $this->repository = m::mock(EggRepositoryInterface::class);
$this->serviceRepository = m::mock(NestRepositoryInterface::class);
$this->serviceVariableRepository = m::mock(ServiceVariableRepositoryInterface::class);
$this->uuid = m::mock('overload:' . Uuid::class);
$this->service = new EggImporterService( $this->service = new EggImporterService(
$this->connection, $this->serviceRepository, $this->repository, $this->serviceVariableRepository $this->connection, $this->repository, $this->eggVariableRepository, $this->nestRepository
); );
} }
/** /**
* Test that a service option can be successfully imported. * Test that a service option can be successfully imported.
*/ */
public function testServiceOptionIsImported() public function testEggConfigurationIsImported()
{ {
$option = factory(Egg::class)->make(); $egg = factory(Egg::class)->make();
$service = factory(Nest::class)->make(); $nest = factory(Nest::class)->make();
$service->options = collect([factory(Egg::class)->make()]);
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true); $this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true); $this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100); $this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100);
$this->file->shouldReceive('openFile->fread')->with(100)->once()->andReturn(json_encode([ $this->file->shouldReceive('openFile->fread')->with(100)->once()->andReturn(json_encode([
'meta' => ['version' => 'PTDL_v1'], 'meta' => ['version' => 'PTDL_v1'],
'name' => $option->name, 'name' => $egg->name,
'tag' => $option->tag, 'tag' => $egg->tag,
'variables' => [ 'variables' => [
$variable = factory(EggVariable::class)->make(), $variable = factory(EggVariable::class)->make(),
], ],
])); ]));
$this->serviceRepository->shouldReceive('getWithOptions')->with($service->id)->once()->andReturn($service); $this->nestRepository->shouldReceive('getWithEggs')->with($nest->id)->once()->andReturn($nest);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn($option->uuid);
$this->repository->shouldReceive('create')->with(m::subset([ $this->repository->shouldReceive('create')->with(m::subset([
'uuid' => $option->uuid, 'uuid' => $this->getKnownUuid(),
'service_id' => $service->id, 'nest_id' => $nest->id,
'name' => $option->name, 'name' => $egg->name,
'tag' => $option->tag, ]), true, true)->once()->andReturn($egg);
]), true, true)->once()->andReturn($option);
$this->serviceVariableRepository->shouldReceive('create')->with(m::subset([ $this->eggVariableRepository->shouldReceive('create')->with(m::subset([
'option_id' => $option->id, 'egg_id' => $egg->id,
'env_variable' => $variable->env_variable, 'env_variable' => $variable->env_variable,
]))->once()->andReturnNull(); ]))->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($this->file, $service->id); $response = $this->service->handle($this->file, $nest->id);
$this->assertNotEmpty($response); $this->assertNotEmpty($response);
$this->assertInstanceOf(Egg::class, $response); $this->assertInstanceOf(Egg::class, $response);
$this->assertSame($option, $response); $this->assertSame($egg, $response);
} }
/** /**
@ -134,7 +126,7 @@ class ServiceOptionImporterServiceTest extends TestCase
$this->service->handle($this->file, 1234); $this->service->handle($this->file, 1234);
} catch (PterodactylException $exception) { } catch (PterodactylException $exception) {
$this->assertInstanceOf(InvalidFileUploadException::class, $exception); $this->assertInstanceOf(InvalidFileUploadException::class, $exception);
$this->assertEquals(trans('exceptions.service.exporter.import_file_error'), $exception->getMessage()); $this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage());
} }
} }
@ -150,7 +142,7 @@ class ServiceOptionImporterServiceTest extends TestCase
$this->service->handle($this->file, 1234); $this->service->handle($this->file, 1234);
} catch (PterodactylException $exception) { } catch (PterodactylException $exception) {
$this->assertInstanceOf(InvalidFileUploadException::class, $exception); $this->assertInstanceOf(InvalidFileUploadException::class, $exception);
$this->assertEquals(trans('exceptions.service.exporter.import_file_error'), $exception->getMessage()); $this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage());
} }
} }
@ -170,33 +162,7 @@ class ServiceOptionImporterServiceTest extends TestCase
$this->service->handle($this->file, 1234); $this->service->handle($this->file, 1234);
} catch (PterodactylException $exception) { } catch (PterodactylException $exception) {
$this->assertInstanceOf(InvalidFileUploadException::class, $exception); $this->assertInstanceOf(InvalidFileUploadException::class, $exception);
$this->assertEquals(trans('exceptions.service.exporter.invalid_json_provided'), $exception->getMessage()); $this->assertEquals(trans('exceptions.nest.importer.invalid_json_provided'), $exception->getMessage());
}
}
/**
* Test that an exception is thrown if a duplicate tag exists.
*/
public function testExceptionIsThrownIfDuplicateTagExists()
{
$option = factory(Egg::class)->make();
$service = factory(Nest::class)->make();
$service->options = collect([factory(Egg::class)->make(['tag' => $option->tag])]);
$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'],
'tag' => $option->tag,
]));
$this->serviceRepository->shouldReceive('getWithOptions')->with($service->id)->once()->andReturn($service);
try {
$this->service->handle($this->file, $service->id);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DuplicateOptionTagException::class, $exception);
$this->assertEquals(trans('exceptions.service.options.duplicate_tag'), $exception->getMessage());
} }
} }
} }

View file

@ -7,30 +7,24 @@
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
namespace Tests\Unit\Services\Services\Variables; namespace Tests\Unit\Services\Eggs\Variables;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Pterodactyl\Models\Egg; use Pterodactyl\Models\Egg;
use Pterodactyl\Models\EggVariable; use Pterodactyl\Models\EggVariable;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Services\Eggs\Variables\VariableCreationService;
use Pterodactyl\Services\Services\Variables\VariableCreationService; use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface;
class VariableCreationServiceTest extends TestCase class VariableCreationServiceTest extends TestCase
{ {
/** /**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface * @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
*/ */
protected $serviceOptionRepository; protected $repository;
/** /**
* @var \Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface * @var \Pterodactyl\Services\Eggs\Variables\VariableCreationService
*/
protected $serviceVariableRepository;
/**
* @var \Pterodactyl\Services\Services\Variables\VariableCreationService
*/ */
protected $service; protected $service;
@ -41,10 +35,9 @@ class VariableCreationServiceTest extends TestCase
{ {
parent::setUp(); parent::setUp();
$this->serviceOptionRepository = m::mock(EggRepositoryInterface::class); $this->repository = m::mock(EggVariableRepositoryInterface::class);
$this->serviceVariableRepository = m::mock(ServiceVariableRepositoryInterface::class);
$this->service = new VariableCreationService($this->serviceOptionRepository, $this->serviceVariableRepository); $this->service = new VariableCreationService($this->repository);
} }
/** /**
@ -53,8 +46,8 @@ class VariableCreationServiceTest extends TestCase
public function testVariableIsCreatedAndStored() public function testVariableIsCreatedAndStored()
{ {
$data = ['env_variable' => 'TEST_VAR_123']; $data = ['env_variable' => 'TEST_VAR_123'];
$this->serviceVariableRepository->shouldReceive('create')->with([ $this->repository->shouldReceive('create')->with([
'option_id' => 1, 'egg_id' => 1,
'user_viewable' => false, 'user_viewable' => false,
'user_editable' => false, 'user_editable' => false,
'env_variable' => 'TEST_VAR_123', 'env_variable' => 'TEST_VAR_123',
@ -69,8 +62,8 @@ class VariableCreationServiceTest extends TestCase
public function testOptionsPassedInArrayKeyAreParsedProperly() public function testOptionsPassedInArrayKeyAreParsedProperly()
{ {
$data = ['env_variable' => 'TEST_VAR_123', 'options' => ['user_viewable', 'user_editable']]; $data = ['env_variable' => 'TEST_VAR_123', 'options' => ['user_viewable', 'user_editable']];
$this->serviceVariableRepository->shouldReceive('create')->with([ $this->repository->shouldReceive('create')->with([
'option_id' => 1, 'egg_id' => 1,
'user_viewable' => true, 'user_viewable' => true,
'user_editable' => true, 'user_editable' => true,
'env_variable' => 'TEST_VAR_123', 'env_variable' => 'TEST_VAR_123',
@ -84,29 +77,28 @@ class VariableCreationServiceTest extends TestCase
* Test that all of the reserved variables defined in the model trigger an exception. * Test that all of the reserved variables defined in the model trigger an exception.
* *
* @dataProvider reservedNamesProvider * @dataProvider reservedNamesProvider
* @expectedException \Pterodactyl\Exceptions\Service\ServiceVariable\ReservedVariableNameException * @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
*/ */
public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames($variable) public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames(string $variable)
{ {
$this->service->handle(1, ['env_variable' => $variable]); $this->service->handle(1, ['env_variable' => $variable]);
} }
/** /**
* Test that a model can be passed in place of an integer. * Test that the egg ID applied in the function takes higher priority than an
* ID passed into the handler.
*/ */
public function testModelCanBePassedInPlaceOfInteger() public function testEggIdPassedInDataIsNotApplied()
{ {
$model = factory(Egg::class)->make(); $data = ['egg_id' => 123456, 'env_variable' => 'TEST_VAR_123'];
$data = ['env_variable' => 'TEST_VAR_123']; $this->repository->shouldReceive('create')->with([
'egg_id' => 1,
$this->serviceVariableRepository->shouldReceive('create')->with([
'option_id' => $model->id,
'user_viewable' => false, 'user_viewable' => false,
'user_editable' => false, 'user_editable' => false,
'env_variable' => 'TEST_VAR_123', 'env_variable' => 'TEST_VAR_123',
])->once()->andReturn(new EggVariable); ])->once()->andReturn(new EggVariable);
$this->assertInstanceOf(EggVariable::class, $this->service->handle($model, $data)); $this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data));
} }
/** /**

View file

@ -7,15 +7,15 @@
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
namespace Tests\Unit\Services\Services\Variables; namespace Tests\Unit\Services\Eggs\Variables;
use Exception; use Exception;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Pterodactyl\Models\EggVariable; use Pterodactyl\Models\EggVariable;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Services\Variables\VariableUpdateService; use Pterodactyl\Services\Eggs\Variables\VariableUpdateService;
use Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface; use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
class VariableUpdateServiceTest extends TestCase class VariableUpdateServiceTest extends TestCase
{ {
@ -25,12 +25,12 @@ class VariableUpdateServiceTest extends TestCase
protected $model; protected $model;
/** /**
* @var \Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface|\Mockery\Mock * @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
*/ */
protected $repository; protected $repository;
/** /**
* @var \Pterodactyl\Services\Services\Variables\VariableUpdateService * @var \Pterodactyl\Services\Eggs\Variables\VariableUpdateService
*/ */
protected $service; protected $service;
@ -42,7 +42,7 @@ class VariableUpdateServiceTest extends TestCase
parent::setUp(); parent::setUp();
$this->model = factory(EggVariable::class)->make(); $this->model = factory(EggVariable::class)->make();
$this->repository = m::mock(ServiceVariableRepositoryInterface::class); $this->repository = m::mock(EggVariableRepositoryInterface::class);
$this->service = new VariableUpdateService($this->repository); $this->service = new VariableUpdateService($this->repository);
} }
@ -86,7 +86,7 @@ class VariableUpdateServiceTest extends TestCase
$this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf() $this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([ ->shouldReceive('findCountWhere')->with([
['env_variable', '=', 'TEST_VAR_123'], ['env_variable', '=', 'TEST_VAR_123'],
['option_id', '=', $this->model->option_id], ['egg_id', '=', $this->model->option_id],
['id', '!=', $this->model->id], ['id', '!=', $this->model->id],
])->once()->andReturn(0); ])->once()->andReturn(0);
@ -100,6 +100,28 @@ class VariableUpdateServiceTest extends TestCase
$this->assertTrue($this->service->handle($this->model, ['env_variable' => 'TEST_VAR_123'])); $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. * Test that a non-unique environment variable triggers an exception.
*/ */
@ -108,7 +130,7 @@ class VariableUpdateServiceTest extends TestCase
$this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf() $this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([ ->shouldReceive('findCountWhere')->with([
['env_variable', '=', 'TEST_VAR_123'], ['env_variable', '=', 'TEST_VAR_123'],
['option_id', '=', $this->model->option_id], ['egg_id', '=', $this->model->option_id],
['id', '!=', $this->model->id], ['id', '!=', $this->model->id],
])->once()->andReturn(1); ])->once()->andReturn(1);
@ -126,9 +148,9 @@ class VariableUpdateServiceTest extends TestCase
* Test that all of the reserved variables defined in the model trigger an exception. * Test that all of the reserved variables defined in the model trigger an exception.
* *
* @dataProvider reservedNamesProvider * @dataProvider reservedNamesProvider
* @expectedException \Pterodactyl\Exceptions\Service\ServiceVariable\ReservedVariableNameException * @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
*/ */
public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames($variable) public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames(string $variable)
{ {
$this->service->handle($this->model, ['env_variable' => $variable]); $this->service->handle($this->model, ['env_variable' => $variable]);
} }

View file

@ -12,15 +12,15 @@ namespace Tests\Unit\Services\Services;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Tests\Traits\KnownUuid;
use Pterodactyl\Models\Nest; use Pterodactyl\Models\Nest;
use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\Config\Repository;
use Pterodactyl\Traits\Services\CreatesServiceIndex; use Pterodactyl\Services\Nests\NestCreationService;
use Pterodactyl\Services\Services\NestCreationService;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
class ServiceCreationServiceTest extends TestCase class NestCreationServiceTest extends TestCase
{ {
use CreatesServiceIndex; use KnownUuid;
/** /**
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock * @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
@ -33,15 +33,10 @@ class ServiceCreationServiceTest extends TestCase
protected $repository; protected $repository;
/** /**
* @var \Pterodactyl\Services\Services\NestCreationService * @var \Pterodactyl\Services\Nests\NestCreationService
*/ */
protected $service; protected $service;
/**
* @var \Ramsey\Uuid\Uuid|\Mockery\Mock
*/
protected $uuid;
/** /**
* Setup tests. * Setup tests.
*/ */
@ -51,7 +46,6 @@ class ServiceCreationServiceTest extends TestCase
$this->config = m::mock(Repository::class); $this->config = m::mock(Repository::class);
$this->repository = m::mock(NestRepositoryInterface::class); $this->repository = m::mock(NestRepositoryInterface::class);
$this->uuid = m::mock('overload:' . Uuid::class);
$this->service = new NestCreationService($this->config, $this->repository); $this->service = new NestCreationService($this->config, $this->repository);
} }
@ -65,19 +59,14 @@ class ServiceCreationServiceTest extends TestCase
$data = [ $data = [
'name' => $model->name, 'name' => $model->name,
'description' => $model->description, 'description' => $model->description,
'folder' => $model->folder,
'startup' => $model->startup,
]; ];
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-0000'); $this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('testauthor@example.com');
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('0000-author');
$this->repository->shouldReceive('create')->with([ $this->repository->shouldReceive('create')->with([
'uuid' => 'uuid-0000', 'uuid' => $this->getKnownUuid(),
'author' => '0000-author', 'author' => 'testauthor@example.com',
'name' => $data['name'], 'name' => $data['name'],
'description' => $data['description'], 'description' => $data['description'],
'startup' => $data['startup'],
'index_file' => $this->getIndexScript(),
], true, true)->once()->andReturn($model); ], true, true)->once()->andReturn($model);
$response = $this->service->handle($data); $response = $this->service->handle($data);

View file

@ -12,25 +12,26 @@ namespace Tests\Unit\Services\Services;
use Exception; use Exception;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Pterodactyl\Services\Services\NestDeletionService; use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Services\Nests\NestDeletionService;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Exceptions\Service\HasActiveServersException; use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class ServiceDeletionServiceTest extends TestCase class NestDeletionServiceTest extends TestCase
{ {
/** /**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
*/ */
protected $serverRepository; protected $serverRepository;
/** /**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock
*/ */
protected $repository; protected $repository;
/** /**
* @var \Pterodactyl\Services\Services\NestDeletionService * @var \Pterodactyl\Services\Nests\NestDeletionService
*/ */
protected $service; protected $service;
@ -52,7 +53,7 @@ class ServiceDeletionServiceTest extends TestCase
*/ */
public function testServiceIsDeleted() public function testServiceIsDeleted()
{ {
$this->serverRepository->shouldReceive('findCountWhere')->with([['service_id', '=', 1]])->once()->andReturn(0); $this->serverRepository->shouldReceive('findCountWhere')->with([['nest_id', '=', 1]])->once()->andReturn(0);
$this->repository->shouldReceive('delete')->with(1)->once()->andReturn(1); $this->repository->shouldReceive('delete')->with(1)->once()->andReturn(1);
$this->assertEquals(1, $this->service->handle(1)); $this->assertEquals(1, $this->service->handle(1));
@ -62,14 +63,16 @@ class ServiceDeletionServiceTest extends TestCase
* Test that an exception is thrown when there are servers attached to a service. * Test that an exception is thrown when there are servers attached to a service.
* *
* @dataProvider serverCountProvider * @dataProvider serverCountProvider
*
* @param int $count
*/ */
public function testExceptionIsThrownIfServersAreAttached($count) public function testExceptionIsThrownIfServersAreAttached(int $count)
{ {
$this->serverRepository->shouldReceive('findCountWhere')->with([['service_id', '=', 1]])->once()->andReturn($count); $this->serverRepository->shouldReceive('findCountWhere')->with([['nest_id', '=', 1]])->once()->andReturn($count);
try { try {
$this->service->handle(1); $this->service->handle(1);
} catch (Exception $exception) { } catch (PterodactylException $exception) {
$this->assertInstanceOf(HasActiveServersException::class, $exception); $this->assertInstanceOf(HasActiveServersException::class, $exception);
$this->assertEquals(trans('exceptions.service.delete_has_servers'), $exception->getMessage()); $this->assertEquals(trans('exceptions.service.delete_has_servers'), $exception->getMessage());
} }

View file

@ -11,18 +11,18 @@ namespace Tests\Unit\Services\Services;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Pterodactyl\Services\Services\NestUpdateService; use Pterodactyl\Services\Nests\NestUpdateService;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
class ServiceUpdateServiceTest extends TestCase class NestUpdateServiceTest extends TestCase
{ {
/** /**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock
*/ */
protected $repository; protected $repository;
/** /**
* @var \Pterodactyl\Services\Services\NestUpdateService * @var \Pterodactyl\Services\Nests\NestUpdateService
*/ */
protected $service; protected $service;