1
1
mirror of https://github.com/pterodactyl/panel.git synced 2024-11-22 09:02:28 +01:00

Tests a'hoy

This commit is contained in:
Dane Everitt 2017-10-23 21:10:32 -05:00
parent 532025a348
commit 57db949a9c
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
5 changed files with 210 additions and 5 deletions

View File

@ -28,9 +28,9 @@ class DisplayException extends PterodactylException
* @param string $message
* @param Throwable|null $previous
* @param string $level
* @internal param mixed $log
* @param int $code
*/
public function __construct($message, Throwable $previous = null, $level = self::LEVEL_ERROR)
public function __construct($message, Throwable $previous = null, $level = self::LEVEL_ERROR, $code = 0)
{
$this->level = $level;
@ -38,7 +38,7 @@ class DisplayException extends PterodactylException
Log::{$level}($previous);
}
parent::__construct($message);
parent::__construct($message, $code, $previous);
}
/**

View File

@ -0,0 +1,49 @@
<?php
namespace Tests\Traits;
use Mockery;
use Mockery\MockInterface;
use GuzzleHttp\Exception\RequestException;
trait MocksRequestException
{
/**
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
*/
private $exception;
/**
* @var mixed
*/
private $exceptionResponse;
/**
* Configure the exception mock to work with the Panel's default exception
* handler actions.
*/
public function configureExceptionMock()
{
$this->getExceptionMock()->shouldReceive('getResponse')->andReturn($this->exceptionResponse);
}
/**
* Return a mocked instance of the request exception.
*
* @return \Mockery\MockInterface
*/
private function getExceptionMock(): MockInterface
{
return $this->exception ?? $this->exception = Mockery::mock(RequestException::class);
}
/**
* Set the exception response.
*
* @param mixed $response
*/
protected function setExceptionResponse($response)
{
$this->exceptionResponse = $response;
}
}

View File

@ -0,0 +1,156 @@
<?php
namespace Tests\Unit\Services\Allocations;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Allocation;
use Tests\Traits\MocksRequestException;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Services\Allocations\SetDefaultAllocationService;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonRepositoryInterface;
class SetDefaultAllocationServiceTest extends TestCase
{
use MocksRequestException;
/**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/
private $connection;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
*/
private $daemonRepository;
/**
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock
*/
private $repository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
*/
private $serverRepository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->daemonRepository = m::mock(DaemonRepositoryInterface::class);
$this->repository = m::mock(AllocationRepositoryInterface::class);
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
}
/**
* Test that an allocation can be updated.
*
* @dataProvider useModelDataProvider
*/
public function testAllocationIsUpdated(bool $useModel)
{
$allocations = factory(Allocation::class)->times(2)->make();
$model = factory(Server::class)->make();
if (! $useModel) {
$this->serverRepository->shouldReceive('find')->with(1234)->once()->andReturn($model);
}
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn($allocations);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->serverRepository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf();
$this->serverRepository->shouldReceive('update')->with($model->id, [
'allocation_id' => $allocations->first()->id,
])->once()->andReturnNull();
$this->daemonRepository->shouldReceive('setAccessServer')->with($model->uuid)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('update')->with([
'build' => [
'default' => [
'ip' => $allocations->first()->ip,
'port' => $allocations->first()->port,
],
'ports|overwrite' => $allocations->groupBy('ip')->map(function ($item) {
return $item->pluck('port');
})->toArray(),
],
])->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->handle($useModel ? $model : 1234, $allocations->first()->id);
$this->assertNotEmpty($response);
$this->assertSame($allocations->first(), $response);
}
/**
* Test that an allocation that doesn't belong to a server throws an exception.
*
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\AllocationDoesNotBelongToServerException
*/
public function testAllocationNotBelongingToServerThrowsException()
{
$model = factory(Server::class)->make();
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn(collect());
$this->getService()->handle($model, 1234);
}
/**
* Test that an exception thrown by guzzle is handled properly.
*/
public function testExceptionThrownByGuzzleIsHandled()
{
$this->configureExceptionMock();
$allocation = factory(Allocation::class)->make();
$model = factory(Server::class)->make();
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn(collect([$allocation]));
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->serverRepository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf();
$this->serverRepository->shouldReceive('update')->with($model->id, [
'allocation_id' => $allocation->id,
])->once()->andReturnNull();
$this->daemonRepository->shouldReceive('setAccessServer->setNode->update')->once()->andThrow($this->getExceptionMock());
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
try {
$this->getService()->handle($model, $allocation->id);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
}
}
/**
* Data provider to determine if a model should be passed or an int.
*
* @return array
*/
public function useModelDataProvider(): array
{
return [[false], [true]];
}
/**
* Return an instance of the service with mocked dependencies.
*
* @return \Pterodactyl\Services\Allocations\SetDefaultAllocationService
*/
private function getService(): SetDefaultAllocationService
{
return new SetDefaultAllocationService($this->repository, $this->connection, $this->daemonRepository, $this->serverRepository);
}
}

View File

@ -51,7 +51,7 @@ class DatabasePasswordServiceTest extends TestCase
*
* @dataProvider useModelDataProvider
*/
public function testPasswordIsChanged($useModel)
public function testPasswordIsChanged(bool $useModel)
{
$model = factory(Database::class)->make();

View File

@ -51,7 +51,7 @@ class HostDeletionServiceTest extends TestCase
*
* @dataProvider databaseCountDataProvider
*/
public function testExceptionIsThrownIfDeletingHostWithDatabases($count)
public function testExceptionIsThrownIfDeletingHostWithDatabases(int $count)
{
$this->databaseRepository->shouldReceive('findCountWhere')->with([['database_host_id', '=', 1234]])->once()->andReturn($count);