2017-08-05 22:20:07 +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-05 22:20:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Nodes;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Mockery as m;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Tests\TestCase;
|
2017-08-05 22:20:07 +00:00
|
|
|
use phpmock\phpunit\PHPMock;
|
|
|
|
use Pterodactyl\Models\Node;
|
2018-01-06 00:27:47 +00:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
2018-01-11 05:19:03 +00:00
|
|
|
use Tests\Traits\MocksRequestException;
|
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2017-08-27 20:10:51 +00:00
|
|
|
use Pterodactyl\Services\Nodes\NodeUpdateService;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class NodeUpdateServiceTest extends TestCase
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
2018-01-11 05:19:03 +00:00
|
|
|
use PHPMock, MocksRequestException;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-11 05:19:03 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2018-01-11 05:19:03 +00:00
|
|
|
private $connection;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-11 05:19:03 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface|\Mockery\Mock
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2018-01-11 05:19:03 +00:00
|
|
|
private $configRepository;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
|
|
|
/**
|
2017-09-25 03:28:16 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2018-01-11 05:19:03 +00:00
|
|
|
private $repository;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->connection = m::mock(ConnectionInterface::class);
|
2017-08-05 22:20:07 +00:00
|
|
|
$this->configRepository = m::mock(ConfigurationRepositoryInterface::class);
|
|
|
|
$this->repository = m::mock(NodeRepositoryInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the daemon secret is reset when `reset_secret` is passed in the data.
|
|
|
|
*/
|
|
|
|
public function testNodeIsUpdatedAndDaemonSecretIsReset()
|
|
|
|
{
|
2018-01-11 05:19:03 +00:00
|
|
|
$model = factory(Node::class)->make();
|
|
|
|
|
2017-09-14 04:07:02 +00:00
|
|
|
$this->getFunctionMock('\\Pterodactyl\\Services\\Nodes', 'str_random')
|
|
|
|
->expects($this->once())->willReturn('random_string');
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
|
|
|
|
'name' => 'NewName',
|
|
|
|
'daemonSecret' => 'random_string',
|
|
|
|
])->andReturn(true);
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->configRepository->shouldReceive('setNode')->with($model)->once()->andReturnSelf()
|
2018-01-06 00:27:47 +00:00
|
|
|
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2018-01-11 05:19:03 +00:00
|
|
|
$response = $this->getService()->returnUpdatedModel(false)->handle($model, ['name' => 'NewName', 'reset_secret' => true]);
|
|
|
|
$this->assertTrue($response);
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that daemon secret is not modified when no variable is passed in data.
|
|
|
|
*/
|
|
|
|
public function testNodeIsUpdatedAndDaemonSecretIsNotChanged()
|
|
|
|
{
|
2018-01-11 05:19:03 +00:00
|
|
|
$model = factory(Node::class)->make();
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
|
|
|
|
'name' => 'NewName',
|
|
|
|
])->andReturn(true);
|
|
|
|
|
|
|
|
$this->configRepository->shouldReceive('setNode')->with($model)->once()->andReturnSelf()
|
2018-01-06 00:27:47 +00:00
|
|
|
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2018-01-11 05:19:03 +00:00
|
|
|
$response = $this->getService()->returnUpdatedModel(false)->handle($model, ['name' => 'NewName']);
|
|
|
|
$this->assertTrue($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdatedModelIsReturned()
|
|
|
|
{
|
|
|
|
$model = factory(Node::class)->make();
|
|
|
|
$updated = clone $model;
|
|
|
|
$updated->name = 'NewName';
|
|
|
|
|
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('update')->with($model->id, [
|
|
|
|
'name' => $updated->name,
|
|
|
|
])->andReturn($updated);
|
|
|
|
|
|
|
|
$this->configRepository->shouldReceive('setNode')->with($model)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
$response = $this->getService()->returnUpdatedModel()->handle($model, ['name' => $updated->name]);
|
2018-01-01 21:11:44 +00:00
|
|
|
$this->assertInstanceOf(Node::class, $response);
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->assertSame($updated, $response);
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-11 05:19:03 +00:00
|
|
|
* Test that an exception caused by a connection error is handled.
|
|
|
|
*
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\Service\Node\ConfigurationNotPersistedException
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2018-01-11 05:19:03 +00:00
|
|
|
public function testExceptionRelatedToConnection()
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->configureExceptionMock(ConnectException::class);
|
|
|
|
$model = factory(Node::class)->make();
|
|
|
|
|
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->andReturn(new Response);
|
|
|
|
|
|
|
|
$this->configRepository->shouldReceive('setNode->update')->once()->andThrow($this->getExceptionMock());
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->getService()->handle($model, ['name' => 'NewName']);
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-11 05:19:03 +00:00
|
|
|
* Test that an exception not caused by a daemon connection error is handled.
|
|
|
|
*
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2018-01-11 05:19:03 +00:00
|
|
|
public function testExceptionNotRelatedToConnection()
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->configureExceptionMock();
|
|
|
|
$model = factory(Node::class)->make();
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->andReturn(new Response);
|
|
|
|
|
|
|
|
$this->configRepository->shouldReceive('setNode->update')->once()->andThrow($this->getExceptionMock());
|
|
|
|
|
|
|
|
$this->getService()->handle($model, ['name' => 'NewName']);
|
|
|
|
}
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2018-01-11 05:19:03 +00:00
|
|
|
/**
|
|
|
|
* Return an instance of the service with mocked injections.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Nodes\NodeUpdateService
|
|
|
|
*/
|
|
|
|
private function getService(): NodeUpdateService
|
|
|
|
{
|
|
|
|
return new NodeUpdateService($this->connection, $this->configRepository, $this->repository);
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
}
|