2017-08-22 02:00:14 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-08-22 02:00:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Packs;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Mockery as m;
|
2017-08-27 19:55:25 +00:00
|
|
|
use Tests\TestCase;
|
|
|
|
use Pterodactyl\Models\Pack;
|
2017-10-08 20:44:28 +00:00
|
|
|
use Tests\Traits\MocksUuids;
|
2017-08-27 19:55:25 +00:00
|
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
use Illuminate\Contracts\Filesystem\Factory;
|
2017-08-22 02:00:14 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2017-08-27 19:55:25 +00:00
|
|
|
use Pterodactyl\Services\Packs\PackCreationService;
|
2017-08-22 02:00:14 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
|
2017-10-09 04:50:52 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
|
2017-08-27 19:55:25 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\Pack\InvalidFileMimeTypeException;
|
2017-08-22 02:00:14 +00:00
|
|
|
|
|
|
|
class PackCreationServiceTest extends TestCase
|
|
|
|
{
|
2017-10-08 20:44:28 +00:00
|
|
|
use MocksUuids;
|
|
|
|
|
2017-08-22 02:00:14 +00:00
|
|
|
/**
|
2017-10-08 20:44:28 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
2017-08-22 02:00:14 +00:00
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/**
|
2017-10-08 20:44:28 +00:00
|
|
|
* @var \Illuminate\Http\UploadedFile|\Mockery\Mock
|
2017-08-22 02:00:14 +00:00
|
|
|
*/
|
|
|
|
protected $file;
|
|
|
|
|
|
|
|
/**
|
2017-10-08 20:44:28 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface|\Mockery\Mock
|
2017-08-22 02:00:14 +00:00
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Packs\PackCreationService
|
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
/**
|
2017-10-08 20:44:28 +00:00
|
|
|
* @var \Illuminate\Contracts\Filesystem\Factory|\Mockery\Mock
|
2017-08-22 02:00:14 +00:00
|
|
|
*/
|
|
|
|
protected $storage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-08-22 02:00:14 +00:00
|
|
|
{
|
|
|
|
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([
|
2017-10-08 20:44:28 +00:00
|
|
|
'uuid' => $this->getKnownUuid(),
|
2017-08-22 02:00:14 +00:00
|
|
|
'selectable' => false,
|
|
|
|
'visible' => false,
|
|
|
|
'locked' => false,
|
|
|
|
'test-data' => 'value',
|
|
|
|
])->once()->andReturn($model);
|
|
|
|
|
2017-10-08 20:44:28 +00:00
|
|
|
$this->storage->shouldReceive('disk->makeDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
|
2017-08-22 02:00:14 +00:00
|
|
|
$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([
|
2017-10-08 20:44:28 +00:00
|
|
|
'uuid' => $this->getKnownUuid(),
|
2017-08-22 02:00:14 +00:00
|
|
|
'selectable' => false,
|
|
|
|
'visible' => false,
|
|
|
|
'locked' => false,
|
|
|
|
'test-data' => 'value',
|
|
|
|
])->once()->andReturn($model);
|
|
|
|
|
2017-10-08 20:44:28 +00:00
|
|
|
$this->storage->shouldReceive('disk->makeDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
|
2017-08-22 02:00:14 +00:00
|
|
|
$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);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertEquals(trans('exceptions.packs.invalid_upload'), $exception->getMessage());
|
2017-08-22 02:00:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertEquals(trans('exceptions.packs.invalid_mime', [
|
2017-08-22 02:00:14 +00:00
|
|
|
'type' => implode(', ', PackCreationService::VALID_UPLOAD_TYPES),
|
|
|
|
]), $exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-13 14:50:56 +00:00
|
|
|
* Return an array of valid mimetypes to test against.
|
2017-08-22 02:00:14 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function mimetypeProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['application/gzip'],
|
|
|
|
['application/x-gzip'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-13 14:50:56 +00:00
|
|
|
* Provide invalid mimetypes to test exceptions against.
|
2017-08-22 02:00:14 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function invalidMimetypeProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['application/zip'],
|
|
|
|
['text/plain'],
|
|
|
|
['image/jpeg'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|