misc_pterodactyl-panel/tests/Unit/Services/Nodes/NodeUpdateServiceTest.php

159 lines
5.3 KiB
PHP
Raw Normal View History

<?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
*/
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;
use phpmock\phpunit\PHPMock;
use Pterodactyl\Models\Node;
2017-08-05 22:26:30 +00:00
use GuzzleHttp\Exception\RequestException;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Nodes\NodeUpdateService;
2017-08-05 22:26:30 +00:00
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface;
class NodeUpdateServiceTest extends TestCase
{
use PHPMock;
/**
2017-09-25 03:28:16 +00:00
* @var \Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface|\Mockery\Mock
*/
protected $configRepository;
/**
2017-09-25 03:28:16 +00:00
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
*/
protected $exception;
/**
* @var \Pterodactyl\Models\Node
*/
protected $node;
/**
2017-09-25 03:28:16 +00:00
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Nodes\NodeUpdateService
*/
protected $service;
/**
2017-09-25 03:28:16 +00:00
* @var \Illuminate\Log\Writer|\Mockery\Mock
*/
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 NodeUpdateService(
$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', 'str_random')
->expects($this->once())->willReturn('random_string');
$this->repository->->shouldReceive('update')->with($this->node->id, [
'name' => 'NewName',
'daemonSecret' => 'random_string',
])->andReturn($this->node);
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($this->node, ['name' => 'NewName', 'reset_secret' => true]);
$this->assertInstanceOf(Node::class, $response);
$this->assertSame($this->node, $response);
}
/**
* Test that daemon secret is not modified when no variable is passed in data.
*/
public function testNodeIsUpdatedAndDaemonSecretIsNotChanged()
{
$this->repository->shouldReceive('update')->with($this->node->id, [
'name' => 'NewName',
])->andReturn($this->node);
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($this->node, ['name' => 'NewName']);
$this->assertInstanceOf(Node::class, $response);
$this->assertSame($this->node, $response);
}
/**
* Test that an exception caused by the daemon is handled properly.
*/
public function testExceptionCausedByDaemonIsHandled()
{
$this->repository->->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('exceptions.node.daemon_off_config_updated', ['code' => 400]),
2017-08-22 03:10:48 +00:00
$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('update')->withNoArgs()->once()->andReturnNull();
$this->assertTrue($this->service->handle($this->node->id, ['name' => 'NewName']));
}
}