Merge branch 'develop' into feature/api-integration-testing
This commit is contained in:
commit
bde4d4187f
61 changed files with 1017 additions and 453 deletions
|
@ -1,123 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Http\Controllers\Base;
|
||||
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Models\ApiKey;
|
||||
use Prologue\Alerts\AlertsMessageBag;
|
||||
use Pterodactyl\Services\Api\KeyCreationService;
|
||||
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
||||
use Pterodactyl\Http\Requests\Base\StoreAccountKeyRequest;
|
||||
use Pterodactyl\Http\Controllers\Base\AccountKeyController;
|
||||
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
||||
|
||||
class AccountKeyControllerTest extends ControllerTestCase
|
||||
{
|
||||
/**
|
||||
* @var \Prologue\Alerts\AlertsMessageBag|\Mockery\Mock
|
||||
*/
|
||||
protected $alert;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Api\KeyCreationService|\Mockery\Mock
|
||||
*/
|
||||
protected $keyService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->markTestSkipped('Not implemented');
|
||||
|
||||
$this->alert = m::mock(AlertsMessageBag::class);
|
||||
$this->keyService = m::mock(KeyCreationService::class);
|
||||
$this->repository = m::mock(ApiKeyRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the index controller.
|
||||
*/
|
||||
public function testIndexController()
|
||||
{
|
||||
$model = $this->generateRequestUserModel();
|
||||
|
||||
$this->repository->shouldReceive('getAccountKeys')->with($model)->once()->andReturn(collect(['testkeys']));
|
||||
|
||||
$response = $this->getController()->index($this->request);
|
||||
$this->assertIsViewResponse($response);
|
||||
$this->assertViewNameEquals('base.api.index', $response);
|
||||
$this->assertViewHasKey('keys', $response);
|
||||
$this->assertViewKeyEquals('keys', collect(['testkeys']), $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the create API view controller.
|
||||
*/
|
||||
public function testCreateController()
|
||||
{
|
||||
$this->generateRequestUserModel();
|
||||
|
||||
$response = $this->getController()->create($this->request);
|
||||
$this->assertIsViewResponse($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the store functionality for a user.
|
||||
*/
|
||||
public function testStoreController()
|
||||
{
|
||||
$this->setRequestMockClass(StoreAccountKeyRequest::class);
|
||||
$model = $this->generateRequestUserModel();
|
||||
$keyModel = factory(ApiKey::class)->make();
|
||||
|
||||
$this->request->shouldReceive('user')->withNoArgs()->andReturn($model);
|
||||
$this->request->shouldReceive('input')->with('allowed_ips')->once()->andReturnNull();
|
||||
$this->request->shouldReceive('input')->with('memo')->once()->andReturnNull();
|
||||
|
||||
$this->keyService->shouldReceive('setKeyType')->with(ApiKey::TYPE_ACCOUNT)->once()->andReturnSelf();
|
||||
$this->keyService->shouldReceive('handle')->with([
|
||||
'user_id' => $model->id,
|
||||
'allowed_ips' => null,
|
||||
'memo' => null,
|
||||
])->once()->andReturn($keyModel);
|
||||
|
||||
$this->alert->shouldReceive('success')->with(trans('base.api.index.keypair_created'))->once()->andReturnSelf();
|
||||
$this->alert->shouldReceive('flash')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->getController()->store($this->request);
|
||||
$this->assertIsRedirectResponse($response);
|
||||
$this->assertRedirectRouteEquals('account.api', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the API key revocation controller.
|
||||
*/
|
||||
public function testRevokeController()
|
||||
{
|
||||
$model = $this->generateRequestUserModel();
|
||||
|
||||
$this->repository->shouldReceive('deleteAccountKey')->with($model, 'testIdentifier')->once()->andReturn(1);
|
||||
|
||||
$response = $this->getController()->revoke($this->request, 'testIdentifier');
|
||||
$this->assertIsResponse($response);
|
||||
$this->assertEmpty($response->getContent());
|
||||
$this->assertResponseCodeEquals(204, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the controller with mocked dependencies for testing.
|
||||
*
|
||||
* @return \Pterodactyl\Http\Controllers\Base\AccountKeyController
|
||||
*/
|
||||
private function getController(): AccountKeyController
|
||||
{
|
||||
return new AccountKeyController($this->alert, $this->repository, $this->keyService);
|
||||
}
|
||||
}
|
|
@ -37,8 +37,8 @@ class SetSessionDriverTest extends MiddlewareTestCase
|
|||
*/
|
||||
public function testProductionEnvironment()
|
||||
{
|
||||
$this->appMock->shouldReceive('environment')->withNoArgs()->once()->andReturn('production');
|
||||
$this->config->shouldReceive('set')->with('session.driver', 'array')->once()->andReturnNull();
|
||||
$this->config->shouldReceive('get')->once()->with('app.debug')->andReturn(false);
|
||||
$this->config->shouldReceive('set')->once()->with('session.driver', 'array')->andReturnNull();
|
||||
|
||||
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
||||
}
|
||||
|
@ -48,11 +48,10 @@ class SetSessionDriverTest extends MiddlewareTestCase
|
|||
*/
|
||||
public function testLocalEnvironment()
|
||||
{
|
||||
$this->appMock->shouldReceive('environment')->withNoArgs()->once()->andReturn('local');
|
||||
$this->appMock->shouldReceive('make')->with(LaravelDebugbar::class)->once()->andReturnSelf();
|
||||
$this->appMock->shouldReceive('disable')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->config->shouldReceive('set')->with('session.driver', 'array')->once()->andReturnNull();
|
||||
$this->config->shouldReceive('get')->once()->with('app.debug')->andReturn(true);
|
||||
$this->appMock->shouldReceive('make')->once()->with(LaravelDebugbar::class)->andReturnSelf();
|
||||
$this->appMock->shouldReceive('disable')->once()->withNoArgs()->andReturnNull();
|
||||
$this->config->shouldReceive('set')->once()->with('session.driver', 'array')->andReturnNull();
|
||||
|
||||
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
||||
}
|
||||
|
|
|
@ -1,30 +1,19 @@
|
|||
<?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\Allocations;
|
||||
|
||||
use Exception;
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use phpmock\phpunit\PHPMock;
|
||||
use Pterodactyl\Models\Node;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Services\Allocations\AssignmentService;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
|
||||
class AssignmentServiceTest extends TestCase
|
||||
{
|
||||
use PHPMock;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface
|
||||
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
|
@ -34,15 +23,10 @@ class AssignmentServiceTest extends TestCase
|
|||
protected $node;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
|
||||
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Allocations\AssignmentService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
|
@ -50,19 +34,9 @@ class AssignmentServiceTest extends TestCase
|
|||
{
|
||||
parent::setUp();
|
||||
|
||||
// Due to a bug in PHP, this is necessary since we only have a single test
|
||||
// that relies on this mock. If this does not exist the test will fail to register
|
||||
// correctly.
|
||||
//
|
||||
// This can also be avoided if tests were run in isolated processes, or if that test
|
||||
// came first, but neither of those are good solutions, so this is the next best option.
|
||||
PHPMock::defineFunctionMock('\\Pterodactyl\\Services\\Allocations', 'gethostbyname');
|
||||
|
||||
$this->node = factory(Node::class)->make();
|
||||
$this->connection = m::mock(ConnectionInterface::class);
|
||||
$this->repository = m::mock(AllocationRepositoryInterface::class);
|
||||
|
||||
$this->service = new AssignmentService($this->repository, $this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -72,22 +46,22 @@ class AssignmentServiceTest extends TestCase
|
|||
{
|
||||
$data = [
|
||||
'allocation_ip' => '192.168.1.1',
|
||||
'allocation_ports' => ['1024'],
|
||||
'allocation_ports' => ['2222'],
|
||||
];
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
||||
$this->repository->shouldReceive('insertIgnore')->with([
|
||||
[
|
||||
'node_id' => $this->node->id,
|
||||
'ip' => '192.168.1.1',
|
||||
'port' => 1024,
|
||||
'port' => 2222,
|
||||
'ip_alias' => null,
|
||||
'server_id' => null,
|
||||
],
|
||||
])->once()->andReturn(true);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
|
||||
|
||||
$this->service->handle($this->node->id, $data);
|
||||
$this->getService()->handle($this->node, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -97,18 +71,11 @@ class AssignmentServiceTest extends TestCase
|
|||
{
|
||||
$data = [
|
||||
'allocation_ip' => '192.168.1.1',
|
||||
'allocation_ports' => ['1024-1026'],
|
||||
'allocation_ports' => ['1025-1027'],
|
||||
];
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('insertIgnore')->with([
|
||||
[
|
||||
'node_id' => $this->node->id,
|
||||
'ip' => '192.168.1.1',
|
||||
'port' => 1024,
|
||||
'ip_alias' => null,
|
||||
'server_id' => null,
|
||||
],
|
||||
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
||||
$this->repository->shouldReceive('insertIgnore')->once()->with([
|
||||
[
|
||||
'node_id' => $this->node->id,
|
||||
'ip' => '192.168.1.1',
|
||||
|
@ -123,10 +90,17 @@ class AssignmentServiceTest extends TestCase
|
|||
'ip_alias' => null,
|
||||
'server_id' => null,
|
||||
],
|
||||
])->once()->andReturn(true);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
[
|
||||
'node_id' => $this->node->id,
|
||||
'ip' => '192.168.1.1',
|
||||
'port' => 1027,
|
||||
'ip_alias' => null,
|
||||
'server_id' => null,
|
||||
],
|
||||
])->andReturn(true);
|
||||
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
|
||||
|
||||
$this->service->handle($this->node->id, $data);
|
||||
$this->getService()->handle($this->node, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -136,23 +110,23 @@ class AssignmentServiceTest extends TestCase
|
|||
{
|
||||
$data = [
|
||||
'allocation_ip' => '192.168.1.1',
|
||||
'allocation_ports' => ['1024'],
|
||||
'allocation_ports' => ['2222'],
|
||||
'allocation_alias' => 'my.alias.net',
|
||||
];
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('insertIgnore')->with([
|
||||
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
||||
$this->repository->shouldReceive('insertIgnore')->once()->with([
|
||||
[
|
||||
'node_id' => $this->node->id,
|
||||
'ip' => '192.168.1.1',
|
||||
'port' => 1024,
|
||||
'port' => 2222,
|
||||
'ip_alias' => 'my.alias.net',
|
||||
'server_id' => null,
|
||||
],
|
||||
])->once()->andReturn(true);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
])->andReturn(true);
|
||||
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
|
||||
|
||||
$this->service->handle($this->node->id, $data);
|
||||
$this->getService()->handle($this->node, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -161,26 +135,23 @@ class AssignmentServiceTest extends TestCase
|
|||
public function testDomainNamePassedInPlaceOfIPAddress()
|
||||
{
|
||||
$data = [
|
||||
'allocation_ip' => 'test-domain.com',
|
||||
'allocation_ports' => ['1024'],
|
||||
'allocation_ip' => 'unit-test-static.pterodactyl.io',
|
||||
'allocation_ports' => ['2222'],
|
||||
];
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Allocations', 'gethostbyname')
|
||||
->expects($this->once())->willReturn('192.168.1.1');
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('insertIgnore')->with([
|
||||
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
||||
$this->repository->shouldReceive('insertIgnore')->once()->with([
|
||||
[
|
||||
'node_id' => $this->node->id,
|
||||
'ip' => '192.168.1.1',
|
||||
'port' => 1024,
|
||||
'ip' => '127.0.0.1',
|
||||
'port' => 2222,
|
||||
'ip_alias' => null,
|
||||
'server_id' => null,
|
||||
],
|
||||
])->once()->andReturn(true);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
])->andReturn(true);
|
||||
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
|
||||
|
||||
$this->service->handle($this->node->id, $data);
|
||||
$this->getService()->handle($this->node, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -190,54 +161,55 @@ class AssignmentServiceTest extends TestCase
|
|||
{
|
||||
$data = [
|
||||
'allocation_ip' => '192.168.1.100/31',
|
||||
'allocation_ports' => ['1024'],
|
||||
'allocation_ports' => ['2222'],
|
||||
];
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('insertIgnore')->with([
|
||||
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
||||
$this->repository->shouldReceive('insertIgnore')->once()->with([
|
||||
[
|
||||
'node_id' => $this->node->id,
|
||||
'ip' => '192.168.1.100',
|
||||
'port' => 1024,
|
||||
'port' => 2222,
|
||||
'ip_alias' => null,
|
||||
'server_id' => null,
|
||||
],
|
||||
])->once()->andReturn(true);
|
||||
])->andReturn(true);
|
||||
|
||||
$this->repository->shouldReceive('insertIgnore')->with([
|
||||
$this->repository->shouldReceive('insertIgnore')->once()->with([
|
||||
[
|
||||
'node_id' => $this->node->id,
|
||||
'ip' => '192.168.1.101',
|
||||
'port' => 1024,
|
||||
'port' => 2222,
|
||||
'ip_alias' => null,
|
||||
'server_id' => null,
|
||||
],
|
||||
])->once()->andReturn(true);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
])->andReturn(true);
|
||||
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
|
||||
|
||||
$this->service->handle($this->node->id, $data);
|
||||
$this->getService()->handle($this->node, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()
|
||||
{
|
||||
$data = [
|
||||
'allocation_ip' => '192.168.1.100/20',
|
||||
'allocation_ports' => ['1024'],
|
||||
'allocation_ports' => ['2222'],
|
||||
];
|
||||
|
||||
try {
|
||||
$this->service->handle($this->node->id, $data);
|
||||
} catch (Exception $exception) {
|
||||
$this->assertInstanceOf(DisplayException::class, $exception);
|
||||
$this->assertEquals(trans('exceptions.allocations.cidr_out_of_range'), $exception->getMessage());
|
||||
}
|
||||
$this->getService()->handle($this->node, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()
|
||||
{
|
||||
|
@ -246,22 +218,16 @@ class AssignmentServiceTest extends TestCase
|
|||
'allocation_ports' => ['5000-7000'],
|
||||
];
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
||||
|
||||
try {
|
||||
$this->service->handle($this->node->id, $data);
|
||||
} catch (Exception $exception) {
|
||||
if (! $exception instanceof DisplayException) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
$this->assertInstanceOf(DisplayException::class, $exception);
|
||||
$this->assertEquals(trans('exceptions.allocations.too_many_ports'), $exception->getMessage());
|
||||
}
|
||||
$this->getService()->handle($this->node, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()
|
||||
{
|
||||
|
@ -270,42 +236,52 @@ class AssignmentServiceTest extends TestCase
|
|||
'allocation_ports' => ['test123'],
|
||||
];
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
try {
|
||||
$this->service->handle($this->node->id, $data);
|
||||
} catch (Exception $exception) {
|
||||
if (! $exception instanceof DisplayException) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
$this->assertInstanceOf(DisplayException::class, $exception);
|
||||
$this->assertEquals(trans('exceptions.allocations.invalid_mapping', ['port' => 'test123']), $exception->getMessage());
|
||||
}
|
||||
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
||||
$this->getService()->handle($this->node, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a model can be passed in place of an ID.
|
||||
* Test that ports outside of defined limits throw an error.
|
||||
*
|
||||
* @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 testModelCanBePassedInPlaceOfNodeModel()
|
||||
public function testPortRangeOutsideOfRangeLimits(array $ports)
|
||||
{
|
||||
$data = [
|
||||
'allocation_ip' => '192.168.1.1',
|
||||
'allocation_ports' => ['1024'],
|
||||
$data = ['allocation_ip' => '192.168.1.1', 'allocation_ports' => $ports];
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
||||
$this->getService()->handle($this->node, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide ports and ranges of ports that exceed the viable port limits for the software.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function invalidPortsDataProvider(): array
|
||||
{
|
||||
return [
|
||||
[['65536']],
|
||||
[['1024']],
|
||||
[['1000']],
|
||||
[['0']],
|
||||
[['65530-65540']],
|
||||
[['65540-65560']],
|
||||
[[PHP_INT_MAX]],
|
||||
];
|
||||
}
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('insertIgnore')->with([
|
||||
[
|
||||
'node_id' => $this->node->id,
|
||||
'ip' => '192.168.1.1',
|
||||
'port' => 1024,
|
||||
'ip_alias' => null,
|
||||
'server_id' => null,
|
||||
],
|
||||
])->once()->andReturn(true);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->service->handle($this->node, $data);
|
||||
/**
|
||||
* Returns an instance of the service with mocked dependencies for testing.
|
||||
*
|
||||
* @return \Pterodactyl\Services\Allocations\AssignmentService
|
||||
*/
|
||||
private function getService(): AssignmentService
|
||||
{
|
||||
return new AssignmentService($this->repository, $this->connection);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,9 @@ namespace Tests\Unit\Services\Eggs\Variables;
|
|||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use BadMethodCallException;
|
||||
use Pterodactyl\Models\EggVariable;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Pterodactyl\Services\Eggs\Variables\VariableCreationService;
|
||||
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
||||
|
||||
|
@ -13,12 +15,12 @@ class VariableCreationServiceTest extends TestCase
|
|||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Eggs\Variables\VariableCreationService
|
||||
* @var \Illuminate\Contracts\Validation\Factory|\Mockery\Mock
|
||||
*/
|
||||
protected $service;
|
||||
private $validator;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
|
@ -28,8 +30,7 @@ class VariableCreationServiceTest extends TestCase
|
|||
parent::setUp();
|
||||
|
||||
$this->repository = m::mock(EggVariableRepositoryInterface::class);
|
||||
|
||||
$this->service = new VariableCreationService($this->repository);
|
||||
$this->validator = m::mock(Factory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -46,7 +47,7 @@ class VariableCreationServiceTest extends TestCase
|
|||
'env_variable' => 'TEST_VAR_123',
|
||||
]))->once()->andReturn(new EggVariable);
|
||||
|
||||
$this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data));
|
||||
$this->assertInstanceOf(EggVariable::class, $this->getService()->handle(1, $data));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -62,7 +63,7 @@ class VariableCreationServiceTest extends TestCase
|
|||
'env_variable' => 'TEST_VAR_123',
|
||||
]))->once()->andReturn(new EggVariable);
|
||||
|
||||
$this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data));
|
||||
$this->assertInstanceOf(EggVariable::class, $this->getService()->handle(1, $data));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -81,18 +82,20 @@ class VariableCreationServiceTest extends TestCase
|
|||
'user_editable' => false,
|
||||
]))->once()->andReturn(new EggVariable);
|
||||
|
||||
$this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data));
|
||||
$this->assertInstanceOf(EggVariable::class, $this->getService()->handle(1, $data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that all of the reserved variables defined in the model trigger an exception.
|
||||
*
|
||||
* @param string $variable
|
||||
*
|
||||
* @dataProvider reservedNamesProvider
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
|
||||
*/
|
||||
public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames(string $variable)
|
||||
{
|
||||
$this->service->handle(1, ['env_variable' => $variable]);
|
||||
$this->getService()->handle(1, ['env_variable' => $variable]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -106,7 +109,49 @@ class VariableCreationServiceTest extends TestCase
|
|||
'egg_id' => 1,
|
||||
]))->once()->andReturn(new EggVariable);
|
||||
|
||||
$this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data));
|
||||
$this->assertInstanceOf(EggVariable::class, $this->getService()->handle(1, $data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that validation errors due to invalid rules are caught and handled properly.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException
|
||||
* @expectedExceptionMessage The validation rule "hodor_door" is not a valid rule for this application.
|
||||
*/
|
||||
public function testInvalidValidationRulesResultInException()
|
||||
{
|
||||
$data = ['env_variable' => 'TEST_VAR_123', 'rules' => 'string|hodorDoor'];
|
||||
|
||||
$this->validator->shouldReceive('make')->once()
|
||||
->with(['__TEST' => 'test'], ['__TEST' => 'string|hodorDoor'])
|
||||
->andReturnSelf();
|
||||
|
||||
$this->validator->shouldReceive('fails')->once()
|
||||
->withNoArgs()
|
||||
->andThrow(new BadMethodCallException('Method [validateHodorDoor] does not exist.'));
|
||||
|
||||
$this->getService()->handle(1, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception not stemming from a bad rule is not caught.
|
||||
*
|
||||
* @expectedException \BadMethodCallException
|
||||
* @expectedExceptionMessage Received something, but no expectations were specified.
|
||||
*/
|
||||
public function testExceptionNotCausedByBadRuleIsNotCaught()
|
||||
{
|
||||
$data = ['env_variable' => 'TEST_VAR_123', 'rules' => 'string'];
|
||||
|
||||
$this->validator->shouldReceive('make')->once()
|
||||
->with(['__TEST' => 'test'], ['__TEST' => 'string'])
|
||||
->andReturnSelf();
|
||||
|
||||
$this->validator->shouldReceive('fails')->once()
|
||||
->withNoArgs()
|
||||
->andThrow(new BadMethodCallException('Received something, but no expectations were specified.'));
|
||||
|
||||
$this->getService()->handle(1, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -124,4 +169,14 @@ class VariableCreationServiceTest extends TestCase
|
|||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the service with mocked dependencies for testing.
|
||||
*
|
||||
* @return \Pterodactyl\Services\Eggs\Variables\VariableCreationService
|
||||
*/
|
||||
private function getService(): VariableCreationService
|
||||
{
|
||||
return new VariableCreationService($this->repository, $this->validator);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,9 @@ namespace Tests\Unit\Services\Eggs\Variables;
|
|||
use Exception;
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use BadMethodCallException;
|
||||
use Pterodactyl\Models\EggVariable;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Services\Eggs\Variables\VariableUpdateService;
|
||||
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
||||
|
@ -15,17 +17,17 @@ class VariableUpdateServiceTest extends TestCase
|
|||
/**
|
||||
* @var \Pterodactyl\Models\EggVariable|\Mockery\Mock
|
||||
*/
|
||||
protected $model;
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Eggs\Variables\VariableUpdateService
|
||||
* @var \Illuminate\Contracts\Validation\Factory|\Mockery\Mock
|
||||
*/
|
||||
protected $service;
|
||||
private $validator;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
|
@ -36,8 +38,7 @@ class VariableUpdateServiceTest extends TestCase
|
|||
|
||||
$this->model = factory(EggVariable::class)->make();
|
||||
$this->repository = m::mock(EggVariableRepositoryInterface::class);
|
||||
|
||||
$this->service = new VariableUpdateService($this->repository);
|
||||
$this->validator = m::mock(Factory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -51,7 +52,7 @@ class VariableUpdateServiceTest extends TestCase
|
|||
'user_editable' => false,
|
||||
]))->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue($this->service->handle($this->model, []));
|
||||
$this->assertTrue($this->getService()->handle($this->model, []));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -67,7 +68,7 @@ class VariableUpdateServiceTest extends TestCase
|
|||
'default_value' => '',
|
||||
]))->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue($this->service->handle($this->model, ['default_value' => null]));
|
||||
$this->assertTrue($this->getService()->handle($this->model, ['default_value' => null]));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -89,7 +90,7 @@ class VariableUpdateServiceTest extends TestCase
|
|||
'env_variable' => 'TEST_VAR_123',
|
||||
]))->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue($this->service->handle($this->model, ['env_variable' => 'TEST_VAR_123']));
|
||||
$this->assertTrue($this->getService()->handle($this->model, ['env_variable' => 'TEST_VAR_123']));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -107,7 +108,7 @@ class VariableUpdateServiceTest extends TestCase
|
|||
'description' => '',
|
||||
]))->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue($this->service->handle($this->model, ['options' => null, 'description' => null]));
|
||||
$this->assertTrue($this->getService()->handle($this->model, ['options' => null, 'description' => null]));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -129,7 +130,7 @@ class VariableUpdateServiceTest extends TestCase
|
|||
'env_variable' => 'TEST_VAR_123',
|
||||
]))->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue($this->service->handle($this->model, ['user_viewable' => 123456, 'env_variable' => 'TEST_VAR_123']));
|
||||
$this->assertTrue($this->getService()->handle($this->model, ['user_viewable' => 123456, 'env_variable' => 'TEST_VAR_123']));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -145,7 +146,7 @@ class VariableUpdateServiceTest extends TestCase
|
|||
])->once()->andReturn(1);
|
||||
|
||||
try {
|
||||
$this->service->handle($this->model, ['env_variable' => 'TEST_VAR_123']);
|
||||
$this->getService()->handle($this->model, ['env_variable' => 'TEST_VAR_123']);
|
||||
} catch (Exception $exception) {
|
||||
$this->assertInstanceOf(DisplayException::class, $exception);
|
||||
$this->assertEquals(trans('exceptions.service.variables.env_not_unique', [
|
||||
|
@ -162,7 +163,51 @@ class VariableUpdateServiceTest extends TestCase
|
|||
*/
|
||||
public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames(string $variable)
|
||||
{
|
||||
$this->service->handle($this->model, ['env_variable' => $variable]);
|
||||
$this->getService()->handle($this->model, ['env_variable' => $variable]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that validation errors due to invalid rules are caught and handled properly.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException
|
||||
* @expectedExceptionMessage The validation rule "hodor_door" is not a valid rule for this application.
|
||||
*/
|
||||
public function testInvalidValidationRulesResultInException()
|
||||
{
|
||||
$data = ['env_variable' => 'TEST_VAR_123', 'rules' => 'string|hodorDoor'];
|
||||
|
||||
$this->repository->shouldReceive('setColumns->findCountWhere')->once()->andReturn(0);
|
||||
|
||||
$this->validator->shouldReceive('make')->once()
|
||||
->with(['__TEST' => 'test'], ['__TEST' => 'string|hodorDoor'])
|
||||
->andReturnSelf();
|
||||
|
||||
$this->validator->shouldReceive('fails')->once()
|
||||
->withNoArgs()
|
||||
->andThrow(new BadMethodCallException('Method [validateHodorDoor] does not exist.'));
|
||||
|
||||
$this->getService()->handle($this->model, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception not stemming from a bad rule is not caught.
|
||||
*
|
||||
* @expectedException \BadMethodCallException
|
||||
* @expectedExceptionMessage Received something, but no expectations were specified.
|
||||
*/
|
||||
public function testExceptionNotCausedByBadRuleIsNotCaught()
|
||||
{
|
||||
$data = ['rules' => 'string'];
|
||||
|
||||
$this->validator->shouldReceive('make')->once()
|
||||
->with(['__TEST' => 'test'], ['__TEST' => 'string'])
|
||||
->andReturnSelf();
|
||||
|
||||
$this->validator->shouldReceive('fails')->once()
|
||||
->withNoArgs()
|
||||
->andThrow(new BadMethodCallException('Received something, but no expectations were specified.'));
|
||||
|
||||
$this->getService()->handle($this->model, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -180,4 +225,14 @@ class VariableUpdateServiceTest extends TestCase
|
|||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the service with mocked dependencies for testing.
|
||||
*
|
||||
* @return \Pterodactyl\Services\Eggs\Variables\VariableUpdateService
|
||||
*/
|
||||
private function getService(): VariableUpdateService
|
||||
{
|
||||
return new VariableUpdateService($this->repository, $this->validator);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -121,14 +121,18 @@ class StartupModificationServiceTest extends TestCase
|
|||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->validatorService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_ADMIN)->once()->andReturnNull();
|
||||
$this->validatorService->shouldReceive('handle')->with(456, ['test' => 'abcd1234'])->once()->andReturn(
|
||||
collect([(object) ['id' => 1, 'value' => 'stored-value']])
|
||||
collect([(object) ['id' => 1, 'value' => 'stored-value'], (object) ['id' => 2, 'value' => null]])
|
||||
);
|
||||
|
||||
$this->serverVariableRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
|
||||
$this->serverVariableRepository->shouldReceive('updateOrCreate')->with([
|
||||
$this->serverVariableRepository->shouldReceive('withoutFreshModel->updateOrCreate')->once()->with([
|
||||
'server_id' => $model->id,
|
||||
'variable_id' => 1,
|
||||
], ['variable_value' => 'stored-value'])->once()->andReturnNull();
|
||||
], ['variable_value' => 'stored-value'])->andReturnNull();
|
||||
|
||||
$this->serverVariableRepository->shouldReceive('withoutFreshModel->updateOrCreate')->once()->with([
|
||||
'server_id' => $model->id,
|
||||
'variable_id' => 2,
|
||||
], ['variable_value' => ''])->andReturnNull();
|
||||
|
||||
$this->eggRepository->shouldReceive('setColumns->find')->once()->with($eggModel->id)->andReturn($eggModel);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue