2017-10-09 04:50:52 +00:00
|
|
|
<?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.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-10-09 04:50:52 +00:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2020-10-06 04:29:35 +00:00
|
|
|
$egg = factory(Egg::class)->make(['id' => 123]);
|
2017-10-09 04:50:52 +00:00
|
|
|
$variable = factory(EggVariable::class)->make();
|
|
|
|
|
2017-11-04 04:07:18 +00:00
|
|
|
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK);
|
2017-10-09 04:50:52 +00:00
|
|
|
$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);
|
|
|
|
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->variableRepository->shouldReceive('withoutFreshModel->updateOrCreate')->with([
|
2017-10-09 04:50:52 +00:00
|
|
|
'egg_id' => $egg->id,
|
|
|
|
'env_variable' => $variable->env_variable,
|
|
|
|
], collect($variable)->except(['egg_id', 'env_variable'])->toArray())->once()->andReturnNull();
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->variableRepository->shouldReceive('setColumns')->with(['id', 'env_variable'])->once()->andReturnSelf()
|
|
|
|
->shouldReceive('findWhere')->with([['egg_id', '=', $egg->id]])->once()->andReturn(collect([$variable]));
|
2017-10-09 04:50:52 +00:00
|
|
|
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->service->handle($egg, $this->file);
|
2017-10-09 04:50:52 +00:00
|
|
|
$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()
|
|
|
|
{
|
2020-10-06 04:29:35 +00:00
|
|
|
$egg = factory(Egg::class)->make(['id' => 123]);
|
2017-10-09 04:50:52 +00:00
|
|
|
$variable1 = factory(EggVariable::class)->make();
|
|
|
|
$variable2 = factory(EggVariable::class)->make();
|
|
|
|
|
2017-11-04 04:07:18 +00:00
|
|
|
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK);
|
2017-10-09 04:50:52 +00:00
|
|
|
$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);
|
|
|
|
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->variableRepository->shouldReceive('withoutFreshModel->updateOrCreate')->with([
|
2017-10-09 04:50:52 +00:00
|
|
|
'egg_id' => $egg->id,
|
|
|
|
'env_variable' => $variable1->env_variable,
|
|
|
|
], collect($variable1)->except(['egg_id', 'env_variable'])->toArray())->once()->andReturnNull();
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->variableRepository->shouldReceive('setColumns')->with(['id', 'env_variable'])->once()->andReturnSelf()
|
|
|
|
->shouldReceive('findWhere')->with([['egg_id', '=', $egg->id]])->once()->andReturn(collect([$variable1, $variable2]));
|
2017-10-09 04:50:52 +00:00
|
|
|
|
|
|
|
$this->variableRepository->shouldReceive('deleteWhere')->with([
|
|
|
|
['egg_id', '=', $egg->id],
|
|
|
|
['env_variable', '=', $variable2->env_variable],
|
2018-01-05 04:49:50 +00:00
|
|
|
])->once()->andReturn(1);
|
2017-10-09 04:50:52 +00:00
|
|
|
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->service->handle($egg, $this->file);
|
2017-10-09 04:50:52 +00:00
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if the file is invalid.
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfFileIsInvalid()
|
|
|
|
{
|
2020-10-06 04:29:35 +00:00
|
|
|
$egg = factory(Egg::class)->make(['id' => 123]);
|
|
|
|
|
|
|
|
$this->expectException(InvalidFileUploadException::class);
|
|
|
|
$this->expectExceptionMessageMatches('/^The selected file \["test\.txt"\] was not in a valid format to import\./');
|
|
|
|
$file = new UploadedFile('test.txt', 'original.txt', 'application/json', UPLOAD_ERR_NO_FILE, true);
|
|
|
|
|
|
|
|
$this->service->handle($egg, $file);
|
2017-10-09 04:50:52 +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
|
|
|
$egg = factory(Egg::class)->make(['id' => 123]);
|
2017-10-09 04:50:52 +00:00
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->expectException(InvalidFileUploadException::class);
|
|
|
|
$this->expectExceptionMessageMatches('/^The selected file \["test\.txt"\] was not in a valid format to import\./');
|
|
|
|
|
|
|
|
$file = m::mock(
|
|
|
|
new UploadedFile('test.txt', 'original.txt', 'application/json', UPLOAD_ERR_INI_SIZE, true)
|
|
|
|
)->makePartial();
|
|
|
|
|
|
|
|
$file->expects('isFile')->andReturnFalse();
|
|
|
|
|
|
|
|
$this->service->handle($egg, $file);
|
2017-10-09 04:50:52 +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
|
|
|
$egg = factory(Egg::class)->make(['id' => 123]);
|
|
|
|
|
2017-11-04 04:07:18 +00:00
|
|
|
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK);
|
2017-10-09 04:50:52 +00:00
|
|
|
$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 {
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->service->handle($egg, $this->file);
|
2017-10-09 04:50:52 +00:00
|
|
|
} 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()
|
|
|
|
{
|
2020-10-06 04:29:35 +00:00
|
|
|
$egg = factory(Egg::class)->make(['id' => 123]);
|
|
|
|
|
2017-11-04 04:07:18 +00:00
|
|
|
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK);
|
2017-10-09 04:50:52 +00:00
|
|
|
$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 {
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->service->handle($egg, $this->file);
|
2017-10-09 04:50:52 +00:00
|
|
|
} catch (PterodactylException $exception) {
|
|
|
|
$this->assertInstanceOf(BadJsonFormatException::class, $exception);
|
|
|
|
$this->assertEquals(trans('exceptions.nest.importer.json_error', [
|
|
|
|
'error' => json_last_error_msg(),
|
|
|
|
]), $exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|