Add ability to update an Egg by re-uploading a file.
This commit is contained in:
parent
e2cb789b2b
commit
e01d7497f5
17 changed files with 449 additions and 23 deletions
|
@ -21,7 +21,8 @@ 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\Exceptions\Service\Egg\BadJsonFormatException;
|
||||
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
|
||||
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
||||
|
||||
class EggImporterServiceTest extends TestCase
|
||||
|
@ -90,7 +91,7 @@ class EggImporterServiceTest extends TestCase
|
|||
$this->file->shouldReceive('openFile->fread')->with(100)->once()->andReturn(json_encode([
|
||||
'meta' => ['version' => 'PTDL_v1'],
|
||||
'name' => $egg->name,
|
||||
'tag' => $egg->tag,
|
||||
'author' => $egg->author,
|
||||
'variables' => [
|
||||
$variable = factory(EggVariable::class)->make(),
|
||||
],
|
||||
|
@ -165,4 +166,24 @@ class EggImporterServiceTest extends TestCase
|
|||
$this->assertEquals(trans('exceptions.nest.importer.invalid_json_provided'), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if bad JSON is provided.
|
||||
*/
|
||||
public function testExceptionIsThrownIfBadJsonIsProvided()
|
||||
{
|
||||
$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('}');
|
||||
|
||||
try {
|
||||
$this->service->handle($this->file, 1234);
|
||||
} catch (PterodactylException $exception) {
|
||||
$this->assertInstanceOf(BadJsonFormatException::class, $exception);
|
||||
$this->assertEquals(trans('exceptions.nest.importer.json_error', [
|
||||
'error' => json_last_error_msg(),
|
||||
]), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,212 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Eggs\Sharing;
|
||||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\Egg;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Pterodactyl\Models\EggVariable;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Exceptions\PterodactylException;
|
||||
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException;
|
||||
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
|
||||
use Pterodactyl\Services\Eggs\Sharing\EggUpdateImporterService;
|
||||
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
||||
|
||||
class EggUpdateImporterServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Http\UploadedFile|\Mockery\Mock
|
||||
*/
|
||||
protected $file;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Eggs\Sharing\EggUpdateImporterService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $variableRepository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->connection = m::mock(ConnectionInterface::class);
|
||||
$this->file = m::mock(UploadedFile::class);
|
||||
$this->repository = m::mock(EggRepositoryInterface::class);
|
||||
$this->variableRepository = m::mock(EggVariableRepositoryInterface::class);
|
||||
|
||||
$this->service = new EggUpdateImporterService($this->connection, $this->repository, $this->variableRepository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an egg update is handled correctly using an uploaded file.
|
||||
*/
|
||||
public function testEggIsUpdated()
|
||||
{
|
||||
$egg = factory(Egg::class)->make();
|
||||
$variable = factory(EggVariable::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,
|
||||
'author' => 'newauthor@example.com',
|
||||
'variables' => [$variable->toArray()],
|
||||
]));
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('update')->with($egg->id, m::subset([
|
||||
'author' => 'newauthor@example.com',
|
||||
'name' => $egg->name,
|
||||
]), true, true)->once()->andReturn($egg);
|
||||
|
||||
$this->variableRepository->shouldReceive('withoutFresh->updateOrCreate')->with([
|
||||
'egg_id' => $egg->id,
|
||||
'env_variable' => $variable->env_variable,
|
||||
], collect($variable)->except(['egg_id', 'env_variable'])->toArray())->once()->andReturnNull();
|
||||
|
||||
$this->variableRepository->shouldReceive('withColumns')->with(['id', 'env_variable'])->once()->andReturnSelf()
|
||||
->shouldReceive('findWhere')->with([['egg_id', '=', $egg->id]])->once()->andReturn([$variable]);
|
||||
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->service->handle($egg->id, $this->file);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an imported file with less variables than currently existing deletes
|
||||
* the un-needed variables from the database.
|
||||
*/
|
||||
public function testVariablesMissingFromImportAreDeleted()
|
||||
{
|
||||
$egg = factory(Egg::class)->make();
|
||||
$variable1 = factory(EggVariable::class)->make();
|
||||
$variable2 = factory(EggVariable::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,
|
||||
'author' => 'newauthor@example.com',
|
||||
'variables' => [$variable1->toArray()],
|
||||
]));
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('update')->with($egg->id, m::subset([
|
||||
'author' => 'newauthor@example.com',
|
||||
'name' => $egg->name,
|
||||
]), true, true)->once()->andReturn($egg);
|
||||
|
||||
$this->variableRepository->shouldReceive('withoutFresh->updateOrCreate')->with([
|
||||
'egg_id' => $egg->id,
|
||||
'env_variable' => $variable1->env_variable,
|
||||
], collect($variable1)->except(['egg_id', 'env_variable'])->toArray())->once()->andReturnNull();
|
||||
|
||||
$this->variableRepository->shouldReceive('withColumns')->with(['id', 'env_variable'])->once()->andReturnSelf()
|
||||
->shouldReceive('findWhere')->with([['egg_id', '=', $egg->id]])->once()->andReturn([$variable1, $variable2]);
|
||||
|
||||
$this->variableRepository->shouldReceive('deleteWhere')->with([
|
||||
['egg_id', '=', $egg->id],
|
||||
['env_variable', '=', $variable2->env_variable],
|
||||
])->once()->andReturnNull();
|
||||
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->service->handle($egg->id, $this->file);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(1234, $this->file);
|
||||
} 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(1234, $this->file);
|
||||
} 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(1234, $this->file);
|
||||
} catch (PterodactylException $exception) {
|
||||
$this->assertInstanceOf(InvalidFileUploadException::class, $exception);
|
||||
$this->assertEquals(trans('exceptions.nest.importer.invalid_json_provided'), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if bad JSON is provided.
|
||||
*/
|
||||
public function testExceptionIsThrownIfBadJsonIsProvided()
|
||||
{
|
||||
$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('}');
|
||||
|
||||
try {
|
||||
$this->service->handle(1234, $this->file);
|
||||
} catch (PterodactylException $exception) {
|
||||
$this->assertInstanceOf(BadJsonFormatException::class, $exception);
|
||||
$this->assertEquals(trans('exceptions.nest.importer.json_error', [
|
||||
'error' => json_last_error_msg(),
|
||||
]), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,7 +19,7 @@ use Illuminate\Contracts\Filesystem\Factory;
|
|||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Services\Packs\PackCreationService;
|
||||
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\Pack\InvalidFileUploadException;
|
||||
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
|
||||
use Pterodactyl\Exceptions\Service\Pack\InvalidFileMimeTypeException;
|
||||
|
||||
class PackCreationServiceTest extends TestCase
|
||||
|
|
|
@ -16,8 +16,8 @@ use Pterodactyl\Models\Pack;
|
|||
use Illuminate\Http\UploadedFile;
|
||||
use Pterodactyl\Services\Packs\PackCreationService;
|
||||
use Pterodactyl\Services\Packs\TemplateUploadService;
|
||||
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
|
||||
use Pterodactyl\Exceptions\Service\Pack\ZipExtractionException;
|
||||
use Pterodactyl\Exceptions\Service\Pack\InvalidFileUploadException;
|
||||
use Pterodactyl\Exceptions\Service\Pack\InvalidFileMimeTypeException;
|
||||
use Pterodactyl\Exceptions\Service\Pack\UnreadableZipArchiveException;
|
||||
use Pterodactyl\Exceptions\Service\Pack\InvalidPackArchiveFormatException;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue