2017-08-05 22:20:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Nodes;
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
|
|
|
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();
|
2018-12-02 21:38:59 +00:00
|
|
|
$updatedModel = factory(Node::class)->make([
|
|
|
|
'name' => 'New Name',
|
|
|
|
'daemonSecret' => 'abcd1234',
|
|
|
|
]);
|
2018-01-11 05:19:03 +00:00
|
|
|
|
2017-09-14 04:07:02 +00:00
|
|
|
$this->getFunctionMock('\\Pterodactyl\\Services\\Nodes', 'str_random')
|
2018-12-02 21:38:59 +00:00
|
|
|
->expects($this->once())->willReturn($updatedModel->daemonSecret);
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2018-09-03 21:59:00 +00:00
|
|
|
$this->repository->shouldReceive('update')->with($model->id, [
|
2018-12-02 21:38:59 +00:00
|
|
|
'name' => $updatedModel->name,
|
|
|
|
'daemonSecret' => $updatedModel->daemonSecret,
|
2018-09-03 21:59:00 +00:00
|
|
|
])->andReturn($model);
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2018-12-02 21:38:59 +00:00
|
|
|
$cloned = $updatedModel->replicate(['daemonSecret']);
|
|
|
|
$cloned->daemonSecret = $model->daemonSecret;
|
|
|
|
|
|
|
|
$this->configRepository->shouldReceive('setNode')->with(m::on(function ($model) use ($updatedModel) {
|
|
|
|
return $model->daemonSecret !== $updatedModel->daemonSecret;
|
|
|
|
}))->once()->andReturnSelf();
|
|
|
|
|
|
|
|
$this->configRepository->shouldReceive('update')->with([
|
|
|
|
'keys' => ['abcd1234'],
|
|
|
|
])->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-12-02 21:38:59 +00:00
|
|
|
$response = $this->getService()->handle($model, ['name' => $updatedModel->name], true);
|
2018-09-03 21:59:00 +00:00
|
|
|
$this->assertInstanceOf(Node::class, $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('update')->with($model->id, [
|
2018-09-03 21:59:00 +00:00
|
|
|
'name' => 'NewName',
|
|
|
|
])->andReturn($model);
|
2018-01-11 05:19:03 +00:00
|
|
|
|
|
|
|
$this->configRepository->shouldReceive('setNode')->with($model)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
2018-09-03 21:59:00 +00:00
|
|
|
$response = $this->getService()->handle($model, ['name' => 'NewName']);
|
2018-01-01 21:11:44 +00:00
|
|
|
$this->assertInstanceOf(Node::class, $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();
|
2018-09-03 21:59:00 +00:00
|
|
|
$this->repository->shouldReceive('update')->andReturn($model);
|
2018-01-11 05:19:03 +00:00
|
|
|
|
|
|
|
$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();
|
2018-09-03 21:59:00 +00:00
|
|
|
$this->repository->shouldReceive('update')->andReturn($model);
|
2018-01-11 05:19:03 +00:00
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|