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;
|
|
|
|
use Illuminate\Log\Writer;
|
2017-08-05 22:20:07 +00:00
|
|
|
use phpmock\phpunit\PHPMock;
|
|
|
|
use Pterodactyl\Models\Node;
|
2017-08-05 22:26:30 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
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
|
|
|
{
|
|
|
|
use PHPMock;
|
|
|
|
|
|
|
|
/**
|
2017-09-25 03:28:16 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface|\Mockery\Mock
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
|
|
|
protected $configRepository;
|
|
|
|
|
|
|
|
/**
|
2017-09-25 03:28:16 +00:00
|
|
|
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
|
|
|
protected $exception;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Models\Node
|
|
|
|
*/
|
|
|
|
protected $node;
|
|
|
|
|
|
|
|
/**
|
2017-09-25 03:28:16 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
2017-08-27 20:10:51 +00:00
|
|
|
* @var \Pterodactyl\Services\Nodes\NodeUpdateService
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
/**
|
2017-09-25 03:28:16 +00:00
|
|
|
* @var \Illuminate\Log\Writer|\Mockery\Mock
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
$this->service = new NodeUpdateService(
|
2017-08-05 22:20:07 +00:00
|
|
|
$this->configRepository,
|
|
|
|
$this->repository,
|
|
|
|
$this->writer
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the daemon secret is reset when `reset_secret` is passed in the data.
|
|
|
|
*/
|
|
|
|
public function testNodeIsUpdatedAndDaemonSecretIsReset()
|
|
|
|
{
|
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-01 21:11:44 +00:00
|
|
|
$this->repository->->shouldReceive('update')->with($this->node->id, [
|
|
|
|
'name' => 'NewName',
|
|
|
|
'daemonSecret' => 'random_string',
|
|
|
|
])->andReturn($this->node);
|
2017-08-05 22:20:07 +00:00
|
|
|
|
|
|
|
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
2018-01-01 21:11:44 +00:00
|
|
|
$response = $this->service->handle($this->node, ['name' => 'NewName', 'reset_secret' => true]);
|
|
|
|
$this->assertInstanceOf(Node::class, $response);
|
|
|
|
$this->assertSame($this->node, $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-01 21:11:44 +00:00
|
|
|
$this->repository->shouldReceive('update')->with($this->node->id, [
|
|
|
|
'name' => 'NewName',
|
|
|
|
])->andReturn($this->node);
|
2017-08-05 22:20:07 +00:00
|
|
|
|
|
|
|
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
2018-01-01 21:11:44 +00:00
|
|
|
$response = $this->service->handle($this->node, ['name' => 'NewName']);
|
|
|
|
$this->assertInstanceOf(Node::class, $response);
|
|
|
|
$this->assertSame($this->node, $response);
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception caused by the daemon is handled properly.
|
|
|
|
*/
|
|
|
|
public function testExceptionCausedByDaemonIsHandled()
|
|
|
|
{
|
2018-01-01 21:11:44 +00:00
|
|
|
$this->repository->->shouldReceive('update')->with($this->node->id, [
|
2017-08-05 22:20:07 +00:00
|
|
|
'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(
|
2017-09-03 21:32:52 +00:00
|
|
|
trans('exceptions.node.daemon_off_config_updated', ['code' => 400]),
|
2017-08-22 03:10:48 +00:00
|
|
|
$exception->getMessage()
|
2017-08-05 22:20:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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('update')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->assertTrue($this->service->handle($this->node->id, ['name' => 'NewName']));
|
|
|
|
}
|
|
|
|
}
|