Remove unused code
This commit is contained in:
parent
34916e7caf
commit
756a21ff04
15 changed files with 21 additions and 1126 deletions
|
@ -7,6 +7,7 @@ use Tests\TestCase;
|
|||
use Pterodactyl\Models\Allocation;
|
||||
use Pterodactyl\Services\Allocations\AllocationDeletionService;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException;
|
||||
|
||||
class AllocationDeletionServiceTest extends TestCase
|
||||
{
|
||||
|
@ -37,11 +38,11 @@ class AllocationDeletionServiceTest extends TestCase
|
|||
|
||||
/**
|
||||
* Test that an exception gets thrown if an allocation is currently assigned to a server.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
|
||||
*/
|
||||
public function testExceptionThrownIfAssignedToServer()
|
||||
{
|
||||
$this->expectException(ServerUsingAllocationException::class);
|
||||
|
||||
$model = factory(Allocation::class)->make(['server_id' => 123]);
|
||||
|
||||
$this->getService()->handle($model);
|
||||
|
|
|
@ -8,6 +8,10 @@ use Pterodactyl\Models\Node;
|
|||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Services\Allocations\AssignmentService;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException;
|
||||
use Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException;
|
||||
use Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException;
|
||||
use Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException;
|
||||
|
||||
class AssignmentServiceTest extends TestCase
|
||||
{
|
||||
|
@ -190,12 +194,12 @@ class AssignmentServiceTest extends TestCase
|
|||
|
||||
/**
|
||||
* Test that a CIDR IP address with a range works properly.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException
|
||||
* @expectedExceptionMessage CIDR notation only allows masks between /25 and /32.
|
||||
*/
|
||||
public function testCIDRNotatedIPAddressOutsideRangeLimit()
|
||||
{
|
||||
$this->expectException(CidrOutOfRangeException::class);
|
||||
$this->expectExceptionMessage('CIDR notation only allows masks between /25 and /32.');
|
||||
|
||||
$data = [
|
||||
'allocation_ip' => '192.168.1.100/20',
|
||||
'allocation_ports' => ['2222'],
|
||||
|
@ -206,12 +210,12 @@ class AssignmentServiceTest extends TestCase
|
|||
|
||||
/**
|
||||
* Test that an exception is thrown if there are too many ports.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException
|
||||
* @expectedExceptionMessage Adding more than 1000 ports in a single range at once is not supported.
|
||||
*/
|
||||
public function testAllocationWithPortsExceedingLimit()
|
||||
{
|
||||
$this->expectException(TooManyPortsInRangeException::class);
|
||||
$this->expectExceptionMessage('Adding more than 1000 ports in a single range at once is not supported.');
|
||||
|
||||
$data = [
|
||||
'allocation_ip' => '192.168.1.1',
|
||||
'allocation_ports' => ['5000-7000'],
|
||||
|
@ -224,12 +228,12 @@ class AssignmentServiceTest extends TestCase
|
|||
|
||||
/**
|
||||
* Test that an exception is thrown if an invalid port is provided.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException
|
||||
* @expectedExceptionMessage The mapping provided for test123 was invalid and could not be processed.
|
||||
*/
|
||||
public function testInvalidPortProvided()
|
||||
{
|
||||
$this->expectException(InvalidPortMappingException::class);
|
||||
$this->expectExceptionMessage('The mapping provided for test123 was invalid and could not be processed.');
|
||||
|
||||
$data = [
|
||||
'allocation_ip' => '192.168.1.1',
|
||||
'allocation_ports' => ['test123'],
|
||||
|
@ -245,11 +249,12 @@ class AssignmentServiceTest extends TestCase
|
|||
* @param array $ports
|
||||
*
|
||||
* @dataProvider invalidPortsDataProvider
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException
|
||||
* @expectedExceptionMessage Ports in an allocation must be greater than 1024 and less than or equal to 65535.
|
||||
*/
|
||||
public function testPortRangeOutsideOfRangeLimits(array $ports)
|
||||
{
|
||||
$this->expectException(PortOutOfRangeException::class);
|
||||
$this->expectExceptionMessage('Ports in an allocation must be greater than 1024 and less than or equal to 65535.');
|
||||
|
||||
$data = ['allocation_ip' => '192.168.1.1', 'allocation_ports' => $ports];
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
||||
|
|
|
@ -1,156 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Allocations;
|
||||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
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(): void
|
||||
{
|
||||
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('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
|
||||
$this->serverRepository->shouldReceive('update')->with($model->id, [
|
||||
'allocation_id' => $allocations->first()->id,
|
||||
])->once()->andReturn(new Response);
|
||||
|
||||
$this->daemonRepository->shouldReceive('setServer')->with($model)->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()->andReturn(new Response);
|
||||
$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('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
|
||||
$this->serverRepository->shouldReceive('update')->with($model->id, [
|
||||
'allocation_id' => $allocation->id,
|
||||
])->once()->andReturn(new Response);
|
||||
|
||||
$this->daemonRepository->shouldReceive('setServer->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);
|
||||
}
|
||||
}
|
Reference in a new issue