Add support for node management actions using new services
This commit is contained in:
parent
4391defb9f
commit
c1a078bdcf
33 changed files with 1375 additions and 745 deletions
74
tests/Unit/Services/Nodes/CreationServiceTest.php
Normal file
74
tests/Unit/Services/Nodes/CreationServiceTest.php
Normal file
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services\Nodes;
|
||||
|
||||
use Mockery as m;
|
||||
use phpmock\phpunit\PHPMock;
|
||||
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
||||
use Pterodactyl\Services\Nodes\CreationService;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CreationServiceTest extends TestCase
|
||||
{
|
||||
use PHPMock;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Nodes\CreationService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->repository = m::mock(NodeRepositoryInterface::class);
|
||||
|
||||
$this->service = new CreationService($this->repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a node is created and a daemon secret token is created.
|
||||
*/
|
||||
public function testNodeIsCreatedAndDaemonSecretIsGenerated()
|
||||
{
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Nodes', 'bin2hex')
|
||||
->expects($this->once())->willReturn('hexResult');
|
||||
|
||||
$this->repository->shouldReceive('create')->with([
|
||||
'name' => 'NodeName',
|
||||
'daemonSecret' => 'hexResult',
|
||||
])->once()->andReturnNull();
|
||||
|
||||
$this->assertNull($this->service->handle(['name' => 'NodeName']));
|
||||
}
|
||||
}
|
121
tests/Unit/Services/Nodes/DeletionServiceTest.php
Normal file
121
tests/Unit/Services/Nodes/DeletionServiceTest.php
Normal file
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services\Nodes;
|
||||
|
||||
use Illuminate\Contracts\Translation\Translator;
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Pterodactyl\Models\Node;
|
||||
use Pterodactyl\Services\Nodes\DeletionService;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DeletionServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
||||
*/
|
||||
protected $serverRepository;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Translation\Translator
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Nodes\DeletionService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->repository = m::mock(NodeRepositoryInterface::class);
|
||||
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
|
||||
$this->translator = m::mock(Translator::class);
|
||||
|
||||
$this->service = new DeletionService(
|
||||
$this->repository,
|
||||
$this->serverRepository,
|
||||
$this->translator
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a node is deleted if there are no servers attached to it.
|
||||
*/
|
||||
public function testNodeIsDeletedIfNoServersAreAttached()
|
||||
{
|
||||
$this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
|
||||
->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(0);
|
||||
$this->repository->shouldReceive('delete')->with(1)->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue(
|
||||
$this->service->handle(1),
|
||||
'Assert that deletion returns a positive boolean value.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if servers are attached to the node.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\DisplayException
|
||||
*/
|
||||
public function testExceptionIsThrownIfServersAreAttachedToNode()
|
||||
{
|
||||
$this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
|
||||
->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(1);
|
||||
$this->translator->shouldReceive('trans')->with('admin/exceptions.node.servers_attached')->once()->andReturnNull();
|
||||
$this->repository->shouldNotReceive('delete');
|
||||
|
||||
$this->service->handle(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a model can be passed into the handle function rather than an ID.
|
||||
*/
|
||||
public function testModelCanBePassedToFunctionInPlaceOfNodeId()
|
||||
{
|
||||
$node = factory(Node::class)->make();
|
||||
|
||||
$this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
|
||||
->shouldReceive('findCountWhere')->with([['node_id', '=', $node->id]])->once()->andReturn(0);
|
||||
$this->repository->shouldReceive('delete')->with($node->id)->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue(
|
||||
$this->service->handle($node->id),
|
||||
'Assert that deletion returns a positive boolean value.'
|
||||
);
|
||||
}
|
||||
}
|
182
tests/Unit/Services/Nodes/UpdateServiceTest.php
Normal file
182
tests/Unit/Services/Nodes/UpdateServiceTest.php
Normal file
|
@ -0,0 +1,182 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services\Nodes;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Illuminate\Log\Writer;
|
||||
use Mockery as m;
|
||||
use phpmock\phpunit\PHPMock;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Models\Node;
|
||||
use Pterodactyl\Services\Nodes\CreationService;
|
||||
use Pterodactyl\Services\Nodes\UpdateService;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UpdateServiceTest extends TestCase
|
||||
{
|
||||
use PHPMock;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface
|
||||
*/
|
||||
protected $configRepository;
|
||||
|
||||
/**
|
||||
* @var \GuzzleHttp\Exception\RequestException
|
||||
*/
|
||||
protected $exception;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Models\Node
|
||||
*/
|
||||
protected $node;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Nodes\UpdateService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Log\Writer
|
||||
*/
|
||||
protected $writer;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->node = factory(Node::class)->make();
|
||||
|
||||
$this->configRepository = m::mock(ConfigurationRepositoryInterface::class);
|
||||
$this->exception = m::mock(RequestException::class);
|
||||
$this->repository = m::mock(NodeRepositoryInterface::class);
|
||||
$this->writer = m::mock(Writer::class);
|
||||
|
||||
$this->service = new UpdateService(
|
||||
$this->configRepository,
|
||||
$this->repository,
|
||||
$this->writer
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the daemon secret is reset when `reset_secret` is passed in the data.
|
||||
*/
|
||||
public function testNodeIsUpdatedAndDaemonSecretIsReset()
|
||||
{
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Nodes', 'random_bytes')
|
||||
->expects($this->once())->willReturnCallback(function ($bytes) {
|
||||
$this->assertEquals(CreationService::DAEMON_SECRET_LENGTH, $bytes);
|
||||
|
||||
return '\00';
|
||||
});
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Nodes', 'bin2hex')
|
||||
->expects($this->once())->willReturn('hexResponse');
|
||||
|
||||
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('update')->with($this->node->id, [
|
||||
'name' => 'NewName',
|
||||
'daemonSecret' => 'hexResponse',
|
||||
])->andReturn(true);
|
||||
|
||||
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
|
||||
->shouldReceive('setAccessToken')->with($this->node->daemonSecret)->once()->andReturnSelf()
|
||||
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->assertTrue($this->service->handle($this->node, ['name' => 'NewName', 'reset_secret' => true]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that daemon secret is not modified when no variable is passed in data.
|
||||
*/
|
||||
public function testNodeIsUpdatedAndDaemonSecretIsNotChanged()
|
||||
{
|
||||
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('update')->with($this->node->id, [
|
||||
'name' => 'NewName',
|
||||
])->andReturn(true);
|
||||
|
||||
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
|
||||
->shouldReceive('setAccessToken')->with($this->node->daemonSecret)->once()->andReturnSelf()
|
||||
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->assertTrue($this->service->handle($this->node, ['name' => 'NewName']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception caused by the daemon is handled properly.
|
||||
*/
|
||||
public function testExceptionCausedByDaemonIsHandled()
|
||||
{
|
||||
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('update')->with($this->node->id, [
|
||||
'name' => 'NewName',
|
||||
])->andReturn(true);
|
||||
|
||||
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andThrow($this->exception);
|
||||
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
|
||||
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);
|
||||
|
||||
try {
|
||||
$this->service->handle($this->node, ['name' => 'NewName']);
|
||||
} catch (Exception $exception) {
|
||||
$this->assertInstanceOf(DisplayException::class, $exception);
|
||||
$this->assertEquals(
|
||||
trans('admin/exceptions.node.daemon_off_config_updated', ['code' => 400]), $exception->getMessage()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an ID can be passed in place of a model.
|
||||
*/
|
||||
public function testFunctionCanAcceptANodeIdInPlaceOfModel()
|
||||
{
|
||||
$this->repository->shouldReceive('find')->with($this->node->id)->once()->andReturn($this->node);
|
||||
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('update')->with($this->node->id, [
|
||||
'name' => 'NewName',
|
||||
])->andReturn(true);
|
||||
|
||||
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
|
||||
->shouldReceive('setAccessToken')->with($this->node->daemonSecret)->once()->andReturnSelf()
|
||||
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->assertTrue($this->service->handle($this->node->id, ['name' => 'NewName']));
|
||||
}
|
||||
}
|
|
@ -85,7 +85,7 @@ class DeletionServiceTest extends TestCase
|
|||
$this->repository->shouldReceive('delete')->with($this->user->id)->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue(
|
||||
$this->service->handle($this->user),
|
||||
$this->service->handle($this->user->id),
|
||||
'Assert that service responds true.'
|
||||
);
|
||||
}
|
||||
|
@ -100,20 +100,19 @@ class DeletionServiceTest extends TestCase
|
|||
$this->serverRepository->shouldReceive('findWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn(['item']);
|
||||
$this->translator->shouldReceive('trans')->with('admin/user.exceptions.user_has_servers')->once()->andReturnNull();
|
||||
|
||||
$this->service->handle($this->user);
|
||||
$this->service->handle($this->user->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the function supports passing in a model or an ID.
|
||||
*/
|
||||
public function testIntegerCanBePassedInPlaceOfUserModel()
|
||||
public function testModelCanBePassedInPlaceOfUserId()
|
||||
{
|
||||
$this->repository->shouldReceive('find')->with($this->user->id)->once()->andReturn($this->user);
|
||||
$this->serverRepository->shouldReceive('findWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn([]);
|
||||
$this->repository->shouldReceive('delete')->with($this->user->id)->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue(
|
||||
$this->service->handle($this->user->id),
|
||||
$this->service->handle($this->user),
|
||||
'Assert that service responds true.'
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue