2017-10-05 03:41:15 +00:00
|
|
|
<?php
|
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
namespace Tests\Unit\Services\Eggs\Sharing;
|
2017-10-05 03:41:15 +00:00
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Models\Egg;
|
|
|
|
use Pterodactyl\Models\Nest;
|
2017-10-08 20:29:46 +00:00
|
|
|
use Tests\Traits\MocksUuids;
|
2017-10-05 03:41:15 +00:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Models\EggVariable;
|
2017-10-05 03:41:15 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2017-10-08 04:29:08 +00:00
|
|
|
use Pterodactyl\Services\Eggs\Sharing\EggImporterService;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
|
2017-10-09 04:50:52 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException;
|
|
|
|
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
|
2017-10-08 04:29:08 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
2017-10-05 03:41:15 +00:00
|
|
|
|
2017-10-08 04:29:08 +00:00
|
|
|
class EggImporterServiceTest extends TestCase
|
2017-10-05 03:41:15 +00:00
|
|
|
{
|
2017-10-08 20:29:46 +00:00
|
|
|
use MocksUuids;
|
2017-10-08 04:29:08 +00:00
|
|
|
|
2017-10-05 03:41:15 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/**
|
2017-10-08 04:29:08 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
|
2017-10-05 03:41:15 +00:00
|
|
|
*/
|
2017-10-08 04:29:08 +00:00
|
|
|
protected $eggVariableRepository;
|
2017-10-05 03:41:15 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-08 04:29:08 +00:00
|
|
|
* @var \Illuminate\Http\UploadedFile|\Mockery\Mock
|
2017-10-05 03:41:15 +00:00
|
|
|
*/
|
2017-10-08 04:29:08 +00:00
|
|
|
protected $file;
|
2017-10-05 03:41:15 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-07 04:57:53 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock
|
2017-10-05 03:41:15 +00:00
|
|
|
*/
|
2017-10-08 04:29:08 +00:00
|
|
|
protected $nestRepository;
|
2017-10-05 03:41:15 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-08 04:29:08 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
|
2017-10-05 03:41:15 +00:00
|
|
|
*/
|
2017-10-08 04:29:08 +00:00
|
|
|
protected $repository;
|
2017-10-05 03:41:15 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-08 04:29:08 +00:00
|
|
|
* @var \Pterodactyl\Services\Eggs\Sharing\EggImporterService
|
2017-10-05 03:41:15 +00:00
|
|
|
*/
|
2017-10-08 04:29:08 +00:00
|
|
|
protected $service;
|
2017-10-05 03:41:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-10-05 03:41:15 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->file = m::mock(UploadedFile::class);
|
2017-10-05 03:41:15 +00:00
|
|
|
$this->connection = m::mock(ConnectionInterface::class);
|
2017-10-08 04:29:08 +00:00
|
|
|
$this->eggVariableRepository = m::mock(EggVariableRepositoryInterface::class);
|
|
|
|
$this->nestRepository = m::mock(NestRepositoryInterface::class);
|
2017-10-07 04:57:53 +00:00
|
|
|
$this->repository = m::mock(EggRepositoryInterface::class);
|
2017-10-05 03:41:15 +00:00
|
|
|
|
2017-10-07 04:57:53 +00:00
|
|
|
$this->service = new EggImporterService(
|
2017-10-08 04:29:08 +00:00
|
|
|
$this->connection, $this->repository, $this->eggVariableRepository, $this->nestRepository
|
2017-10-05 03:41:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a service option can be successfully imported.
|
|
|
|
*/
|
2017-10-08 04:29:08 +00:00
|
|
|
public function testEggConfigurationIsImported()
|
2017-10-05 03:41:15 +00:00
|
|
|
{
|
2020-10-06 04:29:35 +00:00
|
|
|
$egg = factory(Egg::class)->make(['id' => 123]);
|
|
|
|
$nest = factory(Nest::class)->make(['id' => 456]);
|
2017-10-05 03:41:15 +00:00
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->file->expects('getError')->andReturn(UPLOAD_ERR_OK);
|
|
|
|
$this->file->expects('isFile')->andReturn(true);
|
|
|
|
$this->file->expects('getSize')->andReturn(100);
|
|
|
|
|
|
|
|
$this->file->expects('openFile->fread')->with(100)->once()->andReturn(json_encode([
|
2017-10-05 03:41:15 +00:00
|
|
|
'meta' => ['version' => 'PTDL_v1'],
|
2017-10-08 04:29:08 +00:00
|
|
|
'name' => $egg->name,
|
2017-10-09 04:50:52 +00:00
|
|
|
'author' => $egg->author,
|
2017-10-05 03:41:15 +00:00
|
|
|
'variables' => [
|
2017-10-07 04:57:53 +00:00
|
|
|
$variable = factory(EggVariable::class)->make(),
|
2017-10-05 03:41:15 +00:00
|
|
|
],
|
|
|
|
]));
|
2017-10-08 04:29:08 +00:00
|
|
|
$this->nestRepository->shouldReceive('getWithEggs')->with($nest->id)->once()->andReturn($nest);
|
2017-10-05 03:41:15 +00:00
|
|
|
|
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('create')->with(m::subset([
|
2017-10-08 04:29:08 +00:00
|
|
|
'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,
|
2017-10-05 03:41:15 +00:00
|
|
|
'env_variable' => $variable->env_variable,
|
|
|
|
]))->once()->andReturnNull();
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
2017-10-08 04:29:08 +00:00
|
|
|
$response = $this->service->handle($this->file, $nest->id);
|
2017-10-05 03:41:15 +00:00
|
|
|
$this->assertNotEmpty($response);
|
2017-10-07 04:57:53 +00:00
|
|
|
$this->assertInstanceOf(Egg::class, $response);
|
2017-10-08 04:29:08 +00:00
|
|
|
$this->assertSame($egg, $response);
|
2017-10-05 03:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if the file is invalid.
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfFileIsInvalid()
|
|
|
|
{
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->expectException(InvalidFileUploadException::class);
|
|
|
|
$this->expectExceptionMessage(
|
|
|
|
'The selected file ["test.txt"] was not in a valid format to import. (is_file: true is_valid: true err_code: 4 err: UPLOAD_ERR_NO_FILE)'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->file->expects('getFilename')->andReturns('test.txt');
|
|
|
|
$this->file->expects('isFile')->andReturns(true);
|
|
|
|
$this->file->expects('isValid')->andReturns(true);
|
|
|
|
$this->file->expects('getError')->twice()->andReturns(UPLOAD_ERR_NO_FILE);
|
|
|
|
$this->file->expects('getErrorMessage')->andReturns('UPLOAD_ERR_NO_FILE');
|
|
|
|
|
|
|
|
$this->service->handle($this->file, 1234);
|
2017-10-05 03:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if the file is not a file.
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfFileIsNotAFile()
|
|
|
|
{
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->expectException(InvalidFileUploadException::class);
|
|
|
|
$this->expectExceptionMessage(
|
|
|
|
'The selected file ["test.txt"] was not in a valid format to import. (is_file: false is_valid: true err_code: 4 err: UPLOAD_ERR_NO_FILE)'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->file->expects('getFilename')->andReturns('test.txt');
|
|
|
|
$this->file->expects('isFile')->andReturns(false);
|
|
|
|
$this->file->expects('isValid')->andReturns(true);
|
|
|
|
$this->file->expects('getError')->twice()->andReturns(UPLOAD_ERR_NO_FILE);
|
|
|
|
$this->file->expects('getErrorMessage')->andReturns('UPLOAD_ERR_NO_FILE');
|
|
|
|
|
|
|
|
$this->service->handle($this->file, 1234);
|
2017-10-05 03:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if the JSON metadata is invalid.
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfJsonMetaDataIsInvalid()
|
|
|
|
{
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->expectException(InvalidFileUploadException::class);
|
|
|
|
$this->expectExceptionMessage(trans('exceptions.nest.importer.invalid_json_provided'));
|
|
|
|
|
|
|
|
$this->file->expects('getError')->andReturn(UPLOAD_ERR_OK);
|
|
|
|
$this->file->expects('isFile')->andReturn(true);
|
|
|
|
$this->file->expects('getSize')->andReturn(100);
|
|
|
|
|
|
|
|
$this->file->expects('openFile->fread')->with(100)->andReturn(json_encode([
|
2017-10-05 03:41:15 +00:00
|
|
|
'meta' => ['version' => 'hodor'],
|
|
|
|
]));
|
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->service->handle($this->file, 1234);
|
2017-10-05 03:41:15 +00:00
|
|
|
}
|
2017-10-09 04:50:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if bad JSON is provided.
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfBadJsonIsProvided()
|
|
|
|
{
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->expectException(BadJsonFormatException::class);
|
|
|
|
$this->expectExceptionMessage(trans('exceptions.nest.importer.json_error', [
|
|
|
|
'error' => 'Syntax error',
|
|
|
|
]));
|
|
|
|
|
|
|
|
$this->file->expects('getError')->andReturn(UPLOAD_ERR_OK);
|
|
|
|
$this->file->expects('isFile')->andReturn(true);
|
|
|
|
$this->file->expects('getSize')->andReturn(100);
|
|
|
|
$this->file->expects('openFile->fread')->with(100)->andReturn('}');
|
|
|
|
|
|
|
|
$this->service->handle($this->file, 1234);
|
2017-10-09 04:50:52 +00:00
|
|
|
}
|
2017-10-05 03:41:15 +00:00
|
|
|
}
|