Remove all references of packs from the Panel
This commit is contained in:
parent
f1978683cc
commit
3c7ffaaadb
60 changed files with 129 additions and 2517 deletions
|
@ -1,150 +0,0 @@
|
|||
<?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\Packs;
|
||||
|
||||
use ZipArchive;
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use phpmock\phpunit\PHPMock;
|
||||
use Pterodactyl\Models\Pack;
|
||||
use Illuminate\Contracts\Filesystem\Factory;
|
||||
use Pterodactyl\Services\Packs\ExportPackService;
|
||||
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\Pack\ZipArchiveCreationException;
|
||||
|
||||
class ExportPackServiceTest extends TestCase
|
||||
{
|
||||
use PHPMock;
|
||||
|
||||
/**
|
||||
* @var \ZipArchive
|
||||
*/
|
||||
protected $archive;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Packs\ExportPackService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Filesystem\Factory
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->archive = m::mock(ZipArchive::class);
|
||||
$this->repository = m::mock(PackRepositoryInterface::class);
|
||||
$this->storage = m::mock(Factory::class);
|
||||
|
||||
$this->service = new ExportPackService($this->storage, $this->repository, $this->archive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide standard data to all tests.
|
||||
*/
|
||||
protected function setupTestData()
|
||||
{
|
||||
$this->model = factory(Pack::class)->make();
|
||||
$this->json = [
|
||||
'name' => $this->model->name,
|
||||
'version' => $this->model->version,
|
||||
'description' => $this->model->description,
|
||||
'selectable' => $this->model->selectable,
|
||||
'visible' => $this->model->visible,
|
||||
'locked' => $this->model->locked,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an archive of the entire pack can be exported.
|
||||
*/
|
||||
public function testFilesAreBundledIntoZipWhenRequested()
|
||||
{
|
||||
$this->setupTestData();
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'tempnam')
|
||||
->expects($this->once())->willReturn('/tmp/myfile.test');
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fopen')->expects($this->never());
|
||||
|
||||
$this->archive->shouldReceive('open')->with('/tmp/myfile.test', $this->archive::CREATE)->once()->andReturnSelf();
|
||||
$this->storage->shouldReceive('disk->files')->with('packs/' . $this->model->uuid)->once()->andReturn(['file_one']);
|
||||
$this->archive->shouldReceive('addFile')->with(storage_path('app/file_one'), 'file_one')->once()->andReturnSelf();
|
||||
$this->archive->shouldReceive('addFromString')->with('import.json', json_encode($this->json, JSON_PRETTY_PRINT))->once()->andReturnSelf();
|
||||
$this->archive->shouldReceive('close')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->handle($this->model, true);
|
||||
$this->assertEquals('/tmp/myfile.test', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the pack configuration can be saved as a json file.
|
||||
*/
|
||||
public function testPackConfigurationIsSavedAsJsonFile()
|
||||
{
|
||||
$this->setupTestData();
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'tempnam')
|
||||
->expects($this->once())->willReturn('/tmp/myfile.test');
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fopen')->expects($this->once())->wilLReturn('fp');
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fwrite')
|
||||
->expects($this->once())->with('fp', json_encode($this->json, JSON_PRETTY_PRINT))->willReturn(null);
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fclose')
|
||||
->expects($this->once())->with('fp')->willReturn(null);
|
||||
|
||||
$response = $this->service->handle($this->model);
|
||||
$this->assertEquals('/tmp/myfile.test', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a model ID can be passed in place of the model itself.
|
||||
*/
|
||||
public function testPackIdCanBePassedInPlaceOfModel()
|
||||
{
|
||||
$this->setupTestData();
|
||||
|
||||
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'tempnam')->expects($this->once())->willReturn('/tmp/myfile.test');
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fopen')->expects($this->once())->wilLReturn(null);
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fwrite')->expects($this->once())->willReturn(null);
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fclose')->expects($this->once())->willReturn(null);
|
||||
|
||||
$response = $this->service->handle($this->model->id);
|
||||
$this->assertEquals('/tmp/myfile.test', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown when a ZipArchive cannot be created.
|
||||
*/
|
||||
public function testExceptionIsThrownIfZipArchiveCannotBeCreated()
|
||||
{
|
||||
$this->expectException(ZipArchiveCreationException::class);
|
||||
|
||||
$this->setupTestData();
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'tempnam')
|
||||
->expects($this->once())->willReturn('/tmp/myfile.test');
|
||||
|
||||
$this->archive->shouldReceive('open')->once()->andReturn(false);
|
||||
|
||||
$this->service->handle($this->model, true);
|
||||
}
|
||||
}
|
|
@ -1,182 +0,0 @@
|
|||
<?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\Packs;
|
||||
|
||||
use Exception;
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\Pack;
|
||||
use Tests\Traits\MocksUuids;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Contracts\Filesystem\Factory;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Services\Packs\PackCreationService;
|
||||
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
|
||||
use Pterodactyl\Exceptions\Service\Pack\InvalidFileMimeTypeException;
|
||||
|
||||
class PackCreationServiceTest extends TestCase
|
||||
{
|
||||
use MocksUuids;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Http\UploadedFile|\Mockery\Mock
|
||||
*/
|
||||
protected $file;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Packs\PackCreationService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Filesystem\Factory|\Mockery\Mock
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->connection = m::mock(ConnectionInterface::class);
|
||||
$this->file = m::mock(UploadedFile::class);
|
||||
$this->repository = m::mock(PackRepositoryInterface::class);
|
||||
$this->storage = m::mock(Factory::class);
|
||||
|
||||
$this->service = new PackCreationService($this->connection, $this->storage, $this->repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a pack is created when no file upload is provided.
|
||||
*/
|
||||
public function testPackIsCreatedWhenNoUploadedFileIsPassed()
|
||||
{
|
||||
$model = factory(Pack::class)->make();
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('create')->with([
|
||||
'uuid' => $this->getKnownUuid(),
|
||||
'selectable' => false,
|
||||
'visible' => false,
|
||||
'locked' => false,
|
||||
'test-data' => 'value',
|
||||
])->once()->andReturn($model);
|
||||
|
||||
$this->storage->shouldReceive('disk->makeDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->handle(['test-data' => 'value']);
|
||||
$this->assertInstanceOf(Pack::class, $response);
|
||||
$this->assertEquals($model, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a pack can be created when an uploaded file is provided.
|
||||
*
|
||||
* @dataProvider mimetypeProvider
|
||||
*/
|
||||
public function testPackIsCreatedWhenUploadedFileIsProvided($mime)
|
||||
{
|
||||
$model = factory(Pack::class)->make();
|
||||
|
||||
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
|
||||
$this->file->shouldReceive('getMimeType')->withNoArgs()->once()->andReturn($mime);
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('create')->with([
|
||||
'uuid' => $this->getKnownUuid(),
|
||||
'selectable' => false,
|
||||
'visible' => false,
|
||||
'locked' => false,
|
||||
'test-data' => 'value',
|
||||
])->once()->andReturn($model);
|
||||
|
||||
$this->storage->shouldReceive('disk->makeDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
|
||||
$this->file->shouldReceive('storeAs')->with('packs/' . $model->uuid, 'archive.tar.gz')->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->handle(['test-data' => 'value'], $this->file);
|
||||
$this->assertInstanceOf(Pack::class, $response);
|
||||
$this->assertEquals($model, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if the file upload is not valid.
|
||||
*/
|
||||
public function testExceptionIsThrownIfInvalidUploadIsProvided()
|
||||
{
|
||||
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(false);
|
||||
|
||||
try {
|
||||
$this->service->handle([], $this->file);
|
||||
} catch (Exception $exception) {
|
||||
$this->assertInstanceOf(InvalidFileUploadException::class, $exception);
|
||||
$this->assertEquals(trans('exceptions.packs.invalid_upload'), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown when an invalid mimetype is provided.
|
||||
*
|
||||
* @dataProvider invalidMimetypeProvider
|
||||
*/
|
||||
public function testExceptionIsThrownIfInvalidMimetypeIsFound($mime)
|
||||
{
|
||||
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
|
||||
$this->file->shouldReceive('getMimeType')->withNoArgs()->once()->andReturn($mime);
|
||||
|
||||
try {
|
||||
$this->service->handle([], $this->file);
|
||||
} catch (InvalidFileMimeTypeException $exception) {
|
||||
$this->assertEquals(trans('exceptions.packs.invalid_mime', [
|
||||
'type' => implode(', ', PackCreationService::VALID_UPLOAD_TYPES),
|
||||
]), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of valid mimetypes to test against.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function mimetypeProvider()
|
||||
{
|
||||
return [
|
||||
['application/gzip'],
|
||||
['application/x-gzip'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide invalid mimetypes to test exceptions against.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function invalidMimetypeProvider()
|
||||
{
|
||||
return [
|
||||
['application/zip'],
|
||||
['text/plain'],
|
||||
['image/jpeg'],
|
||||
];
|
||||
}
|
||||
}
|
|
@ -1,120 +0,0 @@
|
|||
<?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\Packs;
|
||||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\Pack;
|
||||
use Illuminate\Contracts\Filesystem\Factory;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Services\Packs\PackDeletionService;
|
||||
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\HasActiveServersException;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
|
||||
class PackDeletionServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
||||
*/
|
||||
protected $serverRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Packs\PackDeletionService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Filesystem\Factory
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->connection = m::mock(ConnectionInterface::class);
|
||||
$this->repository = m::mock(PackRepositoryInterface::class);
|
||||
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
|
||||
$this->storage = m::mock(Factory::class);
|
||||
|
||||
$this->service = new PackDeletionService(
|
||||
$this->connection,
|
||||
$this->storage,
|
||||
$this->repository,
|
||||
$this->serverRepository
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a pack is deleted.
|
||||
*/
|
||||
public function testPackIsDeleted()
|
||||
{
|
||||
$model = factory(Pack::class)->make();
|
||||
|
||||
$this->serverRepository->shouldReceive('findCountWhere')->with([['pack_id', '=', $model->id]])->once()->andReturn(0);
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('delete')->with($model->id)->once()->andReturn(1);
|
||||
$this->storage->shouldReceive('disk')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('deleteDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->service->handle($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a pack ID can be passed in place of the model.
|
||||
*/
|
||||
public function testPackIdCanBePassedInPlaceOfModel()
|
||||
{
|
||||
$model = factory(Pack::class)->make();
|
||||
|
||||
$this->repository->shouldReceive('setColumns')->with(['id', 'uuid'])->once()->andReturnSelf()
|
||||
->shouldReceive('find')->with($model->id)->once()->andReturn($model);
|
||||
$this->serverRepository->shouldReceive('findCountWhere')->with([['pack_id', '=', $model->id]])->once()->andReturn(0);
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('delete')->with($model->id)->once()->andReturn(1);
|
||||
$this->storage->shouldReceive('disk')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('deleteDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->service->handle($model->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception gets thrown if a server is attached to a pack.
|
||||
*/
|
||||
public function testExceptionIsThrownIfServerIsAttachedToPack()
|
||||
{
|
||||
$model = factory(Pack::class)->make();
|
||||
|
||||
$this->serverRepository->shouldReceive('findCountWhere')->with([['pack_id', '=', $model->id]])->once()->andReturn(1);
|
||||
|
||||
try {
|
||||
$this->service->handle($model);
|
||||
} catch (HasActiveServersException $exception) {
|
||||
$this->assertEquals(trans('exceptions.packs.delete_has_servers'), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,99 +0,0 @@
|
|||
<?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\Packs;
|
||||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\Pack;
|
||||
use Pterodactyl\Services\Packs\PackUpdateService;
|
||||
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\HasActiveServersException;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
|
||||
class PackUpdateServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $serverRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Packs\PackUpdateService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->repository = m::mock(PackRepositoryInterface::class);
|
||||
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
|
||||
|
||||
$this->service = new PackUpdateService($this->repository, $this->serverRepository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a pack is updated.
|
||||
*/
|
||||
public function testPackIsUpdated()
|
||||
{
|
||||
$model = factory(Pack::class)->make();
|
||||
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
|
||||
'locked' => false,
|
||||
'visible' => false,
|
||||
'selectable' => false,
|
||||
'test-data' => 'value',
|
||||
])->once()->andReturn(1);
|
||||
|
||||
$this->assertEquals(1, $this->service->handle($model, ['test-data' => 'value']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if the pack option ID is changed while servers are using the pack.
|
||||
*/
|
||||
public function testExceptionIsThrownIfModifyingEggIdWhenServersAreAttached()
|
||||
{
|
||||
$model = factory(Pack::class)->make();
|
||||
$this->serverRepository->shouldReceive('findCountWhere')->with([['pack_id', '=', $model->id]])->once()->andReturn(1);
|
||||
|
||||
try {
|
||||
$this->service->handle($model, ['egg_id' => 0]);
|
||||
} catch (HasActiveServersException $exception) {
|
||||
$this->assertEquals(trans('exceptions.packs.update_has_servers'), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an ID for a pack can be passed in place of the model.
|
||||
*/
|
||||
public function testPackIdCanBePassedInPlaceOfModel()
|
||||
{
|
||||
$model = factory(Pack::class)->make();
|
||||
|
||||
$this->repository->shouldReceive('setColumns')->with(['id', 'egg_id'])->once()->andReturnSelf()
|
||||
->shouldReceive('find')->with($model->id)->once()->andReturn($model);
|
||||
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
|
||||
'locked' => false,
|
||||
'visible' => false,
|
||||
'selectable' => false,
|
||||
'test-data' => 'value',
|
||||
])->once()->andReturn(1);
|
||||
|
||||
$this->assertEquals(1, $this->service->handle($model->id, ['test-data' => 'value']));
|
||||
}
|
||||
}
|
|
@ -1,245 +0,0 @@
|
|||
<?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\Packs;
|
||||
|
||||
use ZipArchive;
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
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\InvalidFileMimeTypeException;
|
||||
use Pterodactyl\Exceptions\Service\Pack\UnreadableZipArchiveException;
|
||||
use Pterodactyl\Exceptions\Service\Pack\InvalidPackArchiveFormatException;
|
||||
|
||||
class TemplateUploadServiceTest extends TestCase
|
||||
{
|
||||
const JSON_FILE_CONTENTS = '{"test_content": "value"}';
|
||||
|
||||
/**
|
||||
* @var \ZipArchive|\Mockery\Mock
|
||||
*/
|
||||
protected $archive;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Packs\PackCreationService|\Mockery\Mock
|
||||
*/
|
||||
protected $creationService;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Http\UploadedFile|\Mockery\Mock
|
||||
*/
|
||||
protected $file;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Packs\TemplateUploadService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->archive = m::mock(ZipArchive::class);
|
||||
$this->creationService = m::mock(PackCreationService::class);
|
||||
$this->file = m::mock(UploadedFile::class);
|
||||
|
||||
$this->service = new TemplateUploadService($this->creationService, $this->archive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a JSON file can be processed and turned into a pack.
|
||||
*
|
||||
* @dataProvider jsonMimetypeProvider
|
||||
*/
|
||||
public function testJsonFileIsProcessed($mime)
|
||||
{
|
||||
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
|
||||
$this->file->shouldReceive('getMimeType')->withNoArgs()->twice()->andReturn($mime);
|
||||
$this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(128);
|
||||
$this->file->shouldReceive('openFile->fread')->with(128)->once()->andReturn(self::JSON_FILE_CONTENTS);
|
||||
|
||||
$this->creationService->shouldReceive('handle')->with(['test_content' => 'value', 'egg_id' => 1])
|
||||
->once()->andReturn(factory(Pack::class)->make());
|
||||
|
||||
$this->assertInstanceOf(Pack::class, $this->service->handle(1, $this->file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a zip file can be processed.
|
||||
*/
|
||||
public function testZipfileIsProcessed()
|
||||
{
|
||||
$model = factory(Pack::class)->make();
|
||||
|
||||
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
|
||||
$this->file->shouldReceive('getMimeType')->withNoArgs()->twice()->andReturn('application/zip');
|
||||
|
||||
$this->file->shouldReceive('getRealPath')->withNoArgs()->once()->andReturn('/test/real');
|
||||
$this->archive->shouldReceive('open')->with('/test/real')->once()->andReturn(true);
|
||||
$this->archive->shouldReceive('locateName')->with('import.json')->once()->andReturn(true);
|
||||
$this->archive->shouldReceive('locateName')->with('archive.tar.gz')->once()->andReturn(true);
|
||||
$this->archive->shouldReceive('getFromName')->with('import.json')->once()->andReturn(self::JSON_FILE_CONTENTS);
|
||||
$this->creationService->shouldReceive('handle')->with(['test_content' => 'value', 'egg_id' => 1])
|
||||
->once()->andReturn($model);
|
||||
$this->archive->shouldReceive('extractTo')->with(storage_path('app/packs/' . $model->uuid), 'archive.tar.gz')
|
||||
->once()->andReturn(true);
|
||||
$this->archive->shouldReceive('close')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->assertInstanceOf(Pack::class, $this->service->handle(1, $this->file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if the file upload is invalid.
|
||||
*/
|
||||
public function testExceptionIsThrownIfFileUploadIsInvalid()
|
||||
{
|
||||
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(false);
|
||||
|
||||
try {
|
||||
$this->service->handle(1, $this->file);
|
||||
} catch (InvalidFileUploadException $exception) {
|
||||
$this->assertEquals(trans('exceptions.packs.invalid_upload'), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an invalid mimetype throws an exception.
|
||||
*
|
||||
* @dataProvider invalidMimetypeProvider
|
||||
*/
|
||||
public function testExceptionIsThrownIfMimetypeIsInvalid($mime)
|
||||
{
|
||||
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
|
||||
$this->file->shouldReceive('getMimeType')->withNoArgs()->once()->andReturn($mime);
|
||||
|
||||
try {
|
||||
$this->service->handle(1, $this->file);
|
||||
} catch (InvalidFileMimeTypeException $exception) {
|
||||
$this->assertEquals(trans('exceptions.packs.invalid_mime', [
|
||||
'type' => implode(', ', TemplateUploadService::VALID_UPLOAD_TYPES),
|
||||
]), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if the zip is unreadable.
|
||||
*/
|
||||
public function testExceptionIsThrownIfZipArchiveIsUnreadable()
|
||||
{
|
||||
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
|
||||
$this->file->shouldReceive('getMimeType')->withNoArgs()->twice()->andReturn('application/zip');
|
||||
|
||||
$this->file->shouldReceive('getRealPath')->withNoArgs()->once()->andReturn('/test/path');
|
||||
$this->archive->shouldReceive('open')->with('/test/path')->once()->andReturn(false);
|
||||
|
||||
try {
|
||||
$this->service->handle(1, $this->file);
|
||||
} catch (UnreadableZipArchiveException $exception) {
|
||||
$this->assertEquals(trans('exceptions.packs.unreadable'), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a zip missing the required files throws an exception.
|
||||
*
|
||||
* @dataProvider filenameProvider
|
||||
*/
|
||||
public function testExceptionIsThrownIfZipDoesNotContainProperFiles($a, $b)
|
||||
{
|
||||
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
|
||||
$this->file->shouldReceive('getMimeType')->withNoArgs()->twice()->andReturn('application/zip');
|
||||
|
||||
$this->file->shouldReceive('getRealPath')->withNoArgs()->once()->andReturn('/test/path');
|
||||
$this->archive->shouldReceive('open')->with('/test/path')->once()->andReturn(true);
|
||||
$this->archive->shouldReceive('locateName')->with('import.json')->once()->andReturn($a);
|
||||
|
||||
if ($a) {
|
||||
$this->archive->shouldReceive('locateName')->with('archive.tar.gz')->once()->andReturn($b);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->service->handle(1, $this->file);
|
||||
} catch (InvalidPackArchiveFormatException $exception) {
|
||||
$this->assertEquals(trans('exceptions.packs.invalid_archive_exception'), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if an archive cannot be extracted from the zip file.
|
||||
*/
|
||||
public function testExceptionIsThrownIfArchiveCannotBeExtractedFromZip()
|
||||
{
|
||||
$model = factory(Pack::class)->make();
|
||||
|
||||
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
|
||||
$this->file->shouldReceive('getMimeType')->withNoArgs()->twice()->andReturn('application/zip');
|
||||
|
||||
$this->file->shouldReceive('getRealPath')->withNoArgs()->once()->andReturn('/test/real');
|
||||
$this->archive->shouldReceive('open')->once()->andReturn(true);
|
||||
$this->archive->shouldReceive('locateName')->twice()->andReturn(true);
|
||||
$this->archive->shouldReceive('getFromName')->once()->andReturn(self::JSON_FILE_CONTENTS);
|
||||
$this->creationService->shouldReceive('handle')->once()->andReturn($model);
|
||||
$this->archive->shouldReceive('extractTo')->once()->andReturn(false);
|
||||
|
||||
try {
|
||||
$this->service->handle(1, $this->file);
|
||||
} catch (ZipExtractionException $exception) {
|
||||
$this->assertEquals(trans('exceptions.packs.zip_extraction'), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide valid JSON mimetypes to use in tests.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function jsonMimetypeProvider()
|
||||
{
|
||||
return [
|
||||
['text/plain'],
|
||||
['application/json'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return invalid mimetypes for testing.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function invalidMimetypeProvider()
|
||||
{
|
||||
return [
|
||||
['application/gzip'],
|
||||
['application/x-gzip'],
|
||||
['image/jpeg'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return values for archive->locateName function, import.json and archive.tar.gz respectively.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function filenameProvider()
|
||||
{
|
||||
return [
|
||||
[true, false],
|
||||
[false, true],
|
||||
[false, false],
|
||||
];
|
||||
}
|
||||
}
|
|
@ -42,7 +42,6 @@ class ServerConfigurationStructureServiceTest extends TestCase
|
|||
{
|
||||
/** @var \Pterodactyl\Models\Server $model */
|
||||
$model = factory(Server::class)->make();
|
||||
$model->setRelation('pack', null);
|
||||
$model->setRelation('allocation', factory(Allocation::class)->make());
|
||||
$model->setRelation('allocations', collect(factory(Allocation::class)->times(2)->make()));
|
||||
$model->setRelation('egg', factory(Egg::class)->make());
|
||||
|
@ -82,7 +81,6 @@ class ServerConfigurationStructureServiceTest extends TestCase
|
|||
|
||||
$this->assertSame([
|
||||
'egg' => $model->egg->uuid,
|
||||
'pack' => null,
|
||||
'skip_scripts' => $model->skip_scripts,
|
||||
], $response['service']);
|
||||
|
||||
|
|
|
@ -140,12 +140,10 @@ class StartupModificationServiceTest extends TestCase
|
|||
'installed' => 0,
|
||||
'nest_id' => $eggModel->nest_id,
|
||||
'egg_id' => $eggModel->id,
|
||||
'pack_id' => 789,
|
||||
'image' => 'docker:image',
|
||||
]))->once()->andReturn($model);
|
||||
$this->repository->shouldReceive('getDaemonServiceData')->with($model, true)->once()->andReturn([
|
||||
'egg' => 'abcd1234',
|
||||
'pack' => 'xyz987',
|
||||
]);
|
||||
|
||||
$this->environmentService->shouldReceive('handle')->with($model)->once()->andReturn(['env']);
|
||||
|
@ -158,7 +156,6 @@ class StartupModificationServiceTest extends TestCase
|
|||
],
|
||||
'service' => [
|
||||
'egg' => 'abcd1234',
|
||||
'pack' => 'xyz987',
|
||||
'skip_scripts' => false,
|
||||
],
|
||||
])->once()->andReturn(new Response);
|
||||
|
@ -170,7 +167,6 @@ class StartupModificationServiceTest extends TestCase
|
|||
$response = $service->handle($model, [
|
||||
'docker_image' => 'docker:image',
|
||||
'egg_id' => $eggModel->id,
|
||||
'pack_id' => 789,
|
||||
'environment' => ['test' => 'abcd1234'],
|
||||
]);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue